-
Notifications
You must be signed in to change notification settings - Fork 2
/
InteractiveForm.tsx
103 lines (88 loc) · 3.18 KB
/
InteractiveForm.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import React, { useState } from 'react'
import { useForm } from 'react-hook-form'
import { useTranslation } from 'react-i18next'
import { enqueueSnackbar } from 'notistack'
import { Box, Grid } from '@mui/material'
import useResults from '../../hooks/useResults'
import useSurvey from '../../hooks/useSurvey'
import usePersistForm from '../../hooks/usePersistForm'
import { useSaveEntryMutation } from '../../hooks/useSaveEntryMutation'
import HelloBanner from './HelloBanner'
import RenderSurvey from './RenderSurvey'
import Results from '../ResultPage/Results'
import { useResultData } from '../../contexts/ResultDataContext'
import styles from '../../styles'
import { FormValues, RiskData } from '../../types'
import { FORM_DATA_KEY, LOCATION_KEY } from '../../../config'
const InteractiveForm = () => {
const { survey, isLoading } = useSurvey()
const { results } = useResults(survey?.id)
const { t } = useTranslation()
const mutation = useSaveEntryMutation(survey?.id)
const [riskData, setRiskData] = useState<RiskData>()
const sessionLocation = sessionStorage.getItem(LOCATION_KEY)
const [showResults, setShowResults] = useState(sessionLocation === 'results')
const [isSubmitted, setIsSubmitted] = useState(false)
const [submitButtonLoading, setSubmitButtonLoading] = useState(false)
const { resultData, setResultData } = useResultData()
const { formStyles } = styles
const { handleSubmit, control, watch } = useForm({
mode: 'onSubmit',
shouldUnregister: true,
defaultValues: resultData,
})
usePersistForm({ value: watch(), sessionStorageKey: FORM_DATA_KEY })
if (!survey || isLoading || !results) return null
const onSubmit = async (data: FormValues) => {
setSubmitButtonLoading(true)
const submittedData = data
setResultData(submittedData)
try {
const createdData = await mutation.mutateAsync(submittedData)
const risks = createdData.data
sessionStorage.setItem(LOCATION_KEY, 'results')
setShowResults(true)
document
?.getElementById('survey-main-section')
?.scrollIntoView({ behavior: 'instant' })
setRiskData(risks)
setIsSubmitted(true)
} catch (error) {
// eslint-disable-next-line no-console
console.log(error)
enqueueSnackbar(t('common:submitError'), { variant: 'error' })
}
setSubmitButtonLoading(false)
}
return (
<Box sx={formStyles.formWrapper}>
<Grid id="survey-main-section">
{!showResults && (
<Grid item sm={12}>
<HelloBanner />
</Grid>
)}
<Grid item sm={12}>
<form
style={{ display: showResults ? 'none' : 'block' }}
onSubmit={handleSubmit(onSubmit)}
>
<RenderSurvey
questions={survey.Questions}
control={control}
watch={watch}
isSubmitted={isSubmitted}
submitButtonLoading={submitButtonLoading}
/>
</form>
</Grid>
{riskData && showResults && (
<Grid item sm={12}>
<Results setShowResults={setShowResults} riskData={riskData} />
</Grid>
)}
</Grid>
</Box>
)
}
export default InteractiveForm