Skip to content

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

Add attribute editing

dominik-zeglenf7d6fe3ac863
diff --git a/saleor/static/dashboard-next/components/Form/Form.tsx b/saleor/static/dashboard-next/components/Form/Form.tsx
index 1e97a8de7e..ea26a73c1c 100644
--- a/saleor/static/dashboard-next/components/Form/Form.tsx
+++ b/saleor/static/dashboard-next/components/Form/Form.tsx
@@ -43,6 +43,7 @@ class Form<T extends {} = {}> extends React.Component<FormProps<T>, T> {
handleSubmit = (event?: React.FormEvent<any>) => {
const { onSubmit } = this.props;
if (event) {
+ event.stopPropagation();
event.preventDefault();
}
if (onSubmit !== undefined) {
diff --git a/saleor/static/dashboard-next/components/ListField/ListField.tsx b/saleor/static/dashboard-next/components/ListField/ListField.tsx
new file mode 100644
index 0000000000..0153155a65
--- /dev/null
+++ b/saleor/static/dashboard-next/components/ListField/ListField.tsx
@@ -0,0 +1,124 @@
+import Button from "@material-ui/core/Button";
+import { withStyles, WithStyles } from "@material-ui/core/styles";
+import TextField, { TextFieldProps } from "@material-ui/core/TextField";
+import Typography from "@material-ui/core/Typography";
+import CloseIcon from "@material-ui/icons/Close";
+import * as React from "react";
+import i18n from "../../i18n";
+
+interface ListFieldProps
+ extends Exclude<Exclude<TextFieldProps, "value">, "classes"> {
+ values: string[];
+}
+
+interface ListFieldState {
+ value: string;
+}
+
+const decorate = withStyles(theme => ({
+ chip: {
+ background: "rgba(90, 179, 120, .1)",
+ borderRadius: 8,
+ display: "inline-block" as "inline-block",
+ marginRight: theme.spacing.unit * 2,
+ padding: "4px 8px"
+ },
+ chipContainer: {
+ marginTop: theme.spacing.unit * 2,
+ width: 552
+ },
+ closeIcon: {
+ cursor: "pointer" as "pointer",
+ fontSize: 16,
+ verticalAlign: "middle" as "middle"
+ }
+}));
+const ListField = decorate<
+ ListFieldProps & WithStyles<"chip" | "chipContainer" | "closeIcon">
+>(
+ class ListFieldComponent extends React.Component<
+ ListFieldProps & WithStyles<"chip" | "chipContainer" | "closeIcon">,
+ ListFieldState
+ > {
+ state: ListFieldState = {
+ value: ""
+ };
+ handleFieldChange = (event: React.ChangeEvent<any>) =>
+ this.setState({
+ value: event.target.value
+ });
+
+ handleFieldSubmit = (event: React.KeyboardEvent<any>) => {
+ // Check if pressed 'enter' key
+ if (event.keyCode === 13) {
+ if (this.state.value !== "") {
+ event.preventDefault();
+ event.stopPropagation();
+ this.handleValueAdd();
+ }
+ }
+ };
+
+ handleValueAdd = () => {
+ if (this.state.value !== "") {
+ this.props.onChange({
+ target: {
+ name: this.props.name,
+ value: [...this.props.values, this.state.value]
+ }
+ } as any);
+ this.setState({ value: "" });
+ }
+ };
+
+ handleValueRemove = (index: number) =>
+ this.props.onChange({
+ target: {
+ name: this.props.name,
+ value: this.props.values
+ .slice(0, index)
+ .concat(this.props.values.slice(index + 1, -1))
+ }
+ } as any);
+
+ render() {
+ const { classes, values, onChange, ...props } = this.props;
+ return (
+ <div>
+ <TextField
+ {...props}
+ InputProps={{
+ endAdornment: (
+ <Button
+ variant="flat"
+ color="secondary"
+ onClick={this.handleValueAdd}
+ >
+ {i18n.t("Add", { context: "button" })}
+ </Button>
+ )
+ }}
+ value={this.state.value}
+ onChange={this.handleFieldChange}
+ onKeyDown={this.handleFieldSubmit}
+ />
+ <div className={classes.chipContainer}>
+ {values.map((value, valueIndex) => (
+ <div className={classes.chip} key={valueIndex}>
+ <Typography variant="caption">
+ {value}{" "}
+ <CloseIcon
+ className={classes.closeIcon}
+ onClick={() => this.handleValueRemove(valueIndex)}
+ />
+ </Typography>
+ </div>
+ ))}
+ </div>
+ </div>
+ );
+ }
+ }
+);
+ListField.displayName = "ListField";
+export default ListField;
diff --git a/saleor/static/dashboard-next/components/ListField/index.ts b/saleor/static/dashboard-next/components/ListField/index.ts
new file mode 100644
index 0000000000..85b671d737
--- /dev/null
+++ b/saleor/static/dashboard-next/components/ListField/index.ts
@@ -0,0 +1,2 @@
+export { default } from "./ListField";
+export * from "./ListField";
diff --git a/saleor/static/dashboard-next/productTypes/components/ProductTypeAttributeEditDialog/ProductTypeAttributeEditDialog.tsx b/saleor/static/dashboard-next/productTypes/components/ProductTypeAttributeEditDialog/ProductTypeAttributeEditDialog.tsx
new file mode 100644
index 0000000000..539cfc9f23
--- /dev/null
+++ b/saleor/static/dashboard-next/productTypes/components/ProductTypeAttributeEditDialog/ProductTypeAttributeEditDialog.tsx
@@ -0,0 +1,76 @@
+import Button from "@material-ui/core/Button";
+import Dialog from "@material-ui/core/Dialog";
+import DialogActions from "@material-ui/core/DialogActions";
+import DialogContent from "@material-ui/core/DialogContent";
+import DialogTitle from "@material-ui/core/DialogTitle";
+import { withStyles } from "@material-ui/core/styles";
+import TextField from "@material-ui/core/TextField";
+import * as React from "react";
+
+import Form from "../../../components/Form";
+import { FormSpacer } from "../../../components/FormSpacer";
+import ListField from "../../../components/ListField/ListField";
+import i18n from "../../../i18n";
+
+interface FormData {
+ name: string;
+ values: string[];
+}
+
+interface ProductTypeAttributeEditDialogProps {
+ name: string;
+ opened: boolean;
+ title: string;
+ values: string[];
+ onClose: () => void;
+ onConfirm: (data: FormData) => void;
+}
+
+const decorate = withStyles({ root: {} });
+const ProductTypeAttributeEditDialog = decorate<
+ ProductTypeAttributeEditDialogProps
+>(({ name, opened, title, values, onClose, onConfirm }) => {
+ const initialForm: FormData = {
+ name: name || "",
+ values: values || []
+ };
+ return (
+ <Dialog open={opened}>
+ <Form initial={initialForm} onSubmit={onConfirm}>
+ {({ change, data }) => (
+ <>
+ <DialogTitle>{title}</DialogTitle>
+ <DialogContent>
+ <TextField
+ fullWidth
+ label={i18n.t("Attribute name")}
+ name="name"
+ value={data.name}
+ onChange={change}
+ />
+ <FormSpacer />
+ <ListField
+ autoComplete="off"
+ fullWidth
+ name="values"
+ label={i18n.t("Attribute values")}
+ values={data.values}
+ onChange={change}
+ />
+ </DialogContent>
+ <DialogActions>
+ <Button onClick={onClose}>
+ {i18n.t("Cancel", { context: "button" })}
+ </Button>
+ <Button color="primary" variant="raised" type="submit">
+ {i18n.t("Save", { context: "button" })}
+ </Button>
+ </DialogActions>
+ </>
+ )}
+ </Form>
+ </Dialog>
+ );
+});
+ProductTypeAttributeEditDialog.displayName = "ProductTypeAttributeEditDialog";
+export default ProductTypeAttributeEditDialog;
diff --git a/saleor/static/dashboard-next/productTypes/components/ProductTypeAttributeEditDialog/index.ts b/saleor/static/dashboard-next/productTypes/components/ProductTypeAttributeEditDialog/index.ts
new file mode 100644
index 0000000000..6344b380f1
--- /dev/null
+++ b/saleor/static/dashboard-next/productTypes/components/ProductTypeAttributeEditDialog/index.ts
@@ -0,0 +1,2 @@
+export { default } from "./ProductTypeAttributeEditDialog";
+export * from "./ProductTypeAttributeEditDialog";
diff --git a/saleor/static/dashboard-next/productTypes/components/ProductTypeAttributes/ProductTypeAttributes.tsx b/saleor/static/dashboard-next/productTypes/components/ProductTypeAttributes/ProductTypeAttributes.tsx
index 2ea27a294f..d9b833d2ac 100644
--- a/saleor/static/dashboard-next/productTypes/components/ProductTypeAttributes/ProductTypeAttributes.tsx
+++ b/saleor/static/dashboard-next/productTypes/components/ProductTypeAttributes/ProductTypeAttributes.tsx
@@ -9,12 +9,14 @@ import * as React from "react";
import CardTitle from "../../../components/CardTitle";
import Skeleton from "../../../components/Skeleton";
+import Toggle from "../../../components/Toggle";
import i18n from "../../../i18n";
import { maybe, renderCollection } from "../../../misc";
import {
ProductTypeDetails_productType_productAttributes_edges_node,
ProductTypeDetails_productType_variantAttributes_edges_node
} from "../../types/ProductTypeDetails";
+import ProductTypeAttributeEditDialog from "../ProductTypeAttributeEditDialog/ProductTypeAttributeEditDialog";
interface ProductTypeAttributesProps {
attributes:
@@ -48,22 +50,46 @@ const ProductTypeAttributes = decorate<ProductTypeAttributesProps>(
{renderCollection(
attributes,
attribute => (
- <TableRow
- className={!!attribute ? classes.link : undefined}
- hover={!!attribute}
- key={maybe(() => attribute.id)}
- >
- <TableCell>
- {maybe(() => attribute.name) ? attribute.name : <Skeleton />}
- </TableCell>
- <TableCell className={classes.textLeft}>
- {maybe(() => attribute.values) !== undefined ? (
- attribute.values.map(value => value.name).join(", ")
- ) : (
- <Skeleton />
- )}
- </TableCell>
- </TableRow>
+ <Toggle key={maybe(() => attribute.id)}>
+ {(
+ openedAttributeEditDialog,
+ { toggle: toggleAttributeEditDialog }
+ ) => (
+ <>
+ <TableRow
+ className={!!attribute ? classes.link : undefined}
+ hover={!!attribute}
+ onClick={toggleAttributeEditDialog}
+ key={maybe(() => attribute.id)}
+ >
+ <TableCell>
+ {maybe(() => attribute.name) ? (
+ attribute.name
+ ) : (
+ <Skeleton />
+ )}
+ </TableCell>
+ <TableCell className={classes.textLeft}>
+ {maybe(() => attribute.values) !== undefined ? (
+ attribute.values.map(value => value.name).join(", ")
+ ) : (
+ <Skeleton />
+ )}
+ </TableCell>
+ </TableRow>
+ <ProductTypeAttributeEditDialog
+ name={maybe(() => attribute.name)}
+ opened={openedAttributeEditDialog}
+ title={i18n.t("Edit attribute")}
+ values={maybe(() =>
+ attribute.values.map(value => value.name)
+ )}
+ onClose={toggleAttributeEditDialog}
+ onConfirm={undefined}
+ />
+ </>
+ )}
+ </Toggle>
),
() => (
<TableRow>