Skip to content

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

Add product type update

dominik-zeglen80b3b87ca819
diff --git a/saleor/static/dashboard-next/gql-types.ts b/saleor/static/dashboard-next/gql-types.ts
index ddfa8db84a..40a73efb3e 100644
--- a/saleor/static/dashboard-next/gql-types.ts
+++ b/saleor/static/dashboard-next/gql-types.ts
@@ -1,6 +1,24 @@
/* tslint:disable */
// This file was automatically generated and should not be edited.
+export interface ProductTypeInput {
+ // Name of the product type.
+ name?: string | null,
+ // Determines if product of this type has multiple
+ // variants. This option mainly simplifies product management
+ // in the dashboard. There is always at least one variant created under
+ // the hood.
+ hasVariants?: boolean | null,
+ // List of attributes shared among all product variants.
+ productAttributes?: Array< string | null > | null,
+ // List of attributes used to distinguish between
+ // different variants of a product.
+ variantAttributes?: Array< string | null > | null,
+ // Determines if shipping is required for products
+ // of this variant.
+ isShippingRequired?: boolean | null,
+};
+
export interface AttributeValueInput {
// Slug of an attribute.
slug: string,
@@ -417,6 +435,58 @@ export interface ProductTypeDeleteMutation {
} | null,
};
+export interface ProductTypeUpdateMutationVariables {
+ id: string,
+ input: ProductTypeInput,
+};
+
+export interface ProductTypeUpdateMutation {
+ productTypeUpdate: {
+ // List of errors that occurred executing the mutation.
+ errors: Array< {
+ // Name of a field that caused the error. A value of
+ // `null` indicates that the error isn't associated with a particular
+ // field.
+ field: string | null,
+ // The error message.
+ message: string | null,
+ } | null > | null,
+ productType: {
+ // The ID of the object.
+ id: string,
+ name: string,
+ hasVariants: boolean,
+ productAttributes: {
+ edges: Array< {
+ // The item at the end of the edge
+ node: {
+ // The ID of the object.
+ id: string,
+ // Internal representation of an attribute name.
+ slug: string | null,
+ // Visible name for display purposes.
+ name: string | null,
+ },
+ } >,
+ } | null,
+ variantAttributes: {
+ edges: Array< {
+ // The item at the end of the edge
+ node: {
+ // The ID of the object.
+ id: string,
+ // Internal representation of an attribute name.
+ slug: string | null,
+ // Visible name for display purposes.
+ name: string | null,
+ },
+ } >,
+ } | null,
+ isShippingRequired: boolean,
+ } | null,
+ } | null,
+};
+
export interface ProductTypeListQueryVariables {
after?: string | null,
before?: string | null,
diff --git a/saleor/static/dashboard-next/productTypes/components/ProductTypeDetailsPage/ProductTypeDetailsPage.tsx b/saleor/static/dashboard-next/productTypes/components/ProductTypeDetailsPage/ProductTypeDetailsPage.tsx
index 39422a0cec..1a108a1e27 100644
--- a/saleor/static/dashboard-next/productTypes/components/ProductTypeDetailsPage/ProductTypeDetailsPage.tsx
+++ b/saleor/static/dashboard-next/productTypes/components/ProductTypeDetailsPage/ProductTypeDetailsPage.tsx
@@ -18,7 +18,7 @@ interface ChoiceType {
label: string;
value: string;
}
-interface ProductTypeForm {
+export interface ProductTypeForm {
name: string;
hasVariants: boolean;
isShippingRequired: boolean;
diff --git a/saleor/static/dashboard-next/productTypes/containers/ProductTypeOperations.tsx b/saleor/static/dashboard-next/productTypes/containers/ProductTypeOperations.tsx
index 977b58abbe..a038203403 100644
--- a/saleor/static/dashboard-next/productTypes/containers/ProductTypeOperations.tsx
+++ b/saleor/static/dashboard-next/productTypes/containers/ProductTypeOperations.tsx
@@ -7,9 +7,12 @@ import {
} from "../..";
import {
ProductTypeDeleteMutation,
- ProductTypeDeleteMutationVariables
+ ProductTypeDeleteMutationVariables,
+ ProductTypeUpdateMutation,
+ ProductTypeUpdateMutationVariables
} from "../../gql-types";
import ProductTypeDeleteProvider from "./ProductTypeDelete";
+import ProductTypeUpdateProvider from "./ProductTypeUpdate";
interface ProductTypeOperationsProps extends MutationProviderProps {
id: string;
@@ -18,27 +21,41 @@ interface ProductTypeOperationsProps extends MutationProviderProps {
ProductTypeDeleteMutation,
ProductTypeDeleteMutationVariables
>;
+ updateProductType: PartialMutationProviderOutput<
+ ProductTypeUpdateMutation,
+ ProductTypeUpdateMutationVariables
+ >;
loading: boolean;
}>;
onDelete?: (data: ProductTypeDeleteMutation) => void;
+ onUpdate?: (data: ProductTypeUpdateMutation) => void;
}
const ProductTypeOperations: React.StatelessComponent<
ProductTypeOperationsProps
-> = ({ id, children, onError, onDelete }) => {
+> = ({ id, children, onError, onDelete, onUpdate }) => {
return (
<ProductTypeDeleteProvider id={id} onError={onError} onSuccess={onDelete}>
- {deleteProductType =>
- children({
- deleteProductType: {
- data: deleteProductType.data,
- loading: deleteProductType.loading,
- mutate: () => deleteProductType.mutate({ variables: { id } })
- },
- errors: [],
- loading: deleteProductType.loading
- })
- }
+ {deleteProductType => (
+ <ProductTypeUpdateProvider onError={onError} onSuccess={onUpdate}>
+ {updateProductType =>
+ children({
+ deleteProductType: {
+ data: deleteProductType.data,
+ loading: deleteProductType.loading,
+ mutate: () => deleteProductType.mutate({ variables: { id } })
+ },
+ errors: [],
+ loading: deleteProductType.loading,
+ updateProductType: {
+ data: updateProductType.data,
+ loading: updateProductType.loading,
+ mutate: variables => updateProductType.mutate({ variables })
+ }
+ })
+ }
+ </ProductTypeUpdateProvider>
+ )}
</ProductTypeDeleteProvider>
);
};
diff --git a/saleor/static/dashboard-next/productTypes/containers/ProductTypeUpdate.tsx b/saleor/static/dashboard-next/productTypes/containers/ProductTypeUpdate.tsx
new file mode 100644
index 0000000000..074c35b535
--- /dev/null
+++ b/saleor/static/dashboard-next/productTypes/containers/ProductTypeUpdate.tsx
@@ -0,0 +1,42 @@
+import * as React from "react";
+
+import {
+ PartialMutationProviderProps,
+ PartialMutationProviderRenderProps
+} from "../..";
+import {
+ ProductTypeUpdateMutation,
+ ProductTypeUpdateMutationVariables
+} from "../../gql-types";
+import {
+ productTypeUpdateMutation,
+ TypedProductTypeUpdateMutation
+} from "../mutations";
+
+interface ProductTypeUpdateProviderProps extends PartialMutationProviderProps {
+ children: PartialMutationProviderRenderProps<
+ ProductTypeUpdateMutation,
+ ProductTypeUpdateMutationVariables
+ >;
+}
+
+const ProductTypeUpdateProvider: React.StatelessComponent<
+ ProductTypeUpdateProviderProps
+> = ({ children, onError, onSuccess }) => (
+ <TypedProductTypeUpdateMutation
+ mutation={productTypeUpdateMutation}
+ onCompleted={onSuccess}
+ onError={onError}
+ >
+ {(mutate, { data, loading, error }) => {
+ return children({
+ data,
+ error,
+ loading,
+ mutate
+ });
+ }}
+ </TypedProductTypeUpdateMutation>
+);
+
+export default ProductTypeUpdateProvider;
diff --git a/saleor/static/dashboard-next/productTypes/mutations.ts b/saleor/static/dashboard-next/productTypes/mutations.ts
index ee413441e2..aac1d83ece 100644
--- a/saleor/static/dashboard-next/productTypes/mutations.ts
+++ b/saleor/static/dashboard-next/productTypes/mutations.ts
@@ -3,7 +3,9 @@ import { Mutation, MutationProps } from "react-apollo";
import {
ProductTypeDeleteMutation,
- ProductTypeDeleteMutationVariables
+ ProductTypeDeleteMutationVariables,
+ ProductTypeUpdateMutation,
+ ProductTypeUpdateMutationVariables
} from "../gql-types";
export const TypedProductTypeDeleteMutation = Mutation as React.ComponentType<
@@ -22,3 +24,41 @@ export const productTypeDeleteMutation = gql`
}
}
`;
+
+export const TypedProductTypeUpdateMutation = Mutation as React.ComponentType<
+ MutationProps<ProductTypeUpdateMutation, ProductTypeUpdateMutationVariables>
+>;
+export const productTypeUpdateMutation = gql`
+ mutation ProductTypeUpdate($id: ID!, $input: ProductTypeInput!) {
+ productTypeUpdate(id: $id, input: $input) {
+ errors {
+ field
+ message
+ }
+ productType {
+ id
+ name
+ hasVariants
+ productAttributes {
+ edges {
+ node {
+ id
+ slug
+ name
+ }
+ }
+ }
+ variantAttributes {
+ edges {
+ node {
+ id
+ slug
+ name
+ }
+ }
+ }
+ isShippingRequired
+ }
+ }
+ }
+`;
diff --git a/saleor/static/dashboard-next/productTypes/views/ProductTypeUpdate.tsx b/saleor/static/dashboard-next/productTypes/views/ProductTypeUpdate.tsx
index 8c171e47f4..791763ef24 100644
--- a/saleor/static/dashboard-next/productTypes/views/ProductTypeUpdate.tsx
+++ b/saleor/static/dashboard-next/productTypes/views/ProductTypeUpdate.tsx
@@ -4,8 +4,14 @@ import Navigator from "../../components/Navigator";
import { productTypeListUrl } from "..";
import ErrorMessageCard from "../../components/ErrorMessageCard";
import Messages from "../../components/messages";
+import {
+ ProductTypeDeleteMutation,
+ ProductTypeUpdateMutation
+} from "../../gql-types";
import i18n from "../../i18n";
-import ProductTypeDetailsPage from "../components/ProductTypeDetailsPage";
+import ProductTypeDetailsPage, {
+ ProductTypeForm
+} from "../components/ProductTypeDetailsPage";
import ProductTypeOperations from "../containers/ProductTypeOperations";
import {
productTypeDetailsQuery,
@@ -13,7 +19,6 @@ import {
TypedProductTypeDetailsQuery,
TypedSearchAttributeQuery
} from "../queries";
-import { ProductTypeDeleteMutation } from "../../gql-types";
const taxRates = ["standard", "electronics", "food", "apparel"]; // FIXME: delet dis
@@ -55,14 +60,44 @@ export const ProductTypeUpdate: React.StatelessComponent<
navigate(productTypeListUrl);
}
};
+ const handleUpdateSuccess = (
+ updateData: ProductTypeUpdateMutation
+ ) => {
+ if (updateData.productTypeUpdate.errors.length === 0) {
+ pushMessage({
+ text: i18n.t("Successfully updated product type")
+ });
+ }
+ };
return (
<ProductTypeOperations
id={id}
onDelete={handleDeleteSuccess}
+ onUpdate={handleUpdateSuccess}
>
- {({ deleteProductType, loading: mutationLoading }) => {
+ {({
+ deleteProductType,
+ loading: mutationLoading,
+ updateProductType
+ }) => {
const handleDelete = () =>
deleteProductType.mutate({ id });
+ const handleUpdate = (formData: ProductTypeForm) => {
+ updateProductType.mutate({
+ id,
+ input: {
+ hasVariants: formData.hasVariants,
+ isShippingRequired: formData.isShippingRequired,
+ name: formData.name,
+ productAttributes: formData.productAttributes.map(
+ choice => choice.value
+ ),
+ variantAttributes: formData.variantAttributes.map(
+ choice => choice.value
+ )
+ }
+ });
+ };
const loading = mutationLoading || dataLoading;
return (
<ProductTypeDetailsPage
@@ -104,7 +139,7 @@ export const ProductTypeUpdate: React.StatelessComponent<
onAttributeSearch={handleSearchAttribute}
onBack={() => navigate(productTypeListUrl)}
onDelete={handleDelete}
- onSubmit={() => undefined}
+ onSubmit={handleUpdate}
/>
);
}}