saleor-dashboard/src/translations/utils.ts

55 lines
1.7 KiB
TypeScript
Raw Normal View History

import { OutputData } from "@editorjs/editorjs";
import { AttributeTranslationDetailsFragment_attribute_choices } from "@saleor/fragments/types/AttributeTranslationDetailsFragment";
import { TranslationField } from "@saleor/translations/types";
import { mapEdgesToItems } from "@saleor/utils/maps";
import { getParsedDataForJsonStringField } from "@saleor/utils/richText/misc";
import { IntlShape } from "react-intl";
import { fieldNames } from "./components/TranslationsAttributesPage";
import { transtionsAttributesPageFieldsMessages as messages } from "./components/TranslationsAttributesPage/messages";
2021-02-18 12:59:47 +00:00
import {
PageTranslationInputFieldName,
TranslationInputFieldName
} from "./types";
export const getParsedTranslationInputData = ({
fieldName,
data
}: {
2021-02-18 12:59:47 +00:00
fieldName: TranslationInputFieldName | PageTranslationInputFieldName;
data: string | OutputData;
}): Record<string, string | null> => {
2021-02-18 12:59:47 +00:00
const fieldsToParse = [
TranslationInputFieldName.description,
PageTranslationInputFieldName.content
];
if (fieldsToParse.includes(fieldName)) {
return {
2021-03-31 12:15:05 +00:00
[fieldName]: getParsedDataForJsonStringField(data as OutputData)
};
}
return { [fieldName]: data as string };
};
export const getTranslationFields = (
fields: AttributeTranslationDetailsFragment_attribute_choices,
intl: IntlShape
) =>
mapEdgesToItems(fields).map(
({ id, name, translation }, attributeValueIndex) => {
const displayName = intl.formatMessage(messages.valueNumber, {
number: attributeValueIndex + 1
});
return {
displayName,
name: `${fieldNames.value}:${id}`,
translation: translation?.name || null,
type: "short" as TranslationField["type"],
value: name
};
}
) || [];