saleor-dashboard/src/translations/views/TranslationsShippingMethod.tsx
Magdalena Markusik 3d636f4789
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 10:58:06 +01:00

128 lines
4 KiB
TypeScript

import useNavigator from "@saleor/hooks/useNavigator";
import useNotifier from "@saleor/hooks/useNotifier";
import useShop from "@saleor/hooks/useShop";
import { commonMessages } from "@saleor/intl";
import { extractMutationErrors } from "@saleor/misc";
import { stringifyQs } from "@saleor/utils/urls";
import React from "react";
import { useIntl } from "react-intl";
import { LanguageCodeEnum } from "../../types/globalTypes";
import TranslationsShippingMethodPage from "../components/TranslationsShippingMethodPage";
import { TypedUpdateShippingMethodTranslations } from "../mutations";
import { useShippingMethodTranslationDetails } from "../queries";
import { TranslationField, TranslationInputFieldName } from "../types";
import { UpdateShippingMethodTranslations } from "../types/UpdateShippingMethodTranslations";
import {
languageEntitiesUrl,
languageEntityUrl,
TranslatableEntities
} from "../urls";
import { getParsedTranslationInputData } from "../utils";
export interface TranslationsShippingMethodQueryParams {
activeField: string;
}
export interface TranslationsShippingMethodProps {
id: string;
languageCode: LanguageCodeEnum;
params: TranslationsShippingMethodQueryParams;
}
const TranslationsShippingMethod: React.FC<TranslationsShippingMethodProps> = ({
id,
languageCode,
params
}) => {
const navigate = useNavigator();
const notify = useNotifier();
const shop = useShop();
const intl = useIntl();
const shippingMethodTranslations = useShippingMethodTranslationDetails({
variables: { id, language: languageCode }
});
const onEdit = (field: string) =>
navigate(
"?" +
stringifyQs({
activeField: field
}),
{ replace: true }
);
const onUpdate = (data: UpdateShippingMethodTranslations) => {
if (data.shippingPriceTranslate.errors.length === 0) {
shippingMethodTranslations.refetch();
notify({
status: "success",
text: intl.formatMessage(commonMessages.savedChanges)
});
navigate("?", { replace: true });
}
};
const onDiscard = () => {
navigate("?", { replace: true });
};
return (
<TypedUpdateShippingMethodTranslations onCompleted={onUpdate}>
{(updateTranslations, updateTranslationsOpts) => {
const handleSubmit = (
{ name: fieldName }: TranslationField<TranslationInputFieldName>,
data: string
) =>
extractMutationErrors(
updateTranslations({
variables: {
id,
input: getParsedTranslationInputData({ fieldName, data }),
language: languageCode
}
})
);
const translation = shippingMethodTranslations?.data?.translation;
return (
<TranslationsShippingMethodPage
activeField={params.activeField}
disabled={
shippingMethodTranslations.loading ||
updateTranslationsOpts.loading
}
languages={shop?.languages || []}
languageCode={languageCode}
saveButtonState={updateTranslationsOpts.status}
onBack={() =>
navigate(
languageEntitiesUrl(languageCode, {
tab: TranslatableEntities.shippingMethods
})
)
}
onEdit={onEdit}
onDiscard={onDiscard}
onSubmit={handleSubmit}
onLanguageChange={lang =>
navigate(
languageEntityUrl(
lang,
TranslatableEntities.shippingMethods,
id
)
)
}
data={
translation?.__typename === "ShippingMethodTranslatableContent"
? translation
: null
}
/>
);
}}
</TypedUpdateShippingMethodTranslations>
);
};
TranslationsShippingMethod.displayName = "TranslationsShippingMethod";
export default TranslationsShippingMethod;