Skip to content

Commit

Permalink
consolidated fe and aligned be - failing on submission still
Browse files Browse the repository at this point in the history
  • Loading branch information
scottheng96 committed Dec 17, 2024
1 parent dd32a67 commit 78a5871
Show file tree
Hide file tree
Showing 31 changed files with 396 additions and 228 deletions.
9 changes: 6 additions & 3 deletions __tests__/unit/backend/helpers/generate-form-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,9 +275,12 @@ export const generateNewSingleAnswerResponse = (
customParams?: Partial<ProcessedSingleAnswerResponse>,
): ProcessedSingleAnswerResponse => {
if (
[BasicField.Attachment, BasicField.Table, BasicField.Checkbox].includes(
fieldType,
)
[
BasicField.Attachment,
BasicField.Table,
BasicField.Checkbox,
BasicField.Address,
].includes(fieldType)
) {
throw new Error(
'Call the custom response generator functions for attachment, table and checkbox.',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { useToast } from '~hooks/useToast'
import IconButton from '~components/IconButton'
import Tooltip from '~components/Tooltip'
import {
AddressField,
AddressCompoundField,
AttachmentField,
CheckboxField,
ChildrenCompoundField,
Expand Down Expand Up @@ -514,6 +514,6 @@ const FieldRow = ({ field, ...rest }: FieldRowProps) => {
case BasicField.Children:
return <ChildrenCompoundField schema={field} {...rest} />
case BasicField.Address:
return <AddressField schema={field} {...rest} />
return <AddressCompoundField schema={field} {...rest} />
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { Meta, StoryFn } from '@storybook/react'

import { AddressFieldBase, BasicField } from '~shared/types'
import { AddressCompoundFieldBase, BasicField } from '~shared/types'

import { createFormBuilderMocks } from '~/mocks/msw/handlers/admin-form'

import { EditFieldDrawerDecorator, StoryRouter } from '~utils/storybook'

import { EditAddress, EditAddressProps } from './EditAddress'

const DEFAULT_ADDRESS_FIELD: AddressFieldBase = {
const DEFAULT_ADDRESS_FIELD: AddressCompoundFieldBase = {
title: 'Local address',
description: '',
required: true,
Expand Down Expand Up @@ -38,7 +38,7 @@ export default {
} as Meta<EditAddressProps>

interface StoryArgs {
field: AddressFieldBase
field: AddressCompoundFieldBase
}

const Template: StoryFn<StoryArgs> = ({ field }) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useMemo } from 'react'
import { FormControl } from '@chakra-ui/react'
import { extend, pick } from 'lodash'

import { AddressFieldBase } from '~shared/types/field'
import { AddressCompoundFieldBase } from '~shared/types/field'

import { createBaseValidationRules } from '~utils/fieldValidation'
import FormErrorMessage from '~components/FormControl/FormErrorMessage'
Expand All @@ -17,10 +17,10 @@ import { FormFieldDrawerActions } from '../common/FormFieldDrawerActions'
import { EditFieldProps } from '../common/types'
import { useEditFieldForm } from '../common/useEditFieldForm'

export type EditAddressProps = EditFieldProps<AddressFieldBase>
export type EditAddressProps = EditFieldProps<AddressCompoundFieldBase>

type EditAddressInputs = Pick<
AddressFieldBase,
AddressCompoundFieldBase,
'title' | 'description' | 'required'
>

Expand All @@ -37,7 +37,7 @@ export const EditAddress = ({ field }: EditAddressProps): JSX.Element => {
isLoading,
handleCancel,
setValue,
} = useEditFieldForm<EditAddressInputs, AddressFieldBase>({
} = useEditFieldForm<EditAddressInputs, AddressCompoundFieldBase>({
field,
transform: {
input: (inputField) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { BasicField } from '~shared/types/field'
import { FormColorTheme, FormResponseMode } from '~shared/types/form'

import {
AddressField,
AddressCompoundField,
AttachmentField,
CheckboxField,
ChildrenCompoundField,
Expand Down Expand Up @@ -121,7 +121,7 @@ export const FieldFactory = memo(
case BasicField.Table:
return <TableField schema={field} {...rest} />
case BasicField.Address:
return <AddressField schema={field} {...rest} />
return <AddressCompoundField schema={field} {...rest} />
case BasicField.Children:
return (
<ChildrenCompoundField
Expand Down
13 changes: 5 additions & 8 deletions frontend/src/features/public-form/utils/createSubmission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ export const createClearSubmissionWithVirusScanningFormData = (
return response
},
)

const attachments = getAttachmentsMap(formFields, formInputs)

// Convert content to FormData object.
Expand Down Expand Up @@ -248,6 +249,9 @@ const createResponsesArray = (
.map((ff) => transformInputsToOutputs(ff, formInputs[ff._id]))
.filter((output): output is FieldResponse => output !== null)

const v = validateResponses(transformedResponses)
console.log('testing')
console.log(v)
return validateResponses(transformedResponses)
}

Expand Down Expand Up @@ -288,14 +292,7 @@ const createResponsesV3 = (
if (!input) break
returnedInputs[ff._id] = {
fieldType: ff.fieldType,
answer: {
postalCode: input.postalCode,
blockNumber: input.blockNumber,
streetName: input.streetName,
buildingName: input.buildingName,
levelNumber: input.levelNumber,
unitNumber: input.unitNumber,
},
answer: input,
} as FieldResponseV3
break
}
Expand Down
41 changes: 32 additions & 9 deletions frontend/src/features/public-form/utils/inputTransformation.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Address } from 'cluster'
import { format, parse } from 'date-fns'
import { times } from 'lodash'

import { DATE_PARSE_FORMAT } from '~shared/constants/dates'
import {
AddressFieldResponseV3,
AddressCompoundFieldResponseV3,
AttachmentFieldResponseV3,
CheckboxFieldResponsesV3,
ChildrenCompoundFieldResponsesV3,
Expand All @@ -13,7 +14,11 @@ import {
VerifiableFieldResponseV3,
YesNoFieldResponseV3,
} from '~shared/types'
import { BasicField, FormFieldDto } from '~shared/types/field'
import {
AddressAttributes,
BasicField,
FormFieldDto,
} from '~shared/types/field'
import {
AddressResponse,
AttachmentResponse,
Expand All @@ -31,8 +36,8 @@ import { CHECKBOX_OTHERS_INPUT_VALUE } from '~templates/Field/Checkbox/constants
import { RADIO_OTHERS_INPUT_VALUE } from '~templates/Field/Radio/constants'
import { createTableRow } from '~templates/Field/Table/utils/createRow'
import {
AddressFieldSchema,
AddressFieldValues,
AddressCompoundFieldSchema,
AddressCompoundFieldValues,
AttachmentFieldSchema,
BaseFieldOutput,
CheckboxFieldSchema,
Expand Down Expand Up @@ -222,14 +227,32 @@ const transformToChildOutput = (
}

const transformToAddressOutput = (
schema: AddressFieldSchema,
input?: AddressFieldValues | AddressFieldResponseV3,
schema: AddressCompoundFieldSchema,
input?: AddressCompoundFieldValues | AddressCompoundFieldResponseV3,
): AddressResponse => {
let answer = ''
if (input?.postalCode !== undefined) answer = input.postalCode // TODO: do for all input fields, just using postalCode to test now
// let answerArray: AddressAttributes
const answerArray: string[][] = []
// if (input !== undefined) {
// answerArray = input.addressSubFields
// } else {
// answerArray = {
// postalCode: '',
// blockNumber: '',
// streetName: '',
// buildingName: '',
// levelNumber: '',
// unitNumber: '',
// }
// }
if (input !== undefined) {
Object.entries(input.addressSubFields).map(([key, value]) =>
answerArray.push([`${key}: ${value}`]),
)
}

return {
...pickBaseOutputFromSchema(schema),
answer,
answerArray,
}
}

Expand Down
1 change: 1 addition & 0 deletions frontend/src/services/ApiService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ export const processFetchResponse = async (response: Response) => {
throw new Error(`Non-2XX response: ${response.status}`)
} else {
const data = await response.json()
// console.log(data)
return data
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down
Loading

0 comments on commit 78a5871

Please sign in to comment.