将数据保存到两个不同的资源中

问题描述 投票:0回答:1

我正在尝试保存用户的完整记录,但是我依赖于两个单独的表。配置文件数据保存在一个资源中,地址保存在另一个资源中。如何编写代码,使其首先保存配置文件,然后根据生成的ID保存地址?有可能吗?

Profile form

Address form

这是我的创建用户代码:

export const BarberCreate = (props) => {
  return (
    <Create {...props}>
      <TabbedForm toolbar={<BarberCreateToolbar />}>
        <FormTab label="Perfil">
          <TextInput source="name" />
          <TextInput source="email" />
          <DateInput source="birthday" />
          <TextInput source="phone" placeholder="(99) 99999-9999" />
          <TextInput source="transport" />
        </FormTab>

        <FormTab label="Endereço">
          <TextInput source="street" label="Rua" />
          <TextInput source="city" label="Cidade" />
          <TextInput source="district" label="Bairro" />
        </FormTab>
      </TabbedForm>
    </Create>
  );
};```
javascript reactjs react-admin
1个回答
0
投票

[最好是在后端执行此操作,可能需要事务支持,但如果不能这样做,一种进行react-admin的方法是修饰dataProvider。

v3

const userSaverDataProvider = dataProvider => ({
    ...dataProvider,
    create: async (resource, params) => {

        if (resource === 'users') {

            const profile = await dataProvider.create('profile', {data: {
                name: params.data.name,
                ....
            }})

            await dataProvider.create('address', {data: {
                profileID: profile.data.id,
                street: params.data.street,
                ...
            }})

            return profile        

        }        

        return dataProvider.create(resource, params)
    }
})

如果您在react-admin上批量创建用户,也可能需要装饰createMany。另外,结帐https://github.com/FusionWorks/react-admin-google-maps,非常有用

© www.soinside.com 2019 - 2024. All rights reserved.