2021-01-13 12:11:01 +00:00
|
|
|
import { getSelectedAttributeValues } from "@saleor/attributes/utils/data";
|
2020-12-16 10:53:28 +00:00
|
|
|
import { AttributeInput } from "@saleor/components/Attributes";
|
2021-01-12 11:13:02 +00:00
|
|
|
import { SearchPages_search_edges_node } from "@saleor/searches/types/SearchPages";
|
2020-12-16 10:53:28 +00:00
|
|
|
|
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,
|
2021-01-13 12:11:01 +00:00
|
|
|
value: getSelectedAttributeValues(attribute)
|
2020-11-19 14:42:14 +00:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2021-01-12 11:13:02 +00:00
|
|
|
export const getAttributeValuesFromReferences = (
|
|
|
|
attributeId: string,
|
2020-12-16 10:53:28 +00:00
|
|
|
attributes: AttributeInput[],
|
2021-01-12 11:13:02 +00:00
|
|
|
referencePages: SearchPages_search_edges_node[]
|
|
|
|
) => {
|
|
|
|
const attribute = attributes?.find(attribute => attribute.id === attributeId);
|
2020-12-16 10:53:28 +00:00
|
|
|
|
2021-01-12 11:13:02 +00:00
|
|
|
return (
|
|
|
|
referencePages?.filter(
|
|
|
|
value =>
|
|
|
|
!attribute?.value?.some(selectedValue => selectedValue === value.id)
|
|
|
|
) || []
|
|
|
|
);
|
|
|
|
};
|