Skip to content

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

Add image management

dominik-zeglend5074f459af8
diff --git a/saleor/static/dashboard-next/gql-types.ts b/saleor/static/dashboard-next/gql-types.ts
index 91c002dde5..a57c22f879 100644
--- a/saleor/static/dashboard-next/gql-types.ts
+++ b/saleor/static/dashboard-next/gql-types.ts
@@ -955,17 +955,6 @@ export interface VariantUpdateMutation {
},
} >,
} | null,
- images: {
- edges: Array< {
- // The item at the end of the edge
- node: {
- // The ID of the object.
- id: string,
- // The URL of the image.
- url: string,
- },
- } >,
- } | null,
},
} >,
} | null,
@@ -1096,17 +1085,6 @@ export interface VariantCreateMutation {
},
} >,
} | null,
- images: {
- edges: Array< {
- // The item at the end of the edge
- node: {
- // The ID of the object.
- id: string,
- // The URL of the image.
- url: string,
- },
- } >,
- } | null,
},
} >,
} | null,
@@ -1155,6 +1133,54 @@ export interface ProductImageUpdateMutation {
} | null,
};
+export interface VariantImageAssignMutationVariables {
+ variantId: string,
+ imageId: string,
+};
+
+export interface VariantImageAssignMutation {
+ variantImageAssign: {
+ // 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,
+ // Assigned product image.
+ image: {
+ // The ID of the object.
+ id: string,
+ } | null,
+ } | null,
+};
+
+export interface VariantImageUnassignMutationVariables {
+ variantId: string,
+ imageId: string,
+};
+
+export interface VariantImageUnassignMutation {
+ variantImageUnassign: {
+ // 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,
+ // Unassigned product image.
+ image: {
+ // The ID of the object.
+ id: string,
+ } | null,
+ } | null,
+};
+
export interface ProductListQueryVariables {
first?: number | null,
after?: string | null,
@@ -1493,17 +1519,6 @@ export interface ProductVariantDetailsQuery {
},
} >,
} | null,
- images: {
- edges: Array< {
- // The item at the end of the edge
- node: {
- // The ID of the object.
- id: string,
- // The URL of the image.
- url: string,
- },
- } >,
- } | null,
},
} >,
} | null,
@@ -1971,17 +1986,6 @@ export interface ProductVariantFragment {
},
} >,
} | null,
- images: {
- edges: Array< {
- // The item at the end of the edge
- node: {
- // The ID of the object.
- id: string,
- // The URL of the image.
- url: string,
- },
- } >,
- } | null,
},
} >,
} | null,
diff --git a/saleor/static/dashboard-next/products/components/ProductVariantCreatePage/ProductVariantCreatePage.tsx b/saleor/static/dashboard-next/products/components/ProductVariantCreatePage/ProductVariantCreatePage.tsx
index c9fe9e09ed..ab689eee9d 100644
--- a/saleor/static/dashboard-next/products/components/ProductVariantCreatePage/ProductVariantCreatePage.tsx
+++ b/saleor/static/dashboard-next/products/components/ProductVariantCreatePage/ProductVariantCreatePage.tsx
@@ -117,112 +117,91 @@ const ProductVariantCreatePage = decorate<ProductVariantCreatePageProps>(
return (
<Toggle>
{(isImageSelectModalActive, { toggle: toggleImageSelectModal }) => (
- <>
- <Container width="md">
- <PageHeader title={header} onBack={onBack} />
- <Form
- initial={initialForm}
- errors={formErrors}
- onSubmit={onSubmit}
- key={product ? JSON.stringify(product) : "noproduct"}
- >
- {({ change, data, errors, hasChanged, submit }) => {
- const images = data.images
- ? data.images.map(
- id =>
- product.images.edges
- .map(edge => edge.node)
- .filter(image => image.id === id)[0]
- )
- : undefined;
- return (
- <>
- <div className={classes.root}>
- <div>
- <ProductVariantNavigation
- variants={
- product &&
- product.variants &&
- product.variants.edges
- ? product.variants.edges.map(edge => edge.node)
- : undefined
+ <Container width="md">
+ <PageHeader title={header} onBack={onBack} />
+ <Form
+ initial={initialForm}
+ errors={formErrors}
+ onSubmit={onSubmit}
+ key={product ? JSON.stringify(product) : "noproduct"}
+ >
+ {({ change, data, errors, hasChanged, submit }) => {
+ const images = data.images
+ ? data.images.map(
+ id =>
+ product.images.edges
+ .map(edge => edge.node)
+ .filter(image => image.id === id)[0]
+ )
+ : undefined;
+ return (
+ <>
+ <div className={classes.root}>
+ <div>
+ <ProductVariantNavigation
+ variants={
+ product &&
+ product.variants &&
+ product.variants.edges
+ ? product.variants.edges.map(edge => edge.node)
+ : undefined
+ }
+ onRowClick={(variantId: string) => {
+ if (product && product.variants) {
+ return onVariantClick(variantId);
}
- onRowClick={(variantId: string) => {
- if (product && product.variants) {
- return onVariantClick(variantId);
- }
- }}
- />
- </div>
- <div>
- <ProductVariantAttributes
- attributes={
- product &&
- product.productType &&
- product.productType.variantAttributes &&
- product.productType.variantAttributes.edges
- ? product.productType.variantAttributes.edges.map(
- edge => edge.node
- )
- : undefined
- }
- data={data}
- disabled={loading}
- onChange={change}
- />
- {/* TODO: uncomment when API will be ready */}
- {/* <ProductVariantImages
- disabled={loading}
- images={images}
- placeholderImage={placeholderImage}
- onImageAdd={toggleImageSelectModal}
- /> */}
- <ProductVariantPrice
- errors={errors}
- priceOverride={data.priceOverride}
- // FIXME: currency symbol should be fetched from API
- currencySymbol="USD"
- costPrice={data.costPrice}
- loading={loading}
- onChange={change}
- />
- <ProductVariantStock
- errors={errors}
- sku={data.sku}
- stock={data.stock}
- loading={loading}
- onChange={change}
- />
- </div>
+ }}
+ />
+ </div>
+ <div>
+ <ProductVariantAttributes
+ attributes={
+ product &&
+ product.productType &&
+ product.productType.variantAttributes &&
+ product.productType.variantAttributes.edges
+ ? product.productType.variantAttributes.edges.map(
+ edge => edge.node
+ )
+ : undefined
+ }
+ data={data}
+ disabled={loading}
+ onChange={change}
+ />
+ <ProductVariantPrice
+ errors={errors}
+ priceOverride={data.priceOverride}
+ // FIXME: currency symbol should be fetched from API
+ currencySymbol="USD"
+ costPrice={data.costPrice}
+ loading={loading}
+ onChange={change}
+ />
+ <ProductVariantStock
+ errors={errors}
+ sku={data.sku}
+ stock={data.stock}
+ loading={loading}
+ onChange={change}
+ />
</div>
- <SaveButtonBar
- disabled={loading || !onSubmit || !hasChanged}
- labels={{
- delete: i18n.t("Remove variant"),
- save: i18n.t("Save variant")
- }}
- state={saveButtonBarState}
- onCancel={onBack}
- onSave={submit}
- />
- </>
- );
- }}
- </Form>
- </Container>
- {product && (
- <ProductVariantImageSelectDialog
- onClose={toggleImageSelectModal}
- onConfirm={handleImageSelect}
- open={isImageSelectModalActive}
- images={
- product.images && product.images.edges
- ? product.images.edges.map(edge => edge.node)
- : []
- }
- />
- )}
- </>
+ </div>
+ <SaveButtonBar
+ disabled={loading || !onSubmit || !hasChanged}
+ labels={{
+ delete: i18n.t("Remove variant"),
+ save: i18n.t("Save variant")
+ }}
+ state={saveButtonBarState}
+ onCancel={onBack}
+ onSave={submit}
+ />
+ </>
+ );
+ }}
+ </Form>
+ </Container>
)}
</Toggle>
);
diff --git a/saleor/static/dashboard-next/products/components/ProductVariantImageSelectDialog/ProductVariantImageSelectDialog.tsx b/saleor/static/dashboard-next/products/components/ProductVariantImageSelectDialog/ProductVariantImageSelectDialog.tsx
index fb9152afa8..11c9006f80 100644
--- a/saleor/static/dashboard-next/products/components/ProductVariantImageSelectDialog/ProductVariantImageSelectDialog.tsx
+++ b/saleor/static/dashboard-next/products/components/ProductVariantImageSelectDialog/ProductVariantImageSelectDialog.tsx
@@ -45,88 +45,40 @@ const decorate = withStyles(theme => ({
interface ProductVariantImageSelectDialogProps {
images?: ProductImageType[];
- selectedImages?: Array<{
- id: string;
- }>;
+ selectedImages?: string[];
open: boolean;
- onClose?();
- onConfirm?(images: string[]);
-}
-
-interface ProductVariantImageSelectDialogState {
- images: string[];
+ onClose();
+ onImageSelect(id: string);
}
const ProductVariantImageSelectDialog = decorate<
ProductVariantImageSelectDialogProps
->(
- class ProductVariantImageSelectDialogComponent extends React.Component<
- ProductVariantImageSelectDialogProps &
- WithStyles<"root" | "gridElement" | "image" | "imageContainer">,
- ProductVariantImageSelectDialogState
- > {
- constructor(props) {
- super(props);
- this.state = {
- images: this.props.selectedImages
- ? this.props.selectedImages.map(image => image.id)
- : []
- };
- }
-
- handleConfirm = () => this.props.onConfirm(this.state.images);
- handleImageSelect = id => () =>
- this.state.images.indexOf(id) === -1
- ? this.setState(prevState => ({ images: [...prevState.images, id] }))
- : this.setState(prevState => ({
- images: prevState.images.filter(image => image !== id)
- }));
-
- render() {
- const { classes, images, open, onClose } = this.props;
- return (
- <Dialog open={open}>
- <DialogTitle>
- {i18n.t("Image selection", { context: "title" })}
- </DialogTitle>
- <DialogContent>
- <div className={classes.root}>
- {images
- .sort(
- (prev, next) => (prev.sortOrder > next.sortOrder ? 1 : -1)
- )
- .map(tile => (
- <div
- className={[
- classes.imageContainer,
- this.state.images.indexOf(tile.id) === -1
- ? undefined
- : "selected"
- ].join(" ")}
- onClick={this.handleImageSelect(tile.id)}
- key={tile.id}
- >
- <img className={classes.image} src={tile.url} />
- </div>
- ))}
- </div>
- </DialogContent>
- <DialogActions>
- <Button onClick={onClose}>
- {i18n.t("Cancel", { context: "button" })}
- </Button>
- <Button
- variant="raised"
- onClick={this.handleConfirm}
- color="primary"
+>(({ classes, images, open, selectedImages, onClose, onImageSelect }) => (
+ <Dialog open={open}>
+ <DialogTitle>{i18n.t("Image selection", { context: "title" })}</DialogTitle>
+ <DialogContent>
+ <div className={classes.root}>
+ {images
+ .sort((prev, next) => (prev.sortOrder > next.sortOrder ? 1 : -1))
+ .map(tile => (
+ <div
+ className={[
+ classes.imageContainer,
+ selectedImages.indexOf(tile.id) === -1 ? undefined : "selected"
+ ].join(" ")}
+ onClick={onImageSelect(tile.id)}
+ key={tile.id}
>
- {i18n.t("Save", { context: "button" })}
- </Button>
- </DialogActions>
- </Dialog>
- );
- }
- }
-);
-
+ <img className={classes.image} src={tile.url} />
+ </div>
+ ))}
+ </div>
+ </DialogContent>
+ <DialogActions>
+ <Button onClick={onClose}>
+ {i18n.t("Close", { context: "button" })}
+ </Button>
+ </DialogActions>
+ </Dialog>
+));
export default ProductVariantImageSelectDialog;
diff --git a/saleor/static/dashboard-next/products/components/ProductVariantImages/ProductVariantImages.tsx b/saleor/static/dashboard-next/products/components/ProductVariantImages/ProductVariantImages.tsx
index 58933f6504..0a4b2d38d0 100644
--- a/saleor/static/dashboard-next/products/components/ProductVariantImages/ProductVariantImages.tsx
+++ b/saleor/static/dashboard-next/products/components/ProductVariantImages/ProductVariantImages.tsx
@@ -50,23 +50,20 @@ const decorate = withStyles(theme => ({
}));
export const ProductVariantImages = decorate<ProductVariantImagesProps>(
- ({ classes, images }) => (
+ ({ classes, disabled, images, onImageAdd }) => (
<Card className={classes.card}>
<CardTitle
title={i18n.t("Images")}
- // FIXME: enable this button when API will be able to pin images
- // to variants
-
- // toolbar={
- // <Button
- // color="secondary"
- // variant="flat"
- // disabled={disabled}
- // onClick={onImageAdd}
- // >
- // {i18n.t("Choose photos")}
- // </Button>
- // }
+ toolbar={
+ <Button
+ color="secondary"
+ variant="flat"
+ disabled={disabled}
+ onClick={onImageAdd}
+ >
+ {i18n.t("Choose photos")}
+ </Button>
+ }
/>
<CardContent>
<div className={classes.root}>
diff --git a/saleor/static/dashboard-next/products/components/ProductVariantPage/ProductVariantPage.tsx b/saleor/static/dashboard-next/products/components/ProductVariantPage/ProductVariantPage.tsx
index e11c5659ac..014dee94b6 100644
--- a/saleor/static/dashboard-next/products/components/ProductVariantPage/ProductVariantPage.tsx
+++ b/saleor/static/dashboard-next/products/components/ProductVariantPage/ProductVariantPage.tsx
@@ -73,7 +73,7 @@ interface ProductVariantPageProps {
onBack();
onDelete();
onSubmit(data: any);
- onImageSelect(images: string[]);
+ onImageSelect(id: string);
onVariantClick(variantId: string);
}
@@ -117,7 +117,6 @@ const ProductVariantPage = decorate<ProductVariantPageProps>(
.filter(image => variantImages.indexOf(image.id) !== -1)
.sort((prev, next) => (prev.sortOrder > next.sortOrder ? 1 : -1))
: undefined;
- const handleImageSelect = (images: string[]) => {};
return (
<Toggle>
{(isModalActive, { toggle: toggleDeleteModal }) => (
@@ -236,9 +235,14 @@ const ProductVariantPage = decorate<ProductVariantPageProps>(
/>
<ProductVariantImageSelectDialog
onClose={toggleImageSelectModal}
- onConfirm={handleImageSelect}
+ onImageSelect={onImageSelect}
open={isImageSelectModalActive}
images={productImages}
+ selectedImages={
+ variant && variant.images && variant.images.edges
+ ? variant.images.edges.map(edge => edge.node.id)
+ : undefined
+ }
/>
</>
)}
diff --git a/saleor/static/dashboard-next/products/containers/ProductVariantImageAssign.tsx b/saleor/static/dashboard-next/products/containers/ProductVariantImageAssign.tsx
new file mode 100644
index 0000000000..4132d69a27
--- /dev/null
+++ b/saleor/static/dashboard-next/products/containers/ProductVariantImageAssign.tsx
@@ -0,0 +1,78 @@
+import * as React from "react";
+
+import {
+ PartialMutationProviderProps,
+ PartialMutationProviderRenderProps
+} from "../..";
+import {
+ ProductVariantDetailsQuery,
+ VariantImageAssignMutation,
+ VariantImageAssignMutationVariables
+} from "../../gql-types";
+import {
+ TypedVariantImageAssign,
+ variantImageAssignMutation
+} from "../mutations";
+import { productVariantQuery } from "../queries";
+
+interface VariantImageAssignProviderProps extends PartialMutationProviderProps {
+ id: string;
+ children: PartialMutationProviderRenderProps<
+ VariantImageAssignMutation,
+ VariantImageAssignMutationVariables
+ >;
+}
+
+const VariantImageAssignProvider: React.StatelessComponent<
+ VariantImageAssignProviderProps
+> = ({ id, children, onError, onSuccess }) => (
+ <TypedVariantImageAssign
+ mutation={variantImageAssignMutation}
+ update={(cache, { data: { variantImageAssign } }) => {
+ if (variantImageAssign.errors.length === 0) {
+ const data: ProductVariantDetailsQuery = cache.readQuery({
+ query: productVariantQuery,
+ variables: { id }
+ });
+ data.productVariant.images.edges = [
+ ...data.productVariant.images.edges,
+ {
+ __typename: "ProductImageCountableEdge",
+ node: {
+ __typename: "ProductImage",
+ id: variantImageAssign.image.id
+ }
+ } as any
+ ];
+ if (data.productVariant.images.edges.length === 1) {
+ data.productVariant.product.variants.edges.filter(
+ variant => variant.node.id === id
+ )[0].node.image.edges = [
+ ...data.productVariant.images.edges,
+ {
+ __typename: "ProductImageCountableEdge",
+ node: {
+ __typename: "ProductImage",
+ id: variantImageAssign.image.id
+ }
+ } as any
+ ];
+ }
+ cache.writeQuery({ query: productVariantQuery, data });
+ }
+ }}
+ onCompleted={onSuccess}
+ onError={onError}
+ >
+ {(mutate, { data, loading, error }) => {
+ return children({
+ data,
+ error,
+ loading,
+ mutate
+ });
+ }}
+ </TypedVariantImageAssign>
+);
+
+export default VariantImageAssignProvider;
diff --git a/saleor/static/dashboard-next/products/containers/ProductVariantImageUnassign.tsx b/saleor/static/dashboard-next/products/containers/ProductVariantImageUnassign.tsx
new file mode 100644
index 0000000000..01a1203b1d
--- /dev/null
+++ b/saleor/static/dashboard-next/products/containers/ProductVariantImageUnassign.tsx
@@ -0,0 +1,63 @@
+import * as React from "react";
+
+import {
+ PartialMutationProviderProps,
+ PartialMutationProviderRenderProps
+} from "../..";
+import {
+ ProductVariantDetailsQuery,
+ VariantImageUnassignMutation,
+ VariantImageUnassignMutationVariables
+} from "../../gql-types";
+import {
+ TypedVariantImageUnassign,
+ variantImageUnassignMutation
+} from "../mutations";
+import { productVariantQuery } from "../queries";
+
+interface VariantImageUnassignProviderProps
+ extends PartialMutationProviderProps {
+ id: string;
+ children: PartialMutationProviderRenderProps<
+ VariantImageUnassignMutation,
+ VariantImageUnassignMutationVariables
+ >;
+}
+
+const VariantImageUnassignProvider: React.StatelessComponent<
+ VariantImageUnassignProviderProps
+> = ({ id, children, onError, onSuccess }) => (
+ <TypedVariantImageUnassign
+ mutation={variantImageUnassignMutation}
+ update={(cache, { data: { variantImageUnassign } }) => {
+ if (variantImageUnassign.errors.length === 0) {
+ const data: ProductVariantDetailsQuery = cache.readQuery({
+ query: productVariantQuery,
+ variables: { id }
+ });
+ data.productVariant.images.edges = data.productVariant.images.edges.filter(
+ image => image.node.id !== variantImageUnassign.image.id
+ );
+ if (data.productVariant.images.edges.length === 0) {
+ data.productVariant.product.variants.edges.filter(
+ variant => variant.node.id === id
+ )[0].node.image.edges = [];
+ }
+ cache.writeQuery({ query: productVariantQuery, data });
+ }
+ }}
+ onCompleted={onSuccess}
+ onError={onError}
+ >
+ {(mutate, { data, loading, error }) => {
+ return children({
+ data,
+ error,
+ loading,
+ mutate
+ });
+ }}
+ </TypedVariantImageUnassign>
+);
+
+export default VariantImageUnassignProvider;
diff --git a/saleor/static/dashboard-next/products/containers/ProductVariantOperations.tsx b/saleor/static/dashboard-next/products/containers/ProductVariantOperations.tsx
index 1beb3d6bda..caea89ff72 100644
--- a/saleor/static/dashboard-next/products/containers/ProductVariantOperations.tsx
+++ b/saleor/static/dashboard-next/products/containers/ProductVariantOperations.tsx
@@ -7,10 +7,16 @@ import {
} from "../..";
import {
VariantDeleteMutation,
+ VariantImageAssignMutation,
+ VariantImageAssignMutationVariables,
+ VariantImageUnassignMutation,
+ VariantImageUnassignMutationVariables,
VariantUpdateMutation,
VariantUpdateMutationVariables
} from "../../gql-types";
import VariantDeleteProvider from "./ProductVariantDelete";
+import VariantImageAssignProvider from "./ProductVariantImageAssign";
+import VariantImageUnassignProvider from "./ProductVariantImageUnassign";
import VariantUpdateProvider from "./ProductVariantUpdate";
interface VariantDeleteOperationsProps extends MutationProviderProps {
@@ -22,42 +28,79 @@ interface VariantDeleteOperationsProps extends MutationProviderProps {
VariantUpdateMutation,
VariantUpdateMutationVariables
>;
+ assignImage: PartialMutationProviderOutput<
+ VariantImageAssignMutation,
+ VariantImageAssignMutationVariables
+ >;
+ unassignImage: PartialMutationProviderOutput<
+ VariantImageUnassignMutation,
+ VariantImageUnassignMutationVariables
+ >;
}>;
onDelete?: (data: VariantDeleteMutation) => void;
+ onImageAssign?: (data: VariantImageAssignMutation) => void;
+ onImageUnassign?: (data: VariantImageUnassignMutation) => void;
onUpdate?: (data: VariantUpdateMutation) => void;
}
-const VariantDeleteOperations: React.StatelessComponent<
+const VariantUpdateOperations: React.StatelessComponent<
VariantDeleteOperationsProps
> = ({ id, children, onError, onDelete, onUpdate }) => {
return (
- <VariantUpdateProvider id={id} onError={onError} onSuccess={onUpdate}>
- {updateVariant => (
- <VariantDeleteProvider id={id} onError={onError} onSuccess={onDelete}>
- {deleteVariant =>
- children({
- deleteVariant: {
- data: deleteVariant.data,
- loading: deleteVariant.loading,
- mutate: variables => deleteVariant.mutate({ variables: { id } })
- },
- errors:
- updateVariant &&
- updateVariant.data &&
- updateVariant.data.productVariantUpdate &&
- updateVariant.data.productVariantUpdate.errors
- ? updateVariant.data.productVariantUpdate.errors
- : [],
- updateVariant: {
- data: updateVariant.data,
- loading: updateVariant.loading,
- mutate: variables => updateVariant.mutate({ variables })
- }
- })
- }
- </VariantDeleteProvider>
+ <VariantImageAssignProvider id={id}>
+ {assignImage => (
+ <VariantImageUnassignProvider id={id}>
+ {unassignImage => (
+ <VariantUpdateProvider
+ id={id}
+ onError={onError}
+ onSuccess={onUpdate}
+ >
+ {updateVariant => (
+ <VariantDeleteProvider
+ id={id}
+ onError={onError}
+ onSuccess={onDelete}
+ >
+ {deleteVariant =>
+ children({
+ assignImage: {
+ data: assignImage.data,
+ loading: assignImage.loading,
+ mutate: variables => assignImage.mutate({ variables })
+ },
+ deleteVariant: {
+ data: deleteVariant.data,
+ loading: deleteVariant.loading,
+ mutate: () =>
+ deleteVariant.mutate({ variables: { id } })
+ },
+ errors:
+ updateVariant &&
+ updateVariant.data &&
+ updateVariant.data.productVariantUpdate &&
+ updateVariant.data.productVariantUpdate.errors
+ ? updateVariant.data.productVariantUpdate.errors
+ : [],
+ unassignImage: {
+ data: unassignImage.data,
+ loading: unassignImage.loading,
+ mutate: variables => unassignImage.mutate({ variables })
+ },
+ updateVariant: {
+ data: updateVariant.data,
+ loading: updateVariant.loading,
+ mutate: variables => updateVariant.mutate({ variables })
+ }
+ })
+ }
+ </VariantDeleteProvider>
+ )}
+ </VariantUpdateProvider>
+ )}
+ </VariantImageUnassignProvider>
)}
- </VariantUpdateProvider>
+ </VariantImageAssignProvider>
);
};
-export default VariantDeleteOperations;
+export default VariantUpdateOperations;
diff --git a/saleor/static/dashboard-next/products/mutations.ts b/saleor/static/dashboard-next/products/mutations.ts
index 208e4d1cd8..69ec81aed1 100644
--- a/saleor/static/dashboard-next/products/mutations.ts
+++ b/saleor/static/dashboard-next/products/mutations.ts
@@ -20,6 +20,10 @@ import {
VariantCreateMutationVariables,
VariantDeleteMutation,
VariantDeleteMutationVariables,
+ VariantImageAssignMutation,
+ VariantImageAssignMutationVariables,
+ VariantImageUnassignMutation,
+ VariantImageUnassignMutationVariables,
VariantUpdateMutation,
VariantUpdateMutationVariables
} from "../gql-types";
@@ -303,3 +307,42 @@ export const productImageUpdateMutation = gql`
}
}
`;
+
+export const TypedVariantImageAssign = Mutation as React.ComponentType<
+ MutationProps<VariantImageAssignMutation, VariantImageAssignMutationVariables>
+>;
+
+export const variantImageAssignMutation = gql`
+ mutation VariantImageAssign($variantId: ID!, $imageId: ID!) {
+ variantImageAssign(variantId: $variantId, imageId: $imageId) {
+ errors {
+ field
+ message
+ }
+ image {
+ id
+ }
+ }
+ }
+`;
+
+export const TypedVariantImageUnassign = Mutation as React.ComponentType<
+ MutationProps<
+ VariantImageUnassignMutation,
+ VariantImageAssignMutationVariables
+ >
+>;
+
+export const variantImageUnassignMutation = gql`
+ mutation VariantImageUnassign($variantId: ID!, $imageId: ID!) {
+ variantImageUnassign(variantId: $variantId, imageId: $imageId) {
+ errors {
+ field
+ message
+ }
+ image {
+ id
+ }
+ }
+ }
+`;
diff --git a/saleor/static/dashboard-next/products/queries.ts b/saleor/static/dashboard-next/products/queries.ts
index dfe91e497e..b71467efb9 100644
--- a/saleor/static/dashboard-next/products/queries.ts
+++ b/saleor/static/dashboard-next/products/queries.ts
@@ -10,7 +10,9 @@ import {
ProductListQuery,
ProductListQueryVariables,
ProductVariantCreateDataQuery,
- ProductVariantCreateDataQueryVariables
+ ProductVariantCreateDataQueryVariables,
+ ProductVariantDetailsQuery,
+ ProductVariantDetailsQueryVariables
} from "../gql-types";
export const fragmentMoney = gql`
@@ -191,14 +193,6 @@ export const fragmentVariant = gql`
}
}
}
- images {
- edges {
- node {
- id
- url
- }
- }
- }
}
}
}
@@ -274,7 +268,7 @@ export const productDetailsQuery = gql`
`;
export const TypedProductVariantQuery = Query as React.ComponentType<
- QueryProps<any, { id: string }>
+ QueryProps<ProductVariantDetailsQuery, ProductVariantDetailsQueryVariables>
>;
export const productVariantQuery = gql`
diff --git a/saleor/static/dashboard-next/products/views/ProductVariant.tsx b/saleor/static/dashboard-next/products/views/ProductVariant.tsx
index 3289d72e92..bea3c823a5 100644
--- a/saleor/static/dashboard-next/products/views/ProductVariant.tsx
+++ b/saleor/static/dashboard-next/products/views/ProductVariant.tsx
@@ -57,6 +57,7 @@ export const ProductVariant: React.StatelessComponent<ProductUpdateProps> = ({
};
const handleUpdate = () =>
pushMessage({ text: i18n.t("Changes saved") });
+
return (
<ProductVariantOperations
productId={productId}
@@ -65,12 +66,42 @@ export const ProductVariant: React.StatelessComponent<ProductUpdateProps> = ({
onError={handleError}
onUpdate={handleUpdate}
>
- {({ deleteVariant, updateVariant }) => {
+ {({
+ assignImage,
+ deleteVariant,
+ updateVariant,
+ unassignImage
+ }) => {
const disableFormSave =
- loading || deleteVariant.loading || updateVariant.loading;
+ loading ||
+ deleteVariant.loading ||
+ updateVariant.loading ||
+ assignImage.loading ||
+ unassignImage.loading;
const formSubmitState = disableFormSave
? "loading"
: "idle";
+ const handleImageSelect = (id: string) => () => {
+ if (variant) {
+ if (
+ variant.images &&
+ variant.images.edges
+ .map(edge => edge.node.id)
+ .indexOf(id) !== -1
+ ) {
+ unassignImage.mutate({
+ imageId: id,
+ variantId: variant.id
+ });
+ } else {
+ assignImage.mutate({
+ imageId: id,
+ variantId: variant.id
+ });
+ }
+ }
+ };
+
return (
<ProductVariantPage
errors={
@@ -90,7 +121,7 @@ export const ProductVariant: React.StatelessComponent<ProductUpdateProps> = ({
}
onBack={handleBack}
onDelete={() => deleteVariant.mutate(variantId)}
- onImageSelect={() => {}}
+ onImageSelect={handleImageSelect}
onSubmit={(data: FormData) => {
if (variant) {
updateVariant.mutate({