Skip to content

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

Add menu details page component

dominik-zeglenf1674be62482
diff --git a/package-lock.json b/package-lock.json
index 4eff2558d0..956c48d5e7 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -13626,6 +13626,11 @@
"integrity": "sha1-lpGEQOMEGnpBT4xS48V06zw+HgU=",
"dev": true
},
+ "fast-array-diff": {
+ "version": "0.2.0",
+ "resolved": "https://registry.npmjs.org/fast-array-diff/-/fast-array-diff-0.2.0.tgz",
+ "integrity": "sha1-9T6JEQl0/+lgP2LnpePvcfCvU3k="
+ },
"fast-deep-equal": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/fast-deep-equal/-/fast-deep-equal-1.1.0.tgz",
diff --git a/package.json b/package.json
index f0a2011e13..bc03b69370 100644
--- a/package.json
+++ b/package.json
@@ -48,6 +48,7 @@
"draftail": "^1.1.0",
"draftjs-to-html": "^0.8.4",
"dropzone": "^5.2.0",
+ "fast-array-diff": "^0.2.0",
"fuzzaldrin": "^2.1.0",
"graphql": "^14.0.2",
"graphql-tag": "^2.10.0",
diff --git a/saleor/static/dashboard-next/icons/Draggable.tsx b/saleor/static/dashboard-next/icons/Draggable.tsx
new file mode 100644
index 0000000000..948ef738ed
--- /dev/null
+++ b/saleor/static/dashboard-next/icons/Draggable.tsx
@@ -0,0 +1,31 @@
+import createSvgIcon from "@material-ui/icons/utils/createSvgIcon";
+import * as React from "react";
+
+export const Draggable = createSvgIcon(
+ <>
+ <path
+ fillRule="evenodd"
+ clipRule="evenodd"
+ d="M3.5 2C3.5 2.82843 2.82843 3.5 2 3.5C1.17157 3.5 0.5 2.82843 0.5 2C0.5 1.17157 1.17157 0.5 2 0.5C2.82843 0.5 3.5 1.17157 3.5 2ZM4 2C4 3.10457 3.10457 4 2 4C0.895431 4 0 3.10457 0 2C0 0.895431 0.895431 0 2 0C3.10457 0 4 0.895431 4 2ZM9.5 2C9.5 2.82843 8.82843 3.5 8 3.5C7.17157 3.5 6.5 2.82843 6.5 2C6.5 1.17157 7.17157 0.5 8 0.5C8.82843 0.5 9.5 1.17157 9.5 2ZM10 2C10 3.10457 9.10457 4 8 4C6.89543 4 6 3.10457 6 2C6 0.895431 6.89543 0 8 0C9.10457 0 10 0.895431 10 2ZM8 9.5C8.82843 9.5 9.5 8.82843 9.5 8C9.5 7.17157 8.82843 6.5 8 6.5C7.17157 6.5 6.5 7.17157 6.5 8C6.5 8.82843 7.17157 9.5 8 9.5ZM8 10C9.10457 10 10 9.10457 10 8C10 6.89543 9.10457 6 8 6C6.89543 6 6 6.89543 6 8C6 9.10457 6.89543 10 8 10ZM3.5 8C3.5 8.82843 2.82843 9.5 2 9.5C1.17157 9.5 0.5 8.82843 0.5 8C0.5 7.17157 1.17157 6.5 2 6.5C2.82843 6.5 3.5 7.17157 3.5 8ZM4 8C4 9.10457 3.10457 10 2 10C0.895431 10 0 9.10457 0 8C0 6.89543 0.895431 6 2 6C3.10457 6 4 6.89543 4 8ZM2 15.5C2.82843 15.5 3.5 14.8284 3.5 14C3.5 13.1716 2.82843 12.5 2 12.5C1.17157 12.5 0.5 13.1716 0.5 14C0.5 14.8284 1.17157 15.5 2 15.5ZM2 16C3.10457 16 4 15.1046 4 14C4 12.8954 3.10457 12 2 12C0.895431 12 0 12.8954 0 14C0 15.1046 0.895431 16 2 16ZM9.5 14C9.5 14.8284 8.82843 15.5 8 15.5C7.17157 15.5 6.5 14.8284 6.5 14C6.5 13.1716 7.17157 12.5 8 12.5C8.82843 12.5 9.5 13.1716 9.5 14ZM10 14C10 15.1046 9.10457 16 8 16C6.89543 16 6 15.1046 6 14C6 12.8954 6.89543 12 8 12C9.10457 12 10 12.8954 10 14Z"
+ fill="url(#paint0_linear)"
+ style={{
+ transform: "translate(7px, 4px)"
+ }}
+ />
+ <defs>
+ <linearGradient
+ id="paint0_linear"
+ x1="0"
+ y1="0"
+ x2="16.0896"
+ y2="10.4478"
+ gradientUnits="userSpaceOnUse"
+ >
+ <stop stopColor="#13BEBB" />
+ <stop offset="1" stopColor="#3EE7CD" />
+ </linearGradient>
+ </defs>
+ </>
+);
+Draggable.displayName = "Draggable";
+export default Draggable;
diff --git a/saleor/static/dashboard-next/navigation/components/MenuDetailsPage/MenuDetailsPage.tsx b/saleor/static/dashboard-next/navigation/components/MenuDetailsPage/MenuDetailsPage.tsx
index 0d54791b57..6eeb81002d 100644
--- a/saleor/static/dashboard-next/navigation/components/MenuDetailsPage/MenuDetailsPage.tsx
+++ b/saleor/static/dashboard-next/navigation/components/MenuDetailsPage/MenuDetailsPage.tsx
@@ -5,13 +5,15 @@ import AppHeader from "../../../components/AppHeader";
import CardSpacer from "../../../components/CardSpacer";
import { ConfirmButtonTransitionState } from "../../../components/ConfirmButton";
import Container from "../../../components/Container";
+import Form from "../../../components/Form";
import Grid from "../../../components/Grid";
import SaveButtonBar from "../../../components/SaveButtonBar";
import i18n from "../../../i18n";
+import { maybe } from "../../../misc";
import { MenuDetails_menu } from "../../types/MenuDetails";
+import MenuItems, { TreePermutation } from "../MenuItems";
import MenuProperties from "../MenuProperties";
-import Form from "../../../components/Form";
-import { maybe } from "../../../misc";
+import { computeTree } from "./tree";
export interface MenuDetailsFormData {
name: string;
@@ -38,9 +40,17 @@ const MenuDetailsPage: React.StatelessComponent<MenuDetailsPageProps> = ({
name: maybe(() => menu.name)
};
+ const [treePermutations, setTreePermutations] = React.useState<
+ TreePermutation[]
+ >([]);
+
+ const handleSubmit = () => {
+ onSubmit();
+ };
+
return (
<Form initial={initialForm}>
- {({ change, data, hasChanged, errors: FormErrors }) => (
+ {({ change, data, hasChanged }) => (
<Container>
<AppHeader onBack={onBack}>{i18n.t("Navigation")}</AppHeader>
<Grid variant="inverted">
@@ -59,13 +69,22 @@ const MenuDetailsPage: React.StatelessComponent<MenuDetailsPageProps> = ({
disabled={disabled}
onChange={change}
/>
+ <CardSpacer />
+ <MenuItems
+ items={computeTree(maybe(() => menu.items), treePermutations)}
+ onChange={permutation =>
+ !!permutation
+ ? setTreePermutations([...treePermutations, permutation])
+ : undefined
+ }
+ />
</div>
</Grid>
<SaveButtonBar
disabled={disabled || !hasChanged}
onCancel={onBack}
onDelete={onDelete}
- onSave={onSubmit}
+ onSave={handleSubmit}
state={saveButtonState}
/>
</Container>
diff --git a/saleor/static/dashboard-next/navigation/components/MenuDetailsPage/__snapshots__/tree.test.ts.snap b/saleor/static/dashboard-next/navigation/components/MenuDetailsPage/__snapshots__/tree.test.ts.snap
new file mode 100644
index 0000000000..9e9c6098d7
--- /dev/null
+++ b/saleor/static/dashboard-next/navigation/components/MenuDetailsPage/__snapshots__/tree.test.ts.snap
@@ -0,0 +1,46 @@
+// Jest Snapshot v1, https://goo.gl/fbAQLP
+
+exports[`Properly computes trees # 1`] = `
+"
+Accessories
+··Jewelry
+··Glasses
+Groceries
+Apparel"
+`;
+
+exports[`Properly computes trees # 2`] = `
+"
+Groceries
+··Accessories
+····Jewelry
+······Glasses
+Apparel"
+`;
+
+exports[`Properly computes trees # 3`] = `
+"
+Apparel
+··Groceries
+····Accessories
+······Jewelry
+········Glasses"
+`;
+
+exports[`Properly computes trees # 4`] = `
+"
+Accessories
+Glasses
+Jewelry
+··Groceries
+····Apparel"
+`;
+
+exports[`Properly computes trees # 5`] = `
+"
+Jewelry
+··Groceries
+Glasses
+··Apparel
+····Accessories"
+`;
diff --git a/saleor/static/dashboard-next/navigation/components/MenuDetailsPage/tree.test.ts b/saleor/static/dashboard-next/navigation/components/MenuDetailsPage/tree.test.ts
new file mode 100644
index 0000000000..89bf2c5b6b
--- /dev/null
+++ b/saleor/static/dashboard-next/navigation/components/MenuDetailsPage/tree.test.ts
@@ -0,0 +1,83 @@
+import { menu } from "../../fixtures";
+import { MenuDetails_menu_items } from "../../types/MenuDetails";
+import { computeTree } from "./tree";
+
+// Readability FTW
+function innerTreeToString(
+ tree: MenuDetails_menu_items,
+ level: number
+): string {
+ return (
+ "\n" +
+ "··".repeat(level) +
+ tree.name +
+ tree.children.reduce(
+ (acc, node) => acc + innerTreeToString(node, level + 1),
+ ""
+ )
+ );
+}
+function treeToString(tree: MenuDetails_menu_items[]): string {
+ return tree.reduce((acc, node) => acc + innerTreeToString(node, 0), "");
+}
+
+describe("Properly computes trees", () => {
+ const testTable = [
+ [],
+ [
+ { id: "1glasses", parentId: "0jewelry", sortOrder: 0 },
+ { id: "2accessories", parentId: "3groceries", sortOrder: 0 }
+ ],
+ [
+ { id: "1glasses", parentId: "0jewelry", sortOrder: 0 },
+ { id: "2accessories", parentId: "3groceries", sortOrder: 0 },
+ { id: "3groceries", parentId: "4apparel", sortOrder: 0 }
+ ],
+ [
+ { id: "0jewelry", sortOrder: 1 },
+ { id: "1glasses", sortOrder: 1 },
+ { id: "4apparel", parentId: "3groceries", sortOrder: 0 },
+ { id: "3groceries", parentId: "0jewelry", sortOrder: 0 },
+ { id: "0jewelry", parentId: "1glasses", sortOrder: 0 },
+ { id: "1glasses", parentId: "2accessories", sortOrder: 0 },
+ { id: "1glasses", sortOrder: 1 },
+ { id: "0jewelry", sortOrder: 2 }
+ ],
+ [
+ { id: "1glasses", sortOrder: 1 },
+ { id: "1glasses", parentId: "0jewelry", sortOrder: 0 },
+ { id: "0jewelry", sortOrder: 1 },
+ { id: "0jewelry", parentId: "2accessories", sortOrder: 0 },
+ { id: "3groceries", sortOrder: 0 },
+ { id: "2accessories", parentId: "3groceries", sortOrder: 0 },
+ { id: "2accessories", sortOrder: 1 },
+ { id: "0jewelry", sortOrder: 2 },
+ { id: "1glasses", sortOrder: 3 },
+ { id: "4apparel", sortOrder: 0 },
+ { id: "1glasses", sortOrder: 1 },
+ { id: "2accessories", sortOrder: 0 },
+ { id: "4apparel", parentId: "2accessories", sortOrder: 0 },
+ { id: "3groceries", parentId: "1glasses", sortOrder: 0 },
+ { id: "0jewelry", sortOrder: 0 },
+ { id: "0jewelry", parentId: "2accessories", sortOrder: 0 },
+ { id: "4apparel", parentId: "2accessories", sortOrder: 0 },
+ { id: "0jewelry", parentId: "2accessories", sortOrder: 0 },
+ { id: "1glasses", parentId: "2accessories", sortOrder: 2 },
+ { id: "0jewelry", parentId: "2accessories", sortOrder: 2 },
+ { id: "1glasses", parentId: "2accessories", sortOrder: 2 },
+ { id: "4apparel", parentId: "2accessories", sortOrder: 2 },
+ { id: "3groceries", parentId: "0jewelry", sortOrder: 0 },
+ { id: "4apparel", parentId: "1glasses", sortOrder: 0 },
+ { id: "1glasses", sortOrder: 1 },
+ { id: "0jewelry", sortOrder: 1 },
+ { id: "2accessories", parentId: "4apparel", sortOrder: 0 }
+ ]
+ ];
+
+ testTable.forEach(testData =>
+ it("#", () => {
+ const computedTree = computeTree(menu.items, testData);
+ expect(treeToString(computedTree)).toMatchSnapshot();
+ })
+ );
+});
diff --git a/saleor/static/dashboard-next/navigation/components/MenuDetailsPage/tree.ts b/saleor/static/dashboard-next/navigation/components/MenuDetailsPage/tree.ts
new file mode 100644
index 0000000000..7b229f6d2d
--- /dev/null
+++ b/saleor/static/dashboard-next/navigation/components/MenuDetailsPage/tree.ts
@@ -0,0 +1,93 @@
+import { MenuDetails_menu_items } from "../../types/MenuDetails";
+import { TreePermutation } from "../MenuItems";
+
+function findNode(tree: MenuDetails_menu_items[], id: string): number[] {
+ const foundNodeIndex = tree.findIndex(node => node.id === id);
+ if (tree.length === 0) {
+ return null;
+ }
+ if (foundNodeIndex !== -1) {
+ return [foundNodeIndex];
+ }
+ const nodeMap = tree.map((node, nodeIndex) => [
+ nodeIndex,
+ ...findNode(node.children, id)
+ ]);
+ return nodeMap.find(path => path[path.length - 1] !== null) || [null];
+}
+
+function getNode(
+ tree: MenuDetails_menu_items[],
+ path: number[]
+): MenuDetails_menu_items {
+ if (path.length === 1) {
+ return tree[path[0]];
+ }
+ return getNode([...tree[path[0]].children], path.slice(1));
+}
+
+function removeNode(
+ tree: MenuDetails_menu_items[],
+ path: number[]
+): MenuDetails_menu_items[] {
+ if (path.length === 1) {
+ const removeIndex = path[0];
+ return [...tree.slice(0, removeIndex), ...tree.slice(removeIndex + 1)];
+ }
+ tree[path[0]].children = removeNode(tree[path[0]].children, path.slice(1));
+ return tree;
+}
+
+function insertNode(
+ tree: MenuDetails_menu_items[],
+ path: number[],
+ node: MenuDetails_menu_items,
+ position: number
+): MenuDetails_menu_items[] {
+ if (path.length === 0) {
+ return [...tree.slice(0, position), node, ...tree.slice(position)];
+ }
+
+ tree[path[0]].children = insertNode(
+ tree[path[0]].children,
+ path.slice(1),
+ node,
+ position
+ );
+ return tree;
+}
+
+function permuteNode(
+ tree: MenuDetails_menu_items[],
+ permutation: TreePermutation
+): MenuDetails_menu_items[] {
+ const sourcePath = findNode(tree, permutation.id);
+ const node = getNode(tree, sourcePath);
+
+ const treeAfterRemoval = removeNode(tree, sourcePath);
+
+ const targetPath = permutation.parentId
+ ? findNode(treeAfterRemoval, permutation.parentId)
+ : [];
+
+ const treeAfterInsertion = insertNode(
+ treeAfterRemoval,
+ targetPath,
+ node,
+ permutation.sortOrder
+ );
+
+ return treeAfterInsertion;
+}
+
+export function computeTree(
+ tree: MenuDetails_menu_items[],
+ permutations: TreePermutation[]
+) {
+ // console.log(JSON.stringify(permutations));
+ const newTree = permutations.reduce(
+ (acc, permutation) => permuteNode(acc, permutation),
+ JSON.parse(JSON.stringify(tree))
+ );
+ return newTree;
+}
diff --git a/saleor/static/dashboard-next/navigation/components/MenuItems/MenuItems.tsx b/saleor/static/dashboard-next/navigation/components/MenuItems/MenuItems.tsx
new file mode 100644
index 0000000000..bfc14ac289
--- /dev/null
+++ b/saleor/static/dashboard-next/navigation/components/MenuItems/MenuItems.tsx
@@ -0,0 +1,126 @@
+import Card from "@material-ui/core/Card";
+import Paper from "@material-ui/core/Paper";
+import {
+ createStyles,
+ Theme,
+ withStyles,
+ WithStyles
+} from "@material-ui/core/styles";
+import Typography from "@material-ui/core/Typography";
+import * as React from "react";
+import SortableTree, { NodeRendererProps } from "react-sortable-tree";
+
+import CardTitle from "../../../components/CardTitle";
+import i18n from "../../../i18n";
+import Draggable from "../../../icons/Draggable";
+import { MenuDetails_menu_items } from "../../types/MenuDetails";
+import {
+ getDiff,
+ getNodeData,
+ getNodeQuantity,
+ TreeNode,
+ TreePermutation
+} from "./tree";
+
+const NODE_HEIGHT = 56;
+const NODE_MARGIN = 40;
+
+export interface MenuItemsProps {
+ items: MenuDetails_menu_items[];
+ onChange: (operation: TreePermutation) => void;
+}
+
+const styles = (theme: Theme) =>
+ createStyles({
+ dragIcon: {
+ cursor: "grab"
+ },
+ nodeTitle: {
+ marginLeft: theme.spacing.unit * 7
+ },
+ root: {
+ "& .rst__collapseButton": {
+ display: "none"
+ },
+ background: theme.palette.grey[200]
+ },
+ row: {
+ alignItems: "center",
+ background: theme.palette.background.paper,
+ borderBottom: `1px ${theme.overrides.MuiCard.root.borderColor} solid`,
+ borderRadius: 0,
+ display: "flex",
+ flexDirection: "row",
+ height: NODE_HEIGHT,
+ justifyContent: "flex-start",
+ paddingLeft: theme.spacing.unit * 3,
+ transition: theme.transitions.duration.standard + "ms"
+ }
+ });
+
+const Node: React.FC<NodeRendererProps & WithStyles<typeof styles>> = ({
+ classes,
+ node,
+ path,
+ connectDragPreview,
+ connectDragSource
+}) => {
+ return (
+ <>
+ {connectDragPreview(
+ <div>
+ <Paper
+ className={classes.row}
+ elevation={0}
+ style={{
+ marginLeft: NODE_MARGIN * (path.length - 1)
+ }}
+ >
+ {connectDragSource(
+ <div>
+ <Draggable className={classes.dragIcon} />
+ </div>
+ )}
+ <Typography className={classes.nodeTitle}>{node.title}</Typography>
+ </Paper>
+ </div>
+ )}
+ </>
+ );
+};
+
+const MenuItems = withStyles(styles, { name: "MenuItems" })(
+ ({
+ classes,
+ items,
+ onChange
+ }: MenuItemsProps & WithStyles<typeof styles>) => (
+ <Card>
+ <CardTitle title={i18n.t("Menu Items")} />
+ <div style={{ height: getNodeQuantity(items) * NODE_HEIGHT }}>
+ <SortableTree
+ className={classes.root}
+ generateNodeProps={({ path }) => ({
+ className: classes.row,
+ style: {
+ marginLeft: NODE_MARGIN * (path.length - 1)
+ }
+ })}
+ isVirtualized={false}
+ rowHeight={NODE_HEIGHT}
+ treeData={items.map(getNodeData)}
+ theme={{
+ nodeContentRenderer: (props => (
+ <Node {...props} classes={classes} />
+ )) as any
+ }}
+ onChange={newTree =>
+ onChange(getDiff(items.map(getNodeData), newTree as TreeNode[]))
+ }
+ />
+ </div>
+ </Card>
+ )
+);
+MenuItems.displayName = "MenuItems";
+export default MenuItems;
diff --git a/saleor/static/dashboard-next/navigation/components/MenuItems/__snapshots__/tree.test.ts.snap b/saleor/static/dashboard-next/navigation/components/MenuItems/__snapshots__/tree.test.ts.snap
new file mode 100644
index 0000000000..ee8e17f9d9
--- /dev/null
+++ b/saleor/static/dashboard-next/navigation/components/MenuItems/__snapshots__/tree.test.ts.snap
@@ -0,0 +1,25 @@
+// Jest Snapshot v1, https://goo.gl/fbAQLP
+
+exports[`Properly computes diffs # 1`] = `
+Object {
+ "id": "1glasses",
+ "parentId": "0jewelry",
+ "sortOrder": 0,
+}
+`;
+
+exports[`Properly computes diffs # 2`] = `
+Object {
+ "id": "1glasses",
+ "parentId": "2accessories",
+ "sortOrder": 0,
+}
+`;
+
+exports[`Properly computes diffs # 3`] = `
+Object {
+ "id": "2accessories",
+ "parentId": "4apparel",
+ "sortOrder": 0,
+}
+`;
diff --git a/saleor/static/dashboard-next/navigation/components/MenuItems/index.ts b/saleor/static/dashboard-next/navigation/components/MenuItems/index.ts
new file mode 100644
index 0000000000..8f13d0b5ac
--- /dev/null
+++ b/saleor/static/dashboard-next/navigation/components/MenuItems/index.ts
@@ -0,0 +1,3 @@
+export { default } from "./MenuItems";
+export * from "./MenuItems";
+export * from "./tree";
diff --git a/saleor/static/dashboard-next/navigation/components/MenuItems/tree.test.ts b/saleor/static/dashboard-next/navigation/components/MenuItems/tree.test.ts
new file mode 100644
index 0000000000..64fd05cdf0
--- /dev/null
+++ b/saleor/static/dashboard-next/navigation/components/MenuItems/tree.test.ts
@@ -0,0 +1,87 @@
+import {
+ addNodeUnderParent,
+ find,
+ insertNode,
+ removeNode
+} from "react-sortable-tree";
+
+import { getDiff, TreeNode } from "./tree";
+
+const originalTree: TreeNode[] = [
+ {
+ children: [
+ { children: [], expanded: true, id: "0jewelry", title: "Jewelry" },
+ { children: [], expanded: true, id: "1glasses", title: "Glasses" }
+ ],
+ expanded: true,
+ id: "2accessories",
+ title: "Accessories"
+ },
+ { children: [], expanded: true, id: "3groceries", title: "Groceries" },
+ { children: [], expanded: true, id: "4apparel", title: "Apparel" }
+];
+
+function getNodeKey(node: any) {
+ return node.treeIndex;
+}
+
+function moveNode(
+ tree: TreeNode[],
+ src: string,
+ target: string,
+ asChild: boolean
+) {
+ const { matches: srcNodeCandidates } = find({
+ getNodeKey,
+ searchMethod: ({ node }) => node.id === src,
+ treeData: tree
+ });
+ const srcNodeData = srcNodeCandidates[0];
+
+ const treeAfterRemoval = removeNode({
+ getNodeKey,
+ path: srcNodeData.path,
+ treeData: tree
+ }).treeData;
+
+ const { matches: targetNodeCandidates } = find({
+ getNodeKey,
+ searchMethod: ({ node }) => node.id === target,
+ treeData: treeAfterRemoval
+ });
+ const targetNodeData = targetNodeCandidates[0];
+
+ const treeAfterInsertion = asChild
+ ? addNodeUnderParent({
+ addAsFirstChild: true,
+ getNodeKey,
+ ignoreCollapsed: false,
+ newNode: srcNodeData.node,
+ parentKey: targetNodeData.treeIndex,
+ treeData: treeAfterRemoval
+ }).treeData
+ : insertNode({
+ depth: targetNodeData.path.length,
+ getNodeKey,
+ minimumTreeIndex: targetNodeData.treeIndex,
+ newNode: srcNodeData.node,
+ treeData: treeAfterRemoval
+ }).treeData;
+
+ return treeAfterInsertion as TreeNode[];
+}
+
+describe("Properly computes diffs", () => {
+ const testTable = [
+ moveNode(originalTree, "1glasses", "0jewelry", true),
+ moveNode(originalTree, "1glasses", "0jewelry", false),
+ moveNode(originalTree, "2accessories", "4apparel", true)
+ ];
+
+ testTable.forEach(testData =>
+ it("#", () => {
+ const diff = getDiff(originalTree, testData);
+ expect(diff).toMatchSnapshot();
+ })
+ );
+});
diff --git a/saleor/static/dashboard-next/navigation/components/MenuItems/tree.ts b/saleor/static/dashboard-next/navigation/components/MenuItems/tree.ts
new file mode 100644
index 0000000000..ee273b6205
--- /dev/null
+++ b/saleor/static/dashboard-next/navigation/components/MenuItems/tree.ts
@@ -0,0 +1,76 @@
+import { getPatch } from "fast-array-diff";
+import { TreeItem } from "react-sortable-tree";
+
+import { MenuDetails_menu_items } from "../../types/MenuDetails";
+
+export interface TreePermutation {
+ id: string;
+ parentId?: string;
+ sortOrder?: number;
+}
+
+export type TreeNode = TreeItem & { id: string };
+
+function treeToMap(tree: TreeNode[], parent: string): Record<string, string[]> {
+ const childrenList = tree.map(node => node.id);
+ const childrenMaps = tree.map(node => ({
+ id: node.id,
+ mappedNodes: treeToMap(node.children as TreeNode[], node.id)
+ }));
+
+ return {
+ [parent]: childrenList,
+ ...childrenMaps.reduce(
+ (acc, childMap) => ({
+ ...acc,
+ ...childMap.mappedNodes
+ }),
+ {}
+ )
+ };
+}
+
+export function getDiff(
+ originalTree: TreeNode[],
+ newTree: TreeNode[]
+): TreePermutation {
+ const originalMap = treeToMap(originalTree, "root");
+ const newMap = treeToMap(newTree, "root");
+
+ const diff = Object.keys(newMap).map(key => {
+ const originalNode = originalMap[key];
+ const newNode = newMap[key];
+
+ const patch = getPatch(originalNode, newNode);
+
+ if (patch.length > 0) {
+ const addedNode = patch.find(operation => operation.type === "add");
+ if (!!addedNode) {
+ return {
+ id: addedNode.items[0],
+ parentId: key === "root" ? undefined : key,
+ sortOrder: addedNode.newPos
+ };
+ }
+ }
+ return null;
+ });
+
+ return diff.find(d => !!d);
+}
+
+export function getNodeData(item: MenuDetails_menu_items): TreeNode {
+ return {
+ children: item.children.map(getNodeData),
+ expanded: true,
+ id: item.id,
+ title: item.name
+ };
+}
+
+export function getNodeQuantity(items: MenuDetails_menu_items[]): number {
+ return items.reduce(
+ (acc, curr) => acc + getNodeQuantity(curr.children),
+ items.length
+ );
+}
diff --git a/saleor/static/dashboard-next/navigation/fixtures.ts b/saleor/static/dashboard-next/navigation/fixtures.ts
index d5d5833265..b21e5b2b5f 100644
--- a/saleor/static/dashboard-next/navigation/fixtures.ts
+++ b/saleor/static/dashboard-next/navigation/fixtures.ts
@@ -34,8 +34,42 @@ export const menu: MenuDetails_menu = {
id: "Q2F0ZWdvcnk6Nw==",
name: "Accessories"
},
- children: [],
+ children: [
+ {
+ __typename: "MenuItem",
+ category: {
+ __typename: "Category",
+ id: "Q2F0ZWdvcnk6OAX==",
+ name: "Jewelry"
+ },
+ children: [],
+ collection: null,
+ id: "0jewelry",
+ level: 0,
+ name: "Jewelry",
+ page: null,
+ sortOrder: 0,
+ url: null
+ },
+ {
+ __typename: "MenuItem",
+ category: {
+ __typename: "Category",
+ id: "Q2F0ZWdvcnk6OQX==",
+ name: "Glasses"
+ },
+ children: [],
+ collection: null,
+ id: "1glasses",
+ level: 0,
+ name: "Glasses",
+ page: null,
+ sortOrder: 1,
+ url: null
+ }
+ ],
collection: null,
+ id: "2accessories",
level: 0,
name: "Accessories",
page: null,
@@ -51,6 +85,7 @@ export const menu: MenuDetails_menu = {
},
children: [],
collection: null,
+ id: "3groceries",
level: 0,
name: "Groceries",
page: null,
@@ -66,6 +101,7 @@ export const menu: MenuDetails_menu = {
},
children: [],
collection: null,
+ id: "4apparel",
level: 0,
name: "Apparel",
page: null,
diff --git a/saleor/static/dashboard-next/navigation/queries.ts b/saleor/static/dashboard-next/navigation/queries.ts
index 50c4cee75c..922f194e88 100644
--- a/saleor/static/dashboard-next/navigation/queries.ts
+++ b/saleor/static/dashboard-next/navigation/queries.ts
@@ -23,6 +23,7 @@ const menuItemFragment = gql`
id
name
}
+ id
level
name
page {
diff --git a/saleor/static/dashboard-next/navigation/types/MenuDetails.ts b/saleor/static/dashboard-next/navigation/types/MenuDetails.ts
index 32f0f16b91..74d606d729 100644
--- a/saleor/static/dashboard-next/navigation/types/MenuDetails.ts
+++ b/saleor/static/dashboard-next/navigation/types/MenuDetails.ts
@@ -136,6 +136,7 @@ export interface MenuDetails_menu_items_children_children_children_children_chil
__typename: "MenuItem";
category: MenuDetails_menu_items_children_children_children_children_children_children_category | null;
collection: MenuDetails_menu_items_children_children_children_children_children_children_collection | null;
+ id: string;
level: number;
name: string;
page: MenuDetails_menu_items_children_children_children_children_children_children_page | null;
@@ -147,6 +148,7 @@ export interface MenuDetails_menu_items_children_children_children_children_chil
__typename: "MenuItem";
category: MenuDetails_menu_items_children_children_children_children_children_category | null;
collection: MenuDetails_menu_items_children_children_children_children_children_collection | null;
+ id: string;
level: number;
name: string;
page: MenuDetails_menu_items_children_children_children_children_children_page | null;
@@ -159,6 +161,7 @@ export interface MenuDetails_menu_items_children_children_children_children {
__typename: "MenuItem";
category: MenuDetails_menu_items_children_children_children_children_category | null;
collection: MenuDetails_menu_items_children_children_children_children_collection | null;
+ id: string;
level: number;
name: string;
page: MenuDetails_menu_items_children_children_children_children_page | null;
@@ -171,6 +174,7 @@ export interface MenuDetails_menu_items_children_children_children {
__typename: "MenuItem";
category: MenuDetails_menu_items_children_children_children_category | null;
collection: MenuDetails_menu_items_children_children_children_collection | null;
+ id: string;
level: number;
name: string;
page: MenuDetails_menu_items_children_children_children_page | null;
@@ -183,6 +187,7 @@ export interface MenuDetails_menu_items_children_children {
__typename: "MenuItem";
category: MenuDetails_menu_items_children_children_category | null;
collection: MenuDetails_menu_items_children_children_collection | null;
+ id: string;
level: number;
name: string;
page: MenuDetails_menu_items_children_children_page | null;
@@ -195,6 +200,7 @@ export interface MenuDetails_menu_items_children {
__typename: "MenuItem";
category: MenuDetails_menu_items_children_category | null;
collection: MenuDetails_menu_items_children_collection | null;
+ id: string;
level: number;
name: string;
page: MenuDetails_menu_items_children_page | null;
@@ -207,6 +213,7 @@ export interface MenuDetails_menu_items {
__typename: "MenuItem";
category: MenuDetails_menu_items_category | null;
collection: MenuDetails_menu_items_collection | null;
+ id: string;
level: number;
name: string;
page: MenuDetails_menu_items_page | null;
diff --git a/saleor/static/dashboard-next/navigation/types/MenuItemFragment.ts b/saleor/static/dashboard-next/navigation/types/MenuItemFragment.ts
index 65c56be16d..74664fca6b 100644
--- a/saleor/static/dashboard-next/navigation/types/MenuItemFragment.ts
+++ b/saleor/static/dashboard-next/navigation/types/MenuItemFragment.ts
@@ -28,6 +28,7 @@ export interface MenuItemFragment {
__typename: "MenuItem";
category: MenuItemFragment_category | null;
collection: MenuItemFragment_collection | null;
+ id: string;
level: number;
name: string;
page: MenuItemFragment_page | null;
diff --git a/saleor/static/dashboard-next/storybook/Decorator.tsx b/saleor/static/dashboard-next/storybook/Decorator.tsx
index beb127ca68..b92171d491 100644
--- a/saleor/static/dashboard-next/storybook/Decorator.tsx
+++ b/saleor/static/dashboard-next/storybook/Decorator.tsx
@@ -1,28 +1,46 @@
import * as React from "react";
+import AppActionContext from "../components/AppLayout/AppActionContext";
import { Provider as DateProvider } from "../components/Date/DateContext";
import { FormProvider } from "../components/Form";
import { MessageManager } from "../components/messages";
import ThemeProvider from "../components/Theme";
import { TimezoneProvider } from "../components/Timezone";
-export const Decorator = storyFn => (
- <FormProvider>
- <DateProvider value={+new Date("2018-08-07T14:30:44+00:00")}>
- <TimezoneProvider value="America/New_York">
- <ThemeProvider isDefaultDark={false}>
- <MessageManager>
- <div
- style={{
- padding: 24
- }}
- >
- {storyFn()}
- </div>
- </MessageManager>
- </ThemeProvider>
- </TimezoneProvider>
- </DateProvider>
- </FormProvider>
-);
+const DecoratorComponent: React.FC<{ story: any }> = ({ story }) => {
+ const appActionAnchor = React.useRef<HTMLDivElement>();
+
+ return (
+ <FormProvider>
+ <DateProvider value={+new Date("2018-08-07T14:30:44+00:00")}>
+ <TimezoneProvider value="America/New_York">
+ <ThemeProvider isDefaultDark={false}>
+ <MessageManager>
+ <AppActionContext.Provider value={appActionAnchor}>
+ <div
+ style={{
+ display: "flex",
+ flexGrow: 1,
+ padding: 24
+ }}
+ >
+ {story}
+ </div>
+ <div
+ style={{
+ bottom: 0,
+ gridColumn: 2,
+ position: "sticky"
+ }}
+ ref={appActionAnchor}
+ />
+ </AppActionContext.Provider>
+ </MessageManager>
+ </ThemeProvider>
+ </TimezoneProvider>
+ </DateProvider>
+ </FormProvider>
+ );
+};
+export const Decorator = storyFn => <DecoratorComponent story={storyFn()} />;
export default Decorator;
diff --git a/saleor/static/dashboard-next/storybook/stories/navigation/MenuDetailsPage.tsx b/saleor/static/dashboard-next/storybook/stories/navigation/MenuDetailsPage.tsx
index d6d296f5ff..50ac8e6f63 100644
--- a/saleor/static/dashboard-next/storybook/stories/navigation/MenuDetailsPage.tsx
+++ b/saleor/static/dashboard-next/storybook/stories/navigation/MenuDetailsPage.tsx
@@ -10,7 +10,10 @@ import Decorator from "../../Decorator";
const props: MenuDetailsPageProps = {
disabled: false,
menu,
- onBack: () => undefined
+ onBack: () => undefined,
+ onDelete: () => undefined,
+ onSubmit: () => undefined,
+ saveButtonState: "default"
};
storiesOf("Views / Navigation / Menu details", module)