Merge pull request #14422 from strapi/feature/relations-main-view-initial-data-2

RelationInputDataManager: Simplify initialData assignments
This commit is contained in:
Gustav Hansen 2022-09-15 15:24:38 +02:00 committed by GitHub
commit ce18f9cb4d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 45 deletions

View File

@ -37,9 +37,6 @@ export const RelationInputDataManger = ({
relation: {
enabled: relationsCount > 0 && !!endpoints.relation,
endpoint: endpoints.relation,
onLoad(data) {
loadRelation({ target: { name, value: data.results } });
},
pageParams: {
...defaultParams,
locale: query?.plugins?.i18n?.locale,
@ -58,17 +55,34 @@ export const RelationInputDataManger = ({
},
});
useEffect(() => {
if (!endpoints.relation) {
loadRelation({ target: { name, value: [] } });
}
}, [loadRelation, name, endpoints.relation]);
const stringifiedRelations = JSON.stringify(relations);
const normalizedRelations = useMemo(
() =>
normalizeRelations(relations, {
modifiedData: modifiedData?.[name],
mainFieldName: mainField.name,
shouldAddLink: shouldDisplayRelationLink,
targetModel,
}),
// eslint-disable-next-line react-hooks/exhaustive-deps
[
stringifiedRelations,
modifiedData,
name,
mainField.name,
shouldDisplayRelationLink,
targetModel,
]
);
useEffect(() => {
if (relationsCount === 0 || !!endpoints.relation) {
loadRelation({ target: { name, value: [] } });
if (relations.status === 'success') {
loadRelation({
target: { name, value: normalizedRelations.data.pages.flat() },
});
}
}, [relationsCount, endpoints.relation, loadRelation, name]);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [loadRelation, relations.status, stringifiedRelations, name]);
const isMorph = useMemo(() => relationType.toLowerCase().includes('morph'), [relationType]);
const isSingleRelation = [
@ -176,13 +190,8 @@ export const RelationInputDataManger = ({
defaultMessage: 'Published',
}),
}}
relations={normalizedRelations}
required={required}
relations={normalizeRelations(relations, {
modifiedData: modifiedData?.[name],
mainFieldName: mainField.name,
shouldAddLink: shouldDisplayRelationLink,
targetModel,
})}
searchResults={normalizeRelations(search, {
mainFieldName: mainField.name,
})}

View File

@ -80,30 +80,6 @@ describe('useRelation', () => {
});
});
test('calls onLoad callback', async () => {
const spy = jest.fn();
const FIXTURE_DATA = {
values: [1, 2],
pagination: {
page: 1,
pageCount: 3,
},
};
axiosInstance.get = jest.fn().mockResolvedValue({
data: FIXTURE_DATA,
});
const { waitForNextUpdate } = await setup(undefined, {
relation: {
onLoad: spy,
},
});
await waitForNextUpdate();
expect(spy).toBeCalledTimes(1);
expect(spy).toBeCalledWith(FIXTURE_DATA);
});
test('fetch relations with different limit', async () => {
const { waitForNextUpdate } = await setup(undefined, {
relation: { pageParams: { limit: 5 } },

View File

@ -15,10 +15,6 @@ export const useRelation = (cacheKey, { relation, search }) => {
},
});
if (relation?.onLoad) {
relation.onLoad(data);
}
return data;
} catch (err) {
return null;