Skip to content

Commit

Permalink
fix: verify application errors (#3736)
Browse files Browse the repository at this point in the history
  • Loading branch information
emilyjablonski authored Dec 11, 2023
1 parent 7bf05c1 commit 48cb352
Show file tree
Hide file tree
Showing 18 changed files with 87 additions and 52 deletions.
2 changes: 1 addition & 1 deletion sites/public/src/lib/applications/AppSubmissionContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ export const retrieveApplicationConfig = (listing: Listing) => {

export const AppSubmissionContext = createContext({
conductor: {} as ApplicationConductor,
application: { ...blankApplication },
application: JSON.parse(JSON.stringify(blankApplication)),
listing: null as Listing,
/* eslint-disable */
syncApplication: (data) => {},
Expand Down
10 changes: 5 additions & 5 deletions sites/public/src/lib/applications/ApplicationConductor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import PreferencesAllStep from "./PreferencesAllStep"
import ProgramsStep from "./ProgramsStep"

export const loadApplicationFromAutosave = () => {
if (typeof window != "undefined") {
if (typeof window !== "undefined") {
const autosavedApplication = window.sessionStorage.getItem("bloom-app-autosave")
if (autosavedApplication) {
const application = JSON.parse(autosavedApplication)
Expand All @@ -28,7 +28,7 @@ export const loadApplicationFromAutosave = () => {
}

export const loadSavedListing = () => {
if (typeof window != "undefined") {
if (typeof window !== "undefined") {
const savedListing = window.sessionStorage.getItem("bloom-app-listing")
if (savedListing) {
return JSON.parse(savedListing)
Expand Down Expand Up @@ -193,7 +193,7 @@ export default class ApplicationConductor {
sync() {
// NOTE: had to remove timeout because of Next doing full-page reloads in
// some cases. Need to revisit after upgrading to v10
if (typeof window != "undefined") {
if (typeof window !== "undefined") {
window.sessionStorage.setItem("bloom-app-autosave", JSON.stringify(this.application))
if (this.listing) {
window.sessionStorage.setItem("bloom-app-listing", JSON.stringify(this.listing))
Expand All @@ -202,10 +202,10 @@ export default class ApplicationConductor {
}

reset() {
this.application = { ...blankApplication }
this.application = JSON.parse(JSON.stringify(blankApplication))
this.listing = {} as Listing
this.currentStepIndex = 0
if (typeof window != "undefined") {
if (typeof window !== "undefined") {
window.sessionStorage.removeItem("bloom-app-autosave")
window.sessionStorage.removeItem("bloom-app-listing")
}
Expand Down
2 changes: 1 addition & 1 deletion sites/public/src/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function BloomApp({ Component, router, pageProps }: AppProps) {
const { locale } = router
// const initialized = useState(true)
const [application, setApplication] = useState(() => {
return loadApplicationFromAutosave() || { ...blankApplication }
return loadApplicationFromAutosave() || JSON.parse(JSON.stringify(blankApplication))
})
const [savedListing, setSavedListing] = useState(() => {
return loadSavedListing()
Expand Down
33 changes: 18 additions & 15 deletions sites/public/src/pages/applications/contact/address.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,24 @@ const ApplicationAddress = () => {
const currentPageSection = 1

// eslint-disable-next-line @typescript-eslint/unbound-method
const { control, register, handleSubmit, setValue, watch, errors } = useForm<Record<string, any>>(
{
defaultValues: {
"applicant.phoneNumber": application.applicant.phoneNumber,
"applicant.noPhone": application.applicant.noPhone,
additionalPhone: application.additionalPhone,
"applicant.phoneNumberType": application.applicant.phoneNumberType,
sendMailToMailingAddress: application.sendMailToMailingAddress,
"applicant.workInRegion": application.applicant.workInRegion,
"applicant.address.state": application.applicant.address.state,
},
shouldFocusError: false,
}
)
const onSubmit = (data) => {
const { control, register, handleSubmit, setValue, watch, errors, trigger } = useForm<
Record<string, any>
>({
defaultValues: {
"applicant.phoneNumber": application.applicant.phoneNumber,
"applicant.noPhone": application.applicant.noPhone,
additionalPhone: application.additionalPhone,
"applicant.phoneNumberType": application.applicant.phoneNumberType,
sendMailToMailingAddress: application.sendMailToMailingAddress,
"applicant.workInRegion": application.applicant.workInRegion,
"applicant.address.state": application.applicant.address.state,
},
shouldFocusError: false,
})
const onSubmit = async (data) => {
const validation = await trigger()
if (!validation) return

if (!verifyAddress) {
setFoundAddress({})
setVerifyAddress(true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,13 @@ export default () => {
const currentPageSection = 1

// eslint-disable-next-line @typescript-eslint/unbound-method
const { control, register, handleSubmit, errors } = useForm<Record<string, any>>({
const { control, register, handleSubmit, errors, trigger } = useForm<Record<string, any>>({
shouldFocusError: false,
})
const onSubmit = (data) => {
const onSubmit = async (data) => {
const validation = await trigger()
if (!validation) return

application.alternateContact.phoneNumber = data.phoneNumber
application.alternateContact.emailAddress = data.emailAddress || null
application.alternateContact.mailingAddress.street = data.mailingAddress.street
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@ export default () => {
const currentPageSection = 1

// eslint-disable-next-line @typescript-eslint/unbound-method
const { register, handleSubmit, errors } = useForm<Record<string, any>>({
const { register, handleSubmit, errors, trigger } = useForm<Record<string, any>>({
shouldFocusError: false,
})
const onSubmit = (data) => {
const onSubmit = async (data) => {
const validation = await trigger()
if (!validation) return

application.alternateContact.firstName = data.firstName
application.alternateContact.lastName = data.lastName
application.alternateContact.agency = data.agency
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,13 @@ const ApplicationAlternateContactType = () => {
const currentPageSection = 1

// eslint-disable-next-line @typescript-eslint/unbound-method
const { register, handleSubmit, errors, watch } = useForm<Record<string, any>>({
const { register, handleSubmit, errors, watch, trigger } = useForm<Record<string, any>>({
shouldFocusError: false,
})
const onSubmit = (data) => {
const onSubmit = async (data) => {
const validation = await trigger()
if (!validation) return

application.alternateContact.type = data.type
application.alternateContact.otherType = data.otherType

Expand Down
7 changes: 5 additions & 2 deletions sites/public/src/pages/applications/contact/name.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,17 @@ const ApplicationName = () => {
const currentPageSection = 1

// eslint-disable-next-line @typescript-eslint/unbound-method
const { register, handleSubmit, watch, errors } = useForm<Record<string, any>>({
const { register, handleSubmit, watch, errors, trigger } = useForm<Record<string, any>>({
shouldFocusError: false,
defaultValues: {
"applicant.emailAddress": application.applicant.emailAddress,
"applicant.noEmail": application.applicant.noEmail,
},
})
const onSubmit = (data) => {
const onSubmit = async (data) => {
const validation = await trigger()
if (!validation) return

if (!autofilled) {
conductor.currentStep.save({ applicant: { ...application.applicant, ...data.applicant } })
}
Expand Down
7 changes: 5 additions & 2 deletions sites/public/src/pages/applications/financial/income.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,18 @@ const ApplicationIncome = () => {
: 3

// eslint-disable-next-line @typescript-eslint/unbound-method
const { register, handleSubmit, errors, getValues, setValue } = useForm({
const { register, handleSubmit, errors, getValues, setValue, trigger } = useForm({
defaultValues: {
income: application.income,
incomePeriod: application.incomePeriod,
},
shouldFocusError: false,
})

const onSubmit = (data) => {
const onSubmit = async (data) => {
const validation = await trigger()
if (!validation) return

const { income, incomePeriod } = data
const incomeValue = income.replaceAll(",", "")
// Skip validation of total income if the applicant has income vouchers.
Expand Down
7 changes: 5 additions & 2 deletions sites/public/src/pages/applications/financial/vouchers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,15 @@ const ApplicationVouchers = () => {
: 3

// eslint-disable-next-line @typescript-eslint/unbound-method
const { register, handleSubmit, errors, getValues } = useForm({
const { register, handleSubmit, errors, getValues, trigger } = useForm({
defaultValues: { incomeVouchers: application.incomeVouchers?.toString() },
shouldFocusError: false,
})

const onSubmit = (data) => {
const onSubmit = async (data) => {
const validation = await trigger()
if (!validation) return

const { incomeVouchers } = data
const toSave = { incomeVouchers: JSON.parse(incomeVouchers) }

Expand Down
6 changes: 4 additions & 2 deletions sites/public/src/pages/applications/household/ada.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const ApplicationAda = () => {
const currentPageSection = 2

// eslint-disable-next-line @typescript-eslint/unbound-method
const { register, handleSubmit, setValue, errors, getValues, clearErrors } = useForm<
const { register, handleSubmit, setValue, errors, getValues, clearErrors, trigger } = useForm<
Record<string, any>
>({
defaultValues: {
Expand All @@ -34,7 +34,9 @@ const ApplicationAda = () => {
shouldFocusError: false,
})

const onSubmit = (data) => {
const onSubmit = async (data) => {
const validation = await trigger()
if (!validation) return
conductor.currentStep.save({
accessibility: {
mobility: !!data["app-accessibility-mobility"],
Expand Down
4 changes: 2 additions & 2 deletions sites/public/src/pages/applications/household/add-members.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ const ApplicationAddMembers = () => {
const { conductor, application, listing } = useFormConductor("addMembers")
const router = useRouter()
const currentPageSection = 2
const householdSize = application.householdMembers.length + 1
const householdSize = parseInt(application.householdMembers.length) + 1

// eslint-disable-next-line @typescript-eslint/unbound-method
const { errors, handleSubmit, register, clearErrors } = useForm()
const onSubmit = () => {
conductor.currentStep.save({
householdSize: application.householdMembers.length + 1,
householdSize: parseInt(application.householdMembers.length) + 1,
})
conductor.routeToNextOrReturnUrl()
}
Expand Down
6 changes: 4 additions & 2 deletions sites/public/src/pages/applications/household/changes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ const ApplicationHouseholdChanges = () => {
const currentPageSection = 2

// eslint-disable-next-line @typescript-eslint/unbound-method
const { register, handleSubmit, errors, getValues } = useForm<Record<string, any>>({
const { register, handleSubmit, errors, getValues, trigger } = useForm<Record<string, any>>({
defaultValues: { householdExpectingChanges: application.householdExpectingChanges?.toString() },
shouldFocusError: false,
})
const onSubmit = (data) => {
const onSubmit = async (data) => {
const validation = await trigger()
if (!validation) return
const { householdExpectingChanges } = data
conductor.currentStep.save({
householdExpectingChanges: householdExpectingChanges === "true",
Expand Down
7 changes: 5 additions & 2 deletions sites/public/src/pages/applications/household/live-alone.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@ const ApplicationLiveAlone = () => {
const currentPageSection = 2

// eslint-disable-next-line @typescript-eslint/unbound-method
const { handleSubmit, register, errors, clearErrors } = useForm()
const onSubmit = () => {
const { handleSubmit, register, errors, clearErrors, trigger } = useForm()

const onSubmit = async () => {
const validation = await trigger()
if (!validation) return
conductor.sync()
conductor.routeToNextOrReturnUrl()
}
Expand Down
7 changes: 5 additions & 2 deletions sites/public/src/pages/applications/household/member.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,13 @@ const ApplicationMember = () => {
}

// eslint-disable-next-line @typescript-eslint/unbound-method
const { register, handleSubmit, errors, watch } = useForm({
const { register, handleSubmit, errors, watch, trigger } = useForm({
shouldFocusError: false,
})
const onSubmit = (data) => {
const onSubmit = async (data) => {
const validation = await trigger()
if (!validation) return

application.householdMembers[memberId] = { ...member, ...data } as HouseholdMember
conductor.sync()
void router.push("/applications/household/add-members")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ const ApplicationPreferredUnits = () => {
const currentPageSection = 2

// eslint-disable-next-line @typescript-eslint/unbound-method
const { register, handleSubmit, errors } = useForm()
const { register, handleSubmit, errors, trigger } = useForm()

const onSubmit = (data) => {
const onSubmit = async (data) => {
const validation = await trigger()
if (!validation) return
const { preferredUnit } = data

// save units always as an array (when is only one option, react-hook-form stores an option as string)
Expand Down
7 changes: 5 additions & 2 deletions sites/public/src/pages/applications/household/student.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@ const ApplicationHouseholdStudent = () => {
const currentPageSection = 2

// eslint-disable-next-line @typescript-eslint/unbound-method
const { register, handleSubmit, errors, getValues } = useForm<Record<string, any>>({
const { register, handleSubmit, errors, getValues, trigger } = useForm<Record<string, any>>({
defaultValues: { householdStudent: application.householdStudent?.toString() },
shouldFocusError: false,
})
const onSubmit = (data) => {

const onSubmit = async (data) => {
const validation = await trigger()
if (!validation) return
const { householdStudent } = data
conductor.currentStep.save({
householdStudent: householdStudent === "true",
Expand Down
7 changes: 3 additions & 4 deletions sites/public/src/pages/applications/start/autofill.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,16 @@ export default () => {
setSubmitted(true)
if (previousApplication && useDetails) {
const withUpdatedLang = {
...previousApplication,
...JSON.parse(JSON.stringify(previousApplication)),
language: router.locale,
}

conductor.application = withUpdatedLang
} else {
const newApplication = {
...blankApplication,
conductor.application = {
...JSON.parse(JSON.stringify(blankApplication)),
language: router.locale,
}
conductor.application = newApplication
}

context.syncApplication(conductor.application)
Expand Down

0 comments on commit 48cb352

Please sign in to comment.