Skip to content

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

Add menu item create dialog

dominik-zeglenf681da14af96
diff --git a/saleor/static/dashboard-next/components/AutocompleteSelectMenu/AutocompleteSelectMenu.tsx b/saleor/static/dashboard-next/components/AutocompleteSelectMenu/AutocompleteSelectMenu.tsx
index 1bb56866f3..6fdbac056f 100644
--- a/saleor/static/dashboard-next/components/AutocompleteSelectMenu/AutocompleteSelectMenu.tsx
+++ b/saleor/static/dashboard-next/components/AutocompleteSelectMenu/AutocompleteSelectMenu.tsx
@@ -32,6 +32,7 @@ export interface AutocompleteSelectMenuProps {
options: SelectMenuItem[];
placeholder: string;
onChange: (event: React.ChangeEvent<any>) => void;
+ onInputChange?: (value: string) => void;
}
function getOptionValues(option: SelectMenuItem): string[] {
@@ -100,7 +101,8 @@ const AutocompleteSelectMenu = withStyles(styles, {
name,
options,
placeholder,
- onChange
+ onChange,
+ onInputChange
}: AutocompleteSelectMenuProps & WithStyles<typeof styles>) => {
const [inputValue, setInputValue] = React.useState(displayValue || "");
const [menuPath, setMenuPath] = React.useState<number[]>([]);
@@ -124,13 +126,15 @@ const AutocompleteSelectMenu = withStyles(styles, {
// Navigate back to main menu after input field change
React.useEffect(() => setMenuPath([]), [JSON.stringify(options)]);
+ // Reset input value after displayValue change
+ React.useEffect(() => setInputValue(displayValue), [displayValue]);
+
return (
- <DebounceAutocomplete debounceFn={undefined}>
+ <DebounceAutocomplete debounceFn={onInputChange}>
{debounceFn => (
<Downshift
itemToString={item => (item ? item.label : "")}
onSelect={handleChange}
- onInputValueChange={value => debounceFn(value)}
>
{({ getItemProps, isOpen, openMenu, closeMenu, selectItem }) => {
return (
@@ -143,7 +147,10 @@ const AutocompleteSelectMenu = withStyles(styles, {
closeMenu();
setMenuPath([]);
},
- onChange: event => setInputValue(event.target.value),
+ onChange: event => {
+ debounceFn(event.target.value);
+ setInputValue(event.target.value);
+ },
onFocus: () => openMenu(),
placeholder
}}
diff --git a/saleor/static/dashboard-next/navigation/components/MenuCreateItemDialog/MenuCreateItemDialog.tsx b/saleor/static/dashboard-next/navigation/components/MenuCreateItemDialog/MenuCreateItemDialog.tsx
new file mode 100644
index 0000000000..b27682b690
--- /dev/null
+++ b/saleor/static/dashboard-next/navigation/components/MenuCreateItemDialog/MenuCreateItemDialog.tsx
@@ -0,0 +1,214 @@
+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 TextField from "@material-ui/core/TextField";
+import * as React from "react";
+
+import AutocompleteSelectMenu, {
+ SelectMenuItem
+} from "../../../components/AutocompleteSelectMenu";
+import ConfirmButton, {
+ ConfirmButtonTransitionState
+} from "../../../components/ConfirmButton";
+import Form from "../../../components/Form";
+import FormSpacer from "../../../components/FormSpacer";
+import { SearchCategories_categories_edges_node } from "../../../containers/SearchCategories/types/SearchCategories";
+import { SearchCollections_collections_edges_node } from "../../../containers/SearchCollections/types/SearchCollections";
+import { SearchProducts_products_edges_node } from "../../../containers/SearchProducts/types/SearchProducts";
+import i18n from "../../../i18n";
+
+export type MenuItemType =
+ | "category"
+ | "collection"
+ | "link"
+ | "page"
+ | "product";
+export interface MenuItemData {
+ id: string;
+ type: MenuItemType;
+}
+
+export interface MenuCreateItemDialogFormData extends MenuItemData {
+ name: string;
+}
+
+export interface MenuCreateItemDialogProps {
+ confirmButtonState: ConfirmButtonTransitionState;
+ disabled: boolean;
+ initial?: MenuCreateItemDialogFormData;
+ loading: boolean;
+ open: boolean;
+ products: SearchProducts_products_edges_node[];
+ collections: SearchCollections_collections_edges_node[];
+ categories: SearchCategories_categories_edges_node[];
+ onClose: () => void;
+ onSubmit: (data: MenuCreateItemDialogFormData) => void;
+ onQueryChange: (query: string) => void;
+}
+
+const defaultInitial: MenuCreateItemDialogFormData = {
+ id: "",
+ name: "",
+ type: "category"
+};
+
+function findMenuItem(menu: SelectMenuItem[], value: string): SelectMenuItem {
+ const matches = menu.map(menuItem =>
+ menuItem.children
+ ? findMenuItem(menuItem.children, value)
+ : menuItem.value === value
+ ? menuItem
+ : null
+ );
+ return matches.find(match => match !== null);
+}
+
+function getMenuItemData(value: string): MenuItemData {
+ const [type, ...idParts] = value.split(":");
+ return {
+ id: idParts.reduce((acc, part) => acc + part, ""),
+ type: type as MenuItemType
+ };
+}
+
+function getDisplayValue(menu: SelectMenuItem[], value: string): string {
+ return findMenuItem(menu, value).label.toString();
+}
+
+const MenuCreateItemDialog: React.StatelessComponent<
+ MenuCreateItemDialogProps
+> = ({
+ confirmButtonState,
+ disabled,
+ initial,
+ loading,
+ onClose,
+ onSubmit,
+ onQueryChange,
+ open,
+ categories,
+ collections,
+ products
+}) => {
+ const [displayValue, setDisplayValue] = React.useState("");
+
+ const options: SelectMenuItem[] = [
+ {
+ children: categories.map(category => ({
+ label: category.name,
+ value: "category:" + category.id
+ })),
+ label: i18n.t("Categories ({{ number }})", {
+ number: categories.length
+ })
+ },
+ {
+ children: collections.map(collection => ({
+ label: collection.name,
+ value: "collection:" + collection.id
+ })),
+ label: i18n.t("Collections ({{ number }})", {
+ number: collections.length
+ })
+ },
+ {
+ children: products.map(product => ({
+ label: product.name,
+ value: "product:" + product.id
+ })),
+ label: i18n.t("Products ({{ number }})", {
+ number: products.length
+ })
+ }
+ ];
+
+ return (
+ <Dialog
+ open={open}
+ maxWidth="sm"
+ fullWidth
+ PaperProps={{
+ style: { overflowY: "visible" }
+ }}
+ >
+ <DialogTitle>
+ {i18n.t("Add Item", {
+ context: "create new menu item"
+ })}
+ </DialogTitle>
+ <Form initial={initial || defaultInitial} onSubmit={onSubmit}>
+ {({ change, data, submit }) => {
+ const handleSelectChange = (event: React.ChangeEvent<any>) => {
+ const value = event.target.value;
+ const menuItemData = getMenuItemData(value);
+ change(
+ {
+ target: {
+ name: "id",
+ value: menuItemData.id
+ }
+ } as any,
+ () =>
+ change(
+ {
+ target: {
+ name: "type",
+ value: menuItemData.type
+ }
+ } as any,
+ () => setDisplayValue(getDisplayValue(options, value))
+ )
+ );
+ };
+
+ return (
+ <>
+ <DialogContent style={{ overflowY: "visible" }}>
+ <TextField
+ disabled={disabled}
+ label={i18n.t("Name")}
+ fullWidth
+ value={data.name}
+ onChange={change}
+ name={"name" as keyof MenuCreateItemDialogFormData}
+ helperText=""
+ />
+ <FormSpacer />
+ <AutocompleteSelectMenu
+ disabled={disabled}
+ onChange={handleSelectChange}
+ name={"id" as keyof MenuCreateItemDialogFormData}
+ helperText=""
+ label={i18n.t("Link")}
+ displayValue={displayValue}
+ loading={loading}
+ error={false}
+ options={options}
+ placeholder={i18n.t("Start typing to begin search...")}
+ onInputChange={onQueryChange}
+ />
+ </DialogContent>
+ <DialogActions>
+ <Button onClick={onClose}>
+ {i18n.t("Cancel", { context: "button" })}
+ </Button>
+ <ConfirmButton
+ transitionState={confirmButtonState}
+ color="primary"
+ variant="contained"
+ onClick={submit}
+ >
+ {i18n.t("Submit", { context: "button" })}
+ </ConfirmButton>
+ </DialogActions>
+ </>
+ );
+ }}
+ </Form>
+ </Dialog>
+ );
+};
+MenuCreateItemDialog.displayName = "MenuCreateItemDialog";
+export default MenuCreateItemDialog;
diff --git a/saleor/static/dashboard-next/navigation/components/MenuCreateItemDialog/index.ts b/saleor/static/dashboard-next/navigation/components/MenuCreateItemDialog/index.ts
new file mode 100644
index 0000000000..cbb3fed8c9
--- /dev/null
+++ b/saleor/static/dashboard-next/navigation/components/MenuCreateItemDialog/index.ts
@@ -0,0 +1,2 @@
+export { default } from './MenuCreateItemDialog';
+export * from './MenuCreateItemDialog';
\ No newline at end of file
diff --git a/saleor/static/dashboard-next/navigation/components/MenuDetailsPage/MenuDetailsPage.tsx b/saleor/static/dashboard-next/navigation/components/MenuDetailsPage/MenuDetailsPage.tsx
index c4146348b8..a5592e3413 100644
--- a/saleor/static/dashboard-next/navigation/components/MenuDetailsPage/MenuDetailsPage.tsx
+++ b/saleor/static/dashboard-next/navigation/components/MenuDetailsPage/MenuDetailsPage.tsx
@@ -25,6 +25,7 @@ export interface MenuDetailsPageProps {
menu: MenuDetails_menu;
onBack: () => void;
onDelete: () => void;
+ onItemAdd: () => void;
onSubmit: () => void;
}
@@ -34,6 +35,7 @@ const MenuDetailsPage: React.StatelessComponent<MenuDetailsPageProps> = ({
saveButtonState,
onBack,
onDelete,
+ onItemAdd,
onSubmit
}) => {
const initialForm: MenuDetailsFormData = {
@@ -80,6 +82,7 @@ const MenuDetailsPage: React.StatelessComponent<MenuDetailsPageProps> = ({
? setTreePermutations([...treePermutations, permutation])
: undefined
}
+ onItemAdd={onItemAdd}
/>
</div>
</Grid>
diff --git a/saleor/static/dashboard-next/navigation/components/MenuItems/MenuItems.tsx b/saleor/static/dashboard-next/navigation/components/MenuItems/MenuItems.tsx
index 9013e42ea1..62170a5754 100644
--- a/saleor/static/dashboard-next/navigation/components/MenuItems/MenuItems.tsx
+++ b/saleor/static/dashboard-next/navigation/components/MenuItems/MenuItems.tsx
@@ -32,6 +32,7 @@ const NODE_MARGIN = 40;
export interface MenuItemsProps {
items: MenuDetails_menu_items[];
onChange: (operation: TreePermutation) => void;
+ onItemAdd: () => void;
}
const styles = (theme: Theme) =>
@@ -166,7 +167,8 @@ const MenuItems = withStyles(styles, { name: "MenuItems" })(
({
classes,
items,
- onChange
+ onChange,
+ onItemAdd
}: MenuItemsProps & WithStyles<typeof styles>) => {
const { isDark } = useTheme();
@@ -199,7 +201,7 @@ const MenuItems = withStyles(styles, { name: "MenuItems" })(
/>
</div>
<CardActions className={classes.actions}>
- <Button color="primary">
+ <Button color="primary" onClick={onItemAdd}>
{i18n.t("Add new item", {
context: "add menu item"
})}
diff --git a/saleor/static/dashboard-next/navigation/views/MenuDetails.tsx b/saleor/static/dashboard-next/navigation/views/MenuDetails.tsx
index a2726d9958..bff814f108 100644
--- a/saleor/static/dashboard-next/navigation/views/MenuDetails.tsx
+++ b/saleor/static/dashboard-next/navigation/views/MenuDetails.tsx
@@ -1,8 +1,12 @@
import * as React from "react";
+import { SearchProductsProvider } from "../../containers/SearchProducts";
import useNavigator from "../../hooks/useNavigator";
import useNotifier from "../../hooks/useNotifier";
import { maybe } from "../../misc";
+import { CategorySearchProvider } from "../../products/containers/CategorySearch";
+import { CollectionSearchProvider } from "../../products/containers/CollectionSearch";
+import MenuCreateItemDialog from "../components/MenuCreateItemDialog";
import MenuDetailsPage from "../components/MenuDetailsPage";
import { MenuDetailsQuery } from "../queries";
import { menuListUrl, menuUrl, MenuUrlQueryParams } from "../urls";
@@ -36,9 +40,69 @@ const MenuDetails: React.FC<MenuDetailsProps> = ({ id, params }) => {
menu={maybe(() => data.menu)}
onBack={() => navigate(menuListUrl())}
onDelete={() => undefined}
+ onItemAdd={() =>
+ navigate(
+ menuUrl(id, {
+ action: "add-item"
+ })
+ )
+ }
onSubmit={() => undefined}
saveButtonState="default"
/>
+ <CategorySearchProvider>
+ {categorySearch => (
+ <CollectionSearchProvider>
+ {collectionSearch => (
+ <SearchProductsProvider>
+ {(productSearch, productSearchOpts) => {
+ const handleQueryChange = (query: string) => {
+ categorySearch.search(query);
+ collectionSearch.search(query);
+ productSearch(query);
+ };
+ return (
+ <MenuCreateItemDialog
+ open={params.action === "add-item"}
+ categories={maybe(
+ () =>
+ categorySearch.searchOpts.data.categories.edges.map(
+ edge => edge.node
+ ),
+ []
+ )}
+ collections={maybe(
+ () =>
+ collectionSearch.searchOpts.data.collections.edges.map(
+ edge => edge.node
+ ),
+ []
+ )}
+ products={maybe(
+ () =>
+ productSearchOpts.data.products.edges.map(
+ edge => edge.node
+ ),
+ []
+ )}
+ loading={
+ categorySearch.searchOpts.loading ||
+ collectionSearch.searchOpts.loading ||
+ productSearchOpts.loading
+ }
+ confirmButtonState="default"
+ disabled={false}
+ onClose={closeModal}
+ onSubmit={() => undefined}
+ onQueryChange={handleQueryChange}
+ />
+ );
+ }}
+ </SearchProductsProvider>
+ )}
+ </CollectionSearchProvider>
+ )}
+ </CategorySearchProvider>
</>
);
}}
diff --git a/saleor/static/dashboard-next/storybook/config.js b/saleor/static/dashboard-next/storybook/config.js
index bbb5a1e9b4..a92a7e83c4 100644
--- a/saleor/static/dashboard-next/storybook/config.js
+++ b/saleor/static/dashboard-next/storybook/config.js
@@ -76,6 +76,7 @@ function loadStories() {
// Navigation
require("./stories/navigation/MenuCreateDialog");
+ require("./stories/navigation/MenuCreateItemDialog");
require("./stories/navigation/MenuDetailsPage");
require("./stories/navigation/MenuListPage");
diff --git a/saleor/static/dashboard-next/storybook/stories/navigation/MenuCreateItemDialog.tsx b/saleor/static/dashboard-next/storybook/stories/navigation/MenuCreateItemDialog.tsx
new file mode 100644
index 0000000000..679fb3e6b9
--- /dev/null
+++ b/saleor/static/dashboard-next/storybook/stories/navigation/MenuCreateItemDialog.tsx
@@ -0,0 +1,34 @@
+import { storiesOf } from "@storybook/react";
+import * as React from "react";
+
+import MenuCreateItemDialog, {
+ MenuCreateItemDialogProps
+} from "../../../navigation/components/MenuCreateItemDialog";
+import Decorator from "../../Decorator";
+
+const props: MenuCreateItemDialogProps = {
+ categories: [
+ {
+ __typename: "Category",
+ id: "1",
+ name: "Chairs"
+ },
+ {
+ __typename: "Category",
+ id: "2",
+ name: "Desks"
+ }
+ ],
+ collections: [],
+ confirmButtonState: "default",
+ disabled: false,
+ loading: false,
+ onClose: () => undefined,
+ onSubmit: () => undefined,
+ open: true,
+ products: []
+};
+
+storiesOf("Navigation / Menu item create", module)
+ .addDecorator(Decorator)
+ .add("default", () => <MenuCreateItemDialog {...props} />);