Add form validation
This commit is contained in:
parent
17575478f7
commit
6d0607032a
3 changed files with 16 additions and 11 deletions
|
@ -9,6 +9,7 @@ import Form from "@saleor/components/Form";
|
||||||
import Grid from "@saleor/components/Grid";
|
import Grid from "@saleor/components/Grid";
|
||||||
import PageHeader from "@saleor/components/PageHeader";
|
import PageHeader from "@saleor/components/PageHeader";
|
||||||
import SaveButtonBar from "@saleor/components/SaveButtonBar";
|
import SaveButtonBar from "@saleor/components/SaveButtonBar";
|
||||||
|
import useAddressValidation from "@saleor/hooks/useAddressValidation";
|
||||||
import { sectionNames } from "@saleor/intl";
|
import { sectionNames } from "@saleor/intl";
|
||||||
import createSingleAutocompleteSelectHandler from "@saleor/utils/handlers/singleAutocompleteSelectChangeHandler";
|
import createSingleAutocompleteSelectHandler from "@saleor/utils/handlers/singleAutocompleteSelectChangeHandler";
|
||||||
import { UserError } from "../../../types";
|
import { UserError } from "../../../types";
|
||||||
|
@ -67,12 +68,16 @@ const CustomerCreatePage: React.FC<CustomerCreatePageProps> = ({
|
||||||
label: country.country,
|
label: country.country,
|
||||||
value: country.code
|
value: country.code
|
||||||
}));
|
}));
|
||||||
|
const {
|
||||||
|
errors: validationErrors,
|
||||||
|
submit: handleSubmit
|
||||||
|
} = useAddressValidation(onSubmit);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Form
|
<Form
|
||||||
initial={initialForm}
|
initial={initialForm}
|
||||||
onSubmit={onSubmit}
|
onSubmit={handleSubmit}
|
||||||
errors={errors}
|
errors={[...errors, ...validationErrors]}
|
||||||
confirmLeave
|
confirmLeave
|
||||||
>
|
>
|
||||||
{({ change, data, errors: formErrors, hasChanged, submit }) => {
|
{({ change, data, errors: formErrors, hasChanged, submit }) => {
|
||||||
|
|
|
@ -8,14 +8,14 @@ import { UserError } from "@saleor/types";
|
||||||
import { AddressInput } from "@saleor/types/globalTypes";
|
import { AddressInput } from "@saleor/types/globalTypes";
|
||||||
import { add, remove } from "@saleor/utils/lists";
|
import { add, remove } from "@saleor/utils/lists";
|
||||||
|
|
||||||
interface UseAddressValidation {
|
interface UseAddressValidation<T> {
|
||||||
errors: UserError[];
|
errors: UserError[];
|
||||||
submit: (data: AddressTypeInput) => void;
|
submit: (data: T & AddressTypeInput) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
function useAddressValidation(
|
function useAddressValidation<T>(
|
||||||
onSubmit: (address: AddressInput) => void
|
onSubmit: (address: T & AddressInput) => void
|
||||||
): UseAddressValidation {
|
): UseAddressValidation<T> {
|
||||||
const intl = useIntl();
|
const intl = useIntl();
|
||||||
const [validationErrors, setValidationErrors] = useState<UserError[]>([]);
|
const [validationErrors, setValidationErrors] = useState<UserError[]>([]);
|
||||||
|
|
||||||
|
@ -26,7 +26,7 @@ function useAddressValidation(
|
||||||
|
|
||||||
return {
|
return {
|
||||||
errors: validationErrors,
|
errors: validationErrors,
|
||||||
submit: (data: AddressTypeInput) => {
|
submit: (data: T & AddressTypeInput) => {
|
||||||
try {
|
try {
|
||||||
setValidationErrors(
|
setValidationErrors(
|
||||||
remove(
|
remove(
|
||||||
|
|
|
@ -339,9 +339,9 @@ export function capitalize(s: string) {
|
||||||
return s.charAt(0).toLocaleUpperCase() + s.slice(1);
|
return s.charAt(0).toLocaleUpperCase() + s.slice(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function transformFormToAddress(
|
export function transformFormToAddress<T>(
|
||||||
address: AddressTypeInput
|
address: T & AddressTypeInput
|
||||||
): AddressInput {
|
): T & AddressInput {
|
||||||
return {
|
return {
|
||||||
...address,
|
...address,
|
||||||
country: findInEnum(address.country, CountryCode)
|
country: findInEnum(address.country, CountryCode)
|
||||||
|
|
Loading…
Reference in a new issue