-
Notifications
You must be signed in to change notification settings - Fork 234
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WSTEAM1-851 - UGC form state management (#11425)
- Loading branch information
Showing
18 changed files
with
576 additions
and
160 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/** @jsx jsx */ | ||
import React from 'react'; | ||
import { jsx } from '@emotion/react'; | ||
import { useFormContext } from '../FormContext'; | ||
import { Field } from '../types'; | ||
import FormField from '../FormField'; | ||
import styles from './styles'; | ||
import Submit from '../SubmitButton'; | ||
|
||
export default function Form({ fields }: { fields: Field[] }) { | ||
const { handleSubmit, submissionError } = useFormContext(); | ||
|
||
const formFields = fields?.map(({ id, label, type, htmlType, textArea }) => ( | ||
<FormField | ||
key={id} | ||
id={id} | ||
label={label} | ||
type={type} | ||
htmlType={htmlType} | ||
textArea={textArea} | ||
/> | ||
)); | ||
|
||
return ( | ||
<> | ||
<form onSubmit={handleSubmit}> | ||
{formFields} | ||
<Submit /> | ||
</form> | ||
{submissionError && ( | ||
<div css={styles.submissionError}> | ||
{`${submissionError.message} - ${submissionError.status}`} | ||
</div> | ||
)} | ||
</> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { css } from '@emotion/react'; | ||
|
||
export default { | ||
submissionError: () => | ||
css({ | ||
color: 'black', | ||
fontFamily: 'sans-serif', | ||
backgroundColor: 'pink', | ||
padding: '1rem', | ||
margin: '1rem 0', | ||
}), | ||
}; |
104 changes: 104 additions & 0 deletions
104
ws-nextjs-app/pages/[service]/send/[id]/FormContext/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import React, { | ||
createContext, | ||
FormEvent, | ||
PropsWithChildren, | ||
useContext, | ||
useState, | ||
} from 'react'; | ||
import { FetchError } from '#app/models/types/fetch'; | ||
import { v4 as uuid } from 'uuid'; | ||
|
||
import { useRouter } from 'next/router'; | ||
import { OK } from '#app/lib/statusCodes.const'; | ||
import { | ||
Field, | ||
OnChangeHandler, | ||
OnChangeInputName, | ||
OnChangeInputValue, | ||
} from '../types'; | ||
|
||
type SubmissionError = { | ||
message: string; | ||
status: number; | ||
} | null; | ||
|
||
type ContextProps = { | ||
formState: Record<OnChangeInputName, OnChangeInputValue | null>; | ||
handleChange: OnChangeHandler; | ||
handleSubmit: (event: FormEvent) => Promise<void>; | ||
submissionError?: SubmissionError; | ||
}; | ||
|
||
const FormContext = createContext({} as ContextProps); | ||
|
||
const getInitialFormState = ( | ||
fields: Field[], | ||
): Record<OnChangeInputName, OnChangeInputValue | null> => | ||
fields?.reduce((acc, field) => ({ ...acc, [field.id]: null }), {}); | ||
|
||
export const FormContextProvider = ({ | ||
fields, | ||
children, | ||
}: PropsWithChildren<{ fields: Field[] }>) => { | ||
const { | ||
query: { id }, | ||
} = useRouter(); | ||
|
||
const [formState, setFormState] = useState(getInitialFormState(fields)); | ||
const [submissionError, setSubmissionError] = useState<SubmissionError>(null); | ||
|
||
const handleChange = (name: OnChangeInputName, value: OnChangeInputValue) => { | ||
setFormState(prevState => { | ||
return { ...prevState, [name]: value }; | ||
}); | ||
}; | ||
|
||
const handleSubmit = async (event: FormEvent) => { | ||
event.preventDefault(); | ||
|
||
// Reset error state | ||
setSubmissionError(null); | ||
|
||
const formData = new FormData(); | ||
|
||
// TODO: This is a mock data, we should use the formState instead | ||
Object.entries({ surname: 'BBC TEST NAME' }).forEach(([key, value]) => { | ||
formData.append(key, value); | ||
}); | ||
|
||
try { | ||
const url = `https://www.bbc.com/ugc/send/${id}?said=${uuid()}`; | ||
|
||
const req = new XMLHttpRequest(); | ||
req.open('POST', url, true); | ||
|
||
req.onreadystatechange = () => { | ||
if (req.readyState === XMLHttpRequest.DONE) { | ||
if (req.status !== OK) { | ||
setSubmissionError({ | ||
message: req.responseText, | ||
status: req.status, | ||
}); | ||
} | ||
} | ||
}; | ||
|
||
req.send(formData); | ||
} catch (error) { | ||
const { message, status } = error as FetchError; | ||
setSubmissionError({ message, status }); | ||
} | ||
}; | ||
|
||
return ( | ||
<FormContext.Provider | ||
value={{ formState, handleChange, handleSubmit, submissionError }} | ||
> | ||
{children} | ||
</FormContext.Provider> | ||
); | ||
}; | ||
|
||
export function useFormContext() { | ||
return useContext(FormContext); | ||
} |
Oops, something went wrong.