2020-12-16 10:53:28 +00:00
|
|
|
import { AttributeInput } from "@saleor/components/Attributes";
|
|
|
|
import { FormsetData } from "@saleor/hooks/useFormset";
|
|
|
|
|
2020-11-19 14:42:14 +00:00
|
|
|
import {
|
|
|
|
PageDetails_page,
|
|
|
|
PageDetails_page_pageType
|
|
|
|
} from "../types/PageDetails";
|
|
|
|
|
|
|
|
export function getAttributeInputFromPage(
|
|
|
|
page: PageDetails_page
|
2020-12-16 10:53:28 +00:00
|
|
|
): AttributeInput[] {
|
2020-11-19 14:42:14 +00:00
|
|
|
return page?.attributes.map(attribute => ({
|
|
|
|
data: {
|
|
|
|
inputType: attribute.attribute.inputType,
|
|
|
|
isRequired: attribute.attribute.valueRequired,
|
2020-12-16 10:53:28 +00:00
|
|
|
selectedValues: attribute.values,
|
2020-11-19 14:42:14 +00:00
|
|
|
values: attribute.attribute.values
|
|
|
|
},
|
|
|
|
id: attribute.attribute.id,
|
|
|
|
label: attribute.attribute.name,
|
|
|
|
value: attribute.values.map(value => value.slug)
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getAttributeInputFromPageType(
|
|
|
|
pageType: PageDetails_page_pageType
|
2020-12-16 10:53:28 +00:00
|
|
|
): AttributeInput[] {
|
2020-11-19 14:42:14 +00:00
|
|
|
return pageType?.attributes.map(attribute => ({
|
|
|
|
data: {
|
|
|
|
inputType: attribute.inputType,
|
|
|
|
isRequired: attribute.valueRequired,
|
|
|
|
values: attribute.values
|
|
|
|
},
|
|
|
|
id: attribute.id,
|
|
|
|
label: attribute.name,
|
|
|
|
value: []
|
|
|
|
}));
|
|
|
|
}
|
2020-12-16 10:53:28 +00:00
|
|
|
|
|
|
|
export const getAttributesDisplayData = (
|
|
|
|
attributes: AttributeInput[],
|
|
|
|
attributesWithNewFileValue: FormsetData<null, File>
|
|
|
|
) =>
|
|
|
|
attributes.map(attribute => {
|
|
|
|
const attributeWithNewFileValue = attributesWithNewFileValue.find(
|
|
|
|
attributeWithNewFile => attribute.id === attributeWithNewFile.id
|
|
|
|
);
|
|
|
|
|
|
|
|
if (attributeWithNewFileValue) {
|
|
|
|
return {
|
|
|
|
...attribute,
|
|
|
|
value: attributeWithNewFileValue?.value?.name
|
|
|
|
? [attributeWithNewFileValue.value.name]
|
|
|
|
: []
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return attribute;
|
|
|
|
});
|