fix: cm/i18n/admin rtk issues

fix: multiple loading issue in the CM
This commit is contained in:
Alexandre Bodin 2024-06-19 14:00:32 +02:00
parent a91fff3ff8
commit e5ea1c3e60
6 changed files with 67 additions and 25 deletions

View File

@ -99,7 +99,10 @@ const useDocument: UseDocument = (args, opts) => {
isLoading: isLoadingDocument, isLoading: isLoadingDocument,
isFetching: isFetchingDocument, isFetching: isFetchingDocument,
error, error,
} = useGetDocumentQuery(args, opts); } = useGetDocumentQuery(args, {
...opts,
skip: (!args.documentId && args.collectionType !== SINGLE_TYPES) || opts?.skip,
});
const { components, schema, isLoading: isLoadingSchema } = useContentTypeSchema(args.model); const { components, schema, isLoading: isLoadingSchema } = useContentTypeSchema(args.model);

View File

@ -97,7 +97,11 @@ const ListViewPage = () => {
}); });
const params = React.useMemo(() => buildValidParams(query), [query]); const params = React.useMemo(() => buildValidParams(query), [query]);
const { data, error, isFetching } = useGetAllDocumentsQuery({ const {
currentData: data,
error,
isLoading,
} = useGetAllDocumentsQuery({
model, model,
params, params,
}); });
@ -170,7 +174,7 @@ const ListViewPage = () => {
return formattedHeaders; return formattedHeaders;
}, [displayedHeaders, formatMessage, list, runHookWaterfall, schema?.options?.draftAndPublish]); }, [displayedHeaders, formatMessage, list, runHookWaterfall, schema?.options?.draftAndPublish]);
if (isFetching) { if (isLoading) {
return <Page.Loading />; return <Page.Loading />;
} }
@ -239,7 +243,7 @@ const ListViewPage = () => {
/> />
<Layouts.Content> <Layouts.Content>
<Flex gap={4} direction="column" alignItems="stretch"> <Flex gap={4} direction="column" alignItems="stretch">
<Table.Root rows={results} headers={tableHeaders} isLoading={isFetching}> <Table.Root rows={results} headers={tableHeaders} isLoading={isLoading}>
<TableActionsBar /> <TableActionsBar />
<Table.Content> <Table.Content>
<Table.Head> <Table.Head>

View File

@ -21,6 +21,7 @@ import type {
PublishRelease, PublishRelease,
MapEntriesToReleases, MapEntriesToReleases,
} from '../../../shared/contracts/releases'; } from '../../../shared/contracts/releases';
import type { EndpointDefinition } from '@reduxjs/toolkit/query';
export interface GetReleasesQueryParams { export interface GetReleasesQueryParams {
page?: number; page?: number;
@ -45,33 +46,59 @@ type GetReleasesTabResponse = GetReleases.Response & {
}; };
}; };
type AnyEndpointDefinition = EndpointDefinition<any, any, any, any>;
// TODO: move this into the admin code & expose an improved version of enhanceEndpoints or a new function
const extendInvalidatesTags = (
endpoint: AnyEndpointDefinition,
extraTags: string[] | { type: string; id: string }[]
) => {
const originalInvalidatesTags = endpoint.invalidatesTags;
const newInvalidatesTags: AnyEndpointDefinition['invalidatesTags'] = (
result,
err,
args,
meta
) => {
const originalTags =
typeof originalInvalidatesTags === 'function'
? originalInvalidatesTags(result, err, args, meta)
: originalInvalidatesTags;
return [...(originalTags ?? []), ...extraTags];
};
Object.assign(endpoint, { invalidatesTags: newInvalidatesTags });
};
const releaseApi = adminApi const releaseApi = adminApi
.enhanceEndpoints({ .enhanceEndpoints({
addTagTypes: ['Release', 'ReleaseAction', 'EntriesInRelease'], addTagTypes: ['Release', 'ReleaseAction', 'EntriesInRelease'],
endpoints: { endpoints: {
updateDocument: { updateDocument(endpoint: AnyEndpointDefinition) {
invalidatesTags: [ extendInvalidatesTags(endpoint, [
{ type: 'Release', id: 'LIST' }, { type: 'Release', id: 'LIST' },
{ type: 'ReleaseAction', id: 'LIST' }, { type: 'ReleaseAction', id: 'LIST' },
], ]);
}, },
deleteDocument: { deleteDocument(endpoint: AnyEndpointDefinition) {
invalidatesTags: [ extendInvalidatesTags(endpoint, [
{ type: 'Release', id: 'LIST' }, { type: 'Release', id: 'LIST' },
{ type: 'ReleaseAction', id: 'LIST' }, { type: 'ReleaseAction', id: 'LIST' },
], ]);
}, },
deleteManyDocuments: { deleteManyDocuments(endpoint: AnyEndpointDefinition) {
invalidatesTags: [ extendInvalidatesTags(endpoint, [
{ type: 'Release', id: 'LIST' }, { type: 'Release', id: 'LIST' },
{ type: 'ReleaseAction', id: 'LIST' }, { type: 'ReleaseAction', id: 'LIST' },
], ]);
}, },
discardDocument: { discardDocument(endpoint: AnyEndpointDefinition) {
invalidatesTags: [ extendInvalidatesTags(endpoint, [
{ type: 'Release', id: 'LIST' }, { type: 'Release', id: 'LIST' },
{ type: 'ReleaseAction', id: 'LIST' }, { type: 'ReleaseAction', id: 'LIST' },
], ]);
}, },
}, },
}) })

View File

@ -31,6 +31,7 @@ import { BulkLocaleActionModal } from './BulkLocaleActionModal';
import type { Locale } from '../../../shared/contracts/locales'; import type { Locale } from '../../../shared/contracts/locales';
import type { I18nBaseQuery } from '../types'; import type { I18nBaseQuery } from '../types';
import { skipToken } from '@reduxjs/toolkit/query';
/* ------------------------------------------------------------------------------------------------- /* -------------------------------------------------------------------------------------------------
* LocalePickerAction * LocalePickerAction
@ -275,16 +276,21 @@ const BulkLocalePublishAction: DocumentActionComponent = ({
meta: documentMeta, meta: documentMeta,
schema, schema,
validate, validate,
} = useDocument({ } = useDocument(
{
model, model,
collectionType, collectionType,
documentId, documentId,
params: { params: {
locale: baseLocale, locale: baseLocale,
}, },
}); },
{
skip: !hasI18n,
}
);
const { data: localesMetadata = [] } = useGetLocalesQuery(); const { data: localesMetadata = [] } = useGetLocalesQuery(hasI18n ? undefined : skipToken);
const headers = [ const headers = [
{ {

View File

@ -20,6 +20,7 @@ const LocaleListCell = ({
collectionType, collectionType,
model, model,
}: LocaleListCellProps) => { }: LocaleListCellProps) => {
// TODO: avoid loading availableLocales for each row but get that from the BE
const { meta, isLoading } = useDocument({ const { meta, isLoading } = useDocument({
documentId, documentId,
collectionType, collectionType,

View File

@ -43,6 +43,7 @@ const useI18n: UseI18n = () => {
); );
}, [params.slug, userPermissions]); }, [params.slug, userPermissions]);
// TODO: use specific hook to get schema only
const { schema } = useDocument( const { schema } = useDocument(
{ {
// We can non-null assert these because below we skip the query if they are not present // We can non-null assert these because below we skip the query if they are not present
@ -50,7 +51,7 @@ const useI18n: UseI18n = () => {
model: params.slug!, model: params.slug!,
}, },
{ {
skip: !params.slug || !params.collectionType, skip: true,
} }
); );