2022-03-09 08:56:55 +00:00
|
|
|
import {
|
|
|
|
LanguageCodeEnum,
|
|
|
|
useShippingMethodTranslationDetailsQuery,
|
|
|
|
useUpdateShippingMethodTranslationsMutation
|
|
|
|
} from "@saleor/graphql";
|
2020-11-27 15:53:48 +00:00
|
|
|
import useNavigator from "@saleor/hooks/useNavigator";
|
|
|
|
import useNotifier from "@saleor/hooks/useNotifier";
|
|
|
|
import useShop from "@saleor/hooks/useShop";
|
|
|
|
import { commonMessages } from "@saleor/intl";
|
2022-02-01 09:58:06 +00:00
|
|
|
import { extractMutationErrors } from "@saleor/misc";
|
2021-08-09 15:27:13 +00:00
|
|
|
import { stringifyQs } from "@saleor/utils/urls";
|
2020-11-27 15:53:48 +00:00
|
|
|
import React from "react";
|
|
|
|
import { useIntl } from "react-intl";
|
|
|
|
|
2021-04-16 12:33:14 +00:00
|
|
|
import TranslationsShippingMethodPage from "../components/TranslationsShippingMethodPage";
|
2021-08-09 14:59:12 +00:00
|
|
|
import { TranslationField, TranslationInputFieldName } from "../types";
|
2020-11-27 15:53:48 +00:00
|
|
|
import {
|
|
|
|
languageEntitiesUrl,
|
|
|
|
languageEntityUrl,
|
|
|
|
TranslatableEntities
|
|
|
|
} from "../urls";
|
2021-04-16 12:33:14 +00:00
|
|
|
import { getParsedTranslationInputData } from "../utils";
|
2020-11-27 15:53:48 +00:00
|
|
|
|
|
|
|
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();
|
|
|
|
|
2022-03-09 08:56:55 +00:00
|
|
|
const shippingMethodTranslations = useShippingMethodTranslationDetailsQuery({
|
2020-11-27 15:53:48 +00:00
|
|
|
variables: { id, language: languageCode }
|
|
|
|
});
|
|
|
|
|
2022-03-09 08:56:55 +00:00
|
|
|
const [
|
|
|
|
updateTranslations,
|
|
|
|
updateTranslationsOpts
|
|
|
|
] = useUpdateShippingMethodTranslationsMutation({
|
|
|
|
onCompleted: data => {
|
|
|
|
if (data.shippingPriceTranslate.errors.length === 0) {
|
|
|
|
shippingMethodTranslations.refetch();
|
|
|
|
notify({
|
|
|
|
status: "success",
|
|
|
|
text: intl.formatMessage(commonMessages.savedChanges)
|
|
|
|
});
|
|
|
|
navigate("?", { replace: true });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-11-27 15:53:48 +00:00
|
|
|
const onEdit = (field: string) =>
|
|
|
|
navigate(
|
|
|
|
"?" +
|
|
|
|
stringifyQs({
|
|
|
|
activeField: field
|
|
|
|
}),
|
2021-10-21 08:34:56 +00:00
|
|
|
{ replace: true }
|
2020-11-27 15:53:48 +00:00
|
|
|
);
|
2022-03-09 08:56:55 +00:00
|
|
|
|
2020-11-27 15:53:48 +00:00
|
|
|
const onDiscard = () => {
|
2021-10-21 08:34:56 +00:00
|
|
|
navigate("?", { replace: true });
|
2020-11-27 15:53:48 +00:00
|
|
|
};
|
|
|
|
|
2022-03-09 08:56:55 +00:00
|
|
|
const handleSubmit = (
|
|
|
|
{ name: fieldName }: TranslationField<TranslationInputFieldName>,
|
|
|
|
data: string
|
|
|
|
) =>
|
|
|
|
extractMutationErrors(
|
|
|
|
updateTranslations({
|
|
|
|
variables: {
|
|
|
|
id,
|
|
|
|
input: getParsedTranslationInputData({ fieldName, data }),
|
|
|
|
language: languageCode
|
|
|
|
}
|
|
|
|
})
|
|
|
|
);
|
2022-02-01 09:58:06 +00:00
|
|
|
|
2022-03-09 08:56:55 +00:00
|
|
|
const translation = shippingMethodTranslations?.data?.translation;
|
2020-11-27 15:53:48 +00:00
|
|
|
|
2022-03-09 08:56:55 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
/>
|
2020-11-27 15:53:48 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
TranslationsShippingMethod.displayName = "TranslationsShippingMethod";
|
|
|
|
export default TranslationsShippingMethod;
|