diff --git a/static/src/js/components/MetadataForm/MetadataForm.jsx b/static/src/js/components/MetadataForm/MetadataForm.jsx index f16008cee..a59bd1905 100644 --- a/static/src/js/components/MetadataForm/MetadataForm.jsx +++ b/static/src/js/components/MetadataForm/MetadataForm.jsx @@ -51,6 +51,7 @@ import toKebabCase from '@/js/utils/toKebabCase' import usePublishMutation from '@/js/hooks/usePublishMutation' import './MetadataForm.scss' +import { useSearchParams } from 'react-router-dom' const MetadataForm = () => { const { @@ -64,10 +65,8 @@ const MetadataForm = () => { draft, originalDraft, providerId, - revisionId: appContextRevisionId, setDraft, setOriginalDraft, - setRevisionId, setSavedDraft } = useAppContext() @@ -95,6 +94,9 @@ const MetadataForm = () => { const [visitedFields, setVisitedFields] = useState([]) const [focusField, setFocusField] = useState(null) + const [searchParams] = useSearchParams() + + const revisionIdAtIngest = searchParams.get('revisionId') || null // On load, set the focused field and redirect if a field name was present useEffect(() => { @@ -143,11 +145,10 @@ const MetadataForm = () => { const { draft: fetchedDraft } = data || {} const { revisionId: cmrRetrievedRevisionId } = fetchedDraft || '' - checkForCMRFetchDraftLag(cmrRetrievedRevisionId, appContextRevisionId) + checkForCMRFetchDraftLag(cmrRetrievedRevisionId, revisionIdAtIngest) setOriginalDraft(fetchedDraft) setDraft(fetchedDraft) - setRevisionId() }, [data]) const { @@ -226,9 +227,6 @@ const MetadataForm = () => { // Set savedDraft so the preview page can request the correct version setSavedDraft(ingestDraft) - // Triggers useEffect for newest revision (CMR Lag related) - setRevisionId(savedRevisionId) - // Add a success notification addNotification({ message: 'Draft saved successfully', @@ -238,7 +236,7 @@ const MetadataForm = () => { if (type === saveTypes.save) { // Navigate to current form? just scroll to top of page instead? - if (currentSection) navigate(`/drafts/${draftType}/${savedConceptId}/${currentSection}`, { replace: true }) + if (currentSection) navigate(`/drafts/${draftType}/${savedConceptId}/${currentSection}?revisionId=${savedRevisionId}`, { replace: true }) window.scroll(0, 0) } @@ -246,7 +244,7 @@ const MetadataForm = () => { if (type === saveTypes.saveAndContinue) { // Navigate to next form (using formSections), maybe scroll top too const nextFormName = getNextFormName(formSections, currentSection) - navigate(`/drafts/${draftType}/${savedConceptId}/${toKebabCase(nextFormName)}`) + navigate(`/drafts/${draftType}/${savedConceptId}/${toKebabCase(nextFormName)}?revisionId=${savedRevisionId}`) window.scroll(0, 0) } @@ -254,7 +252,7 @@ const MetadataForm = () => { if (type === saveTypes.saveAndPreview) { // Navigate to preview page window.scroll(0, 0) - navigate(`/drafts/${draftType}/${savedConceptId}`) + navigate(`/drafts/${draftType}/${savedConceptId}?revisionId=${savedRevisionId}`) } if (type === saveTypes.saveAndPublish) { diff --git a/static/src/js/components/MetadataForm/__tests__/MetadataForm.test.jsx b/static/src/js/components/MetadataForm/__tests__/MetadataForm.test.jsx index 85330da8c..e9e9d78ee 100644 --- a/static/src/js/components/MetadataForm/__tests__/MetadataForm.test.jsx +++ b/static/src/js/components/MetadataForm/__tests__/MetadataForm.test.jsx @@ -601,7 +601,7 @@ describe('MetadataForm', () => { await user.click(button) expect(navigateSpy).toHaveBeenCalledTimes(1) - expect(navigateSpy).toHaveBeenCalledWith('/drafts/tools/TD1000000-MMT/tool-information', { replace: true }) + expect(navigateSpy).toHaveBeenCalledWith('/drafts/tools/TD1000000-MMT/tool-information?revisionId=3', { replace: true }) expect(window.scroll).toHaveBeenCalledTimes(1) expect(window.scroll).toHaveBeenCalledWith(0, 0) @@ -647,7 +647,7 @@ describe('MetadataForm', () => { await user.click(button) expect(navigateSpy).toHaveBeenCalledTimes(1) - expect(navigateSpy).toHaveBeenCalledWith('/drafts/tools/TD1000000-MMT/related-urls') + expect(navigateSpy).toHaveBeenCalledWith('/drafts/tools/TD1000000-MMT/related-urls?revisionId=3') expect(window.scroll).toHaveBeenCalledTimes(1) expect(window.scroll).toHaveBeenCalledWith(0, 0) @@ -696,7 +696,7 @@ describe('MetadataForm', () => { await user.click(button) expect(navigateSpy).toHaveBeenCalledTimes(1) - expect(navigateSpy).toHaveBeenCalledWith('/drafts/tools/TD1000000-MMT') + expect(navigateSpy).toHaveBeenCalledWith('/drafts/tools/TD1000000-MMT?revisionId=3') expect(window.scroll).toHaveBeenCalledTimes(1) expect(window.scroll).toHaveBeenCalledWith(0, 0) @@ -959,7 +959,7 @@ describe('MetadataForm', () => { await user.click(modalSubmit) expect(navigateSpy).toHaveBeenCalledTimes(1) - expect(navigateSpy).toHaveBeenCalledWith('/drafts/collections/CD1000000-MMT/collection-information', { replace: true }) + expect(navigateSpy).toHaveBeenCalledWith('/drafts/collections/CD1000000-MMT/collection-information?revisionId=1', { replace: true }) expect(window.scroll).toHaveBeenCalledTimes(1) expect(window.scroll).toHaveBeenCalledWith(0, 0) diff --git a/static/src/js/utils/__tests__/checkForCMRFetchDraftLag.test.js b/static/src/js/utils/__tests__/checkForCMRFetchDraftLag.test.js index 7c1d17a84..b2254d887 100644 --- a/static/src/js/utils/__tests__/checkForCMRFetchDraftLag.test.js +++ b/static/src/js/utils/__tests__/checkForCMRFetchDraftLag.test.js @@ -9,7 +9,7 @@ describe('checkForCMRFetchDraftLag', () => { describe('when the fetched revision Id does not match expected revision Id', () => { test('throws error', () => { - expect(() => checkForCMRFetchDraftLag('1', '2')).toThrow('Delay in CMR has detected. Refresh the page in order to see latest revision') + expect(() => checkForCMRFetchDraftLag('1', '2')).toThrow('Delay in CMR has been detected. Refresh the page in order to see latest revision') }) }) }) diff --git a/static/src/js/utils/checkForCMRFetchDraftLag.js b/static/src/js/utils/checkForCMRFetchDraftLag.js index 4966bcc1b..2b6f6265d 100644 --- a/static/src/js/utils/checkForCMRFetchDraftLag.js +++ b/static/src/js/utils/checkForCMRFetchDraftLag.js @@ -6,7 +6,7 @@ const checkForCMRFetchDraftLag = (fetchedRevisionId, expectedRevisionId) => { if ((fetchedRevisionId && expectedRevisionId) && (fetchedRevisionId !== expectedRevisionId)) { - throw new Error('Delay in CMR has detected. Refresh the page in order to see latest revision') + throw new Error('Delay in CMR has been detected. Refresh the page in order to see latest revision') } }