Skip to content

Commit

Permalink
fix: refresh revisions list on publish/unpublish action (#4291)
Browse files Browse the repository at this point in the history
  • Loading branch information
adrians5j authored Sep 24, 2024
1 parent 2c453dc commit 7665935
Showing 1 changed file with 40 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useMemo, useState } from "react";
import React, { useCallback, useEffect, useMemo, useState } from "react";
import { useRouter } from "@webiny/react-router";
import { useIsMounted, useSnackbar } from "@webiny/app-admin";
import { useCms, useQuery } from "~/admin/hooks";
Expand Down Expand Up @@ -129,6 +129,25 @@ export const ContentEntryProvider = ({
const [isLoading, setLoading] = useState<boolean>(false);
const contentEntryProviderProps = useContentEntryProviderProps();

const updateRevisionInRevisionsCache = useCallback(
(updatedRevisions: CmsContentEntryRevision | CmsContentEntryRevision[]) => {
const updatedRevisionsArray = Array.isArray(updatedRevisions)
? updatedRevisions
: [updatedRevisions];

setRevisions(revisions => {
return revisions.map(revision => {
const updatedRevision = updatedRevisionsArray.find(
updatedRevision => updatedRevision.id === revision.id
);

return updatedRevision || revision;
});
});
},
[]
);

const newEntry =
typeof isNewEntry === "function" ? isNewEntry() : contentEntryProviderProps.isNewEntry();
const contentId =
Expand Down Expand Up @@ -255,15 +274,7 @@ export const ContentEntryProvider = ({
if (response.entry) {
setEntry(response.entry);
updateRecordInCache(response.entry);

const updatedRevisionsList = revisions.map(rev => {
if (rev.id === response.entry.id) {
return response.entry;
}
return rev;
});

setRevisions(updatedRevisionsList);
updateRevisionInRevisionsCache(response.entry);
}
return response;
};
Expand Down Expand Up @@ -299,6 +310,24 @@ export const ContentEntryProvider = ({
if (response.entry) {
setEntry(response.entry);
updateRecordInCache(response.entry);

const revisionsToUpdateInRevisionsCache: CmsContentEntryRevision[] = [response.entry];

const previousPublishedRevision = revisions.find(
rev => rev.meta.status === "published"
);

if (previousPublishedRevision) {
revisionsToUpdateInRevisionsCache.push({
...previousPublishedRevision,
meta: {
...previousPublishedRevision.meta,
status: "unpublished"
}
});
}

updateRevisionInRevisionsCache(revisionsToUpdateInRevisionsCache);
}
return response;
};
Expand All @@ -308,6 +337,7 @@ export const ContentEntryProvider = ({
if (response.entry) {
setEntry(response.entry);
updateRecordInCache(response.entry);
updateRevisionInRevisionsCache(response.entry);
}
return response;
};
Expand Down

0 comments on commit 7665935

Please sign in to comment.