-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
79 additions
and
81 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
51 changes: 51 additions & 0 deletions
51
editor.planx.uk/src/@planx/components/Pay/Editor/helpers.ts
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,51 @@ | ||
import { REQUIRED_GOVPAY_METADATA } from "../model"; | ||
import { FormikGovPayMetadata } from "./GovPayMetadataSection"; | ||
|
||
/** | ||
* Helper method to handle Formik errors in arrays | ||
* Required as errors can be at array-level or field-level and the useFormikContext hook cannot correctly type infer this from the validation schema | ||
* Docs: https://formik.org/docs/api/fieldarray#fieldarray-validation-gotchas | ||
*/ | ||
export const parseError = ( | ||
errors: FormikGovPayMetadata, | ||
index: number, | ||
): string | undefined => { | ||
// No errors | ||
if (!errors) return; | ||
|
||
// Array-level error - handled at a higher level | ||
if (typeof errors === "string") return; | ||
|
||
// No error for this field | ||
if (!errors[index]) return; | ||
|
||
// Specific field-level error | ||
return errors[index].key || errors[index].value; | ||
}; | ||
/** | ||
* Helper method to handle Formik "touched" in arrays | ||
* Please see parseError() for additional context | ||
*/ | ||
export const parseTouched = ( | ||
touched: string | undefined | FormikGovPayMetadata, | ||
index: number, | ||
): string | undefined => { | ||
// No errors | ||
if (!touched) return; | ||
|
||
// Array-level error - handled at a higher level | ||
if (typeof touched === "string") return; | ||
|
||
// No error for this field | ||
if (!touched[index]) return; | ||
|
||
// Specific field-level error | ||
return touched[index].key && touched[index].value; | ||
}; | ||
/** | ||
* Disable required fields so they cannot be edited | ||
* Only disable first instance, otherwise any field beginning with a required field will be disabled, and user will not be able to fix their mistake as the delete icon is also disabled | ||
*/ | ||
export const isFieldDisabled = (key: string, index: number) => | ||
REQUIRED_GOVPAY_METADATA.includes(key) && | ||
index === REQUIRED_GOVPAY_METADATA.indexOf(key); |
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