2

I have a form for editing an Entity that hasMany children. Within this form there's a ReferenceManyField component. This component lists items along with an action button that I have created to let's say “approve” things.

<Edit>
  <SimpleForm>
    <ReferenceManyField reference="Expenses" target="expenseReportId" label="Expenses">
      <Datagrid>
        <UnassignButton />
        <TextField source="name" />
      </Datagrid>
    </ReferenceManyField>
  </SimpleForm>
</Edit>

UnassignButton performs a regular action:

const action = (id, data, basePath) => ({
  type: 'EXPENSE_UNASSIGN',
  payload: { id: id, data: { ...data, expenseReportId: null } },
  meta: { resource: basePath, fetch: UPDATE, cancelPrevious: false },
})

How can I force the ReferenceManyField or the whole Edit form to be refreshed when this action is performed? What's the best practice for that?

Thanks!

Konstantin Bodnia
  • 1,372
  • 3
  • 20
  • 43

2 Answers2

1

I would dispatch the

import { refreshView } from "react-admin";

action after changing the state.

twDuke
  • 927
  • 1
  • 8
  • 22
1

Now you can use useRefresh hook in react-admin

const refresh = useRefresh();

just use refresh when you want to update the List

import * as React from "react";
import { useMutation, Button } from 'react-admin';
import {  useNotify, useRedirect, useRefresh } from 'react-admin';

export const MyButton = ({ record }) => {

    const notify = useNotify();
    const redirect = useRedirect();
    const refresh = useRefresh();
 
    const [createPost, { loading }] = useMutation({
        type: 'create',
        resource: 'posts',
        payload: { data:
                    {
                      userId: "xxxxx",
                      body: "something"
                      isConfirmed: false
                    }
                 }

    },
    {
        onSuccess: ({ data }) => {
            redirect('/posts');
            refresh();
            notify('post created');
        },
        onFailure: (error) => notify(`error: ${error.message}`, 'warning'),
    });

    return (
        <Button label="CREATE" onClick={ createPost } } disabled={loading} >
            Create Post
        </Button>
    )
};