2019-06-19 14:40:52 +00:00
|
|
|
import { stringify as stringifyQs } from "qs";
|
2019-08-09 10:26:22 +00:00
|
|
|
import React from "react";
|
2019-08-26 21:50:08 +00:00
|
|
|
import { useIntl } from "react-intl";
|
2019-06-19 14:40:52 +00:00
|
|
|
|
|
|
|
import useNavigator from "@saleor/hooks/useNavigator";
|
|
|
|
import useNotifier from "@saleor/hooks/useNotifier";
|
|
|
|
import useShop from "@saleor/hooks/useShop";
|
2019-08-26 21:50:08 +00:00
|
|
|
import { commonMessages } from "@saleor/intl";
|
2019-12-06 17:11:46 +00:00
|
|
|
import { maybe } from "../../misc";
|
2019-06-19 14:40:52 +00:00
|
|
|
import { LanguageCodeEnum, TranslationInput } from "../../types/globalTypes";
|
|
|
|
import TranslationsProductsPage, {
|
|
|
|
fieldNames
|
|
|
|
} from "../components/TranslationsProductsPage";
|
|
|
|
import { TypedUpdateProductTranslations } from "../mutations";
|
|
|
|
import { TypedProductTranslationDetails } from "../queries";
|
|
|
|
import { UpdateProductTranslations } from "../types/UpdateProductTranslations";
|
|
|
|
import {
|
|
|
|
languageEntitiesUrl,
|
|
|
|
languageEntityUrl,
|
|
|
|
TranslatableEntities
|
|
|
|
} from "../urls";
|
|
|
|
|
|
|
|
export interface TranslationsProductsQueryParams {
|
|
|
|
activeField: string;
|
|
|
|
}
|
|
|
|
export interface TranslationsProductsProps {
|
|
|
|
id: string;
|
|
|
|
languageCode: LanguageCodeEnum;
|
|
|
|
params: TranslationsProductsQueryParams;
|
|
|
|
}
|
|
|
|
|
|
|
|
const TranslationsProducts: React.FC<TranslationsProductsProps> = ({
|
|
|
|
id,
|
|
|
|
languageCode,
|
|
|
|
params
|
|
|
|
}) => {
|
|
|
|
const navigate = useNavigator();
|
|
|
|
const notify = useNotifier();
|
|
|
|
const shop = useShop();
|
2019-08-26 21:50:08 +00:00
|
|
|
const intl = useIntl();
|
2019-06-19 14:40:52 +00:00
|
|
|
|
|
|
|
const onEdit = (field: string) =>
|
|
|
|
navigate(
|
|
|
|
"?" +
|
|
|
|
stringifyQs({
|
|
|
|
activeField: field
|
|
|
|
}),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
const onUpdate = (data: UpdateProductTranslations) => {
|
|
|
|
if (data.productTranslate.errors.length === 0) {
|
|
|
|
notify({
|
2019-08-26 21:50:08 +00:00
|
|
|
text: intl.formatMessage(commonMessages.savedChanges)
|
2019-06-19 14:40:52 +00:00
|
|
|
});
|
|
|
|
navigate("?", true);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
const onDiscard = () => {
|
|
|
|
navigate("?", true);
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<TypedProductTranslationDetails variables={{ id, language: languageCode }}>
|
|
|
|
{productTranslations => (
|
|
|
|
<TypedUpdateProductTranslations onCompleted={onUpdate}>
|
|
|
|
{(updateTranslations, updateTranslationsOpts) => {
|
|
|
|
const handleSubmit = (field: string, data: string) => {
|
|
|
|
const input: TranslationInput = {};
|
|
|
|
if (field === fieldNames.descriptionJson) {
|
|
|
|
input.descriptionJson = JSON.stringify(data);
|
|
|
|
} else if (field === fieldNames.name) {
|
|
|
|
input.name = data;
|
|
|
|
} else if (field === fieldNames.seoDescription) {
|
|
|
|
input.seoDescription = data;
|
|
|
|
} else if (field === fieldNames.seoTitle) {
|
|
|
|
input.seoTitle = data;
|
|
|
|
}
|
|
|
|
updateTranslations({
|
|
|
|
variables: {
|
|
|
|
id,
|
|
|
|
input,
|
|
|
|
language: languageCode
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<TranslationsProductsPage
|
|
|
|
activeField={params.activeField}
|
|
|
|
disabled={
|
|
|
|
productTranslations.loading || updateTranslationsOpts.loading
|
|
|
|
}
|
|
|
|
languageCode={languageCode}
|
|
|
|
languages={maybe(() => shop.languages, [])}
|
2019-12-06 17:17:44 +00:00
|
|
|
saveButtonState={updateTranslationsOpts.status}
|
2019-06-19 14:40:52 +00:00
|
|
|
onBack={() =>
|
|
|
|
navigate(
|
2019-09-12 12:43:37 +00:00
|
|
|
languageEntitiesUrl(languageCode, {
|
|
|
|
tab: TranslatableEntities.products
|
|
|
|
})
|
2019-06-19 14:40:52 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
onEdit={onEdit}
|
|
|
|
onDiscard={onDiscard}
|
|
|
|
onLanguageChange={lang =>
|
|
|
|
navigate(
|
|
|
|
languageEntityUrl(lang, TranslatableEntities.products, id)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
onSubmit={handleSubmit}
|
|
|
|
product={maybe(() => productTranslations.data.product)}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
</TypedUpdateProductTranslations>
|
|
|
|
)}
|
|
|
|
</TypedProductTranslationDetails>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
TranslationsProducts.displayName = "TranslationsProducts";
|
|
|
|
export default TranslationsProducts;
|