2021-02-18 12:38:41 +00:00
|
|
|
import { OutputData } from "@editorjs/editorjs";
|
2022-11-07 12:20:58 +00:00
|
|
|
import { AttributeTranslationDetailsFragment } from "@saleor/graphql";
|
|
|
|
import { TranslationField } 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
|
|
|
) || [];
|