2021-06-22 08:00:00 +00:00
|
|
|
import {
|
2021-11-29 12:32:51 +00:00
|
|
|
getDefaultAttributeValues,
|
2021-06-22 08:00:00 +00:00
|
|
|
getSelectedAttributeValues,
|
2022-06-21 09:36:55 +00:00
|
|
|
mergeChoicesWithValues,
|
2021-06-22 08:00:00 +00:00
|
|
|
} from "@saleor/attributes/utils/data";
|
2020-12-16 10:53:28 +00:00
|
|
|
import {
|
|
|
|
AttributeInput,
|
2022-06-21 09:36:55 +00:00
|
|
|
VariantAttributeScope,
|
2020-12-16 10:53:28 +00:00
|
|
|
} from "@saleor/components/Attributes";
|
2019-08-09 10:17:04 +00:00
|
|
|
import { SingleAutocompleteChoiceType } from "@saleor/components/SingleAutocompleteSelectField";
|
2022-03-09 08:56:55 +00:00
|
|
|
import {
|
|
|
|
ProductDetailsVariantFragment,
|
|
|
|
ProductFragment,
|
|
|
|
ProductTypeQuery,
|
|
|
|
ProductVariantCreateDataQuery,
|
|
|
|
ProductVariantFragment,
|
|
|
|
SelectedVariantAttributeFragment,
|
|
|
|
StockInput,
|
2022-06-21 09:36:55 +00:00
|
|
|
VariantAttributeFragment,
|
2022-03-09 08:56:55 +00:00
|
|
|
} from "@saleor/graphql";
|
2021-01-12 11:13:02 +00:00
|
|
|
import { FormsetAtomicData } from "@saleor/hooks/useFormset";
|
2019-08-09 10:17:04 +00:00
|
|
|
import { maybe } from "@saleor/misc";
|
2021-05-28 09:59:25 +00:00
|
|
|
import { mapEdgesToItems, mapMetadataItemToInput } from "@saleor/utils/maps";
|
2021-10-01 12:41:31 +00:00
|
|
|
import moment from "moment";
|
2020-05-14 09:30:32 +00:00
|
|
|
|
|
|
|
import { ProductStockInput } from "../components/ProductStocks";
|
2022-10-18 09:10:15 +00:00
|
|
|
import { ProductUpdateFormData } from "../components/ProductUpdatePage/types";
|
2019-08-09 10:17:04 +00:00
|
|
|
|
|
|
|
export interface Collection {
|
|
|
|
id: string;
|
|
|
|
label: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface Node {
|
|
|
|
id: string;
|
|
|
|
name: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface ProductType {
|
|
|
|
hasVariants: boolean;
|
|
|
|
id: string;
|
|
|
|
name: string;
|
2022-03-09 08:56:55 +00:00
|
|
|
productAttributes: ProductTypeQuery["productType"]["productAttributes"];
|
2019-08-09 10:17:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function getAttributeInputFromProduct(
|
2022-06-21 09:36:55 +00:00
|
|
|
product: ProductFragment,
|
2020-12-16 10:53:28 +00:00
|
|
|
): AttributeInput[] {
|
2021-07-29 12:15:14 +00:00
|
|
|
return (
|
|
|
|
product?.attributes?.map(attribute => ({
|
|
|
|
data: {
|
|
|
|
entityType: attribute.attribute.entityType,
|
|
|
|
inputType: attribute.attribute.inputType,
|
|
|
|
isRequired: attribute.attribute.valueRequired,
|
|
|
|
selectedValues: attribute.values,
|
|
|
|
values: mergeChoicesWithValues(attribute),
|
2022-06-21 09:36:55 +00:00
|
|
|
unit: attribute.attribute.unit,
|
2021-07-29 12:15:14 +00:00
|
|
|
},
|
|
|
|
id: attribute.attribute.id,
|
|
|
|
label: attribute.attribute.name,
|
2022-06-21 09:36:55 +00:00
|
|
|
value: getSelectedAttributeValues(attribute),
|
2021-07-29 12:15:14 +00:00
|
|
|
})) ?? []
|
2019-08-09 10:17:04 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getAttributeInputFromProductType(
|
2022-06-21 09:36:55 +00:00
|
|
|
productType: ProductType,
|
2020-12-16 10:53:28 +00:00
|
|
|
): AttributeInput[] {
|
2019-08-09 10:17:04 +00:00
|
|
|
return productType.productAttributes.map(attribute => ({
|
|
|
|
data: {
|
2021-01-20 16:37:36 +00:00
|
|
|
entityType: attribute.entityType,
|
2019-08-09 10:17:04 +00:00
|
|
|
inputType: attribute.inputType,
|
|
|
|
isRequired: attribute.valueRequired,
|
2021-07-07 14:09:35 +00:00
|
|
|
values: mapEdgesToItems(attribute.choices) || [],
|
2022-06-21 09:36:55 +00:00
|
|
|
unit: attribute.unit,
|
2019-08-09 10:17:04 +00:00
|
|
|
},
|
|
|
|
id: attribute.id,
|
|
|
|
label: attribute.name,
|
2022-06-21 09:36:55 +00:00
|
|
|
value: [],
|
2019-08-09 10:17:04 +00:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2020-12-16 10:53:28 +00:00
|
|
|
export function getAttributeInputFromAttributes(
|
|
|
|
variantAttributes: VariantAttributeFragment[],
|
2022-06-21 09:36:55 +00:00
|
|
|
variantAttributeScope: VariantAttributeScope,
|
2020-12-16 10:53:28 +00:00
|
|
|
): AttributeInput[] {
|
|
|
|
return variantAttributes?.map(attribute => ({
|
|
|
|
data: {
|
2021-01-20 16:37:36 +00:00
|
|
|
entityType: attribute.entityType,
|
2020-12-16 10:53:28 +00:00
|
|
|
inputType: attribute.inputType,
|
|
|
|
isRequired: attribute.valueRequired,
|
2021-07-07 14:09:35 +00:00
|
|
|
values: mapEdgesToItems(attribute.choices) || [],
|
2021-04-29 08:58:03 +00:00
|
|
|
unit: attribute.unit,
|
2022-06-21 09:36:55 +00:00
|
|
|
variantAttributeScope,
|
2020-12-16 10:53:28 +00:00
|
|
|
},
|
|
|
|
id: attribute.id,
|
|
|
|
label: attribute.name,
|
2022-06-21 09:36:55 +00:00
|
|
|
value: getDefaultAttributeValues(attribute),
|
2020-12-16 10:53:28 +00:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getAttributeInputFromSelectedAttributes(
|
|
|
|
variantAttributes: SelectedVariantAttributeFragment[],
|
2022-06-21 09:36:55 +00:00
|
|
|
variantAttributeScope: VariantAttributeScope,
|
2020-12-16 10:53:28 +00:00
|
|
|
): AttributeInput[] {
|
2021-01-13 12:11:01 +00:00
|
|
|
return variantAttributes?.map(attribute => ({
|
|
|
|
data: {
|
2021-01-20 16:37:36 +00:00
|
|
|
entityType: attribute.attribute.entityType,
|
2021-01-13 12:11:01 +00:00
|
|
|
inputType: attribute.attribute.inputType,
|
|
|
|
isRequired: attribute.attribute.valueRequired,
|
|
|
|
selectedValues: attribute.values,
|
2021-06-22 08:00:00 +00:00
|
|
|
values: mergeChoicesWithValues(attribute),
|
2021-04-29 08:58:03 +00:00
|
|
|
unit: attribute.attribute.unit,
|
2022-06-21 09:36:55 +00:00
|
|
|
variantAttributeScope,
|
2021-01-13 12:11:01 +00:00
|
|
|
},
|
|
|
|
id: attribute.attribute.id,
|
|
|
|
label: attribute.attribute.name,
|
2022-06-21 09:36:55 +00:00
|
|
|
value: getSelectedAttributeValues(attribute),
|
2021-01-13 12:11:01 +00:00
|
|
|
}));
|
2020-12-16 10:53:28 +00:00
|
|
|
}
|
|
|
|
|
2019-08-09 10:17:04 +00:00
|
|
|
export function getAttributeInputFromVariant(
|
2022-06-21 09:36:55 +00:00
|
|
|
variant: ProductVariantFragment,
|
2020-12-16 10:53:28 +00:00
|
|
|
): AttributeInput[] {
|
|
|
|
const selectionAttributeInput = getAttributeInputFromSelectedAttributes(
|
|
|
|
variant?.selectionAttributes,
|
2022-06-21 09:36:55 +00:00
|
|
|
VariantAttributeScope.VARIANT_SELECTION,
|
2020-12-16 10:53:28 +00:00
|
|
|
);
|
|
|
|
const nonSelectionAttributeInput = getAttributeInputFromSelectedAttributes(
|
|
|
|
variant?.nonSelectionAttributes,
|
2022-06-21 09:36:55 +00:00
|
|
|
VariantAttributeScope.NOT_VARIANT_SELECTION,
|
2020-12-16 10:53:28 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
|
|
|
selectionAttributeInput?.concat(nonSelectionAttributeInput ?? []) ?? []
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getVariantAttributeInputFromProduct(
|
2022-06-21 09:36:55 +00:00
|
|
|
product: ProductVariantCreateDataQuery["product"],
|
2020-12-16 10:53:28 +00:00
|
|
|
): AttributeInput[] {
|
|
|
|
const selectionAttributeInput = getAttributeInputFromAttributes(
|
|
|
|
product?.productType?.selectionVariantAttributes,
|
2022-06-21 09:36:55 +00:00
|
|
|
VariantAttributeScope.VARIANT_SELECTION,
|
2020-12-16 10:53:28 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
const nonSelectionAttributeInput = getAttributeInputFromAttributes(
|
|
|
|
product?.productType?.nonSelectionVariantAttributes,
|
2022-06-21 09:36:55 +00:00
|
|
|
VariantAttributeScope.NOT_VARIANT_SELECTION,
|
2020-12-16 10:53:28 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
|
|
|
selectionAttributeInput?.concat(nonSelectionAttributeInput ?? []) ?? []
|
2019-08-09 10:17:04 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-03-27 10:40:34 +00:00
|
|
|
export function getStockInputFromVariant(
|
2022-06-21 09:36:55 +00:00
|
|
|
variant: ProductVariantFragment,
|
2020-03-27 10:40:34 +00:00
|
|
|
): ProductStockInput[] {
|
|
|
|
return (
|
|
|
|
variant?.stocks.map(stock => ({
|
2020-12-15 09:50:29 +00:00
|
|
|
data: {
|
2022-06-21 09:36:55 +00:00
|
|
|
quantityAllocated: stock.quantityAllocated,
|
2020-12-15 09:50:29 +00:00
|
|
|
},
|
2020-03-27 10:40:34 +00:00
|
|
|
id: stock.warehouse.id,
|
|
|
|
label: stock.warehouse.name,
|
2022-06-21 09:36:55 +00:00
|
|
|
value: stock.quantity.toString(),
|
2020-03-27 10:40:34 +00:00
|
|
|
})) || []
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-08-09 10:17:04 +00:00
|
|
|
export function getCollectionInput(
|
2022-06-21 09:36:55 +00:00
|
|
|
productCollections: ProductFragment["collections"],
|
2019-08-09 10:17:04 +00:00
|
|
|
): Collection[] {
|
|
|
|
return maybe(
|
|
|
|
() =>
|
|
|
|
productCollections.map(collection => ({
|
|
|
|
id: collection.id,
|
2022-06-21 09:36:55 +00:00
|
|
|
label: collection.name,
|
2019-08-09 10:17:04 +00:00
|
|
|
})),
|
2022-06-21 09:36:55 +00:00
|
|
|
[],
|
2019-08-09 10:17:04 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getChoices(nodes: Node[]): SingleAutocompleteChoiceType[] {
|
|
|
|
return maybe(
|
|
|
|
() =>
|
|
|
|
nodes.map(node => ({
|
|
|
|
label: node.name,
|
2022-06-21 09:36:55 +00:00
|
|
|
value: node.id,
|
2019-08-09 10:17:04 +00:00
|
|
|
})),
|
2022-06-21 09:36:55 +00:00
|
|
|
[],
|
2019-08-09 10:17:04 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getProductUpdatePageFormData(
|
2022-03-09 08:56:55 +00:00
|
|
|
product: ProductFragment,
|
|
|
|
variants: ProductDetailsVariantFragment[],
|
2022-10-18 09:10:15 +00:00
|
|
|
): ProductUpdateFormData {
|
2021-10-01 12:41:31 +00:00
|
|
|
const variant = product?.variants[0];
|
2022-10-18 09:10:15 +00:00
|
|
|
|
2019-08-09 10:17:04 +00:00
|
|
|
return {
|
|
|
|
category: maybe(() => product.category.id, ""),
|
2020-09-24 10:55:02 +00:00
|
|
|
changeTaxCode: !!product?.taxType.taxCode,
|
2019-08-09 10:17:04 +00:00
|
|
|
chargeTaxes: maybe(() => product.chargeTaxes, false),
|
|
|
|
collections: maybe(
|
|
|
|
() => product.collections.map(collection => collection.id),
|
2022-06-21 09:36:55 +00:00
|
|
|
[],
|
2019-08-09 10:17:04 +00:00
|
|
|
),
|
2020-09-03 10:25:16 +00:00
|
|
|
isAvailable: !!product?.isAvailable,
|
2020-08-28 12:45:11 +00:00
|
|
|
metadata: product?.metadata?.map(mapMetadataItemToInput),
|
2019-08-09 10:17:04 +00:00
|
|
|
name: maybe(() => product.name, ""),
|
2020-08-28 12:45:11 +00:00
|
|
|
privateMetadata: product?.privateMetadata?.map(mapMetadataItemToInput),
|
2020-11-19 17:07:42 +00:00
|
|
|
rating: maybe(() => product.rating, null),
|
2019-08-09 10:17:04 +00:00
|
|
|
seoDescription: maybe(() => product.seoDescription, ""),
|
|
|
|
seoTitle: maybe(() => product.seoTitle, ""),
|
2019-11-14 14:40:02 +00:00
|
|
|
sku: maybe(
|
|
|
|
() =>
|
|
|
|
product.productType.hasVariants
|
|
|
|
? undefined
|
|
|
|
: variants && variants[0]
|
|
|
|
? variants[0].sku
|
|
|
|
: undefined,
|
2022-06-21 09:36:55 +00:00
|
|
|
"",
|
2019-08-09 10:17:04 +00:00
|
|
|
),
|
2020-09-18 14:40:48 +00:00
|
|
|
slug: product?.slug || "",
|
2020-09-23 08:29:13 +00:00
|
|
|
taxCode: product?.taxType.taxCode,
|
2021-10-01 12:41:31 +00:00
|
|
|
trackInventory: !!variant?.trackInventory,
|
|
|
|
weight: product?.weight?.value.toString() || "",
|
|
|
|
isPreorder: !!variant?.preorder || false,
|
2021-10-14 11:15:59 +00:00
|
|
|
globalThreshold: variant?.preorder?.globalThreshold?.toString() || "",
|
2021-10-01 12:41:31 +00:00
|
|
|
globalSoldUnits: variant?.preorder?.globalSoldUnits || 0,
|
|
|
|
hasPreorderEndDate: !!variant?.preorder?.endDate,
|
2022-06-21 09:36:55 +00:00
|
|
|
preorderEndDateTime: variant?.preorder?.endDate,
|
2019-08-09 10:17:04 +00:00
|
|
|
};
|
|
|
|
}
|
2020-05-04 15:29:06 +00:00
|
|
|
|
|
|
|
export function mapFormsetStockToStockInput(
|
2022-06-21 09:36:55 +00:00
|
|
|
stock: FormsetAtomicData<null, string>,
|
2020-05-04 15:29:06 +00:00
|
|
|
): StockInput {
|
|
|
|
return {
|
2020-10-13 11:29:42 +00:00
|
|
|
quantity: parseInt(stock.value, 10) || 0,
|
2022-06-21 09:36:55 +00:00
|
|
|
warehouse: stock.id,
|
2020-05-04 15:29:06 +00:00
|
|
|
};
|
|
|
|
}
|
2021-10-01 12:41:31 +00:00
|
|
|
|
|
|
|
export const getPreorderEndDateFormData = (endDate?: string) =>
|
|
|
|
endDate ? moment(endDate).format("YYYY-MM-DD") : "";
|
|
|
|
|
|
|
|
export const getPreorderEndHourFormData = (endDate?: string) =>
|
|
|
|
endDate ? moment(endDate).format("HH:mm") : "";
|