mirror of
https://github.com/strapi/strapi.git
synced 2025-11-02 02:44:55 +00:00
Merge pull request #14422 from strapi/feature/relations-main-view-initial-data-2
RelationInputDataManager: Simplify initialData assignments
This commit is contained in:
commit
ce18f9cb4d
@ -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,
|
||||
})}
|
||||
|
||||
@ -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 } },
|
||||
|
||||
@ -15,10 +15,6 @@ export const useRelation = (cacheKey, { relation, search }) => {
|
||||
},
|
||||
});
|
||||
|
||||
if (relation?.onLoad) {
|
||||
relation.onLoad(data);
|
||||
}
|
||||
|
||||
return data;
|
||||
} catch (err) {
|
||||
return null;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user