2020-08-28 12:45:11 +00:00
|
|
|
import { MetadataFormData } from "@saleor/components/Metadata/types";
|
2019-08-09 10:17:04 +00:00
|
|
|
import { MultiAutocompleteChoiceType } from "@saleor/components/MultiAutocompleteSelectField";
|
|
|
|
import { SingleAutocompleteChoiceType } from "@saleor/components/SingleAutocompleteSelectField";
|
2020-07-07 10:14:12 +00:00
|
|
|
import { ProductVariant } from "@saleor/fragments/types/ProductVariant";
|
2020-05-14 09:30:32 +00:00
|
|
|
import { FormsetAtomicData } from "@saleor/hooks/useFormset";
|
2019-08-09 10:17:04 +00:00
|
|
|
import { maybe } from "@saleor/misc";
|
|
|
|
import {
|
|
|
|
ProductDetails_product,
|
|
|
|
ProductDetails_product_collections,
|
|
|
|
ProductDetails_product_variants
|
|
|
|
} from "@saleor/products/types/ProductDetails";
|
2019-11-19 17:00:14 +00:00
|
|
|
import { SearchProductTypes_search_edges_node_productAttributes } from "@saleor/searches/types/SearchProductTypes";
|
2020-05-04 15:29:06 +00:00
|
|
|
import { StockInput } from "@saleor/types/globalTypes";
|
2020-08-28 12:45:11 +00:00
|
|
|
import { mapMetadataItemToInput } from "@saleor/utils/maps";
|
2020-05-14 09:30:32 +00:00
|
|
|
import { RawDraftContentState } from "draft-js";
|
|
|
|
|
2019-08-09 10:17:04 +00:00
|
|
|
import { ProductAttributeInput } from "../components/ProductAttributes";
|
2020-05-14 09:30:32 +00:00
|
|
|
import { ProductStockInput } from "../components/ProductStocks";
|
2019-08-09 10:17:04 +00:00
|
|
|
import { VariantAttributeInput } from "../components/ProductVariantAttributes";
|
2019-10-17 15:29:13 +00:00
|
|
|
import { ProductVariantCreateData_product } from "../types/ProductVariantCreateData";
|
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;
|
2019-10-15 12:17:35 +00:00
|
|
|
productAttributes: SearchProductTypes_search_edges_node_productAttributes[];
|
2019-08-09 10:17:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function getAttributeInputFromProduct(
|
|
|
|
product: ProductDetails_product
|
|
|
|
): ProductAttributeInput[] {
|
|
|
|
return maybe(
|
|
|
|
(): ProductAttributeInput[] =>
|
|
|
|
product.attributes.map(attribute => ({
|
|
|
|
data: {
|
|
|
|
inputType: attribute.attribute.inputType,
|
|
|
|
isRequired: attribute.attribute.valueRequired,
|
|
|
|
values: attribute.attribute.values
|
|
|
|
},
|
|
|
|
id: attribute.attribute.id,
|
|
|
|
label: attribute.attribute.name,
|
|
|
|
value: attribute.values.map(value => value.slug)
|
|
|
|
})),
|
|
|
|
[]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface ProductAttributeValueChoices {
|
|
|
|
id: string;
|
|
|
|
values: MultiAutocompleteChoiceType[];
|
|
|
|
}
|
|
|
|
export function getSelectedAttributesFromProduct(
|
|
|
|
product: ProductDetails_product
|
|
|
|
): ProductAttributeValueChoices[] {
|
|
|
|
return maybe(
|
|
|
|
() =>
|
|
|
|
product.attributes.map(attribute => ({
|
|
|
|
id: attribute.attribute.id,
|
|
|
|
values: attribute.values.map(value => ({
|
|
|
|
label: value.name,
|
|
|
|
value: value.slug
|
|
|
|
}))
|
|
|
|
})),
|
|
|
|
[]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getAttributeInputFromProductType(
|
|
|
|
productType: ProductType
|
|
|
|
): ProductAttributeInput[] {
|
|
|
|
return productType.productAttributes.map(attribute => ({
|
|
|
|
data: {
|
|
|
|
inputType: attribute.inputType,
|
|
|
|
isRequired: attribute.valueRequired,
|
|
|
|
values: attribute.values
|
|
|
|
},
|
|
|
|
id: attribute.id,
|
|
|
|
label: attribute.name,
|
|
|
|
value: []
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getAttributeInputFromVariant(
|
2019-10-18 11:19:43 +00:00
|
|
|
variant: ProductVariant
|
2019-08-09 10:17:04 +00:00
|
|
|
): VariantAttributeInput[] {
|
|
|
|
return maybe(
|
|
|
|
(): VariantAttributeInput[] =>
|
2019-10-18 11:19:43 +00:00
|
|
|
variant.attributes.map(attribute => ({
|
2019-08-09 10:17:04 +00:00
|
|
|
data: {
|
|
|
|
values: attribute.attribute.values
|
|
|
|
},
|
|
|
|
id: attribute.attribute.id,
|
|
|
|
label: attribute.attribute.name,
|
2020-01-09 11:13:24 +00:00
|
|
|
value: maybe(() => attribute.values[0].slug, null)
|
2019-08-09 10:17:04 +00:00
|
|
|
})),
|
|
|
|
[]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-03-27 10:40:34 +00:00
|
|
|
export function getStockInputFromVariant(
|
|
|
|
variant: ProductVariant
|
|
|
|
): ProductStockInput[] {
|
|
|
|
return (
|
|
|
|
variant?.stocks.map(stock => ({
|
|
|
|
data: null,
|
|
|
|
id: stock.warehouse.id,
|
|
|
|
label: stock.warehouse.name,
|
|
|
|
value: stock.quantity.toString()
|
|
|
|
})) || []
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-08-09 10:17:04 +00:00
|
|
|
export function getVariantAttributeInputFromProduct(
|
|
|
|
product: ProductVariantCreateData_product
|
|
|
|
): VariantAttributeInput[] {
|
|
|
|
return maybe(() =>
|
|
|
|
product.productType.variantAttributes.map(attribute => ({
|
|
|
|
data: {
|
|
|
|
values: attribute.values
|
|
|
|
},
|
|
|
|
id: attribute.id,
|
|
|
|
label: attribute.name,
|
|
|
|
value: ""
|
|
|
|
}))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-03-19 17:15:22 +00:00
|
|
|
export function getStockInputFromProduct(
|
|
|
|
product: ProductDetails_product
|
|
|
|
): ProductStockInput[] {
|
2020-03-31 17:46:07 +00:00
|
|
|
return product?.variants[0]?.stocks.map(stock => ({
|
2020-03-19 17:15:22 +00:00
|
|
|
data: null,
|
|
|
|
id: stock.warehouse.id,
|
|
|
|
label: stock.warehouse.name,
|
|
|
|
value: stock.quantity.toString()
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2019-08-09 10:17:04 +00:00
|
|
|
export function getCollectionInput(
|
|
|
|
productCollections: ProductDetails_product_collections[]
|
|
|
|
): Collection[] {
|
|
|
|
return maybe(
|
|
|
|
() =>
|
|
|
|
productCollections.map(collection => ({
|
|
|
|
id: collection.id,
|
|
|
|
label: collection.name
|
|
|
|
})),
|
|
|
|
[]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getChoices(nodes: Node[]): SingleAutocompleteChoiceType[] {
|
|
|
|
return maybe(
|
|
|
|
() =>
|
|
|
|
nodes.map(node => ({
|
|
|
|
label: node.name,
|
|
|
|
value: node.id
|
|
|
|
})),
|
|
|
|
[]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-08-28 12:45:11 +00:00
|
|
|
export interface ProductUpdatePageFormData extends MetadataFormData {
|
2019-08-09 10:17:04 +00:00
|
|
|
basePrice: number;
|
|
|
|
category: string | null;
|
|
|
|
collections: string[];
|
|
|
|
chargeTaxes: boolean;
|
|
|
|
description: RawDraftContentState;
|
|
|
|
isPublished: boolean;
|
|
|
|
name: string;
|
|
|
|
publicationDate: string;
|
|
|
|
seoDescription: string;
|
|
|
|
seoTitle: string;
|
|
|
|
sku: string;
|
2020-03-19 17:15:22 +00:00
|
|
|
trackInventory: boolean;
|
2020-07-13 16:51:05 +00:00
|
|
|
weight: string;
|
2019-08-09 10:17:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function getProductUpdatePageFormData(
|
|
|
|
product: ProductDetails_product,
|
|
|
|
variants: ProductDetails_product_variants[]
|
|
|
|
): ProductUpdatePageFormData {
|
|
|
|
return {
|
2020-06-16 12:57:22 +00:00
|
|
|
basePrice: maybe(() => product.variants[0].price.amount, 0),
|
2019-08-09 10:17:04 +00:00
|
|
|
category: maybe(() => product.category.id, ""),
|
|
|
|
chargeTaxes: maybe(() => product.chargeTaxes, false),
|
|
|
|
collections: maybe(
|
|
|
|
() => product.collections.map(collection => collection.id),
|
|
|
|
[]
|
|
|
|
),
|
|
|
|
description: maybe(() => JSON.parse(product.descriptionJson)),
|
|
|
|
isPublished: maybe(() => product.isPublished, false),
|
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),
|
2019-08-09 10:17:04 +00:00
|
|
|
publicationDate: maybe(() => product.publicationDate, ""),
|
|
|
|
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,
|
|
|
|
""
|
2019-08-09 10:17:04 +00:00
|
|
|
),
|
2020-07-13 16:51:05 +00:00
|
|
|
trackInventory: !!product?.variants[0]?.trackInventory,
|
|
|
|
weight: product?.weight?.value.toString() || ""
|
2019-08-09 10:17:04 +00:00
|
|
|
};
|
|
|
|
}
|
2020-05-04 15:29:06 +00:00
|
|
|
|
|
|
|
export function mapFormsetStockToStockInput(
|
|
|
|
stock: FormsetAtomicData<null, string>
|
|
|
|
): StockInput {
|
|
|
|
return {
|
|
|
|
quantity: parseInt(stock.value, 10),
|
|
|
|
warehouse: stock.id
|
|
|
|
};
|
|
|
|
}
|