saleor-dashboard/src/hooks/useAddressValidation.ts

58 lines
1.6 KiB
TypeScript
Raw Normal View History

2019-11-06 15:21:58 +00:00
import { AddressTypeInput } from "@saleor/customers/types";
import { AccountErrorFragment } from "@saleor/fragments/types/AccountErrorFragment";
import { transformFormToAddressInput } from "@saleor/misc";
import {
AccountErrorCode,
AddressInput,
AddressTypeEnum
} from "@saleor/types/globalTypes";
2019-11-06 15:21:58 +00:00
import { add, remove } from "@saleor/utils/lists";
import { useState } from "react";
2019-11-06 15:21:58 +00:00
2020-10-22 11:33:29 +00:00
interface UseAddressValidation<TInput, TOutput> {
2020-03-11 09:55:14 +00:00
errors: AccountErrorFragment[];
Exit dirty form (#1816) * Add Exit form prompt component and change some minor styles in other components to match * Add Exit form prompt provider * Adjust generic form and useform hook to allow using exit form prompt provider * Add exit form prompt provider to index * wip * Fix types * Fix styling * Fix types * Revert warehouse details refactor * Add handling of edge cases to exit prompt * Refactor, add comments, fix some types * Refactor after exit form dialog name change * fix types * Fixes after review * Add default value for useform prop opts so the app doesn't crash * Add missing category prop to getting initial data for category details form * Add exit dialog to everywhere WIP (#1600) * Add Exit form prompt component and change some minor styles in other components to match * Add Exit form prompt provider * Adjust generic form and useform hook to allow using exit form prompt provider * Add exit form prompt provider to index * wip * Fix types * Fix styling * Fix types * Revert warehouse details refactor * Add handling of edge cases to exit prompt * Refactor, add comments, fix some types * Refactor after exit form dialog name change * fix types * Add CommonUseFormResultWithHandlers type for later use and refactor handleFormSubmit util * Refactor login form not to use custom form since it doesn't need to * Add exit form dialog to order refund page * Add exit form dialog to order return page * Add exit form dialog to order order settings page * Add exit form dialog to product variant page * Add exit form dialog to product create page * Add exit form dialog to product update page * Add exit form dialog to product variant create page * Fix confirm leave prop passing in generic Form * Add util function to handle for submit to extract errors * Add confirmLeave prop to generic forms * Move handleChange for custom forms to useForm * Add exit dialog to more forms * Add extract mutation errors util function * Add extracting errors to submit functions that use metadata create handler * Fix typo * Add missing category prop to getting initial data for category details form * Fix types * wip * wip * wip * wip * Fix types & refactor * Fix types & refactor * Fix typescript * Fix unmatching tag * Fixes * Add handling of multiple forms at once to exit dirty form provider * Change all usages of ExitFormDialogContext to designated hook * wip * wip * wip * Fix types wip * Fix types * Remove console logs * Add isSubmitting prop to exit form dialog in order to avoid enabling exit dialog while submit is still in progresS * Replace handleSubmit global util with a hook to use exit form dialog props inside * Move useHandleSubmit to general hooks dir, update imports * Small fixes * Update snapshots * Fix types * Small fixes due to extensive rebase * Update package lock * Fixes after rebase * Remove exit form from customer address dialog * Fix types and update messages * Fix types * Change imports names * Refactor * Remove unnecessary console.log * Update types, snapshots. etc after rebase
2022-02-01 09:58:06 +00:00
submit: (
data: TInput & AddressTypeInput
) => TOutput | Promise<AccountErrorFragment[]>;
2019-11-06 15:21:58 +00:00
}
2020-10-22 11:33:29 +00:00
function useAddressValidation<TInput, TOutput>(
onSubmit: (address: TInput & AddressInput) => TOutput,
addressType?: AddressTypeEnum
2020-10-22 11:33:29 +00:00
): UseAddressValidation<TInput, TOutput> {
2020-03-11 09:55:14 +00:00
const [validationErrors, setValidationErrors] = useState<
AccountErrorFragment[]
>([]);
2019-11-06 15:21:58 +00:00
2020-03-11 09:55:14 +00:00
const countryRequiredError: AccountErrorFragment = {
__typename: "AccountError",
code: AccountErrorCode.REQUIRED,
field: "country",
addressType,
message: "Country required"
2019-11-06 15:21:58 +00:00
};
return {
errors: validationErrors,
2020-10-22 11:33:29 +00:00
submit: (data: TInput & AddressTypeInput) => {
2019-11-06 15:21:58 +00:00
try {
setValidationErrors(
remove(
countryRequiredError,
validationErrors,
(a, b) => a.field === b.field
)
);
return onSubmit(transformFormToAddressInput(data));
2019-11-06 15:21:58 +00:00
} catch {
Exit dirty form (#1816) * Add Exit form prompt component and change some minor styles in other components to match * Add Exit form prompt provider * Adjust generic form and useform hook to allow using exit form prompt provider * Add exit form prompt provider to index * wip * Fix types * Fix styling * Fix types * Revert warehouse details refactor * Add handling of edge cases to exit prompt * Refactor, add comments, fix some types * Refactor after exit form dialog name change * fix types * Fixes after review * Add default value for useform prop opts so the app doesn't crash * Add missing category prop to getting initial data for category details form * Add exit dialog to everywhere WIP (#1600) * Add Exit form prompt component and change some minor styles in other components to match * Add Exit form prompt provider * Adjust generic form and useform hook to allow using exit form prompt provider * Add exit form prompt provider to index * wip * Fix types * Fix styling * Fix types * Revert warehouse details refactor * Add handling of edge cases to exit prompt * Refactor, add comments, fix some types * Refactor after exit form dialog name change * fix types * Add CommonUseFormResultWithHandlers type for later use and refactor handleFormSubmit util * Refactor login form not to use custom form since it doesn't need to * Add exit form dialog to order refund page * Add exit form dialog to order return page * Add exit form dialog to order order settings page * Add exit form dialog to product variant page * Add exit form dialog to product create page * Add exit form dialog to product update page * Add exit form dialog to product variant create page * Fix confirm leave prop passing in generic Form * Add util function to handle for submit to extract errors * Add confirmLeave prop to generic forms * Move handleChange for custom forms to useForm * Add exit dialog to more forms * Add extract mutation errors util function * Add extracting errors to submit functions that use metadata create handler * Fix typo * Add missing category prop to getting initial data for category details form * Fix types * wip * wip * wip * wip * Fix types & refactor * Fix types & refactor * Fix typescript * Fix unmatching tag * Fixes * Add handling of multiple forms at once to exit dirty form provider * Change all usages of ExitFormDialogContext to designated hook * wip * wip * wip * Fix types wip * Fix types * Remove console logs * Add isSubmitting prop to exit form dialog in order to avoid enabling exit dialog while submit is still in progresS * Replace handleSubmit global util with a hook to use exit form dialog props inside * Move useHandleSubmit to general hooks dir, update imports * Small fixes * Update snapshots * Fix types * Small fixes due to extensive rebase * Update package lock * Fixes after rebase * Remove exit form from customer address dialog * Fix types and update messages * Fix types * Change imports names * Refactor * Remove unnecessary console.log * Update types, snapshots. etc after rebase
2022-02-01 09:58:06 +00:00
const errors = add(countryRequiredError, validationErrors);
setValidationErrors(errors);
// since every onSubmit must return Promise<error>
return Promise.resolve(errors);
2019-11-06 15:21:58 +00:00
}
}
};
}
export default useAddressValidation;