Thanks to visit codestin.com
Credit goes to github.com

Skip to content

fix: error messages from workspaceScheduleXService #3255

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

Merged
merged 14 commits into from
Jul 28, 2022
Merged
Prev Previous commit
Next Next commit
Extend getFormHelpers to remap field name
  • Loading branch information
presleyp committed Jul 27, 2022
commit 77df9ca36e9025f6e33e568e7772452c8eea06c2
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ export const WorkspaceScheduleForm: FC<WorkspaceScheduleFormProps> = ({
</FormControl>

<TextField
{...formHelpers("ttl", ttlShutdownAt(form.values.ttl))}
{...formHelpers("ttl", ttlShutdownAt(form.values.ttl), "ttl_ms")}
disabled={isLoading}
inputProps={{ min: 0, step: 1 }}
label={Language.ttlLabel}
Expand Down
14 changes: 8 additions & 6 deletions site/src/util/formUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,21 @@ interface FormHelpers {
helperText?: ReactNode
}

// backendErrorName can be used if the backend names a field differently than the frontend does
export const getFormHelpers =
<T>(form: FormikContextType<T>, formErrors?: FormikErrors<T>) =>
(name: keyof T, HelperText: ReactNode = ""): FormHelpers => {
<T>(form: FormikContextType<T>, apiValidationErrors?: FormikErrors<T>) =>
(name: keyof T, HelperText: ReactNode = "", backendErrorName?: string): FormHelpers => {
if (typeof name !== "string") {
throw new Error(`name must be type of string, instead received '${typeof name}'`)
}
const apiErrorName = backendErrorName ?? name

// getIn is a util function from Formik that gets at any depth of nesting
// and is necessary for the types to work
const touched = getIn(form.touched, name)
const apiError = getIn(formErrors, name)
const validationError = getIn(form.errors, name)
const error = apiError ?? validationError
const apiError = getIn(apiValidationErrors, apiErrorName)
const frontendError = getIn(form.errors, name)
const error = apiError ?? frontendError
return {
...form.getFieldProps(name),
id: name,
Expand All @@ -49,7 +51,7 @@ export const getFormHelpers =
export const getFormHelpersWithError = <T>(
form: FormikContextType<T>,
error?: Error | unknown,
): ((name: keyof T, HelperText?: ReactNode) => FormHelpers) => {
): ((name: keyof T, HelperText?: ReactNode, errorName?: string) => FormHelpers) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Instead of ‘errorName’, we can call the argument field ‘fieldName’ or ‘backendFieldName’.

const apiValidationErrors =
isApiError(error) && hasApiFieldErrors(error)
? (mapApiErrorToFieldErrors(error.response.data) as FormikErrors<T>)
Expand Down