Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Add attribute creating and updating

dominik-zeglen2535a51740dd
diff --git a/saleor/static/dashboard-next/components/ListField/ListField.tsx b/saleor/static/dashboard-next/components/ListField/ListField.tsx
index 0153155a65..172710996d 100644
--- a/saleor/static/dashboard-next/components/ListField/ListField.tsx
+++ b/saleor/static/dashboard-next/components/ListField/ListField.tsx
@@ -8,10 +8,14 @@ import i18n from "../../i18n";
interface ListFieldProps
extends Exclude<Exclude<TextFieldProps, "value">, "classes"> {
- values: string[];
+ values: Array<{
+ label: string;
+ value: string;
+ }>;
}
interface ListFieldState {
+ newValueCounter: number;
value: string;
}
@@ -41,6 +45,7 @@ const ListField = decorate<
ListFieldState
> {
state: ListFieldState = {
+ newValueCounter: 0,
value: ""
};
handleFieldChange = (event: React.ChangeEvent<any>) =>
@@ -64,10 +69,19 @@ const ListField = decorate<
this.props.onChange({
target: {
name: this.props.name,
- value: [...this.props.values, this.state.value]
+ value: [
+ ...this.props.values,
+ {
+ label: this.state.value,
+ value: "generated-" + this.state.newValueCounter
+ }
+ ]
}
} as any);
- this.setState({ value: "" });
+ this.setState({
+ newValueCounter: this.state.newValueCounter + 1,
+ value: ""
+ });
}
};
@@ -106,7 +120,7 @@ const ListField = decorate<
{values.map((value, valueIndex) => (
<div className={classes.chip} key={valueIndex}>
<Typography variant="caption">
- {value}{" "}
+ {value.label}{" "}
<CloseIcon
className={classes.closeIcon}
onClick={() => this.handleValueRemove(valueIndex)}
diff --git a/saleor/static/dashboard-next/productTypes/components/ProductTypeAttributeEditDialog/ProductTypeAttributeEditDialog.tsx b/saleor/static/dashboard-next/productTypes/components/ProductTypeAttributeEditDialog/ProductTypeAttributeEditDialog.tsx
index ead9ea1686..ddf086cdf2 100644
--- a/saleor/static/dashboard-next/productTypes/components/ProductTypeAttributeEditDialog/ProductTypeAttributeEditDialog.tsx
+++ b/saleor/static/dashboard-next/productTypes/components/ProductTypeAttributeEditDialog/ProductTypeAttributeEditDialog.tsx
@@ -14,14 +14,20 @@ import i18n from "../../../i18n";
export interface FormData {
name: string;
- values: string[];
+ values: Array<{
+ label: string;
+ value: string;
+ }>;
}
interface ProductTypeAttributeEditDialogProps {
name: string;
opened: boolean;
title: string;
- values: string[];
+ values: Array<{
+ label: string;
+ value: string;
+ }>;
onClose: () => void;
onConfirm: (data: FormData) => void;
}
diff --git a/saleor/static/dashboard-next/productTypes/components/ProductTypeAttributes/ProductTypeAttributes.tsx b/saleor/static/dashboard-next/productTypes/components/ProductTypeAttributes/ProductTypeAttributes.tsx
index cea716a5fb..b69cdd9c7f 100644
--- a/saleor/static/dashboard-next/productTypes/components/ProductTypeAttributes/ProductTypeAttributes.tsx
+++ b/saleor/static/dashboard-next/productTypes/components/ProductTypeAttributes/ProductTypeAttributes.tsx
@@ -13,6 +13,7 @@ import Skeleton from "../../../components/Skeleton";
import Toggle from "../../../components/Toggle";
import i18n from "../../../i18n";
import { maybe, renderCollection } from "../../../misc";
+import { AttributeTypeEnum } from "../../../types/globalTypes";
import {
ProductTypeDetails_productType_productAttributes,
ProductTypeDetails_productType_variantAttributes
@@ -25,8 +26,11 @@ interface ProductTypeAttributesProps {
attributes:
| ProductTypeDetails_productType_productAttributes[]
| ProductTypeDetails_productType_variantAttributes[];
- title: string;
- onAttributeAdd: (data: ProductTypeAttributeEditDialogFormData) => void;
+ type: AttributeTypeEnum;
+ onAttributeAdd: (
+ data: ProductTypeAttributeEditDialogFormData,
+ type: AttributeTypeEnum
+ ) => void;
onAttributeUpdate: (
id: string,
data: ProductTypeAttributeEditDialogFormData
@@ -42,13 +46,17 @@ const decorate = withStyles({
}
});
const ProductTypeAttributes = decorate<ProductTypeAttributesProps>(
- ({ attributes, classes, title, onAttributeAdd, onAttributeUpdate }) => (
+ ({ attributes, classes, type, onAttributeAdd, onAttributeUpdate }) => (
<Toggle>
{(openedAttributeAddDialog, { toggle: toggleAttributeAddDialog }) => (
<>
<Card>
<CardTitle
- title={title}
+ title={
+ type === AttributeTypeEnum.PRODUCT
+ ? i18n.t("Product Attributes")
+ : i18n.t("Variant Attributes")
+ }
toolbar={
<Button
color="secondary"
@@ -106,7 +114,10 @@ const ProductTypeAttributes = decorate<ProductTypeAttributesProps>(
opened={openedAttributeEditDialog}
title={i18n.t("Edit attribute")}
values={maybe(() =>
- attribute.values.map(value => value.name)
+ attribute.values.map(value => ({
+ label: value.name,
+ value: value.id
+ }))
)}
onClose={toggleAttributeEditDialog}
onConfirm={data =>
@@ -134,7 +145,7 @@ const ProductTypeAttributes = decorate<ProductTypeAttributesProps>(
name=""
values={[]}
onClose={toggleAttributeAddDialog}
- onConfirm={onAttributeAdd}
+ onConfirm={data => onAttributeAdd(data, type)}
/>
</>
)}
diff --git a/saleor/static/dashboard-next/productTypes/components/ProductTypeDetailsPage/ProductTypeDetailsPage.tsx b/saleor/static/dashboard-next/productTypes/components/ProductTypeDetailsPage/ProductTypeDetailsPage.tsx
index da008f0cb8..31da2c67e7 100644
--- a/saleor/static/dashboard-next/productTypes/components/ProductTypeDetailsPage/ProductTypeDetailsPage.tsx
+++ b/saleor/static/dashboard-next/productTypes/components/ProductTypeDetailsPage/ProductTypeDetailsPage.tsx
@@ -13,7 +13,11 @@ import SaveButtonBar, {
import Toggle from "../../../components/Toggle";
import i18n from "../../../i18n";
import { maybe } from "../../../misc";
-import { TaxRateType, WeightUnitsEnum } from "../../../types/globalTypes";
+import {
+ AttributeTypeEnum,
+ TaxRateType,
+ WeightUnitsEnum
+} from "../../../types/globalTypes";
import { ProductTypeDetails_productType } from "../../types/ProductTypeDetails";
import { FormData as ProductTypeAttributeEditDialogFormData } from "../ProductTypeAttributeEditDialog/ProductTypeAttributeEditDialog";
import ProductTypeAttributes from "../ProductTypeAttributes/ProductTypeAttributes";
@@ -44,13 +48,16 @@ interface ProductTypeDetailsPageProps {
disabled: boolean;
pageTitle: string;
saveButtonBarState: SaveButtonBarState;
- onAttributeAdd: (data: ProductTypeAttributeEditDialogFormData) => void;
+ onAttributeAdd: (
+ data: ProductTypeAttributeEditDialogFormData,
+ type: AttributeTypeEnum
+ ) => void;
onAttributeUpdate: (
id: string,
data: ProductTypeAttributeEditDialogFormData
) => void;
onBack: () => void;
- onDelete?: () => void;
+ onDelete: () => void;
onSubmit: (data: ProductTypeForm) => void;
}
@@ -134,7 +141,7 @@ const ProductTypeDetailsPage = decorate<ProductTypeDetailsPageProps>(
attributes={maybe(
() => productType.productAttributes
)}
- title={i18n.t("Product Attributes")}
+ type={AttributeTypeEnum.PRODUCT}
onAttributeAdd={onAttributeAdd}
onAttributeUpdate={onAttributeUpdate}
/>
@@ -154,7 +161,7 @@ const ProductTypeDetailsPage = decorate<ProductTypeDetailsPageProps>(
attributes={maybe(
() => productType.variantAttributes
)}
- title={i18n.t("Variant Attributes")}
+ type={AttributeTypeEnum.VARIANT}
onAttributeAdd={onAttributeAdd}
onAttributeUpdate={onAttributeUpdate}
/>
diff --git a/saleor/static/dashboard-next/productTypes/mutations.ts b/saleor/static/dashboard-next/productTypes/mutations.ts
index 6f01fbcc5e..f2bf8b3947 100644
--- a/saleor/static/dashboard-next/productTypes/mutations.ts
+++ b/saleor/static/dashboard-next/productTypes/mutations.ts
@@ -1,7 +1,7 @@
import gql from "graphql-tag";
import { TypedMutation } from "../mutations";
-import { productTypeDetailsFragment } from "./queries";
+import { productTypeDetailsFragment, attributeFragment } from "./queries";
import {
AttributeCreate,
AttributeCreateVariables
@@ -103,12 +103,16 @@ export const TypedAttributeCreateMutation = TypedMutation<
>(attributeCreateMutation);
export const attributeUpdateMutation = gql`
+ ${attributeFragment}
mutation AttributeUpdate($id: ID!, $input: AttributeUpdateInput!) {
attributeUpdate(id: $id, input: $input) {
errors {
field
message
}
+ attribute {
+ ...AttributeFragment
+ }
}
}
`;
diff --git a/saleor/static/dashboard-next/productTypes/types/AttributeUpdate.ts b/saleor/static/dashboard-next/productTypes/types/AttributeUpdate.ts
index 3df7c1b38d..ca7329f908 100644
--- a/saleor/static/dashboard-next/productTypes/types/AttributeUpdate.ts
+++ b/saleor/static/dashboard-next/productTypes/types/AttributeUpdate.ts
@@ -13,9 +13,25 @@ export interface AttributeUpdate_attributeUpdate_errors {
message: string | null;
}
+export interface AttributeUpdate_attributeUpdate_attribute_values {
+ __typename: "AttributeValue";
+ id: string;
+ name: string | null;
+ slug: string | null;
+}
+
+export interface AttributeUpdate_attributeUpdate_attribute {
+ __typename: "Attribute";
+ id: string;
+ name: string | null;
+ slug: string | null;
+ values: (AttributeUpdate_attributeUpdate_attribute_values | null)[] | null;
+}
+
export interface AttributeUpdate_attributeUpdate {
__typename: "AttributeUpdate";
errors: (AttributeUpdate_attributeUpdate_errors | null)[] | null;
+ attribute: AttributeUpdate_attributeUpdate_attribute | null;
}
export interface AttributeUpdate {
diff --git a/saleor/static/dashboard-next/productTypes/views/ProductTypeUpdate.tsx b/saleor/static/dashboard-next/productTypes/views/ProductTypeUpdate.tsx
index f295731280..c938a71ac0 100644
--- a/saleor/static/dashboard-next/productTypes/views/ProductTypeUpdate.tsx
+++ b/saleor/static/dashboard-next/productTypes/views/ProductTypeUpdate.tsx
@@ -5,6 +5,8 @@ import { productTypeListUrl } from "..";
import Messages from "../../components/messages";
import i18n from "../../i18n";
import { maybe } from "../../misc";
+import { AttributeTypeEnum } from "../../types/globalTypes";
+import { FormData as AttributeForm } from "../components/ProductTypeAttributeEditDialog";
import ProductTypeDetailsPage, {
ProductTypeForm
} from "../components/ProductTypeDetailsPage";
@@ -90,9 +92,12 @@ export const ProductTypeUpdate: React.StatelessComponent<
onProductTypeUpdate={handleProductTypeUpdateSuccess}
>
{({
+ attributeCreate,
+ // deleteAttribute,
deleteProductType,
errors,
loading: mutationLoading,
+ updateAttribute,
updateProductType
}) => {
const handleProductTypeDelete = () =>
@@ -117,6 +122,56 @@ export const ProductTypeUpdate: React.StatelessComponent<
}
});
};
+ const handleAttributeCreate = (
+ data: AttributeForm,
+ type: AttributeTypeEnum
+ ) =>
+ attributeCreate.mutate({
+ id,
+ input: {
+ name: data.name,
+ values: data.values.map(value => ({
+ name: value.label,
+ value: value.label
+ }))
+ },
+ type
+ });
+ // const handleAttributeDelete = (id: string) =>
+ // deleteAttribute.mutate({ id });
+ const handleAttributeUpdate = (
+ id: string,
+ formData: AttributeForm
+ ) => {
+ const attribute = data.productType.variantAttributes
+ .concat(data.productType.productAttributes)
+ .filter(attribute => attribute.id === id)[0];
+ updateAttribute.mutate({
+ id,
+ input: {
+ addValues: formData.values
+ .filter(
+ value =>
+ !attribute.values
+ .map(value => value.id)
+ .includes(value.value)
+ )
+ .map(value => ({
+ name: value.label,
+ value: value.value
+ })),
+ name: formData.name,
+ removeValues: attribute.values
+ .filter(
+ value =>
+ !formData.values
+ .map(value => value.value)
+ .includes(value.id)
+ )
+ .map(value => value.id)
+ }
+ });
+ };
const loading = mutationLoading || dataLoading;
return (
<ProductTypeDetailsPage
@@ -125,15 +180,11 @@ export const ProductTypeUpdate: React.StatelessComponent<
)}
disabled={loading}
errors={errors}
- pageTitle={
- data && data.productType
- ? data.productType.name
- : undefined
- }
+ pageTitle={maybe(() => data.productType.name)}
productType={maybe(() => data.productType)}
saveButtonBarState={loading ? "loading" : "default"}
- onAttributeAdd={() => undefined}
- onAttributeUpdate={() => undefined}
+ onAttributeAdd={handleAttributeCreate}
+ onAttributeUpdate={handleAttributeUpdate}
onBack={() => navigate(productTypeListUrl)}
onDelete={handleProductTypeDelete}
onSubmit={handleProductTypeUpdate}