Skip to content

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

Add background image to categories

dominik-zeglen49b49b507d0d
diff --git a/saleor/static/dashboard-next/categories/components/CategoryBackground/CategoryBackground.tsx b/saleor/static/dashboard-next/categories/components/CategoryBackground/CategoryBackground.tsx
index d3bfef68af..8daeec7053 100644
--- a/saleor/static/dashboard-next/categories/components/CategoryBackground/CategoryBackground.tsx
+++ b/saleor/static/dashboard-next/categories/components/CategoryBackground/CategoryBackground.tsx
@@ -4,23 +4,27 @@ import {
withStyles,
WithStyles
} from "@material-ui/core/styles";
+import TextField from "@material-ui/core/TextField";
import * as React from "react";
import Button from "@material-ui/core/Button";
import Card from "@material-ui/core/Card";
import CardContent from "@material-ui/core/CardContent";
import AddPhotoIcon from "@material-ui/icons/AddAPhoto";
-
import CardTitle from "../../../components/CardTitle";
+import Hr from "../../../components/Hr";
import ImageTile from "../../../components/ImageTile";
+import Skeleton from "../../../components/Skeleton";
import i18n from "../../../i18n";
+import { CategoryDetails_category_backgroundImage } from "../../types/CategoryDetails";
+import { FormData } from "../CategoryUpdatePage";
const styles = (theme: Theme) =>
createStyles({
PhotosIcon: {
- height: 64,
+ height: "64px",
margin: "0 auto",
- width: 64
+ width: "64px"
},
PhotosIconContainer: {
margin: `${theme.spacing.unit * 5}px 0`,
@@ -48,13 +52,12 @@ const styles = (theme: Theme) =>
}
});
-interface CategoryBackgroundProps extends WithStyles<typeof styles> {
+export interface CategoryBackgroundProps extends WithStyles<typeof styles> {
+ data: FormData;
+ image: CategoryDetails_category_backgroundImage;
+ onChange: (event: React.ChangeEvent<any>) => void;
onImageDelete: () => void;
- placeholderImage: string;
- backgroundImage: {
- url: string;
- };
- onImageUpload(event: React.ChangeEvent<any>);
+ onImageUpload: (event: React.ChangeEvent<any>) => void;
}
export const CategoryBackground = withStyles(styles)(
@@ -69,9 +72,10 @@ export const CategoryBackground = withStyles(styles)(
render() {
const {
classes,
+ data,
onImageUpload,
- backgroundImage,
- placeholderImage,
+ image,
+ onChange,
onImageDelete
} = this.props;
return (
@@ -98,23 +102,36 @@ export const CategoryBackground = withStyles(styles)(
}
/>
<CardContent>
- {backgroundImage === undefined ? (
+ {image === undefined ? (
<div>
<div className={classes.imageContainer}>
- <img className={classes.image} src={placeholderImage} />
+ <Skeleton />
</div>
</div>
- ) : backgroundImage === null ? (
+ ) : image === null ? (
<div className={classes.PhotosIconContainer}>
<AddPhotoIcon className={classes.PhotosIcon} />
</div>
) : (
- <ImageTile
- image={backgroundImage}
- onImageDelete={onImageDelete}
- />
+ <ImageTile image={image} onImageDelete={onImageDelete} />
)}
</CardContent>
+ {image && (
+ <>
+ <Hr />
+ <CardContent>
+ <TextField
+ name="backgroundImageAlt"
+ label={i18n.t("Description")}
+ helperText={i18n.t("Optional")}
+ value={data.backgroundImageAlt}
+ onChange={onChange}
+ fullWidth
+ multiline
+ />
+ </CardContent>
+ </>
+ )}
</Card>
);
}
diff --git a/saleor/static/dashboard-next/categories/components/CategoryBackground/index.ts b/saleor/static/dashboard-next/categories/components/CategoryBackground/index.ts
index 46667f78a5..f5a59d0a36 100644
--- a/saleor/static/dashboard-next/categories/components/CategoryBackground/index.ts
+++ b/saleor/static/dashboard-next/categories/components/CategoryBackground/index.ts
@@ -1,4 +1,2 @@
export { default } from "./CategoryBackground";
-
export * from "./CategoryBackground";
-export * from "./CategoryBackgroundImage";
diff --git a/saleor/static/dashboard-next/categories/components/CategoryUpdatePage/CategoryUpdatePage.tsx b/saleor/static/dashboard-next/categories/components/CategoryUpdatePage/CategoryUpdatePage.tsx
index f0f9ef4855..0c751c03a0 100644
--- a/saleor/static/dashboard-next/categories/components/CategoryUpdatePage/CategoryUpdatePage.tsx
+++ b/saleor/static/dashboard-next/categories/components/CategoryUpdatePage/CategoryUpdatePage.tsx
@@ -19,10 +19,11 @@ import {
CategoryDetails_category_children_edges_node,
CategoryDetails_category_products_edges_node
} from "../../types/CategoryDetails";
-// import CategoryBackground from "../CategoryBackground";
+import CategoryBackground from "../CategoryBackground";
import CategoryProductsCard from "../CategoryProductsCard";
-interface FormData {
+export interface FormData {
+ backgroundImageAlt: string;
description: string;
name: string;
seoTitle: string;
@@ -45,7 +46,6 @@ export interface CategoryUpdatePageProps extends WithStyles<typeof styles> {
currentTab: CategoryPageTab;
errors: UserError[];
disabled: boolean;
- placeholderImage: string;
category: CategoryDetails_category;
products: CategoryDetails_category_products_edges_node[];
subcategories: CategoryDetails_category_children_edges_node[];
@@ -92,16 +92,20 @@ export const CategoryUpdatePage = withStyles(styles, {
onNextPage,
onPreviousPage,
onProductClick,
- onSubmit
+ onSubmit,
+ onImageDelete,
+ onImageUpload
}: CategoryUpdatePageProps) => {
- const initialData = category
+ const initialData: FormData = category
? {
+ backgroundImageAlt: maybe(() => category.backgroundImage.alt, ""),
description: category.description || "",
- name: category.name,
+ name: category.name || "",
seoDescription: category.seoDescription || "",
seoTitle: category.seoTitle || ""
}
: {
+ backgroundImageAlt: "",
description: "",
name: "",
seoDescription: "",
@@ -127,13 +131,13 @@ export const CategoryUpdatePage = withStyles(styles, {
onChange={change}
/>
<CardSpacer />
- {/* TODO: Uncomment this section after API fixes */}
- {/* <CategoryBackground
+ <CategoryBackground
+ data={data}
onImageUpload={onImageUpload}
onImageDelete={onImageDelete}
- backgroundImage={maybe(() => category.backgroundImage)}
- placeholderImage={placeholderImage}
- /> */}
+ image={maybe(() => category.backgroundImage)}
+ onChange={change}
+ />
<CardSpacer />
<SeoForm
helperText={i18n.t(
diff --git a/saleor/static/dashboard-next/categories/queries.ts b/saleor/static/dashboard-next/categories/queries.ts
index 6184ead263..47a900a66e 100644
--- a/saleor/static/dashboard-next/categories/queries.ts
+++ b/saleor/static/dashboard-next/categories/queries.ts
@@ -10,6 +10,10 @@ import { RootCategories } from "./types/RootCategories";
export const categoryDetailsFragment = gql`
fragment CategoryDetailsFragment on Category {
id
+ backgroundImage {
+ alt
+ url
+ }
name
description
seoDescription
diff --git a/saleor/static/dashboard-next/categories/types/CategoryDetails.ts b/saleor/static/dashboard-next/categories/types/CategoryDetails.ts
index 3d516421c0..540522f263 100644
--- a/saleor/static/dashboard-next/categories/types/CategoryDetails.ts
+++ b/saleor/static/dashboard-next/categories/types/CategoryDetails.ts
@@ -10,6 +10,12 @@ export interface CategoryDetails_category_parent {
id: string;
}
+export interface CategoryDetails_category_backgroundImage {
+ __typename: "Image";
+ alt: string | null;
+ url: string;
+}
+
export interface CategoryDetails_category_children_edges_node_children {
__typename: "CategoryCountableConnection";
totalCount: number | null;
@@ -93,6 +99,7 @@ export interface CategoryDetails_category {
seoDescription: string | null;
seoTitle: string | null;
parent: CategoryDetails_category_parent | null;
+ backgroundImage: CategoryDetails_category_backgroundImage | null;
children: CategoryDetails_category_children | null;
products: CategoryDetails_category_products | null;
}
diff --git a/saleor/static/dashboard-next/categories/views/CategoryDetails/index.tsx b/saleor/static/dashboard-next/categories/views/CategoryDetails/index.tsx
index 96c38a769a..193dbc1d3a 100644
--- a/saleor/static/dashboard-next/categories/views/CategoryDetails/index.tsx
+++ b/saleor/static/dashboard-next/categories/views/CategoryDetails/index.tsx
@@ -124,7 +124,6 @@ export const CategoryDetails: React.StatelessComponent<
() =>
updateResult.data.categoryUpdate.errors
)}
- placeholderImage={""}
onAddCategory={() =>
navigate(categoryAddUrl(id))
}
@@ -143,8 +142,26 @@ export const CategoryDetails: React.StatelessComponent<
onDelete={() =>
navigate(categoryDeleteUrl(id))
}
- onImageDelete={() => undefined}
- onImageUpload={() => undefined}
+ onImageDelete={() =>
+ updateCategory({
+ variables: {
+ id,
+ input: {
+ backgroundImage: null
+ }
+ }
+ })
+ }
+ onImageUpload={event =>
+ updateCategory({
+ variables: {
+ id,
+ input: {
+ backgroundImage: event.target.files[0]
+ }
+ }
+ })
+ }
onNextPage={loadNextPage}
onPreviousPage={loadPreviousPage}
pageInfo={pageInfo}
@@ -155,6 +172,8 @@ export const CategoryDetails: React.StatelessComponent<
variables: {
id,
input: {
+ backgroundImageAlt:
+ formData.backgroundImageAlt,
description: formData.description,
name: formData.name,
seo: {
diff --git a/saleor/static/dashboard-next/collections/components/CollectionImage/CollectionImage.tsx b/saleor/static/dashboard-next/collections/components/CollectionImage/CollectionImage.tsx
index 854675f236..87d1328dad 100644
--- a/saleor/static/dashboard-next/collections/components/CollectionImage/CollectionImage.tsx
+++ b/saleor/static/dashboard-next/collections/components/CollectionImage/CollectionImage.tsx
@@ -127,6 +127,7 @@ export const CollectionImage = withStyles(styles)(
value={data.backgroundImageAlt}
onChange={onChange}
fullWidth
+ multiline
/>
</CardContent>
</>