1

I have a nextJS project that use redux for state management and react admin for admin panel. I took my keys not as id instead _id. So I followed this custom identifiers.

My App.js

// in src/App.js
import * as React from "react";
import { Provider } from "react-redux";
import { createHashHistory } from "history";
import { Admin, Resource, ListGuesser, fetchUtils } from "react-admin";
import jsonServerProvider from "ra-data-json-server";
import createAdminStore from "./createAdminStore";
import convertHTTPResponseToREST from "./dataProvider";

// dependency injection
const dataProvider = jsonServerProvider(
  convertHTTPResponseToREST("http://localhost:3000/api/products")
);
const authProvider = () => Promise.resolve();

const history = createHashHistory();

const App = () => (
  <Provider
    store={createAdminStore({
      authProvider,
      dataProvider,
      history,
    })}
  >
    <Admin title="Dashboard" dataProvider={dataProvider} history={history}>
      <Resource name="products" list={ListGuesser} />
    </Admin>
  </Provider>
);

export default App;


my dataProvider.js

import {
  CREATE,
  DELETE,
  GET_LIST,
  GET_MANY_REFERENCE,
  GET_ONE,
  UPDATE,
} from "react-admin";

const convertHTTPResponseToREST = (response, type, resource, params) => {
  const { headers, json } = response;
  switch (type) {
    case GET_LIST:
      return {
        data: json.map((resource) => ({ ...resource, id: resource._id })),
        total: parseInt(headers.get("content-range").split("/").pop(), 10),
      };
    case UPDATE:
    case DELETE:
    case GET_ONE:
      return { ...json, id: json._id };
    case CREATE:
      return { ...params.data, id: json._id };
    default:
      return json;
  }
};

export default convertHTTPResponseToREST;

So how to use convertHTTPResponseToRest function inside the Admin tag?

2 Answers 2

1

You are seeing the documentation of admin-on-rest.

This is the documentation of react-admin:

https://marmelab.com/react-admin/FAQ.html#can-i-have-custom-identifiersprimary-keys-for-my-resources

Sign up to request clarification or add additional context in comments.

Comments

0

If you are using a community data provider, before overriding it, check if it already supports custom primary keys.

eg. @raphiniert/ra-data-postgrest, you can set them per resource via a primaryKeys Map (including composite keys), so React-Admin keeps working normally

const config = {
  // ...
  primaryKeys: new Map([
    ['some_table', ['custom_id']],
    ['another_table', ['col1', 'col2']], // composite PK
  ]),
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.