Skip to content

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

Allow in-memory data manipulation

dominik-zeglenf4ed61445101
diff --git a/saleor/static/dashboard-next/attributes/components/AttributeDetailsPage/AttributeDetailsPage.tsx b/saleor/static/dashboard-next/attributes/components/AttributeDetailsPage/AttributeDetailsPage.tsx
index 834b4d2961..d31bd1bca0 100644
--- a/saleor/static/dashboard-next/attributes/components/AttributeDetailsPage/AttributeDetailsPage.tsx
+++ b/saleor/static/dashboard-next/attributes/components/AttributeDetailsPage/AttributeDetailsPage.tsx
@@ -32,10 +32,6 @@ interface AttributeDetailsPageProps {
onBack: () => void;
onDelete: () => void;
onSubmit: () => void;
- onValueAdd: () => void;
- onValueDelete: (id: string) => () => void;
- onValueEdit: (id: string) => (name: string) => () => void;
- onValueReorder: (event: { oldIndex: number; newIndex: number }) => void;
}
const decorate = withStyles(theme => ({
@@ -53,18 +49,18 @@ const AttributeDetailsPage = decorate<AttributeDetailsPageProps>(
saveButtonBarState,
onBack,
onDelete,
- onSubmit,
- onValueAdd,
- onValueDelete,
- onValueEdit,
- onValueReorder
+ onSubmit
}) => (
<Toggle>
{(openedDeleteDialog, { toggle: toggleDeleteDialog }) => (
<Form
initial={{
name:
- attribute && attribute.name !== undefined ? attribute.name : ""
+ attribute && attribute.name !== undefined ? attribute.name : "",
+ values:
+ attribute && attribute.values !== undefined
+ ? attribute.values
+ : []
}}
onSubmit={onSubmit}
>
@@ -88,11 +84,8 @@ const AttributeDetailsPage = decorate<AttributeDetailsPageProps>(
/>
<AttributeValueList
disabled={disabled}
- values={attribute ? attribute.values : undefined}
- onAdd={onValueAdd}
- onDelete={onValueDelete}
- onEdit={onValueEdit}
- onReorder={onValueReorder}
+ values={data.values}
+ onChange={change}
/>
</div>
</div>
diff --git a/saleor/static/dashboard-next/attributes/components/AttributeValueList/AttributeValueList.tsx b/saleor/static/dashboard-next/attributes/components/AttributeValueList/AttributeValueList.tsx
index 740749fd57..3ac150f5f0 100644
--- a/saleor/static/dashboard-next/attributes/components/AttributeValueList/AttributeValueList.tsx
+++ b/saleor/static/dashboard-next/attributes/components/AttributeValueList/AttributeValueList.tsx
@@ -6,12 +6,9 @@ import TableBody from "@material-ui/core/TableBody";
import TableCell from "@material-ui/core/TableCell";
import TableHead from "@material-ui/core/TableHead";
import TableRow from "@material-ui/core/TableRow";
-import Typography from "@material-ui/core/Typography";
import AddIcon from "@material-ui/icons/Add";
import CloseIcon from "@material-ui/icons/Close";
-import ReorderIcon from "@material-ui/icons/Reorder";
import * as React from "react";
-import { SortableContainer, SortableElement } from "react-sortable-hoc";
import Skeleton from "../../../components/Skeleton";
import { EditableTableCell } from "../../../components/EditableTableCell/EditableTableCell";
@@ -23,13 +20,9 @@ interface AttributeValueListProps {
values: Array<{
id: string;
name: string;
- sortOrder: number;
- slug: string;
+ isNew?: boolean;
}>;
- onAdd: () => void;
- onDelete: (id: string) => () => void;
- onEdit: (id: string) => (name: string) => () => void;
- onReorder: (event: { oldIndex: number; newIndex: number }) => void;
+ onChange: (event: React.ChangeEvent<any>) => void;
}
const decorate = withStyles(theme => ({
@@ -38,76 +31,103 @@ const decorate = withStyles(theme => ({
paddingRight: "0 !important",
width: theme.spacing.unit * 7
},
- reorderIcon: {
- cursor: "move" as "move",
- paddingLeft: theme.spacing.unit * 2,
- paddingRight: 0,
- width: theme.spacing.unit * 9
+ edit: {
+ width: "unset"
},
root: {
marginTop: theme.spacing.unit * 2,
- [theme.breakpoints.down("md")]: { marginTop: theme.spacing.unit }
+ overflow: "visible",
+ [theme.breakpoints.down("md")]: {
+ marginTop: theme.spacing.unit
+ }
}
}));
const AttributeValueList = decorate<AttributeValueListProps>(
- ({ classes, disabled, values, onAdd, onDelete, onEdit, onReorder }) => (
- <Card className={classes.root}>
- <PageHeader title={i18n.t("Attribute values")}>
- <IconButton disabled={disabled} onClick={onAdd}>
- <AddIcon />
- </IconButton>
- </PageHeader>
- <Table>
- <TableHead>
- <TableRow>
- <TableCell className={classes.reorderIcon} />
- <TableCell>{i18n.t("Name")}</TableCell>
- <TableCell className={classes.deleteIcon} />
- </TableRow>
- </TableHead>
- <TableBody>
- {values === undefined ? (
+ ({ classes, disabled, values, onChange }) => {
+ const defaultValue = i18n.t("New value");
+ const handleAdd = () =>
+ onChange({
+ target: {
+ name: "values",
+ value: [
+ ...values,
+ {
+ id: "new-" + values.length,
+ isNew: true,
+ name: defaultValue
+ }
+ ]
+ }
+ } as any);
+ const handleEdit = (id: string) => (name: string) =>
+ onChange({
+ target: {
+ name: "values",
+ value: values.map(v => (v.id === id ? { id, name } : v))
+ }
+ } as any);
+ const handleDelete = (id: string) => () =>
+ onChange({
+ target: { name: "values", value: values.filter(v => v.id !== id) }
+ } as any);
+ return (
+ <Card className={classes.root}>
+ <PageHeader title={i18n.t("Attribute values")}>
+ <IconButton disabled={disabled} onClick={handleAdd}>
+ <AddIcon />
+ </IconButton>
+ </PageHeader>
+ <Table>
+ <TableHead>
<TableRow>
- <TableCell className={classes.reorderIcon}>
- <ReorderIcon />
- </TableCell>
- <TableCell>
- <Skeleton />
- </TableCell>
- <TableCell className={classes.deleteIcon}>
- <IconButton disabled={disabled}>
- <CloseIcon />
- </IconButton>
- </TableCell>
+ <TableCell>{i18n.t("Name")}</TableCell>
+ <TableCell className={classes.deleteIcon} />
</TableRow>
- ) : values.length > 0 ? (
- values.map(value => (
+ </TableHead>
+ <TableBody>
+ {values === undefined ? (
<TableRow>
- <TableCell className={classes.reorderIcon}>
- <ReorderIcon />
+ <TableCell>
+ <Skeleton />
</TableCell>
- <EditableTableCell
- value={value.name}
- onConfirm={onEdit(value.id)}
- />
<TableCell className={classes.deleteIcon}>
- <IconButton disabled={disabled} onClick={onDelete(value.id)}>
+ <IconButton disabled={disabled}>
<CloseIcon />
</IconButton>
</TableCell>
</TableRow>
- ))
- ) : (
- <TableRow>
- <TableCell colSpan={3}>
- {i18n.t("No attribute values found")}
- </TableCell>
- </TableRow>
- )}
- </TableBody>
- </Table>
- </Card>
- )
+ ) : values.length > 0 ? (
+ values.map(value => (
+ <TableRow>
+ <EditableTableCell
+ classes={{ root: classes.edit }}
+ focused={value.isNew}
+ defaultValue={defaultValue}
+ value={value.name}
+ onConfirm={handleEdit(value.id)}
+ />
+ <TableCell className={classes.deleteIcon}>
+ <IconButton
+ disabled={disabled}
+ onClick={handleDelete(value.id)}
+ >
+ <CloseIcon />
+ </IconButton>
+ </TableCell>
+ </TableRow>
+ ))
+ ) : (
+ <TableRow>
+ <TableCell colSpan={2}>
+ {i18n.t("No attribute values found")}
+ </TableCell>
+ </TableRow>
+ )}
+ </TableBody>
+ </Table>
+ </Card>
+ );
+ }
);
AttributeValueList.displayName = "AttributeValueList";
export default AttributeValueList;
diff --git a/saleor/static/dashboard-next/components/EditableTableCell/EditableTableCell.tsx b/saleor/static/dashboard-next/components/EditableTableCell/EditableTableCell.tsx
index ad134f58df..dc7406b3cd 100644
--- a/saleor/static/dashboard-next/components/EditableTableCell/EditableTableCell.tsx
+++ b/saleor/static/dashboard-next/components/EditableTableCell/EditableTableCell.tsx
@@ -11,9 +11,11 @@ import Toggle from "../../components/Toggle";
interface EditableTableCellProps {
className?: string;
+ defaultValue?: string;
+ focused?: boolean;
InputProps?: TextFieldProps;
value: string;
- onConfirm(value: string): () => void;
+ onConfirm(value: string): any;
}
const decorate = withStyles(theme => ({
@@ -42,9 +44,17 @@ const decorate = withStyles(theme => ({
}
}));
export const EditableTableCell = decorate<EditableTableCellProps>(
- ({ classes, className, InputProps, value, onConfirm }) => (
+ ({
+ classes,
+ className,
+ defaultValue,
+ focused,
+ InputProps,
+ value,
+ onConfirm
+ }) => (
<TableCell className={[classes.container, className].join(" ")}>
- <Toggle>
+ <Toggle initial={focused}>
{(opened, { enable, disable }) => {
const handleConfirm = (data: { value: string }) => {
disable();
@@ -53,7 +63,11 @@ export const EditableTableCell = decorate<EditableTableCellProps>(
return (
<>
{opened && <div className={classes.overlay} onClick={disable} />}
- <Form initial={{ value }} onSubmit={handleConfirm}>
+ <Form
+ initial={{ value }}
+ onSubmit={handleConfirm}
+ useForm={false}
+ >
{({ change, data, submit }) => (
<>
<Typography
@@ -61,7 +75,7 @@ export const EditableTableCell = decorate<EditableTableCellProps>(
onClick={enable}
className={classes.text}
>
- {value}
+ {value || defaultValue}
</Typography>
{opened && (
<div className={classes.root}>
diff --git a/saleor/static/dashboard-next/components/Form/Form.tsx b/saleor/static/dashboard-next/components/Form/Form.tsx
index 39d43a76a3..968fdcf4dc 100644
--- a/saleor/static/dashboard-next/components/Form/Form.tsx
+++ b/saleor/static/dashboard-next/components/Form/Form.tsx
@@ -12,6 +12,7 @@ export interface FormProps<T extends {}> {
) => React.ReactElement<any>)
| React.ReactNode;
initial?: T;
+ useForm?: boolean;
onSubmit?(data: T);
}
@@ -37,7 +38,16 @@ class Form<T extends {} = {}> extends React.Component<FormProps<T>, T> {
this.setState(({ [target.name]: target.value } as any) as Pick<T, keyof T>);
};
- handleSubmit = (event: React.FormEvent<any>) => {
+ handleKeyDown = (event: React.KeyboardEvent<any>) => {
+ switch (event.keyCode) {
+ // Enter
+ case 13:
+ this.props.onSubmit(this.state);
+ break;
+ }
+ };
+
+ handleSubmit = (event?: React.FormEvent<any>) => {
const { onSubmit } = this.props;
event.preventDefault();
if (onSubmit !== undefined) {
@@ -46,7 +56,7 @@ class Form<T extends {} = {}> extends React.Component<FormProps<T>, T> {
};
render() {
- const { children } = this.props;
+ const { children, useForm = true } = this.props;
let contents = children;
@@ -59,7 +69,11 @@ class Form<T extends {} = {}> extends React.Component<FormProps<T>, T> {
});
}
- return <form onSubmit={this.handleSubmit}>{contents}</form>;
+ return useForm ? (
+ <form onSubmit={this.handleSubmit}>{contents}</form>
+ ) : (
+ <div onKeyDown={this.handleKeyDown}>{contents}</div>
+ );
}
}
diff --git a/saleor/static/dashboard-next/storybook/stories/attributes/AttributeDetailsPage.tsx b/saleor/static/dashboard-next/storybook/stories/attributes/AttributeDetailsPage.tsx
index 60354fa558..1bd88aad61 100644
--- a/saleor/static/dashboard-next/storybook/stories/attributes/AttributeDetailsPage.tsx
+++ b/saleor/static/dashboard-next/storybook/stories/attributes/AttributeDetailsPage.tsx
@@ -15,10 +15,6 @@ storiesOf("Views / Attributes / Attribute details", module)
onBack={() => {}}
onDelete={() => {}}
onSubmit={() => {}}
- onValueAdd={() => {}}
- onValueDelete={() => () => {}}
- onValueEdit={() => () => () => {}}
- onValueReorder={() => () => {}}
/>
))
.add("when loading", () => (
@@ -28,9 +24,5 @@ storiesOf("Views / Attributes / Attribute details", module)
onBack={() => {}}
onDelete={() => {}}
onSubmit={() => {}}
- onValueAdd={() => {}}
- onValueDelete={() => () => {}}
- onValueEdit={() => () => () => {}}
- onValueReorder={() => () => {}}
/>
));