Skip to content

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

Add logic for background component

Lukasz Szewczyk8a109a99ab11
diff --git a/saleor/static/dashboard-next/categories/components/CategoryBackground/CategoryBackground.tsx b/saleor/static/dashboard-next/categories/components/CategoryBackground/CategoryBackground.tsx
index 5468234328..256fc81b87 100644
--- a/saleor/static/dashboard-next/categories/components/CategoryBackground/CategoryBackground.tsx
+++ b/saleor/static/dashboard-next/categories/components/CategoryBackground/CategoryBackground.tsx
@@ -5,35 +5,142 @@ import Card from "@material-ui/core/Card";
import Button from "@material-ui/core/Button";
import CardContent from "@material-ui/core/CardContent";
import AddPhotoIcon from "@material-ui/icons/AddAPhoto";
+import DeleteIcon from "@material-ui/icons/Delete";
+import IconButton from "@material-ui/core/IconButton";
+import Toggle from "../../../components/Toggle";
import CardTitle from "../../../components/CardTitle";
+import ActionDialog from "../../../components/ActionDialog";
+import DialogContentText from "@material-ui/core/DialogContentText";
import i18n from "../../../i18n";
interface CategoryBackgroundProps {
onImageUpload?(event: React.ChangeEvent<any>);
- disabled?: boolean;
+ onImageDelete?: (id: string) => () => void;
+ placeholderImage: string;
+ backgroundImage?: {
+ id?: string;
+ url?: string;
+ };
+}
+
+interface BackgroundImageProps {
+ onImageDelete?: (id: string) => () => void;
+ backgroundImage?: {
+ id?: string;
+ url?: string;
+ };
}
const decorate = withStyles(theme => ({
- noPhotosIcon: {
+ PhotosIcon: {
height: theme.spacing.unit * 8,
margin: "0 auto",
width: theme.spacing.unit * 8
},
- noPhotosIconContainer: {
+ PhotosIconContainer: {
margin: `${theme.spacing.unit * 5}px 0`,
textAlign: "center" as "center"
},
fileField: {
display: "none"
+ },
+ root: {
+ display: "grid" as "grid",
+ gridTemplateColumns: "repeat(4, 1fr)"
+ },
+ image: {
+ height: "100%",
+ objectFit: "contain" as "contain",
+ userSelect: "none" as "none",
+ width: "100%"
+ },
+ imageContainer: {
+ "&:hover, &.dragged": {
+ "& $imageOverlay": {
+ display: "block" as "block"
+ }
+ },
+ justifySelf: "start",
+ height: 148,
+ width: 148,
+ background: "#ffffff",
+ border: "1px solid #eaeaea",
+ borderRadius: theme.spacing.unit,
+ overflow: "hidden" as "hidden",
+ padding: theme.spacing.unit * 2,
+ position: "relative" as "relative"
+ },
+ imageOverlay: {
+ background: "rgba(0, 0, 0, 0.6)",
+ cursor: "pointer",
+ display: "none" as "none",
+ height: 148,
+ left: 0,
+ padding: theme.spacing.unit * 2,
+ position: "absolute" as "absolute",
+ top: 0,
+ width: 148
+ },
+ imageOverlayToolbar: {
+ display: "flex" as "flex",
+ justifyContent: "center",
+ position: "relative" as "relative",
+ top: "-10px"
}
}));
+const BackgroundImage = decorate<BackgroundImageProps>(
+ ({ classes, onImageDelete, backgroundImage }) => (
+ <Toggle>
+ {(opened, { toggle }) => (
+ <>
+ <div className={classes.root}>
+ <div className={classes.imageContainer}>
+ <div className={classes.imageOverlay}>
+ <div className={classes.imageOverlayToolbar}>
+ <IconButton color="secondary" onClick={toggle}>
+ <DeleteIcon />
+ </IconButton>
+ </div>
+ </div>
+ <img className={classes.image} src={backgroundImage.url} />
+ </div>
+ </div>
+ <ActionDialog
+ open={opened}
+ onClose={toggle}
+ onConfirm={() => {
+ onImageDelete(backgroundImage.id)();
+ toggle();
+ }}
+ variant="delete"
+ title={i18n.t("Remove category image")}
+ >
+ <DialogContentText>
+ {i18n.t("Are you sure you want to delete this image?")}
+ </DialogContentText>
+ </ActionDialog>
+ </>
+ )}
+ </Toggle>
+ )
+);
+
export const CategoryBackground = decorate(
class CategoryBackgroundClass extends React.Component<
CategoryBackgroundProps &
- WithStyles<"noPhotosIcon" | "noPhotosIconContainer" | "fileField">,
+ WithStyles<
+ | "PhotosIcon"
+ | "PhotosIconContainer"
+ | "fileField"
+ | "root"
+ | "image"
+ | "imageContainer"
+ | "imageOverlay"
+ | "imageOverlayToolbar"
+ >,
{}
> {
imgInputAnchor = React.createRef<HTMLInputElement>();
@@ -41,7 +148,13 @@ export const CategoryBackground = decorate(
clickImgInput = () => this.imgInputAnchor.current.click();
render() {
- const { classes, onImageUpload, disabled } = this.props;
+ const {
+ classes,
+ onImageUpload,
+ backgroundImage,
+ placeholderImage,
+ onImageDelete
+ } = this.props;
return (
<Card>
<CardTitle
@@ -51,7 +164,6 @@ export const CategoryBackground = decorate(
<Button
variant="flat"
color="secondary"
- disabled={disabled}
onClick={this.clickImgInput}
>
{i18n.t("Upload image")}
@@ -67,9 +179,22 @@ export const CategoryBackground = decorate(
}
/>
<CardContent>
- <div className={classes.noPhotosIconContainer}>
- <AddPhotoIcon className={classes.noPhotosIcon} />
- </div>
+ {backgroundImage === undefined ? (
+ <div className={classes.root}>
+ <div className={classes.imageContainer}>
+ <img className={classes.image} src={placeholderImage} />
+ </div>
+ </div>
+ ) : backgroundImage.url ? (
+ <BackgroundImage
+ onImageDelete={onImageDelete}
+ backgroundImage={backgroundImage}
+ />
+ ) : (
+ <div className={classes.PhotosIconContainer}>
+ <AddPhotoIcon className={classes.PhotosIcon} />
+ </div>
+ )}
</CardContent>
</Card>
);
diff --git a/saleor/static/dashboard-next/categories/components/CategoryCreatePage/CategoryCreatePage.tsx b/saleor/static/dashboard-next/categories/components/CategoryCreatePage/CategoryCreatePage.tsx
index 14561c8b0d..a8622a5c7b 100644
--- a/saleor/static/dashboard-next/categories/components/CategoryCreatePage/CategoryCreatePage.tsx
+++ b/saleor/static/dashboard-next/categories/components/CategoryCreatePage/CategoryCreatePage.tsx
@@ -9,7 +9,7 @@ import { UserError } from "../../../";
import i18n from "../../../i18n";
import CategoryDetailsForm from "../../components/CategoryDetailsForm";
import SeoForm from "../../../components/SeoForm";
-import CategoryBackground from "../CategoryBackground";
+// import CategoryBackground from "../CategoryBackground";
import CategoryCreateSubcategories from "../CategoryCreateSubcategories";
import Toggle from "../../../components/Toggle";
import CategoryDeleteDialog from "../../components/CategoryDeleteDialog";
@@ -57,7 +57,7 @@ export const CategoryCreatePage = decorate<CategoryCreatePageProps>(
onSubmit,
onBack,
errors: userErrors,
- onImageUpload,
+ // onImageUpload,
onDelete,
saveButtonBarState
}) => {
@@ -66,7 +66,6 @@ export const CategoryCreatePage = decorate<CategoryCreatePageProps>(
description: "",
seoTitle: "",
seoDescription: ""
- // backgroundImage: ""
};
return (
<Toggle>
@@ -82,12 +81,11 @@ export const CategoryCreatePage = decorate<CategoryCreatePageProps>(
data={data}
onChange={change}
errors={errors}
- loading={false}
/>
- <CategoryBackground
+ {/* <CategoryBackground
onImageUpload={onImageUpload}
disabled={disabled}
- />
+ /> */}
<CategoryCreateSubcategories disabled={disabled} />
<SeoForm
helperText={i18n.t(
@@ -117,7 +115,6 @@ export const CategoryCreatePage = decorate<CategoryCreatePageProps>(
<CategoryDeleteDialog
name={undefined}
open={openedDeleteDialog}
- productCount={undefined}
onClose={toggleDeleteDialog}
onConfirm={onDelete}
/>
diff --git a/saleor/static/dashboard-next/categories/components/CategoryDetailsForm/CategoryDetailsForm.tsx b/saleor/static/dashboard-next/categories/components/CategoryDetailsForm/CategoryDetailsForm.tsx
index a1b38683d7..03243e9c57 100644
--- a/saleor/static/dashboard-next/categories/components/CategoryDetailsForm/CategoryDetailsForm.tsx
+++ b/saleor/static/dashboard-next/categories/components/CategoryDetailsForm/CategoryDetailsForm.tsx
@@ -8,7 +8,6 @@ import TextField from "@material-ui/core/TextField";
import CardTitle from "../../../components/CardTitle";
import FormSpacer from "../../../components/FormSpacer";
import { RichTextEditor } from "../../../components/RichTextEditor";
-import Skeleton from "../../../components/Skeleton";
import i18n from "../../../i18n";
@@ -20,7 +19,6 @@ interface CategoryDetailsFormProps {
disabled: boolean;
errors: { [key: string]: string };
onChange(event: any);
- loading?: boolean;
}
const decorate = withStyles(theme => ({
@@ -32,43 +30,39 @@ const decorate = withStyles(theme => ({
}));
export const CategoryDetailsForm = decorate<CategoryDetailsFormProps>(
- ({ classes, disabled, data, onChange, errors, loading }) => {
+ ({ classes, disabled, data, onChange, errors }) => {
return (
<Card>
<CardTitle title={i18n.t("General information")} />
<CardContent>
- {loading ? (
- <Skeleton />
- ) : (
- <>
- <div className={classes.root}>
- <TextField
- label={i18n.t("Name")}
- name="name"
- disabled={disabled}
- value={data.name}
- onChange={onChange}
- error={!!errors.name}
- helperText={errors.name}
- />
- </div>
- <FormSpacer />
- <RichTextEditor
+ <>
+ <div className={classes.root}>
+ <TextField
+ label={i18n.t("Name")}
+ name="name"
disabled={disabled}
- label={i18n.t("Description")}
- fullWidth
- helperText={
- errors.description
- ? errors.description
- : i18n.t("Select text to enable text-formatting tools.")
- }
- name="description"
- value={data.description}
+ value={data.name}
onChange={onChange}
- error={!!errors.description}
+ error={!!errors.name}
+ helperText={errors.name}
/>
- </>
- )}
+ </div>
+ <FormSpacer />
+ <RichTextEditor
+ disabled={disabled}
+ label={i18n.t("Description")}
+ fullWidth
+ helperText={
+ errors.description
+ ? errors.description
+ : i18n.t("Select text to enable text-formatting tools.")
+ }
+ name="description"
+ value={data.description}
+ onChange={onChange}
+ error={!!errors.description}
+ />
+ </>
</CardContent>
</Card>
);
diff --git a/saleor/static/dashboard-next/categories/components/CategoryDetailsPage/CategoryDetailsPage.tsx b/saleor/static/dashboard-next/categories/components/CategoryDetailsPage/CategoryDetailsPage.tsx
index 4d5d8cd0dc..3d4f4ed97f 100644
--- a/saleor/static/dashboard-next/categories/components/CategoryDetailsPage/CategoryDetailsPage.tsx
+++ b/saleor/static/dashboard-next/categories/components/CategoryDetailsPage/CategoryDetailsPage.tsx
@@ -42,7 +42,6 @@ interface CategoryDetailsPageProps {
hasNextPage: boolean;
hasPreviousPage: boolean;
};
- productCount?: number;
loading?: boolean;
onAddCategory?();
onAddProduct?();
@@ -86,7 +85,6 @@ const CategoryDetailsPage = decorate<CategoryDetailsPageProps>(
classes,
loading,
pageInfo,
- productCount,
products,
subcategories,
onAddCategory,
@@ -174,7 +172,6 @@ const CategoryDetailsPage = decorate<CategoryDetailsPageProps>(
<CategoryDeleteDialog
name={category.name}
open={opened}
- productCount={productCount}
onClose={toggleDialog}
onConfirm={onDelete}
/>
diff --git a/saleor/static/dashboard-next/categories/components/CategoryUpdatePage/CategoryUpdatePage.tsx b/saleor/static/dashboard-next/categories/components/CategoryUpdatePage/CategoryUpdatePage.tsx
index cc515d0178..acb9245b64 100644
--- a/saleor/static/dashboard-next/categories/components/CategoryUpdatePage/CategoryUpdatePage.tsx
+++ b/saleor/static/dashboard-next/categories/components/CategoryUpdatePage/CategoryUpdatePage.tsx
@@ -33,12 +33,17 @@ interface FormData {
export interface CategoryUpdatePageProps {
errors: UserError[];
disabled: boolean;
+ placeholderImage?: string;
category: {
SeoDescription: string;
SeoTitle: string;
name: string;
description: string;
};
+ backgroundImage?: {
+ id?: string;
+ url?: string;
+ };
products: Array<{
id: string;
name: string;
@@ -75,7 +80,8 @@ export interface CategoryUpdatePageProps {
onAddProduct?();
onBack?();
onDelete?();
- onAddCategory?();
+ onImageDelete?: (id: string) => () => void;
+ onAddCategory();
onCategoryClick?(id: string): () => void;
}
@@ -110,7 +116,10 @@ export const CategoryUpdatePage = decorate<CategoryUpdatePageProps>(
onSubmit,
onBack,
onAddCategory,
- onCategoryClick
+ onCategoryClick,
+ onImageDelete,
+ placeholderImage,
+ backgroundImage
}) => {
const initialData = category
? {
@@ -161,11 +170,12 @@ export const CategoryUpdatePage = decorate<CategoryUpdatePageProps>(
disabled={disabled}
errors={errors}
onChange={change}
- loading={loading}
/>
<CategoryBackground
onImageUpload={onImageUpload}
- disabled={disabled}
+ onImageDelete={onImageDelete}
+ backgroundImage={backgroundImage}
+ placeholderImage={placeholderImage}
/>
<SeoForm
helperText={i18n.t(
@@ -258,8 +268,8 @@ export const CategoryUpdatePage = decorate<CategoryUpdatePageProps>(
<DialogContentText
dangerouslySetInnerHTML={{
__html: i18n.t(
- "Are you sure you want to remove <strong>{{ name }}</strong>?",
- { name: category.name }
+ "Are you sure you want to remove <strong>{{ name }}</strong>?"
+ // { name: category.name }
)
}}
/>
diff --git a/saleor/static/dashboard-next/categories/fixtures.ts b/saleor/static/dashboard-next/categories/fixtures.ts
index b65b3f8887..1b01e4a4db 100644
--- a/saleor/static/dashboard-next/categories/fixtures.ts
+++ b/saleor/static/dashboard-next/categories/fixtures.ts
@@ -71,6 +71,10 @@ export const category = (placeholderImage: string) => ({
"Across pressure PM food discover recognize. Send letter reach listen. Quickly work plan rule.\nTell lose part purpose do when. Whatever drug contain particularly defense.",
id: "c1",
name: "Apparel",
+ backgroundImage: {
+ id: "UPJvZHVjfEs4YWdlOjV",
+ url: placeholderImage
+ },
products: products(placeholderImage)
});
export const errors = [
diff --git a/saleor/static/dashboard-next/storybook/stories/categories/CategoryDeleteDialog.tsx b/saleor/static/dashboard-next/storybook/stories/categories/CategoryDeleteDialog.tsx
index 8e1fd6b463..8c19f21fd4 100644
--- a/saleor/static/dashboard-next/storybook/stories/categories/CategoryDeleteDialog.tsx
+++ b/saleor/static/dashboard-next/storybook/stories/categories/CategoryDeleteDialog.tsx
@@ -15,14 +15,12 @@ storiesOf("Categories / CategoryDeleteDialog", module)
open={true}
onClose={undefined}
onConfirm={undefined}
- productCount={0}
/>
))
.add("with products", () => (
<CategoryDeleteDialog
name={category.name}
open={true}
- productCount={100}
onClose={undefined}
onConfirm={undefined}
/>
diff --git a/saleor/static/dashboard-next/storybook/stories/categories/CategoryUpdatePage.tsx b/saleor/static/dashboard-next/storybook/stories/categories/CategoryUpdatePage.tsx
index 31570cb1c3..7622327835 100644
--- a/saleor/static/dashboard-next/storybook/stories/categories/CategoryUpdatePage.tsx
+++ b/saleor/static/dashboard-next/storybook/stories/categories/CategoryUpdatePage.tsx
@@ -1,5 +1,6 @@
import { storiesOf } from "@storybook/react";
import * as React from "react";
+import * as placeholderImage from "../../../../images/placeholder255x255.png";
import { category as categoryFixture } from "../../../categories/fixtures";
@@ -8,11 +9,13 @@ import CategoryUpdatePage, {
} from "../../../categories/components/CategoryUpdatePage";
import Decorator from "../../Decorator";
-const category = categoryFixture("");
+const category = categoryFixture(placeholderImage);
const updateProps: CategoryUpdatePageProps = {
category: category,
subcategories: category.children,
+ backgroundImage: category.backgroundImage,
+ placeholderImage: placeholderImage,
disabled: false,
errors: [],
products: category.products,
@@ -23,6 +26,7 @@ const updateProps: CategoryUpdatePageProps = {
},
onNextPage: undefined,
onPreviousPage: undefined,
+ onImageDelete: () => undefined,
onProductClick: () => undefined,
onAddProduct: undefined,
onCategoryClick: () => undefined,
@@ -34,6 +38,9 @@ const updateProps: CategoryUpdatePageProps = {
storiesOf("Views / Categories / Update category", module)
.addDecorator(Decorator)
.add("default", () => <CategoryUpdatePage {...updateProps} />)
+ .add("default without background", () => (
+ <CategoryUpdatePage {...updateProps} backgroundImage={{}} />
+ ))
.add("When in root", () => (
<CategoryUpdatePage
{...updateProps}
@@ -48,5 +55,7 @@ storiesOf("Views / Categories / Update category", module)
disabled={true}
products={undefined}
loading={true}
+ category={undefined}
+ backgroundImage={undefined}
/>
));