Skip to content

Commit

Permalink
fix: pop up suggesting users has uncommited changesafter form submit (#…
Browse files Browse the repository at this point in the history
…150)

Co-authored-by: Osneil Drakes <[email protected]>
  • Loading branch information
odrakes-cpr and Osneil Drakes authored Jan 22, 2025
1 parent 9f26fd3 commit a16c22b
Showing 1 changed file with 11 additions and 16 deletions.
27 changes: 11 additions & 16 deletions src/components/forms/FamilyForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ import {
FieldType,
IFormMetadata,
} from '@/interfaces/Metadata'

export interface IFamilyFormBase {
title: string
summary: string
Expand All @@ -81,7 +80,6 @@ const getCollection = (collectionId: string, collections: ICollection[]) => {

export const FamilyForm = ({ family: loadedFamily }: TProps) => {
const [isLeavingModalOpen, setIsLeavingModalOpen] = useState(false)
const [isFormSubmitting, setIsFormSubmitting] = useState(false)
const [loadedAndReset, setLoadedAndReset] = useState(false)
const { isOpen, onOpen, onClose } = useDisclosure()
const navigate = useNavigate()
Expand Down Expand Up @@ -138,7 +136,7 @@ export const FamilyForm = ({ family: loadedFamily }: TProps) => {
reset,
setValue,
watch,
formState: { errors, isSubmitting, dirtyFields },
formState: { errors, isSubmitting, isSubmitted, touchedFields },
} = useForm<TFamilyFormSubmit>({
resolver: yupResolver<TFamilyFormSubmit>(validationSchema),
})
Expand Down Expand Up @@ -203,7 +201,6 @@ export const FamilyForm = ({ family: loadedFamily }: TProps) => {
}, [loadedFamily])

const handleFormSubmission = async (formData: TFamilyFormSubmit) => {
setIsFormSubmitting(true)
setFormError(null)

// Validate corpus type
Expand Down Expand Up @@ -256,8 +253,6 @@ export const FamilyForm = ({ family: loadedFamily }: TProps) => {
description: (error as IError).message,
status: 'error',
})
} finally {
setIsFormSubmitting(false)
}
}

Expand Down Expand Up @@ -417,8 +412,8 @@ export const FamilyForm = ({ family: loadedFamily }: TProps) => {
}

const summaryOnChange = (html: string) => {
if (stripHtml(html) === '') {
return setValue('summary', '', { shouldDirty: true })
if (stripHtml(html) === '' || loadedFamily?.summary === stripHtml(html)) {
return
}
setValue('summary', html, { shouldDirty: true })
}
Expand Down Expand Up @@ -461,15 +456,15 @@ export const FamilyForm = ({ family: loadedFamily }: TProps) => {
const canLoadForm =
!configLoading && !collectionsLoading && !configError && !collectionsError

const blocker = useBlocker(
({ currentLocation, nextLocation }) =>
!isFormSubmitting &&
Object.keys(dirtyFields).length > 0 &&
currentLocation.pathname !== nextLocation.pathname,
)
const blocker = useBlocker(({ currentLocation, nextLocation }) => {
if (currentLocation.pathname !== nextLocation.pathname) {
return !isSubmitted && Object.keys(touchedFields).length > 0
}
return false
})

useEffect(() => {
if (blocker && blocker.state === 'blocked') {
if (blocker?.state === 'blocked') {
setIsLeavingModalOpen(true)
}
}, [blocker])
Expand Down Expand Up @@ -510,7 +505,7 @@ export const FamilyForm = ({ family: loadedFamily }: TProps) => {
name='summary'
label='Summary'
control={control}
defaultValue={loadedFamily?.summary}
defaultValue={loadedFamily?.summary ?? ''}
onChange={summaryOnChange}
error={errors.summary}
isRequired={true}
Expand Down

0 comments on commit a16c22b

Please sign in to comment.