2021-01-18 15:37:38 +00:00
|
|
|
import {
|
|
|
|
AttributeInput,
|
|
|
|
AttributeInputData
|
|
|
|
} from "@saleor/components/Attributes";
|
2020-12-16 10:53:28 +00:00
|
|
|
import {
|
|
|
|
FileUpload,
|
|
|
|
FileUploadVariables
|
|
|
|
} from "@saleor/files/types/FileUpload";
|
2021-01-18 15:37:38 +00:00
|
|
|
import {
|
|
|
|
FormsetAtomicData,
|
|
|
|
FormsetChange,
|
|
|
|
FormsetData
|
|
|
|
} from "@saleor/hooks/useFormset";
|
2020-12-16 10:53:28 +00:00
|
|
|
import { PageDetails_page_attributes } from "@saleor/pages/types/PageDetails";
|
2021-01-18 15:37:38 +00:00
|
|
|
import { ReorderEvent } from "@saleor/types";
|
2020-12-16 10:53:28 +00:00
|
|
|
import {
|
|
|
|
AttributeInputTypeEnum,
|
|
|
|
AttributeValueInput
|
|
|
|
} from "@saleor/types/globalTypes";
|
2021-01-18 15:37:38 +00:00
|
|
|
import { move, toggle } from "@saleor/utils/lists";
|
2020-12-16 10:53:28 +00:00
|
|
|
import { MutationFetchResult } from "react-apollo";
|
|
|
|
|
|
|
|
import {
|
|
|
|
AttributeValueDelete,
|
|
|
|
AttributeValueDeleteVariables
|
|
|
|
} from "../types/AttributeValueDelete";
|
|
|
|
import { getFileValuesToUploadFromAttributes, isFileValueUnused } from "./data";
|
|
|
|
|
2021-01-18 15:37:38 +00:00
|
|
|
export function createAttributeChangeHandler(
|
|
|
|
changeAttributeData: FormsetChange<string[]>,
|
|
|
|
triggerChange: () => void
|
|
|
|
): FormsetChange<string> {
|
|
|
|
return (attributeId: string, value: string) => {
|
|
|
|
triggerChange();
|
|
|
|
changeAttributeData(attributeId, value === "" ? [] : [value]);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function createAttributeMultiChangeHandler(
|
|
|
|
changeAttributeData: FormsetChange<string[]>,
|
|
|
|
attributes: FormsetData<AttributeInputData, string[]>,
|
|
|
|
triggerChange: () => void
|
|
|
|
): FormsetChange<string> {
|
|
|
|
return (attributeId: string, value: string) => {
|
|
|
|
const attribute = attributes.find(
|
|
|
|
attribute => attribute.id === attributeId
|
|
|
|
);
|
|
|
|
|
|
|
|
const newAttributeValues = toggle(
|
|
|
|
value,
|
|
|
|
attribute.value,
|
|
|
|
(a, b) => a === b
|
|
|
|
);
|
|
|
|
|
|
|
|
triggerChange();
|
|
|
|
changeAttributeData(attributeId, newAttributeValues);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function createAttributeReferenceChangeHandler(
|
|
|
|
changeAttributeData: FormsetChange<string[]>,
|
|
|
|
triggerChange: () => void
|
|
|
|
): FormsetChange<string[]> {
|
|
|
|
return (attributeId: string, values: string[]) => {
|
|
|
|
changeAttributeData(attributeId, values);
|
|
|
|
triggerChange();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function createAttributeFileChangeHandler(
|
|
|
|
changeAttributeData: FormsetChange<string[]>,
|
|
|
|
attributesWithNewFileValue: FormsetData<FormsetData<null, File>>,
|
|
|
|
addAttributeNewFileValue: (data: FormsetAtomicData<null, File>) => void,
|
|
|
|
changeAttributeNewFileValue: FormsetChange<File>,
|
|
|
|
triggerChange: () => void
|
|
|
|
): FormsetChange<File> {
|
|
|
|
return (attributeId: string, value: File) => {
|
|
|
|
triggerChange();
|
|
|
|
|
|
|
|
const newFileValueAssigned = attributesWithNewFileValue.find(
|
|
|
|
attribute => attribute.id === attributeId
|
|
|
|
);
|
|
|
|
|
|
|
|
if (newFileValueAssigned) {
|
|
|
|
changeAttributeNewFileValue(attributeId, value);
|
|
|
|
} else {
|
|
|
|
addAttributeNewFileValue({
|
|
|
|
data: null,
|
|
|
|
id: attributeId,
|
|
|
|
label: null,
|
|
|
|
value
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
changeAttributeData(attributeId, value ? [value.name] : []);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function createAttributeValueReorderHandler(
|
|
|
|
changeAttributeData: FormsetChange<string[]>,
|
|
|
|
attributes: FormsetData<AttributeInputData, string[]>,
|
|
|
|
triggerChange: () => void
|
|
|
|
): FormsetChange<ReorderEvent> {
|
|
|
|
return (attributeId: string, reorder: ReorderEvent) => {
|
|
|
|
triggerChange();
|
|
|
|
|
|
|
|
const attribute = attributes.find(
|
|
|
|
attribute => attribute.id === attributeId
|
|
|
|
);
|
|
|
|
|
|
|
|
const reorderedValues = move(
|
|
|
|
attribute.value[reorder.oldIndex],
|
|
|
|
attribute.value,
|
|
|
|
(a, b) => a === b,
|
|
|
|
reorder.newIndex
|
|
|
|
);
|
|
|
|
|
|
|
|
changeAttributeData(attributeId, reorderedValues);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-12-16 10:53:28 +00:00
|
|
|
interface AttributesArgs {
|
|
|
|
attributes: AttributeInput[];
|
|
|
|
updatedFileAttributes: AttributeValueInput[];
|
|
|
|
}
|
|
|
|
|
|
|
|
export const prepareAttributesInput = ({
|
|
|
|
attributes,
|
|
|
|
updatedFileAttributes
|
|
|
|
}: AttributesArgs): AttributeValueInput[] =>
|
|
|
|
attributes.map(attribute => {
|
|
|
|
if (attribute.data.inputType === AttributeInputTypeEnum.FILE) {
|
|
|
|
const updatedFileAttribute = updatedFileAttributes.find(
|
|
|
|
attributeWithNewFile => attribute.id === attributeWithNewFile.id
|
|
|
|
);
|
|
|
|
if (updatedFileAttribute) {
|
|
|
|
return {
|
|
|
|
file: updatedFileAttribute.file,
|
|
|
|
id: updatedFileAttribute.id
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
file:
|
|
|
|
attribute.data.selectedValues &&
|
|
|
|
attribute.data.selectedValues[0]?.file?.url,
|
|
|
|
id: attribute.id
|
|
|
|
};
|
|
|
|
}
|
2021-01-13 12:11:01 +00:00
|
|
|
if (attribute.data.inputType === AttributeInputTypeEnum.REFERENCE) {
|
|
|
|
return {
|
|
|
|
id: attribute.id,
|
|
|
|
references: attribute.value
|
|
|
|
};
|
|
|
|
}
|
2020-12-16 10:53:28 +00:00
|
|
|
return {
|
|
|
|
id: attribute.id,
|
|
|
|
values: attribute.value[0] === "" ? [] : attribute.value
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
export const handleUploadMultipleFiles = async (
|
|
|
|
attributesWithNewFileValue: FormsetData<null, File>,
|
|
|
|
uploadFile: (
|
|
|
|
variables: FileUploadVariables
|
|
|
|
) => Promise<MutationFetchResult<FileUpload>>
|
|
|
|
) =>
|
|
|
|
Promise.all(
|
|
|
|
getFileValuesToUploadFromAttributes(attributesWithNewFileValue).map(
|
|
|
|
fileAttribute =>
|
|
|
|
uploadFile({
|
|
|
|
file: fileAttribute.value
|
|
|
|
})
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
export const handleDeleteMultipleAttributeValues = async (
|
|
|
|
attributesWithNewFileValue: FormsetData<null, File>,
|
|
|
|
attributes: PageDetails_page_attributes[],
|
|
|
|
deleteAttributeValue: (
|
|
|
|
variables: AttributeValueDeleteVariables
|
|
|
|
) => Promise<MutationFetchResult<AttributeValueDelete>>
|
|
|
|
) =>
|
|
|
|
Promise.all(
|
|
|
|
attributes.map(existingAttribute => {
|
|
|
|
const fileValueUnused = isFileValueUnused(
|
|
|
|
attributesWithNewFileValue,
|
|
|
|
existingAttribute
|
|
|
|
);
|
|
|
|
|
|
|
|
if (fileValueUnused) {
|
|
|
|
return deleteAttributeValue({
|
|
|
|
id: existingAttribute.values[0].id
|
|
|
|
});
|
|
|
|
}
|
|
|
|
})
|
|
|
|
);
|