Skip to content

Commit

Permalink
Fix category missing for MCF families (#147)
Browse files Browse the repository at this point in the history
  • Loading branch information
katybaulch authored Jan 9, 2025
1 parent f161f28 commit 8052658
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions src/components/forms/FamilyForm.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useEffect, useState, useMemo, useCallback } from 'react'
import { useForm, SubmitHandler } from 'react-hook-form'
import { useForm, SubmitHandler, SubmitErrorHandler } from 'react-hook-form'
import { yupResolver } from '@hookform/resolvers/yup'
import { useBlocker, useNavigate } from 'react-router-dom'
import {
Expand Down Expand Up @@ -141,6 +141,9 @@ export const FamilyForm = ({ family: loadedFamily }: TProps) => {
formState: { errors, isSubmitting, dirtyFields },
} = useForm<TFamilyFormSubmit>({
resolver: yupResolver<TFamilyFormSubmit>(validationSchema),
defaultValues: {
category: 'MCF', // Can we be smarter about this?
},
})

// Watch for corpus changes and update schema only when creating a new family
Expand Down Expand Up @@ -247,7 +250,7 @@ export const FamilyForm = ({ family: loadedFamily }: TProps) => {
try {
await handleFormSubmission(data)
} catch (error) {
console.log('onSubmitErrorHandler', error)
console.error('onSubmit', error)
setFormError(error as IError)
toast({
title: 'Form submission error',
Expand All @@ -257,6 +260,20 @@ export const FamilyForm = ({ family: loadedFamily }: TProps) => {
}
}

const onSubmitErrorHandler: SubmitErrorHandler<TFamilyFormSubmit> =
useCallback(
(errors) => {
console.error('onSubmitErrorHandler', errors)
setFormError(errors as IError)
toast({
title: 'Form submission error',
description: (errors as IError).message,
status: 'error',
})
},
[toast],
)

useEffect(() => {
if (loadedFamily) {
setFamilyDocuments(loadedFamily.documents || [])
Expand Down Expand Up @@ -464,7 +481,7 @@ export const FamilyForm = ({ family: loadedFamily }: TProps) => {
)}

{canLoadForm && (
<form onSubmit={handleSubmit(onSubmit)}>
<form onSubmit={handleSubmit(onSubmit, onSubmitErrorHandler)}>
<VStack gap='4' mb={12} mt={4} align={'stretch'}>
{formError && <ApiError error={formError} />}

Expand Down

0 comments on commit 8052658

Please sign in to comment.