Skip to content

Commit

Permalink
fix(web): Change form field value to unique value (#15612)
Browse files Browse the repository at this point in the history
* Change form field value to unique value

* Handle recipient form field decider

* Add again ts ignore for web build

---------

Co-authored-by: runarvestmann <[email protected]>
  • Loading branch information
mannipje and RunarVestmann authored Aug 9, 2024
1 parent 0b21114 commit 61ba7e9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
31 changes: 18 additions & 13 deletions apps/web/components/Form/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ const CREATE_UPLOAD_URL = gql`
}
}
`
const getUniqueFormFieldValue = (field: FormFieldProps['field']) => {
return slugify(field.title + '-' + field.id)
}

export enum FormFieldType {
CHECKBOXES = 'checkboxes',
Expand Down Expand Up @@ -257,7 +260,7 @@ export const Form = ({ form }: FormProps) => {
const [data, setData] = useState<Record<string, string>>(() => {
const fields = form.fields
.filter((field) => field.type !== FormFieldType.INFORMATION)
.map((field): [string, string] => [slugify(field.title), ''])
.map((field): [string, string] => [getUniqueFormFieldValue(field), ''])

if (defaultNameFieldIsShown) {
fields.push(['name', ''])
Expand All @@ -273,7 +276,7 @@ export const Form = ({ form }: FormProps) => {
Object.fromEntries(
form.fields
.filter((field) => field.type === FormFieldType.FILE)
.map((field) => [slugify(field.title), []]),
.map((field) => [getUniqueFormFieldValue(field), []]),
),
)
const [errors, setErrors] = useState<ErrorData[]>([])
Expand Down Expand Up @@ -331,7 +334,9 @@ export const Form = ({ form }: FormProps) => {
const validate = () => {
const err = Object.keys(data)
.map((slug) => {
const field = form.fields.find((f) => slugify(f.title) === slug)
const field = form.fields.find(
(f) => getUniqueFormFieldValue(f) === slug,
)

// Handle name and email
if (!field) {
Expand Down Expand Up @@ -438,7 +443,7 @@ export const Form = ({ form }: FormProps) => {
form.fields
.filter((field) => field.type !== FormFieldType.INFORMATION)
.map((field) => {
const value = data[slugify(field.title)]
const value = data[getUniqueFormFieldValue(field)]
if (field.type === FormFieldType.ACCEPT_TERMS) {
return `${field.title}\nSvar: ${
value === 'true'
Expand Down Expand Up @@ -469,8 +474,8 @@ export const Form = ({ form }: FormProps) => {

/** Returns the value for the form field that decides what email the form will be sent to */
const getRecipientFormFieldDeciderValue = (): string | undefined => {
if (!form?.recipientFormFieldDecider?.title) return undefined
return data[slugify(form.recipientFormFieldDecider.title)]
if (!form?.recipientFormFieldDecider?.id) return undefined
return data[getUniqueFormFieldValue(form.recipientFormFieldDecider)]
}

const uploadFile = async (
Expand Down Expand Up @@ -545,7 +550,7 @@ export const Form = ({ form }: FormProps) => {
.map((field) => {
return new Promise((resolve, reject) => {
Promise.all(
fileList[slugify(field.title)].map((file) => {
fileList[getUniqueFormFieldValue(field)].map((file) => {
return new Promise((resolve, reject) => {
createUploadUrl({
variables: {
Expand All @@ -557,7 +562,7 @@ export const Form = ({ form }: FormProps) => {
uploadFile(
file,
response.data.createUploadUrl,
slugify(field.title),
getUniqueFormFieldValue(field),
).then(() =>
resolve(
response.data?.createUploadUrl.fields.key,
Expand All @@ -572,7 +577,7 @@ export const Form = ({ form }: FormProps) => {
}),
)
.then((fileNames) =>
resolve([slugify(field.title), fileNames]),
resolve([getUniqueFormFieldValue(field), fileNames]),
)
.catch(() => reject())
})
Expand All @@ -585,10 +590,10 @@ export const Form = ({ form }: FormProps) => {
form.fields
.filter((field) => field.type === FormFieldType.FILE)
.map((field) => [
slugify(field.title),
getUniqueFormFieldValue(field),
JSON.stringify(
(files as string[][]).find(
(file) => file[0] === slugify(field.title),
(file) => file[0] === getUniqueFormFieldValue(field),
)?.[1],
),
]),
Expand Down Expand Up @@ -729,7 +734,7 @@ export const Form = ({ form }: FormProps) => {
{form.fields
.filter((field) => field.type !== FormFieldType.FILE)
.map((field) => {
const slug = slugify(field.title)
const slug = getUniqueFormFieldValue(field)
return (
<FormField
key={field.id}
Expand Down Expand Up @@ -760,7 +765,7 @@ export const Form = ({ form }: FormProps) => {
{form.fields
.filter((field) => field.type === FormFieldType.FILE)
.map((field) => {
const slug = slugify(field.title)
const slug = getUniqueFormFieldValue(field)
return (
<InputFileUpload
key={slug}
Expand Down
2 changes: 2 additions & 0 deletions apps/web/screens/queries/fragments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,7 @@ export const slices = gql`
intro
defaultFieldNamespace
fields {
id
title
name
placeholder
Expand All @@ -547,6 +548,7 @@ export const slices = gql`
aboutYouHeadingText
questionsHeadingText
recipientFormFieldDecider {
id
title
placeholder
type
Expand Down

0 comments on commit 61ba7e9

Please sign in to comment.