saleor-dashboard/src/attributes/utils/data.ts

424 lines
12 KiB
TypeScript
Raw Normal View History

import { FetchResult } from "@apollo/client";
import {
AttributeInput,
AttributeInputData
} from "@saleor/components/Attributes";
File attributes (#884) * Update changelog with file attributes * Add file type attribute * Update attribute properties form * Update translation messages with file upload * Create generic attributes component (#832) * Create generic Attributes component * Add story for Attributes component * Remove deprecated attribute value type field from queries * Update test snapshots of attributes component * Add file upload field to atributes (#888) * Add story for Attributes component * Update test snapshots of attributes component * Create file upload field in attributes * Update upload file input data-test * Update storybook test snapshots of attributes * Add dedicated input props to file field * Run Cypress using custom API * Add missing error handling in file upload field Co-authored-by: Krzysztof Wolski <krzysztof.k.wolski@gmail.com> * Add file attribute upload to page attributes (#894) * Support upload file attribute for pages * Update after review * Add file attribute upload to variant attributes (#892) * Support upload file attribute for variants * Update after review * Refactor attribute values errors merging * Update after review * Add file attribute upload to product attributes (#826) * Support upload file attribute for products * Update after review * Refactor attribute values errors merging * Refactor product attribute value delete handling * Fix deleting file in file upload field * Fix delete attribute values errors handling * Add link to file upload field (#898) * Update file attributes updates (#899) * Update file attributes updates * Refactor file uploads handling * Move attributes utils to attributes directory * Fix product channel listing updates * Clear file field value if file is not passed as prop * Delete attribute values before update (#908) * Delete file attributes after file update * Triggr CI * Show skeleton in file upload field during loading Co-authored-by: Krzysztof Wolski <krzysztof.k.wolski@gmail.com>
2020-12-16 10:53:28 +00:00
import { FileUpload } from "@saleor/files/types/FileUpload";
import { AttributeErrorFragment } from "@saleor/fragments/types/AttributeErrorFragment";
import { AttributeValueFragment } from "@saleor/fragments/types/AttributeValueFragment";
File attributes (#884) * Update changelog with file attributes * Add file type attribute * Update attribute properties form * Update translation messages with file upload * Create generic attributes component (#832) * Create generic Attributes component * Add story for Attributes component * Remove deprecated attribute value type field from queries * Update test snapshots of attributes component * Add file upload field to atributes (#888) * Add story for Attributes component * Update test snapshots of attributes component * Create file upload field in attributes * Update upload file input data-test * Update storybook test snapshots of attributes * Add dedicated input props to file field * Run Cypress using custom API * Add missing error handling in file upload field Co-authored-by: Krzysztof Wolski <krzysztof.k.wolski@gmail.com> * Add file attribute upload to page attributes (#894) * Support upload file attribute for pages * Update after review * Add file attribute upload to variant attributes (#892) * Support upload file attribute for variants * Update after review * Refactor attribute values errors merging * Update after review * Add file attribute upload to product attributes (#826) * Support upload file attribute for products * Update after review * Refactor attribute values errors merging * Refactor product attribute value delete handling * Fix deleting file in file upload field * Fix delete attribute values errors handling * Add link to file upload field (#898) * Update file attributes updates (#899) * Update file attributes updates * Refactor file uploads handling * Move attributes utils to attributes directory * Fix product channel listing updates * Clear file field value if file is not passed as prop * Delete attribute values before update (#908) * Delete file attributes after file update * Triggr CI * Show skeleton in file upload field during loading Co-authored-by: Krzysztof Wolski <krzysztof.k.wolski@gmail.com>
2020-12-16 10:53:28 +00:00
import { SelectedVariantAttributeFragment } from "@saleor/fragments/types/SelectedVariantAttributeFragment";
import { UploadErrorFragment } from "@saleor/fragments/types/UploadErrorFragment";
import { VariantAttributeFragment } from "@saleor/fragments/types/VariantAttributeFragment";
File attributes (#884) * Update changelog with file attributes * Add file type attribute * Update attribute properties form * Update translation messages with file upload * Create generic attributes component (#832) * Create generic Attributes component * Add story for Attributes component * Remove deprecated attribute value type field from queries * Update test snapshots of attributes component * Add file upload field to atributes (#888) * Add story for Attributes component * Update test snapshots of attributes component * Create file upload field in attributes * Update upload file input data-test * Update storybook test snapshots of attributes * Add dedicated input props to file field * Run Cypress using custom API * Add missing error handling in file upload field Co-authored-by: Krzysztof Wolski <krzysztof.k.wolski@gmail.com> * Add file attribute upload to page attributes (#894) * Support upload file attribute for pages * Update after review * Add file attribute upload to variant attributes (#892) * Support upload file attribute for variants * Update after review * Refactor attribute values errors merging * Update after review * Add file attribute upload to product attributes (#826) * Support upload file attribute for products * Update after review * Refactor attribute values errors merging * Refactor product attribute value delete handling * Fix deleting file in file upload field * Fix delete attribute values errors handling * Add link to file upload field (#898) * Update file attributes updates (#899) * Update file attributes updates * Refactor file uploads handling * Move attributes utils to attributes directory * Fix product channel listing updates * Clear file field value if file is not passed as prop * Delete attribute values before update (#908) * Delete file attributes after file update * Triggr CI * Show skeleton in file upload field during loading Co-authored-by: Krzysztof Wolski <krzysztof.k.wolski@gmail.com>
2020-12-16 10:53:28 +00:00
import { FormsetData } from "@saleor/hooks/useFormset";
import { PageDetails_page_attributes } from "@saleor/pages/types/PageDetails";
import { ProductDetails_product_attributes } from "@saleor/products/types/ProductDetails";
import { SearchPages_search_edges_node } from "@saleor/searches/types/SearchPages";
import { SearchProducts_search_edges_node } from "@saleor/searches/types/SearchProducts";
File attributes (#884) * Update changelog with file attributes * Add file type attribute * Update attribute properties form * Update translation messages with file upload * Create generic attributes component (#832) * Create generic Attributes component * Add story for Attributes component * Remove deprecated attribute value type field from queries * Update test snapshots of attributes component * Add file upload field to atributes (#888) * Add story for Attributes component * Update test snapshots of attributes component * Create file upload field in attributes * Update upload file input data-test * Update storybook test snapshots of attributes * Add dedicated input props to file field * Run Cypress using custom API * Add missing error handling in file upload field Co-authored-by: Krzysztof Wolski <krzysztof.k.wolski@gmail.com> * Add file attribute upload to page attributes (#894) * Support upload file attribute for pages * Update after review * Add file attribute upload to variant attributes (#892) * Support upload file attribute for variants * Update after review * Refactor attribute values errors merging * Update after review * Add file attribute upload to product attributes (#826) * Support upload file attribute for products * Update after review * Refactor attribute values errors merging * Refactor product attribute value delete handling * Fix deleting file in file upload field * Fix delete attribute values errors handling * Add link to file upload field (#898) * Update file attributes updates (#899) * Update file attributes updates * Refactor file uploads handling * Move attributes utils to attributes directory * Fix product channel listing updates * Clear file field value if file is not passed as prop * Delete attribute values before update (#908) * Delete file attributes after file update * Triggr CI * Show skeleton in file upload field during loading Co-authored-by: Krzysztof Wolski <krzysztof.k.wolski@gmail.com>
2020-12-16 10:53:28 +00:00
import {
AttributeEntityTypeEnum,
File attributes (#884) * Update changelog with file attributes * Add file type attribute * Update attribute properties form * Update translation messages with file upload * Create generic attributes component (#832) * Create generic Attributes component * Add story for Attributes component * Remove deprecated attribute value type field from queries * Update test snapshots of attributes component * Add file upload field to atributes (#888) * Add story for Attributes component * Update test snapshots of attributes component * Create file upload field in attributes * Update upload file input data-test * Update storybook test snapshots of attributes * Add dedicated input props to file field * Run Cypress using custom API * Add missing error handling in file upload field Co-authored-by: Krzysztof Wolski <krzysztof.k.wolski@gmail.com> * Add file attribute upload to page attributes (#894) * Support upload file attribute for pages * Update after review * Add file attribute upload to variant attributes (#892) * Support upload file attribute for variants * Update after review * Refactor attribute values errors merging * Update after review * Add file attribute upload to product attributes (#826) * Support upload file attribute for products * Update after review * Refactor attribute values errors merging * Refactor product attribute value delete handling * Fix deleting file in file upload field * Fix delete attribute values errors handling * Add link to file upload field (#898) * Update file attributes updates (#899) * Update file attributes updates * Refactor file uploads handling * Move attributes utils to attributes directory * Fix product channel listing updates * Clear file field value if file is not passed as prop * Delete attribute values before update (#908) * Delete file attributes after file update * Triggr CI * Show skeleton in file upload field during loading Co-authored-by: Krzysztof Wolski <krzysztof.k.wolski@gmail.com>
2020-12-16 10:53:28 +00:00
AttributeInputTypeEnum,
AttributeValueInput
} from "@saleor/types/globalTypes";
import {
mapEdgesToItems,
mapNodeToChoice,
mapPagesToChoices
} from "@saleor/utils/maps";
File attributes (#884) * Update changelog with file attributes * Add file type attribute * Update attribute properties form * Update translation messages with file upload * Create generic attributes component (#832) * Create generic Attributes component * Add story for Attributes component * Remove deprecated attribute value type field from queries * Update test snapshots of attributes component * Add file upload field to atributes (#888) * Add story for Attributes component * Update test snapshots of attributes component * Create file upload field in attributes * Update upload file input data-test * Update storybook test snapshots of attributes * Add dedicated input props to file field * Run Cypress using custom API * Add missing error handling in file upload field Co-authored-by: Krzysztof Wolski <krzysztof.k.wolski@gmail.com> * Add file attribute upload to page attributes (#894) * Support upload file attribute for pages * Update after review * Add file attribute upload to variant attributes (#892) * Support upload file attribute for variants * Update after review * Refactor attribute values errors merging * Update after review * Add file attribute upload to product attributes (#826) * Support upload file attribute for products * Update after review * Refactor attribute values errors merging * Refactor product attribute value delete handling * Fix deleting file in file upload field * Fix delete attribute values errors handling * Add link to file upload field (#898) * Update file attributes updates (#899) * Update file attributes updates * Refactor file uploads handling * Move attributes utils to attributes directory * Fix product channel listing updates * Clear file field value if file is not passed as prop * Delete attribute values before update (#908) * Delete file attributes after file update * Triggr CI * Show skeleton in file upload field during loading Co-authored-by: Krzysztof Wolski <krzysztof.k.wolski@gmail.com>
2020-12-16 10:53:28 +00:00
import { AttributePageFormData } from "../components/AttributePage";
import { AtributesOfFiles } from "../types/AttributeOfUploadedFile";
File attributes (#884) * Update changelog with file attributes * Add file type attribute * Update attribute properties form * Update translation messages with file upload * Create generic attributes component (#832) * Create generic Attributes component * Add story for Attributes component * Remove deprecated attribute value type field from queries * Update test snapshots of attributes component * Add file upload field to atributes (#888) * Add story for Attributes component * Update test snapshots of attributes component * Create file upload field in attributes * Update upload file input data-test * Update storybook test snapshots of attributes * Add dedicated input props to file field * Run Cypress using custom API * Add missing error handling in file upload field Co-authored-by: Krzysztof Wolski <krzysztof.k.wolski@gmail.com> * Add file attribute upload to page attributes (#894) * Support upload file attribute for pages * Update after review * Add file attribute upload to variant attributes (#892) * Support upload file attribute for variants * Update after review * Refactor attribute values errors merging * Update after review * Add file attribute upload to product attributes (#826) * Support upload file attribute for products * Update after review * Refactor attribute values errors merging * Refactor product attribute value delete handling * Fix deleting file in file upload field * Fix delete attribute values errors handling * Add link to file upload field (#898) * Update file attributes updates (#899) * Update file attributes updates * Refactor file uploads handling * Move attributes utils to attributes directory * Fix product channel listing updates * Clear file field value if file is not passed as prop * Delete attribute values before update (#908) * Delete file attributes after file update * Triggr CI * Show skeleton in file upload field during loading Co-authored-by: Krzysztof Wolski <krzysztof.k.wolski@gmail.com>
2020-12-16 10:53:28 +00:00
import { AttributeValueDelete } from "../types/AttributeValueDelete";
export const ATTRIBUTE_TYPES_WITH_DEDICATED_VALUES = [
AttributeInputTypeEnum.DROPDOWN,
AttributeInputTypeEnum.MULTISELECT,
AttributeInputTypeEnum.SWATCH
];
export const ATTRIBUTE_TYPES_WITH_CONFIGURABLE_FACED_NAVIGATION = [
AttributeInputTypeEnum.DROPDOWN,
AttributeInputTypeEnum.MULTISELECT,
AttributeInputTypeEnum.BOOLEAN,
AttributeInputTypeEnum.DATE,
AttributeInputTypeEnum.DATE_TIME,
AttributeInputTypeEnum.NUMERIC,
AttributeInputTypeEnum.SWATCH
];
export interface AttributeReference {
label: string;
value: string;
}
export interface AttributeValueEditDialogFormData {
name: string;
value?: string;
fileUrl?: string;
contentType?: string;
}
export function attributeValueFragmentToFormData(
data: AttributeValueFragment | null
): AttributeValueEditDialogFormData {
return {
name: data?.name,
value: data?.value,
contentType: data?.file?.contentType,
fileUrl: data?.file?.url
};
}
function getSimpleAttributeData(
data: AttributePageFormData,
values: AttributeValueEditDialogFormData[]
) {
return {
...data,
metadata: undefined,
privateMetadata: undefined,
storefrontSearchPosition: parseInt(data.storefrontSearchPosition, 10),
values: values.map(value => ({
name: value.name
}))
};
}
function getAttributeValueTypeFields({
fileUrl,
value,
name,
contentType
}: AttributeValueEditDialogFormData) {
return {
name,
...(fileUrl ? { fileUrl, contentType } : { value })
};
}
function getSwatchAttributeData(
data: AttributePageFormData,
values: AttributeValueEditDialogFormData[]
) {
return {
...data,
metadata: undefined,
privateMetadata: undefined,
storefrontSearchPosition: parseInt(data.storefrontSearchPosition, 10),
values: values.map(getAttributeValueTypeFields)
};
}
function getFileOrReferenceAttributeData(
data: AttributePageFormData,
values: AttributeValueEditDialogFormData[]
) {
return {
...getSimpleAttributeData(data, values),
availableInGrid: undefined,
filterableInDashboard: undefined,
filterableInStorefront: undefined
};
}
export function getAttributeData(
data: AttributePageFormData,
values: AttributeValueEditDialogFormData[]
) {
if (data.inputType === AttributeInputTypeEnum.SWATCH) {
return getSwatchAttributeData(data, values);
} else if (ATTRIBUTE_TYPES_WITH_DEDICATED_VALUES.includes(data.inputType)) {
return getSimpleAttributeData(data, values);
} else {
return getFileOrReferenceAttributeData(data, values);
}
}
export function getDefaultAttributeValues(attribute: VariantAttributeFragment) {
switch (attribute.inputType) {
case AttributeInputTypeEnum.BOOLEAN:
return ["false"];
default:
return [];
}
}
export function getSelectedAttributeValues(
attribute:
| PageDetails_page_attributes
| ProductDetails_product_attributes
| SelectedVariantAttributeFragment
) {
switch (attribute.attribute.inputType) {
case AttributeInputTypeEnum.REFERENCE:
return attribute.values.map(value => value.reference);
case AttributeInputTypeEnum.RICH_TEXT:
return [attribute.values[0]?.richText];
case AttributeInputTypeEnum.NUMERIC:
return [attribute.values[0]?.name];
case AttributeInputTypeEnum.BOOLEAN:
return [attribute.values[0]?.boolean ?? "false"];
case AttributeInputTypeEnum.DATE:
return [attribute.values[0]?.date];
case AttributeInputTypeEnum.DATE_TIME:
return [attribute.values[0]?.dateTime];
default:
return attribute.values.map(value => value.slug);
}
}
File attributes (#884) * Update changelog with file attributes * Add file type attribute * Update attribute properties form * Update translation messages with file upload * Create generic attributes component (#832) * Create generic Attributes component * Add story for Attributes component * Remove deprecated attribute value type field from queries * Update test snapshots of attributes component * Add file upload field to atributes (#888) * Add story for Attributes component * Update test snapshots of attributes component * Create file upload field in attributes * Update upload file input data-test * Update storybook test snapshots of attributes * Add dedicated input props to file field * Run Cypress using custom API * Add missing error handling in file upload field Co-authored-by: Krzysztof Wolski <krzysztof.k.wolski@gmail.com> * Add file attribute upload to page attributes (#894) * Support upload file attribute for pages * Update after review * Add file attribute upload to variant attributes (#892) * Support upload file attribute for variants * Update after review * Refactor attribute values errors merging * Update after review * Add file attribute upload to product attributes (#826) * Support upload file attribute for products * Update after review * Refactor attribute values errors merging * Refactor product attribute value delete handling * Fix deleting file in file upload field * Fix delete attribute values errors handling * Add link to file upload field (#898) * Update file attributes updates (#899) * Update file attributes updates * Refactor file uploads handling * Move attributes utils to attributes directory * Fix product channel listing updates * Clear file field value if file is not passed as prop * Delete attribute values before update (#908) * Delete file attributes after file update * Triggr CI * Show skeleton in file upload field during loading Co-authored-by: Krzysztof Wolski <krzysztof.k.wolski@gmail.com>
2020-12-16 10:53:28 +00:00
export const isFileValueUnused = (
attributesWithNewFileValue: FormsetData<null, File>,
existingAttribute:
| PageDetails_page_attributes
| ProductDetails_product_attributes
File attributes (#884) * Update changelog with file attributes * Add file type attribute * Update attribute properties form * Update translation messages with file upload * Create generic attributes component (#832) * Create generic Attributes component * Add story for Attributes component * Remove deprecated attribute value type field from queries * Update test snapshots of attributes component * Add file upload field to atributes (#888) * Add story for Attributes component * Update test snapshots of attributes component * Create file upload field in attributes * Update upload file input data-test * Update storybook test snapshots of attributes * Add dedicated input props to file field * Run Cypress using custom API * Add missing error handling in file upload field Co-authored-by: Krzysztof Wolski <krzysztof.k.wolski@gmail.com> * Add file attribute upload to page attributes (#894) * Support upload file attribute for pages * Update after review * Add file attribute upload to variant attributes (#892) * Support upload file attribute for variants * Update after review * Refactor attribute values errors merging * Update after review * Add file attribute upload to product attributes (#826) * Support upload file attribute for products * Update after review * Refactor attribute values errors merging * Refactor product attribute value delete handling * Fix deleting file in file upload field * Fix delete attribute values errors handling * Add link to file upload field (#898) * Update file attributes updates (#899) * Update file attributes updates * Refactor file uploads handling * Move attributes utils to attributes directory * Fix product channel listing updates * Clear file field value if file is not passed as prop * Delete attribute values before update (#908) * Delete file attributes after file update * Triggr CI * Show skeleton in file upload field during loading Co-authored-by: Krzysztof Wolski <krzysztof.k.wolski@gmail.com>
2020-12-16 10:53:28 +00:00
| SelectedVariantAttributeFragment
) => {
if (existingAttribute.attribute.inputType !== AttributeInputTypeEnum.FILE) {
return false;
}
if (existingAttribute.values.length === 0) {
return false;
}
const modifiedAttribute = attributesWithNewFileValue.find(
dataAttribute => dataAttribute.id === existingAttribute.attribute.id
);
return !!modifiedAttribute;
};
export const mergeFileUploadErrors = (
uploadFilesResult: Array<FetchResult<FileUpload>>
File attributes (#884) * Update changelog with file attributes * Add file type attribute * Update attribute properties form * Update translation messages with file upload * Create generic attributes component (#832) * Create generic Attributes component * Add story for Attributes component * Remove deprecated attribute value type field from queries * Update test snapshots of attributes component * Add file upload field to atributes (#888) * Add story for Attributes component * Update test snapshots of attributes component * Create file upload field in attributes * Update upload file input data-test * Update storybook test snapshots of attributes * Add dedicated input props to file field * Run Cypress using custom API * Add missing error handling in file upload field Co-authored-by: Krzysztof Wolski <krzysztof.k.wolski@gmail.com> * Add file attribute upload to page attributes (#894) * Support upload file attribute for pages * Update after review * Add file attribute upload to variant attributes (#892) * Support upload file attribute for variants * Update after review * Refactor attribute values errors merging * Update after review * Add file attribute upload to product attributes (#826) * Support upload file attribute for products * Update after review * Refactor attribute values errors merging * Refactor product attribute value delete handling * Fix deleting file in file upload field * Fix delete attribute values errors handling * Add link to file upload field (#898) * Update file attributes updates (#899) * Update file attributes updates * Refactor file uploads handling * Move attributes utils to attributes directory * Fix product channel listing updates * Clear file field value if file is not passed as prop * Delete attribute values before update (#908) * Delete file attributes after file update * Triggr CI * Show skeleton in file upload field during loading Co-authored-by: Krzysztof Wolski <krzysztof.k.wolski@gmail.com>
2020-12-16 10:53:28 +00:00
): UploadErrorFragment[] =>
uploadFilesResult.reduce((errors, uploadFileResult) => {
const uploadErrors = uploadFileResult?.data?.fileUpload?.errors;
File attributes (#884) * Update changelog with file attributes * Add file type attribute * Update attribute properties form * Update translation messages with file upload * Create generic attributes component (#832) * Create generic Attributes component * Add story for Attributes component * Remove deprecated attribute value type field from queries * Update test snapshots of attributes component * Add file upload field to atributes (#888) * Add story for Attributes component * Update test snapshots of attributes component * Create file upload field in attributes * Update upload file input data-test * Update storybook test snapshots of attributes * Add dedicated input props to file field * Run Cypress using custom API * Add missing error handling in file upload field Co-authored-by: Krzysztof Wolski <krzysztof.k.wolski@gmail.com> * Add file attribute upload to page attributes (#894) * Support upload file attribute for pages * Update after review * Add file attribute upload to variant attributes (#892) * Support upload file attribute for variants * Update after review * Refactor attribute values errors merging * Update after review * Add file attribute upload to product attributes (#826) * Support upload file attribute for products * Update after review * Refactor attribute values errors merging * Refactor product attribute value delete handling * Fix deleting file in file upload field * Fix delete attribute values errors handling * Add link to file upload field (#898) * Update file attributes updates (#899) * Update file attributes updates * Refactor file uploads handling * Move attributes utils to attributes directory * Fix product channel listing updates * Clear file field value if file is not passed as prop * Delete attribute values before update (#908) * Delete file attributes after file update * Triggr CI * Show skeleton in file upload field during loading Co-authored-by: Krzysztof Wolski <krzysztof.k.wolski@gmail.com>
2020-12-16 10:53:28 +00:00
if (uploadErrors) {
return [...errors, ...uploadErrors];
}
return errors;
}, []);
export const mergeAttributeValueDeleteErrors = (
deleteAttributeValuesResult: Array<FetchResult<AttributeValueDelete>>
File attributes (#884) * Update changelog with file attributes * Add file type attribute * Update attribute properties form * Update translation messages with file upload * Create generic attributes component (#832) * Create generic Attributes component * Add story for Attributes component * Remove deprecated attribute value type field from queries * Update test snapshots of attributes component * Add file upload field to atributes (#888) * Add story for Attributes component * Update test snapshots of attributes component * Create file upload field in attributes * Update upload file input data-test * Update storybook test snapshots of attributes * Add dedicated input props to file field * Run Cypress using custom API * Add missing error handling in file upload field Co-authored-by: Krzysztof Wolski <krzysztof.k.wolski@gmail.com> * Add file attribute upload to page attributes (#894) * Support upload file attribute for pages * Update after review * Add file attribute upload to variant attributes (#892) * Support upload file attribute for variants * Update after review * Refactor attribute values errors merging * Update after review * Add file attribute upload to product attributes (#826) * Support upload file attribute for products * Update after review * Refactor attribute values errors merging * Refactor product attribute value delete handling * Fix deleting file in file upload field * Fix delete attribute values errors handling * Add link to file upload field (#898) * Update file attributes updates (#899) * Update file attributes updates * Refactor file uploads handling * Move attributes utils to attributes directory * Fix product channel listing updates * Clear file field value if file is not passed as prop * Delete attribute values before update (#908) * Delete file attributes after file update * Triggr CI * Show skeleton in file upload field during loading Co-authored-by: Krzysztof Wolski <krzysztof.k.wolski@gmail.com>
2020-12-16 10:53:28 +00:00
): AttributeErrorFragment[] =>
deleteAttributeValuesResult.reduce((errors, deleteValueResult) => {
const deleteErrors = deleteValueResult?.data?.attributeValueDelete?.errors;
if (deleteErrors) {
return [...errors, ...deleteErrors];
}
return errors;
}, []);
export const mergeChoicesWithValues = (
attribute:
| ProductDetails_product_attributes
| PageDetails_page_attributes
| SelectedVariantAttributeFragment
) => {
const choices = mapEdgesToItems(attribute.attribute.choices) || [];
const valuesToConcat = attribute.values.filter(
value => !choices.some(choice => choice.id === value.id)
);
return choices.concat(valuesToConcat);
};
export const mergeAttributeValues = (
attributeId: string,
attributeValues: string[],
attributes: FormsetData<AttributeInputData, string[]>
) => {
const attribute = attributes.find(attribute => attribute.id === attributeId);
return attribute.value
? [...attribute.value, ...attributeValues]
: attributeValues;
};
File attributes (#884) * Update changelog with file attributes * Add file type attribute * Update attribute properties form * Update translation messages with file upload * Create generic attributes component (#832) * Create generic Attributes component * Add story for Attributes component * Remove deprecated attribute value type field from queries * Update test snapshots of attributes component * Add file upload field to atributes (#888) * Add story for Attributes component * Update test snapshots of attributes component * Create file upload field in attributes * Update upload file input data-test * Update storybook test snapshots of attributes * Add dedicated input props to file field * Run Cypress using custom API * Add missing error handling in file upload field Co-authored-by: Krzysztof Wolski <krzysztof.k.wolski@gmail.com> * Add file attribute upload to page attributes (#894) * Support upload file attribute for pages * Update after review * Add file attribute upload to variant attributes (#892) * Support upload file attribute for variants * Update after review * Refactor attribute values errors merging * Update after review * Add file attribute upload to product attributes (#826) * Support upload file attribute for products * Update after review * Refactor attribute values errors merging * Refactor product attribute value delete handling * Fix deleting file in file upload field * Fix delete attribute values errors handling * Add link to file upload field (#898) * Update file attributes updates (#899) * Update file attributes updates * Refactor file uploads handling * Move attributes utils to attributes directory * Fix product channel listing updates * Clear file field value if file is not passed as prop * Delete attribute values before update (#908) * Delete file attributes after file update * Triggr CI * Show skeleton in file upload field during loading Co-authored-by: Krzysztof Wolski <krzysztof.k.wolski@gmail.com>
2020-12-16 10:53:28 +00:00
export const getFileValuesToUploadFromAttributes = (
attributesWithNewFileValue: FormsetData<null, File>
) => attributesWithNewFileValue.filter(fileAttribute => !!fileAttribute.value);
export const getFileValuesRemovedFromAttributes = (
attributesWithNewFileValue: FormsetData<null, File>
) => attributesWithNewFileValue.filter(attribute => !attribute.value);
export const getAttributesOfRemovedFiles = (
fileAttributesRemoved: FormsetData<null, File>
): AtributesOfFiles[] =>
File attributes (#884) * Update changelog with file attributes * Add file type attribute * Update attribute properties form * Update translation messages with file upload * Create generic attributes component (#832) * Create generic Attributes component * Add story for Attributes component * Remove deprecated attribute value type field from queries * Update test snapshots of attributes component * Add file upload field to atributes (#888) * Add story for Attributes component * Update test snapshots of attributes component * Create file upload field in attributes * Update upload file input data-test * Update storybook test snapshots of attributes * Add dedicated input props to file field * Run Cypress using custom API * Add missing error handling in file upload field Co-authored-by: Krzysztof Wolski <krzysztof.k.wolski@gmail.com> * Add file attribute upload to page attributes (#894) * Support upload file attribute for pages * Update after review * Add file attribute upload to variant attributes (#892) * Support upload file attribute for variants * Update after review * Refactor attribute values errors merging * Update after review * Add file attribute upload to product attributes (#826) * Support upload file attribute for products * Update after review * Refactor attribute values errors merging * Refactor product attribute value delete handling * Fix deleting file in file upload field * Fix delete attribute values errors handling * Add link to file upload field (#898) * Update file attributes updates (#899) * Update file attributes updates * Refactor file uploads handling * Move attributes utils to attributes directory * Fix product channel listing updates * Clear file field value if file is not passed as prop * Delete attribute values before update (#908) * Delete file attributes after file update * Triggr CI * Show skeleton in file upload field during loading Co-authored-by: Krzysztof Wolski <krzysztof.k.wolski@gmail.com>
2020-12-16 10:53:28 +00:00
fileAttributesRemoved.map(attribute => ({
file: undefined,
id: attribute.id,
contentType: attribute.value?.type,
File attributes (#884) * Update changelog with file attributes * Add file type attribute * Update attribute properties form * Update translation messages with file upload * Create generic attributes component (#832) * Create generic Attributes component * Add story for Attributes component * Remove deprecated attribute value type field from queries * Update test snapshots of attributes component * Add file upload field to atributes (#888) * Add story for Attributes component * Update test snapshots of attributes component * Create file upload field in attributes * Update upload file input data-test * Update storybook test snapshots of attributes * Add dedicated input props to file field * Run Cypress using custom API * Add missing error handling in file upload field Co-authored-by: Krzysztof Wolski <krzysztof.k.wolski@gmail.com> * Add file attribute upload to page attributes (#894) * Support upload file attribute for pages * Update after review * Add file attribute upload to variant attributes (#892) * Support upload file attribute for variants * Update after review * Refactor attribute values errors merging * Update after review * Add file attribute upload to product attributes (#826) * Support upload file attribute for products * Update after review * Refactor attribute values errors merging * Refactor product attribute value delete handling * Fix deleting file in file upload field * Fix delete attribute values errors handling * Add link to file upload field (#898) * Update file attributes updates (#899) * Update file attributes updates * Refactor file uploads handling * Move attributes utils to attributes directory * Fix product channel listing updates * Clear file field value if file is not passed as prop * Delete attribute values before update (#908) * Delete file attributes after file update * Triggr CI * Show skeleton in file upload field during loading Co-authored-by: Krzysztof Wolski <krzysztof.k.wolski@gmail.com>
2020-12-16 10:53:28 +00:00
values: []
}));
export const getAttributesOfUploadedFiles = (
fileValuesToUpload: FormsetData<null, File>,
uploadFilesResult: Array<FetchResult<FileUpload>>
): AtributesOfFiles[] =>
File attributes (#884) * Update changelog with file attributes * Add file type attribute * Update attribute properties form * Update translation messages with file upload * Create generic attributes component (#832) * Create generic Attributes component * Add story for Attributes component * Remove deprecated attribute value type field from queries * Update test snapshots of attributes component * Add file upload field to atributes (#888) * Add story for Attributes component * Update test snapshots of attributes component * Create file upload field in attributes * Update upload file input data-test * Update storybook test snapshots of attributes * Add dedicated input props to file field * Run Cypress using custom API * Add missing error handling in file upload field Co-authored-by: Krzysztof Wolski <krzysztof.k.wolski@gmail.com> * Add file attribute upload to page attributes (#894) * Support upload file attribute for pages * Update after review * Add file attribute upload to variant attributes (#892) * Support upload file attribute for variants * Update after review * Refactor attribute values errors merging * Update after review * Add file attribute upload to product attributes (#826) * Support upload file attribute for products * Update after review * Refactor attribute values errors merging * Refactor product attribute value delete handling * Fix deleting file in file upload field * Fix delete attribute values errors handling * Add link to file upload field (#898) * Update file attributes updates (#899) * Update file attributes updates * Refactor file uploads handling * Move attributes utils to attributes directory * Fix product channel listing updates * Clear file field value if file is not passed as prop * Delete attribute values before update (#908) * Delete file attributes after file update * Triggr CI * Show skeleton in file upload field during loading Co-authored-by: Krzysztof Wolski <krzysztof.k.wolski@gmail.com>
2020-12-16 10:53:28 +00:00
uploadFilesResult.map((uploadFileResult, index) => {
const attribute = fileValuesToUpload[index];
return {
file: uploadFileResult.data.fileUpload.uploadedFile.url,
contentType: uploadFileResult.data.fileUpload.uploadedFile.contentType,
File attributes (#884) * Update changelog with file attributes * Add file type attribute * Update attribute properties form * Update translation messages with file upload * Create generic attributes component (#832) * Create generic Attributes component * Add story for Attributes component * Remove deprecated attribute value type field from queries * Update test snapshots of attributes component * Add file upload field to atributes (#888) * Add story for Attributes component * Update test snapshots of attributes component * Create file upload field in attributes * Update upload file input data-test * Update storybook test snapshots of attributes * Add dedicated input props to file field * Run Cypress using custom API * Add missing error handling in file upload field Co-authored-by: Krzysztof Wolski <krzysztof.k.wolski@gmail.com> * Add file attribute upload to page attributes (#894) * Support upload file attribute for pages * Update after review * Add file attribute upload to variant attributes (#892) * Support upload file attribute for variants * Update after review * Refactor attribute values errors merging * Update after review * Add file attribute upload to product attributes (#826) * Support upload file attribute for products * Update after review * Refactor attribute values errors merging * Refactor product attribute value delete handling * Fix deleting file in file upload field * Fix delete attribute values errors handling * Add link to file upload field (#898) * Update file attributes updates (#899) * Update file attributes updates * Refactor file uploads handling * Move attributes utils to attributes directory * Fix product channel listing updates * Clear file field value if file is not passed as prop * Delete attribute values before update (#908) * Delete file attributes after file update * Triggr CI * Show skeleton in file upload field during loading Co-authored-by: Krzysztof Wolski <krzysztof.k.wolski@gmail.com>
2020-12-16 10:53:28 +00:00
id: attribute.id,
values: []
};
});
export const getAttributesAfterFileAttributesUpdate = (
attributesWithNewFileValue: FormsetData<null, File>,
uploadFilesResult: Array<FetchResult<FileUpload>>
File attributes (#884) * Update changelog with file attributes * Add file type attribute * Update attribute properties form * Update translation messages with file upload * Create generic attributes component (#832) * Create generic Attributes component * Add story for Attributes component * Remove deprecated attribute value type field from queries * Update test snapshots of attributes component * Add file upload field to atributes (#888) * Add story for Attributes component * Update test snapshots of attributes component * Create file upload field in attributes * Update upload file input data-test * Update storybook test snapshots of attributes * Add dedicated input props to file field * Run Cypress using custom API * Add missing error handling in file upload field Co-authored-by: Krzysztof Wolski <krzysztof.k.wolski@gmail.com> * Add file attribute upload to page attributes (#894) * Support upload file attribute for pages * Update after review * Add file attribute upload to variant attributes (#892) * Support upload file attribute for variants * Update after review * Refactor attribute values errors merging * Update after review * Add file attribute upload to product attributes (#826) * Support upload file attribute for products * Update after review * Refactor attribute values errors merging * Refactor product attribute value delete handling * Fix deleting file in file upload field * Fix delete attribute values errors handling * Add link to file upload field (#898) * Update file attributes updates (#899) * Update file attributes updates * Refactor file uploads handling * Move attributes utils to attributes directory * Fix product channel listing updates * Clear file field value if file is not passed as prop * Delete attribute values before update (#908) * Delete file attributes after file update * Triggr CI * Show skeleton in file upload field during loading Co-authored-by: Krzysztof Wolski <krzysztof.k.wolski@gmail.com>
2020-12-16 10:53:28 +00:00
): AttributeValueInput[] => {
const removedFileValues = getFileValuesRemovedFromAttributes(
attributesWithNewFileValue
);
const fileValuesToUpload = getFileValuesToUploadFromAttributes(
attributesWithNewFileValue
);
const removedFileAttributes = getAttributesOfRemovedFiles(removedFileValues);
const uploadedFileAttributes = getAttributesOfUploadedFiles(
fileValuesToUpload,
uploadFilesResult
);
return uploadedFileAttributes.concat(removedFileAttributes);
};
export const getFileAttributeDisplayData = (
attribute: AttributeInput,
attributesWithNewFileValue: FormsetData<null, File>
) => {
const attributeWithNewFileValue = attributesWithNewFileValue.find(
attributeWithNewFile => attribute.id === attributeWithNewFile.id
);
if (attributeWithNewFileValue) {
return {
...attribute,
value: attributeWithNewFileValue?.value?.name
? [attributeWithNewFileValue.value.name]
: []
};
}
return attribute;
};
export const getPageReferenceAttributeDisplayData = (
attribute: AttributeInput,
referencePages: SearchPages_search_edges_node[]
) => ({
...attribute,
data: {
...attribute.data,
references:
referencePages?.length > 0 && attribute.value?.length > 0
? mapPagesToChoices(
attribute.value.map(value => {
const reference = referencePages.find(
reference => reference.id === value
);
return { ...reference };
})
)
: []
}
});
export const getProductReferenceAttributeDisplayData = (
attribute: AttributeInput,
referenceProducts: SearchProducts_search_edges_node[]
) => ({
...attribute,
data: {
...attribute.data,
references:
referenceProducts?.length > 0 && attribute.value?.length > 0
? mapNodeToChoice(
attribute.value.map(value => {
const reference = referenceProducts.find(
reference => reference.id === value
);
return { ...reference };
})
)
: []
}
});
export const getReferenceAttributeDisplayData = (
attribute: AttributeInput,
referencePages: SearchPages_search_edges_node[],
referenceProducts: SearchProducts_search_edges_node[]
) => {
if (attribute.data.entityType === AttributeEntityTypeEnum.PAGE) {
return getPageReferenceAttributeDisplayData(attribute, referencePages);
} else if (attribute.data.entityType === AttributeEntityTypeEnum.PRODUCT) {
return getProductReferenceAttributeDisplayData(
attribute,
referenceProducts
);
}
};
export const getAttributesDisplayData = (
attributes: AttributeInput[],
attributesWithNewFileValue: FormsetData<null, File>,
referencePages: SearchPages_search_edges_node[],
referenceProducts: SearchProducts_search_edges_node[]
) =>
attributes.map(attribute => {
if (attribute.data.inputType === AttributeInputTypeEnum.REFERENCE) {
return getReferenceAttributeDisplayData(
attribute,
referencePages,
referenceProducts
);
}
if (attribute.data.inputType === AttributeInputTypeEnum.FILE) {
return getFileAttributeDisplayData(attribute, attributesWithNewFileValue);
}
return attribute;
});
export const getSelectedReferencesFromAttribute = <
Node extends SearchPages_search_edges_node | SearchProducts_search_edges_node
>(
attribute?: AttributeInput,
references?: Node[]
) =>
references?.filter(
value =>
!attribute?.value?.some(selectedValue => selectedValue === value.id)
) || [];
export const getAttributeValuesFromReferences = (
attributeId: string,
attributes?: AttributeInput[],
referencePages?: SearchPages_search_edges_node[],
referenceProducts?: SearchProducts_search_edges_node[]
) => {
const attribute = attributes?.find(attribute => attribute.id === attributeId);
if (attribute?.data?.entityType === AttributeEntityTypeEnum.PAGE) {
return mapPagesToChoices(
getSelectedReferencesFromAttribute(attribute, referencePages)
);
} else if (attribute?.data?.entityType === AttributeEntityTypeEnum.PRODUCT) {
return mapNodeToChoice(
getSelectedReferencesFromAttribute(attribute, referenceProducts)
);
}
return [];
};