Skip to content

Commit 713169d

Browse files
TschontiGerviba
authored andcommitted
update to work with new backend
1 parent 66f28a8 commit 713169d

File tree

6 files changed

+48
-45
lines changed

6 files changed

+48
-45
lines changed

frontend/src/pages/form/components/GridField.tsx

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Grid, GridItem } from '@chakra-ui/react'
1+
import { Grid, GridItem, Input } from '@chakra-ui/react'
22
import { FormField, GridFieldValues } from '../../../util/views/form.view'
33
import { Fragment, useEffect } from 'react'
44
import { GridFieldItem } from './GridFieldItem'
@@ -9,21 +9,30 @@ type Props = {
99
field: FormField
1010
choice: boolean
1111
disabled: boolean
12+
dirty: boolean
1213
}
1314

14-
export function GridField({ field, choice, disabled }: Props) {
15+
export function GridField({ field, choice, disabled, dirty }: Props) {
1516
try {
1617
const format: GridFieldValues = JSON.parse(field.values)
1718
if (!isValidGridField(format)) {
1819
throw new Error('Invalid form format')
1920
}
20-
const { setValue } = useFormContext()
21+
const { setValue, watch, register } = useFormContext()
2122

2223
useEffect(() => {
23-
if (choice) {
24-
format.questions.forEach((q) => {
25-
setValue(`${format.prefix}${field.fieldName}${q.key}`, format.options[0].key)
26-
})
24+
if (!dirty) {
25+
if (choice) {
26+
format.questions.forEach((q) => {
27+
setValue(field.fieldName, { ...watch(field.fieldName), [q.key]: format.options[0].key })
28+
})
29+
} else {
30+
format.questions.forEach((q) => {
31+
format.options.forEach((o) => {
32+
setValue(field.fieldName, { ...watch(field.fieldName), [`${q.key}_${o.key}`]: false })
33+
})
34+
})
35+
}
2736
}
2837
}, [])
2938
return (
@@ -34,6 +43,7 @@ export function GridField({ field, choice, disabled }: Props) {
3443
templateColumns={`repeat(${format.options.length + 1}, 1fr)`}
3544
gap={2}
3645
>
46+
<Input {...register(field.fieldName)} disabled hidden />
3747
<GridItem />
3848
{format.options.map((opt) => (
3949
<GridItem key={opt.key}>{opt.label}</GridItem>
@@ -44,13 +54,7 @@ export function GridField({ field, choice, disabled }: Props) {
4454
<GridItem>{q.label}</GridItem>
4555
{format.options.map((o) => (
4656
<GridItem key={o.key}>
47-
<GridFieldItem
48-
radio={choice}
49-
disabled={disabled}
50-
questionKey={q.key}
51-
optionKey={o.key}
52-
fieldName={format.prefix + field.fieldName}
53-
/>
57+
<GridFieldItem radio={choice} disabled={disabled} questionKey={q.key} optionKey={o.key} fieldName={field.fieldName} />
5458
</GridItem>
5559
))}
5660
</Fragment>
@@ -65,7 +69,6 @@ export function GridField({ field, choice, disabled }: Props) {
6569

6670
function isValidGridField(metadata: any): metadata is GridFieldValues {
6771
return (
68-
typeof metadata.prefix === 'string' &&
6972
Array.isArray(metadata.questions) &&
7073
Array.isArray(metadata.options) &&
7174
![...metadata.questions, ...metadata.options].some((v) => !v.label || !v.key)

frontend/src/pages/form/components/GridFieldItem.tsx

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,19 @@ type Props = {
1111
}
1212

1313
export function GridFieldItem({ questionKey, optionKey, fieldName, disabled, radio }: Props) {
14-
const { register, setValue, watch } = useFormContext()
14+
const { setValue, watch } = useFormContext()
1515

1616
const onChange = (e: ChangeEvent<HTMLInputElement>) => {
1717
if (radio) {
18-
setValue(`${fieldName}${questionKey}`, optionKey)
18+
setValue(fieldName, { ...watch(fieldName), [questionKey]: optionKey })
1919
} else {
20-
setValue(`${fieldName}${questionKey}${optionKey}`, `${e.target.checked}`)
20+
setValue(fieldName, { ...watch(fieldName), [`${questionKey}_${optionKey}`]: e.target.checked })
2121
}
2222
}
2323

2424
return radio ? (
25-
<Radio
26-
{...register(`${fieldName}${questionKey}`)}
27-
isChecked={watch(`${fieldName}${questionKey}`) === optionKey}
28-
onChange={onChange}
29-
colorScheme="brand"
30-
isDisabled={disabled}
31-
/>
25+
<Radio isChecked={watch(fieldName)[questionKey] === optionKey} onChange={onChange} colorScheme="brand" isDisabled={disabled} />
3226
) : (
33-
<Checkbox
34-
{...register(`${fieldName}${questionKey}${optionKey}`)}
35-
isChecked={`${watch(`${fieldName}${questionKey}${optionKey}`)}` === 'true'}
36-
onChange={onChange}
37-
colorScheme="brand"
38-
isDisabled={disabled}
39-
/>
27+
<Checkbox isChecked={watch(fieldName)[`${questionKey}_${optionKey}`]} onChange={onChange} colorScheme="brand" isDisabled={disabled} />
4028
)
4129
}

frontend/src/pages/form/components/autoFormField.tsx

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { ReactNode } from 'react'
33
import { Control, useController } from 'react-hook-form'
44
import Markdown from '../../../common-components/Markdown'
55
import { VotingField } from '../../../common-components/VotingField'
6-
import { isCheckbox } from '../../../util/core-functions.util'
6+
import { isCheckbox, isGridField } from '../../../util/core-functions.util'
77
import { FormField, FormFieldVariants, VotingFieldOption } from '../../../util/views/form.view'
88
import { GridField } from './GridField'
99

@@ -19,7 +19,9 @@ export const AutoFormField = ({ fieldProps, control, disabled, submittedValue }:
1919
let defaultValue = isCheckbox(fieldProps.type) ? fieldProps.defaultValue === 'true' : fieldProps.defaultValue
2020

2121
if (submittedValue) {
22-
defaultValue = isCheckbox(fieldProps.type) ? submittedValue === 'true' : submittedValue
22+
if (isCheckbox(fieldProps.type)) defaultValue = submittedValue === 'true'
23+
else if (isGridField(fieldProps.type)) defaultValue = JSON.parse(submittedValue)
24+
else defaultValue = submittedValue
2325
} else if (!defaultValue) {
2426
if (fieldProps.type === FormFieldVariants.SELECT) defaultValue = selectValues[0]
2527
else defaultValue = ''
@@ -123,7 +125,14 @@ export const AutoFormField = ({ fieldProps, control, disabled, submittedValue }:
123125
break
124126
case FormFieldVariants.SELECTION_GRID:
125127
case FormFieldVariants.CHOICE_GRID:
126-
component = <GridField disabled={disabled} field={fieldProps} choice={fieldProps.type === FormFieldVariants.CHOICE_GRID} />
128+
component = (
129+
<GridField
130+
disabled={disabled}
131+
field={fieldProps}
132+
dirty={!!submittedValue}
133+
choice={fieldProps.type === FormFieldVariants.CHOICE_GRID}
134+
/>
135+
)
127136
break
128137
}
129138
return (

frontend/src/pages/form/form.page.tsx

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ import { CmschPage } from '../../common-components/layout/CmschPage'
1515
import Markdown from '../../common-components/Markdown'
1616
import { PageStatus } from '../../common-components/PageStatus'
1717
import { CookieKeys } from '../../util/configs/cookies.config'
18-
import { isCheckbox } from '../../util/core-functions.util'
18+
import { isCheckbox, isGridField } from '../../util/core-functions.util'
1919
import { l } from '../../util/language'
2020
import { AbsolutePaths } from '../../util/paths'
2121
import { FormFieldVariants, FormStatus, FormSubmitMessage, FormSubmitResult } from '../../util/views/form.view'
2222
import { AutoFormField } from './components/autoFormField'
2323
import { FormStatusBadge } from './components/formStatusBadge'
2424

25-
interface FormPageProps {}
25+
interface FormPageProps { }
2626

2727
const FormPage: FunctionComponent<FormPageProps> = () => {
2828
const toast = useToast()
@@ -50,15 +50,15 @@ const FormPage: FunctionComponent<FormPageProps> = () => {
5050
const { form, submission, message, status, detailsValidated } = data
5151
const available = form && form.availableFrom * 1000 < Date.now() && form.availableUntil * 1000 > Date.now() && !detailsValidated
5252

53-
const onSubmit = (values: Record<string, string>) => {
54-
form?.formFields
55-
.filter(({ type }) => type === FormFieldVariants.CHOICE_GRID || type === FormFieldVariants.SELECTION_GRID)
56-
.forEach(({ fieldName }) => {
57-
delete values[fieldName]
58-
})
53+
const onSubmit = (values: Record<string, any>) => {
5954
const newValues: Record<string, string> = {}
6055
Object.keys(values).forEach((v) => {
61-
newValues[v] = values[v].toString()
56+
const formField = form?.formFields.find((ff) => ff.fieldName === v)
57+
if (isGridField(formField?.type)) {
58+
newValues[v] = JSON.stringify(values[v])
59+
} else {
60+
newValues[v] = values[v]
61+
}
6262
})
6363
if (available) {
6464
submit(newValues, status !== FormStatus.NO_SUBMISSION)

frontend/src/util/core-functions.util.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ export function isCheckbox(type: FormFieldVariants) {
7171
return type === FormFieldVariants.CHECKBOX || type === FormFieldVariants.MUST_AGREE
7272
}
7373

74+
export function isGridField(type?: FormFieldVariants | undefined) {
75+
return type === FormFieldVariants.CHOICE_GRID || type === FormFieldVariants.SELECTION_GRID
76+
}
77+
7478
export function getCdnUrl(path: string) {
7579
return joinPath(API_BASE_URL, 'cdn', path)
7680
}

frontend/src/util/views/form.view.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ export type FormField = {
3030
}
3131

3232
export type GridFieldValues = {
33-
prefix: string
3433
options: {
3534
key: string
3635
label: string

0 commit comments

Comments
 (0)