Skip to content

Commit

Permalink
MMT-3922: adding useSearchParams
Browse files Browse the repository at this point in the history
  • Loading branch information
mandyparson committed Oct 22, 2024
1 parent 63496bc commit 11f352a
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 16 deletions.
18 changes: 8 additions & 10 deletions static/src/js/components/MetadataForm/MetadataForm.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -64,10 +65,8 @@ const MetadataForm = () => {
draft,
originalDraft,
providerId,
revisionId: appContextRevisionId,
setDraft,
setOriginalDraft,
setRevisionId,
setSavedDraft
} = useAppContext()

Expand Down Expand Up @@ -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(() => {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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',
Expand All @@ -238,23 +236,23 @@ 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)
}

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)
}

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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})
})
})
2 changes: 1 addition & 1 deletion static/src/js/utils/checkForCMRFetchDraftLag.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
}
}

Expand Down

0 comments on commit 11f352a

Please sign in to comment.