Skip to content

Commit

Permalink
feat: don't block navigation when switch to angular
Browse files Browse the repository at this point in the history
  • Loading branch information
darrelhong committed Jan 28, 2023
1 parent d60000a commit 8955a5e
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 3 deletions.
7 changes: 5 additions & 2 deletions frontend/src/features/env/PublicFeedbackModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import { ModalCloseButton } from '~components/Modal'
import Textarea from '~components/Textarea'

import { useEnvMutations, useFeedbackMutation } from '~features/env/mutations'
import { usePublicFormContext } from '~features/public-form/PublicFormContext'

import { usePublicFeedbackFormView } from './queries'
import { isUsableFeedback } from './utils'
Expand All @@ -50,6 +51,7 @@ export const PublicFeedbackModal = ({
responseMode?: FormResponseMode
authType?: FormAuthType
}): JSX.Element => {
const { setBlockNavigation } = usePublicFormContext()
const modalSize = useBreakpointValue({
base: 'mobile',
xs: 'mobile',
Expand All @@ -68,7 +70,7 @@ export const PublicFeedbackModal = ({

const url = window.location.href
const rumSessionId = datadogRum.getInternalContext()?.session_id
const [showThanksPage, setShowThanksPage] = useState<boolean>(false)
const [showThanksPage, setShowThanksPage] = useState<boolean>(true)

const { publicSwitchEnvMutation } = useEnvMutations()
const { data: feedbackForm, isLoading } = usePublicFeedbackFormView()
Expand All @@ -88,8 +90,9 @@ export const PublicFeedbackModal = ({
}, [onClose])

const handleChangeEnv = useCallback(() => {
setBlockNavigation(false)
publicSwitchEnvMutation.mutate()
}, [publicSwitchEnvMutation])
}, [publicSwitchEnvMutation, setBlockNavigation])

const checkboxInputName = 'attachmentType'

Expand Down
2 changes: 2 additions & 0 deletions frontend/src/features/public-form/PublicFormContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ export interface PublicFormContextProps
*/
onMobileDrawerOpen: () => void
onMobileDrawerClose: () => void
blockNavigation: boolean
setBlockNavigation: (val: boolean) => void
}

export const PublicFormContext = createContext<
Expand Down
3 changes: 3 additions & 0 deletions frontend/src/features/public-form/PublicFormProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ export const PublicFormProvider = ({
}: PublicFormProviderProps): JSX.Element => {
// Once form has been submitted, submission data will be set here.
const [submissionData, setSubmissionData] = useState<SubmissionData>()
const [blockNavigation, setBlockNavigation] = useState(false)

const { data, isLoading, error, ...rest } = usePublicFormView(
formId,
Expand Down Expand Up @@ -323,6 +324,8 @@ export const PublicFormProvider = ({
captchaContainerId: containerId,
expiryInMs,
isLoading: isLoading || (!!data?.form.hasCaptcha && !hasLoaded),
blockNavigation,
setBlockNavigation,
...commonFormValues,
...data,
...rest,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
hasExistingFieldValue,
} from '~features/myinfo/utils'
import { useFetchPrefillQuery } from '~features/public-form/hooks/useFetchPrefillQuery'
import { usePublicFormContext } from '~features/public-form/PublicFormContext'

import { PublicFormSubmitButton } from './PublicFormSubmitButton'
import { VisibleFormFields } from './VisibleFormFields'
Expand All @@ -35,6 +36,8 @@ export const FormFields = ({
colorTheme,
onSubmit,
}: FormFieldsProps): JSX.Element => {
const { blockNavigation, setBlockNavigation } = usePublicFormContext()

useFetchPrefillQuery()
const [searchParams] = useSearchParams()

Expand Down Expand Up @@ -101,7 +104,11 @@ export const FormFields = ({
}
}, [defaultFormValues, isDirty, reset])

useNavigationPrompt(isDirty)
useEffect(() => {
if (isDirty) setBlockNavigation(true)
}, [isDirty, setBlockNavigation])

useNavigationPrompt(blockNavigation)

return (
<FormProvider {...formMethods}>
Expand Down

0 comments on commit 8955a5e

Please sign in to comment.