import { FileUpload } from "@saleor/files/types/FileUpload"; import { AttributeErrorFragment } from "@saleor/fragments/types/AttributeErrorFragment"; import { SelectedVariantAttributeFragment } from "@saleor/fragments/types/SelectedVariantAttributeFragment"; import { UploadErrorFragment } from "@saleor/fragments/types/UploadErrorFragment"; import { FormsetData } from "@saleor/hooks/useFormset"; import { PageDetails_page_attributes } from "@saleor/pages/types/PageDetails"; import { AttributeInputTypeEnum, AttributeValueInput } from "@saleor/types/globalTypes"; import { MutationFetchResult } from "react-apollo"; import { AttributeValueDelete } from "../types/AttributeValueDelete"; export const isFileValueUnused = ( attributesWithNewFileValue: FormsetData, existingAttribute: | PageDetails_page_attributes | 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> ): UploadErrorFragment[] => uploadFilesResult.reduce((errors, uploadFileResult) => { const uploadErrors = uploadFileResult?.data?.fileUpload?.uploadErrors; if (uploadErrors) { return [...errors, ...uploadErrors]; } return errors; }, []); export const mergeAttributeValueDeleteErrors = ( deleteAttributeValuesResult: Array> ): AttributeErrorFragment[] => deleteAttributeValuesResult.reduce((errors, deleteValueResult) => { const deleteErrors = deleteValueResult?.data?.attributeValueDelete?.errors; if (deleteErrors) { return [...errors, ...deleteErrors]; } return errors; }, []); export const getFileValuesToUploadFromAttributes = ( attributesWithNewFileValue: FormsetData ) => attributesWithNewFileValue.filter(fileAttribute => !!fileAttribute.value); export const getFileValuesRemovedFromAttributes = ( attributesWithNewFileValue: FormsetData ) => attributesWithNewFileValue.filter(attribute => !attribute.value); export const getAttributesOfRemovedFiles = ( fileAttributesRemoved: FormsetData ) => fileAttributesRemoved.map(attribute => ({ file: undefined, id: attribute.id, values: [] })); export const getAttributesOfUploadedFiles = ( fileValuesToUpload: FormsetData, uploadFilesResult: Array> ) => uploadFilesResult.map((uploadFileResult, index) => { const attribute = fileValuesToUpload[index]; return { file: uploadFileResult.data.fileUpload.uploadedFile.url, id: attribute.id, values: [] }; }); export const getAttributesAfterFileAttributesUpdate = ( attributesWithNewFileValue: FormsetData, uploadFilesResult: Array> ): AttributeValueInput[] => { const removedFileValues = getFileValuesRemovedFromAttributes( attributesWithNewFileValue ); const fileValuesToUpload = getFileValuesToUploadFromAttributes( attributesWithNewFileValue ); const removedFileAttributes = getAttributesOfRemovedFiles(removedFileValues); const uploadedFileAttributes = getAttributesOfUploadedFiles( fileValuesToUpload, uploadFilesResult ); return uploadedFileAttributes.concat(removedFileAttributes); };