简体   繁体   English

React-admin:如何在 react-admin v3.2.* 中创建用户配置文件表单?

[英]React-admin: How to create user profile form in react-admin v3.2.*?

I've noticed that the official article on how to create user settings (profile) in React-Admin is outdated ( https://marmelab.com/blog/2019/03/07/react-admin-advanced-recipes-user-profile.html ).我注意到有关如何在 React-Admin 中创建用户设置(配置文件)的官方文章已过时( https://marmelab.com/blog/2019/03/07/react-admin-advanced-recipes-user-配置文件.html )。

I followed the example and tried to use a new DataProvider , but couldn't get Edit view working (it just showed blank Card component with no fields even though I've set them in a way that's described in the example).我按照示例操作并尝试使用新的DataProvider ,但无法使Edit视图正常工作(即使我以示例中描述的方式设置了它们,它也只是显示没有字段的空白Card组件)。

I was searching for several days on how to implement it in a simplest/clean way, but there's a very small amount of information about it.我一直在寻找如何以最简单/干净的方式实现它的几天,但是关于它的信息量非常少。

Does somebody know how to do it in react-admin 3.2.*?有人知道如何在 react-admin 3.2.* 中做到这一点吗?

It might be helpful for others who have the same issue.它可能对其他有相同问题的人有所帮助。 Any help will be very appreciated!任何帮助将不胜感激! Thanks!谢谢!

I had the same problem.我有同样的问题。 Looking at the props passed toreact-admin's Edit , I saw the record prop was undefined.查看通过 toreact-admin 的Edit传递的道具,我看到record道具未定义。 It was because the id field inside the record returned by the data provider's getOne method was different than the id prop hardcoded on the Edit component.这是因为数据提供者的getOne方法返回的记录中的id字段与在 Edit 组件上硬编码的id属性不同。 Once that was set to match, both reading/editing works.一旦设置为匹配,阅读/编辑都可以工作。

My working code:我的工作代码:

// remove staticContext to avoid React 'unknown prop on DOM element' error
export const PrincipalEdit = ({ staticContext, ...props }: { staticContext: any; props: any }) => {
  return (
    // `id` has to match with `id` field on record returned by data provider's `getOne`
    // `basePath` is used for re - direction
    // but we specify no redirection since there is no list component
      <Edit {...props} title="My Profile" id="my-profile" resource={b.PRINCIPAL} basePath="my-profile" redirect={false}>
        <SimpleForm>
          <TextInput source="firstName" validate={required()} />
        </SimpleForm>
      </Edit>
  );
};

The issue is how the data is stored by react-admin at the moment (haven't checked how it was before).问题是目前react-admin如何存储数据(之前没有检查过它是如何存储的)。 Now each data entry is saved by its id in the store object (the reasons are obvious).现在每个数据条目都通过它的 id 保存在存储 object 中(原因很明显)。 I think the best approach is to modify the data provider.我认为最好的方法是修改数据提供者。

if (resource === 'profile') {
  const { id, ...p } = params; // removes the id from the params
  if (p.data)
    delete p.data.id; // when updates there is data object that is passed to the backend

  return dataProvider(type, resource, { ...p, id: '' }) // set the id just empty string to hack undefined as http param
    .then(({ data }) => {
      return Promise.resolve({
        data: {
        ...data,
          id // return the data with the initial id
        }
      });
    });
}

This way the backend endpoint could return just the object at the main endpoint of the entity /profile .这样,后端端点可以在实体/profile的主端点仅返回 object。 If you do not set the id prop to '' it will try to fetch /profile/undefined because the id prop was deleted from the params object.如果您没有将 id 属性设置为'' ,它将尝试获取/profile/undefined ,因为 id 属性已从params object 中删除。 If you do not delete the id prop from the data object when updating, depending on the backend sql query (assuming you are using some kind of db) for updating a record, it may try to set or search by this id.如果更新时没有从数据 object 中删除 id 属性,根据后端 sql 查询(假设您使用某种 db)更新记录,它可能会尝试通过此 id 设置或搜索。

In the Edit component you can pass whatever id you want but something must be passed.Edit组件中,您可以传递您想要的任何 id,但必须传递一些东西。

Additional: If you are using NestJS as backend with the Crud package, these @Crud params may be helpful附加:如果您使用 NestJS 作为 Crud package 的后端,这些@Crud参数可能会有所帮助

  ...
  params: {
    id: { // the routes won't have params, very helpful for me/profile controller
      primary: true,
      disabled: true,
    },
  },
  routes: {
    only: ["getOneBase", "updateOneBase"],
  },
  ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM