2021-02-18 12:38:41 +00:00
|
|
|
import { OutputData } from "@editorjs/editorjs";
|
2022-11-17 14:14:45 +00:00
|
|
|
import {
|
|
|
|
AttributeTranslationDetailsFragment,
|
|
|
|
AttributeValueTranslatableFragment,
|
|
|
|
AttributeValueTranslationInput,
|
|
|
|
} from "@saleor/graphql";
|
|
|
|
import {
|
|
|
|
TranslationField,
|
|
|
|
TranslationFieldType,
|
|
|
|
} from "@saleor/translations/types";
|
2021-10-06 11:10:12 +00:00
|
|
|
import { mapEdgesToItems } from "@saleor/utils/maps";
|
2021-04-16 12:33:14 +00:00
|
|
|
import { getParsedDataForJsonStringField } from "@saleor/utils/richText/misc";
|
2021-10-06 11:10:12 +00:00
|
|
|
import { IntlShape } from "react-intl";
|
2021-02-18 12:38:41 +00:00
|
|
|
|
2021-10-06 11:10:12 +00:00
|
|
|
import { fieldNames } from "./components/TranslationsAttributesPage";
|
|
|
|
import { transtionsAttributesPageFieldsMessages as messages } from "./components/TranslationsAttributesPage/messages";
|
2021-02-18 12:59:47 +00:00
|
|
|
import {
|
|
|
|
PageTranslationInputFieldName,
|
2022-06-21 09:36:55 +00:00
|
|
|
TranslationInputFieldName,
|
2021-02-18 12:59:47 +00:00
|
|
|
} from "./types";
|
2021-02-18 12:38:41 +00:00
|
|
|
|
|
|
|
export const getParsedTranslationInputData = ({
|
|
|
|
fieldName,
|
2022-06-21 09:36:55 +00:00
|
|
|
data,
|
2021-02-18 12:38:41 +00:00
|
|
|
}: {
|
2021-02-18 12:59:47 +00:00
|
|
|
fieldName: TranslationInputFieldName | PageTranslationInputFieldName;
|
2021-02-18 12:38:41 +00:00
|
|
|
data: string | OutputData;
|
|
|
|
}): Record<string, string | null> => {
|
2021-02-18 12:59:47 +00:00
|
|
|
const fieldsToParse = [
|
|
|
|
TranslationInputFieldName.description,
|
2022-06-21 09:36:55 +00:00
|
|
|
PageTranslationInputFieldName.content,
|
2021-02-18 12:59:47 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
if (fieldsToParse.includes(fieldName)) {
|
2021-02-18 12:38:41 +00:00
|
|
|
return {
|
2022-06-21 09:36:55 +00:00
|
|
|
[fieldName]: getParsedDataForJsonStringField(data as OutputData),
|
2021-02-18 12:38:41 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return { [fieldName]: data as string };
|
|
|
|
};
|
2021-10-06 11:10:12 +00:00
|
|
|
|
|
|
|
export const getTranslationFields = (
|
2022-03-09 08:56:55 +00:00
|
|
|
fields: AttributeTranslationDetailsFragment["attribute"]["choices"],
|
2022-06-21 09:36:55 +00:00
|
|
|
intl: IntlShape,
|
2021-10-06 11:10:12 +00:00
|
|
|
) =>
|
|
|
|
mapEdgesToItems(fields).map(
|
|
|
|
({ id, name, translation }, attributeValueIndex) => {
|
|
|
|
const displayName = intl.formatMessage(messages.valueNumber, {
|
2022-06-21 09:36:55 +00:00
|
|
|
number: attributeValueIndex + 1,
|
2021-10-06 11:10:12 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
|
|
|
displayName,
|
|
|
|
name: `${fieldNames.value}:${id}`,
|
|
|
|
translation: translation?.name || null,
|
|
|
|
type: "short" as TranslationField["type"],
|
2022-06-21 09:36:55 +00:00
|
|
|
value: name,
|
2021-10-06 11:10:12 +00:00
|
|
|
};
|
2022-06-21 09:36:55 +00:00
|
|
|
},
|
2021-10-06 11:10:12 +00:00
|
|
|
) || [];
|
2022-11-17 14:14:45 +00:00
|
|
|
|
|
|
|
export const mapAttributeValuesToTranslationFields = (
|
|
|
|
attributeValues: AttributeValueTranslatableFragment[],
|
|
|
|
intl: IntlShape,
|
|
|
|
) =>
|
|
|
|
attributeValues.map<TranslationField>(attrVal => ({
|
|
|
|
id: attrVal.attributeValue.id,
|
|
|
|
displayName: intl.formatMessage(
|
|
|
|
{
|
|
|
|
id: "zgqPGF",
|
|
|
|
defaultMessage: "Attribute {name}",
|
|
|
|
description: "attribute list",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: attrVal.attribute.name,
|
|
|
|
},
|
|
|
|
),
|
|
|
|
name: attrVal.name,
|
|
|
|
translation:
|
|
|
|
attrVal.translation?.richText || attrVal.translation?.plainText || null,
|
|
|
|
type: attrVal.richText ? "rich" : "short",
|
|
|
|
value: attrVal.richText || attrVal.plainText,
|
|
|
|
})) || [];
|
|
|
|
|
|
|
|
export const getAttributeValueTranslationsInputData = (
|
|
|
|
type: TranslationFieldType,
|
|
|
|
data: OutputData | string,
|
|
|
|
): AttributeValueTranslationInput =>
|
|
|
|
type === TranslationFieldType.RICH
|
|
|
|
? { richText: JSON.stringify(data) }
|
|
|
|
: { plainText: data as string };
|