-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Trim input text #1391
Trim input text #1391
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,24 +8,35 @@ import { FormattedMessageType } from '../../Types/Questions'; | |
import { FormData } from '../../Types/FormData'; | ||
import { Context } from '../Wrapper/Wrapper'; | ||
import { updateScreen } from '../../Assets/updateScreen'; | ||
import ErrorMessageWrapper from '../ErrorMessage/ErrorMessageWrapper.tsx'; | ||
import { useConfig } from '../Config/configHook.tsx'; | ||
import ErrorMessageWrapper from '../ErrorMessage/ErrorMessageWrapper'; | ||
import { useConfig } from '../Config/configHook'; | ||
import * as z from 'zod'; | ||
import QuestionHeader from '../QuestionComponents/QuestionHeader'; | ||
import QuestionLeadText from '../QuestionComponents/QuestionLeadText'; | ||
import QuestionQuestion from '../QuestionComponents/QuestionQuestion'; | ||
import PrevAndContinueButtons from '../PrevAndContinueButtons/PrevAndContinueButtons.tsx'; | ||
import PrevAndContinueButtons from '../PrevAndContinueButtons/PrevAndContinueButtons'; | ||
import { useDefaultBackNavigationFunction, useGoToNextStep } from '../QuestionComponents/questionHooks'; | ||
|
||
export const ZipcodeStep = () => { | ||
const { formData, locale, setFormData } = useContext(Context); | ||
const { uuid } = useParams(); | ||
const backNavigationFunction = useDefaultBackNavigationFunction('zipcode'); | ||
|
||
const countiesByZipcode = useConfig('counties_by_zipcode'); | ||
const countiesByZipcode = useConfig<{ [key: string]: { [key: string]: string } }>('counties_by_zipcode'); | ||
|
||
const checkCountyIsValid = ({ zipcode, county }: { zipcode: string; county: string }) => { | ||
const validCounties = countiesByZipcode[zipcode]; | ||
|
||
if (validCounties && county in validCounties) { | ||
return true; | ||
} | ||
return false; | ||
}; | ||
|
||
const numberMustBeFiveDigitsLongRegex = /^\d{5}$/; | ||
const zipcodeSchema = z | ||
.string() | ||
.trim() | ||
.regex(numberMustBeFiveDigitsLongRegex) | ||
.refine((data) => data in countiesByZipcode); | ||
|
||
|
@@ -50,27 +61,19 @@ export const ZipcodeStep = () => { | |
}); | ||
|
||
const currentZipcodeValue = watch('zipcode'); | ||
const shouldShowCountyInput = zipcodeSchema.safeParse(currentZipcodeValue).success; | ||
const parsedZipCode = zipcodeSchema.safeParse(currentZipcodeValue); | ||
|
||
const nextStep = useGoToNextStep('zipcode'); | ||
|
||
const formSubmitHandler = async (zipCodeAndCountyData: FormData) => { | ||
const formSubmitHandler = (zipCodeAndCountyData: FormData) => { | ||
if (uuid) { | ||
const updatedFormData = { ...formData, ...zipCodeAndCountyData }; | ||
setFormData(updatedFormData); | ||
await updateScreen(uuid, updatedFormData, locale); | ||
updateScreen(uuid, updatedFormData, locale); | ||
nextStep(); | ||
} | ||
}; | ||
|
||
const checkCountyIsValid = ({ zipcode, county }) => { | ||
const validCounties = countiesByZipcode[zipcode]; | ||
|
||
if (validCounties && county in validCounties) { | ||
return true; | ||
} | ||
return false; | ||
}; | ||
|
||
const createMenuItems = (disabledSelectMenuItemText: FormattedMessageType, options: Record<string, string>) => { | ||
const disabledSelectMenuItem = ( | ||
<MenuItem value="disabled-select" key="disabled-select" disabled> | ||
|
@@ -138,7 +141,7 @@ export const ZipcodeStep = () => { | |
/> | ||
)} | ||
/> | ||
{shouldShowCountyInput && ( | ||
{parsedZipCode.success && ( | ||
<div> | ||
<QuestionQuestion> | ||
<FormattedMessage id="questions.zipcode-a" defaultMessage="Please select a county:" /> | ||
|
@@ -164,7 +167,7 @@ export const ZipcodeStep = () => { | |
id="questions.zipcode-a-disabledSelectMenuItemText" | ||
defaultMessage="Select a county" | ||
/>, | ||
countiesByZipcode[watch('zipcode')], | ||
countiesByZipcode[parsedZipCode.data], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you think about using the original expression? I understand that there are performance trade offs with using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The new way, removes the spaces, so that we don't get a key error. |
||
)} | ||
</Select> | ||
<FormHelperText>{errors.county !== undefined && renderCountyHelperText()}</FormHelperText> | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think about using the original expression? It seems more readable and explicit in terms of why the parsed zip code is being used.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This fixes a TS error, and allows us to use the parsed zip code in multiple places.