From ecd9632516029dcea159fc9237fa4cda7341227e Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Kaiser Date: Thu, 9 Mar 2023 12:18:39 +0100 Subject: [PATCH 1/2] Fix ReferenceInput throws an error when referencing the same resource as Edit and the reference is indefined --- .../src/dataProvider/useGetManyAggregate.ts | 2 +- .../src/input/ReferenceInput.spec.tsx | 15 ++++++ .../src/input/ReferenceInput.stories.tsx | 49 ++++++++++++++++++- 3 files changed, 63 insertions(+), 3 deletions(-) diff --git a/packages/ra-core/src/dataProvider/useGetManyAggregate.ts b/packages/ra-core/src/dataProvider/useGetManyAggregate.ts index dea405e9c0b..c2fe9c93acd 100644 --- a/packages/ra-core/src/dataProvider/useGetManyAggregate.ts +++ b/packages/ra-core/src/dataProvider/useGetManyAggregate.ts @@ -120,7 +120,7 @@ export const useGetManyAggregate = ( placeholderData, onSuccess: data => { // optimistically populate the getOne cache - data.forEach(record => { + (data ?? []).forEach(record => { queryClient.setQueryData( [resource, 'getOne', { id: String(record.id), meta }], oldRecord => oldRecord ?? record diff --git a/packages/ra-ui-materialui/src/input/ReferenceInput.spec.tsx b/packages/ra-ui-materialui/src/input/ReferenceInput.spec.tsx index 5b1887165af..2bb18d1a98a 100644 --- a/packages/ra-ui-materialui/src/input/ReferenceInput.spec.tsx +++ b/packages/ra-ui-materialui/src/input/ReferenceInput.spec.tsx @@ -16,6 +16,7 @@ import { Basic, WithSelectInput, dataProviderWithAuthors, + SelfReference, } from './ReferenceInput.stories'; describe('', () => { @@ -217,4 +218,18 @@ describe('', () => { }) ); }); + + it('should not throw an error on save when it is a self reference and the reference is undefined', async () => { + jest.spyOn(console, 'log').mockImplementationOnce(() => {}); + jest.spyOn(console, 'error').mockImplementationOnce(() => {}); + render(); + fireEvent.click(await screen.findByLabelText('Self reference')); + expect(await screen.findAllByRole('option')).toHaveLength(5); + const titleInput = await screen.findByDisplayValue('War and Peace'); + fireEvent.change(titleInput, { + target: { value: 'War and Peace 2' }, + }); + screen.getByLabelText('Save').click(); + await screen.findByText('Proust', undefined, { timeout: 5000 }); + }); }); diff --git a/packages/ra-ui-materialui/src/input/ReferenceInput.stories.tsx b/packages/ra-ui-materialui/src/input/ReferenceInput.stories.tsx index ef9430586da..bd3f564b7b1 100644 --- a/packages/ra-ui-materialui/src/input/ReferenceInput.stories.tsx +++ b/packages/ra-ui-materialui/src/input/ReferenceInput.stories.tsx @@ -1,8 +1,8 @@ import * as React from 'react'; import { createMemoryHistory } from 'history'; -import { Admin, AdminContext } from 'react-admin'; +import { Admin, AdminContext, Datagrid, List, TextField } from 'react-admin'; import { QueryClient } from 'react-query'; -import { Resource, Form, testDataProvider } from 'ra-core'; +import { Resource, Form, testDataProvider, useRedirect } from 'ra-core'; import polyglotI18nProvider from 'ra-i18n-polyglot'; import englishMessages from 'ra-language-english'; import { Stack, Divider, Typography } from '@mui/material'; @@ -463,3 +463,48 @@ export const ErrorRadioButtonGroupInput = () => ( /> ); + +const AuthorList = () => ( + + + + + + +); + +const BookEditWithSelfReference = () => { + const redirect = useRedirect(); + return ( + { + console.log(data); + // Redirecting to another page is an indirect way to make sure that + // no errors happened during the update nor its side effects + // (used by the jest tests) + redirect('/authors'); + }, + }} + > + + + + + + ); +}; + +export const SelfReference = ({ dataProvider = dataProviderWithAuthors }) => ( + + + `${record.first_name} ${record.last_name}` + } + list={AuthorList} + /> + + +); From 6d2c96a8bb7f002b89b16b53d280de707bcb7a5b Mon Sep 17 00:00:00 2001 From: Gildas Garcia <1122076+djhi@users.noreply.github.com> Date: Thu, 9 Mar 2023 15:37:28 +0100 Subject: [PATCH 2/2] Remove log --- packages/ra-ui-materialui/src/input/ReferenceInput.stories.tsx | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/ra-ui-materialui/src/input/ReferenceInput.stories.tsx b/packages/ra-ui-materialui/src/input/ReferenceInput.stories.tsx index bd3f564b7b1..722427be2e1 100644 --- a/packages/ra-ui-materialui/src/input/ReferenceInput.stories.tsx +++ b/packages/ra-ui-materialui/src/input/ReferenceInput.stories.tsx @@ -480,7 +480,6 @@ const BookEditWithSelfReference = () => { mutationMode="pessimistic" mutationOptions={{ onSuccess: data => { - console.log(data); // Redirecting to another page is an indirect way to make sure that // no errors happened during the update nor its side effects // (used by the jest tests)