saleor-dashboard/src/translations/utils.ts

93 lines
2.7 KiB
TypeScript
Raw Normal View History

import { OutputData } from "@editorjs/editorjs";
import {
AttributeTranslationDetailsFragment,
AttributeValueTranslatableFragment,
AttributeValueTranslationInput,
} from "@saleor/graphql";
import {
TranslationField,
TranslationFieldType,
} 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,
2021-02-18 12:59:47 +00:00
} 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,
2021-02-18 12:59:47 +00:00
];
if (fieldsToParse.includes(fieldName)) {
return {
[fieldName]: getParsedDataForJsonStringField(data as OutputData),
};
}
return { [fieldName]: data as string };
};
export const getTranslationFields = (
Use graphql-codegen (#1874) * Use generated hooks in apps * Remove unused files * Use proper types in apps * Use generated hooks in attributes * Use generated hooks in auth module * Use generated hooks in categories * Use generated hooks in channels * Use generated types in collections * Remove legacy types from background tasks * Use generated hooks in customers * Use generated hooks in discounts * Use generated hook in file upload * Use generated types in gift cards * Use generated types in home * Use generated hooks in navigation * Use generated hooks in orders * Use generated hooks in pages * Use generated hooks in page types * Use generated hooks in permission groups * Use generated hooks in plugins * Use generated hooks in products * Use fragment to mark product variants * Improve code a bit * Use generated hooks in page types * Use generated types in searches * Use generated hooks in shipping * Use generated hooks in site settings * Use generated hooks in staff members * Use generated hooks in taxes * Place all gql generated files in one directory * Use generated hooks in translations * Use global types from new generated module * Use generated hooks in warehouses * Use generated hooks in webhooks * Use generated fragment types * Unclutter types * Remove hoc components * Split hooks and types * Fetch introspection file * Delete obsolete schema file * Fix rebase artifacts * Fix autoreplace * Fix auth provider tests * Fix urls * Remove leftover types * Fix rebase artifacts
2022-03-09 08:56:55 +00:00
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,
};
},
) || [];
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 };