Skip to content

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

Fix discount section searches

dominik-zeglen02324eb22a0e
diff --git a/saleor/static/dashboard-next/components/AssignCategoryDialog/AssignCategoryDialog.tsx b/saleor/static/dashboard-next/components/AssignCategoryDialog/AssignCategoryDialog.tsx
index a272a7da85..242c3f6124 100644
--- a/saleor/static/dashboard-next/components/AssignCategoryDialog/AssignCategoryDialog.tsx
+++ b/saleor/static/dashboard-next/components/AssignCategoryDialog/AssignCategoryDialog.tsx
@@ -15,8 +15,9 @@ import React from "react";
import ConfirmButton, {
ConfirmButtonTransitionState
} from "@saleor/components/ConfirmButton";
-import Form from "@saleor/components/Form";
import FormSpacer from "@saleor/components/FormSpacer";
+import { ChangeEvent } from "@saleor/hooks/useForm";
+import { onQueryChange } from "@saleor/misc";
import { SearchCategories_categories_edges_node } from "../../containers/SearchCategories/types/SearchCategories";
import i18n from "../../i18n";
import Checkbox from "../Checkbox";
@@ -50,13 +51,28 @@ interface AssignCategoriesDialogProps extends WithStyles<typeof styles> {
loading: boolean;
onClose: () => void;
onFetch: (value: string) => void;
- onSubmit: (data: FormData) => void;
+ onSubmit: (data: SearchCategories_categories_edges_node[]) => void;
+}
+
+function handleCategoryAssign(
+ product: SearchCategories_categories_edges_node,
+ isSelected: boolean,
+ selectedCategories: SearchCategories_categories_edges_node[],
+ setSelectedCategories: (
+ data: SearchCategories_categories_edges_node[]
+ ) => void
+) {
+ if (isSelected) {
+ setSelectedCategories(
+ selectedCategories.filter(
+ selectedProduct => selectedProduct.id !== product.id
+ )
+ );
+ } else {
+ setSelectedCategories([...selectedCategories, product]);
+ }
}
-const initialForm: FormData = {
- categories: [],
- query: ""
-};
const AssignCategoriesDialog = withStyles(styles, {
name: "AssignCategoriesDialog"
})(
@@ -69,104 +85,95 @@ const AssignCategoriesDialog = withStyles(styles, {
onClose,
onFetch,
onSubmit
- }: AssignCategoriesDialogProps) => (
- <Dialog
- open={open}
- onClose={onClose}
- classes={{ paper: classes.overflow }}
- fullWidth
- maxWidth="sm"
- >
- <Form initial={initialForm} onSubmit={onSubmit}>
- {({ data, change }) => (
- <>
- <DialogTitle>{i18n.t("Assign Categories")}</DialogTitle>
- <DialogContent className={classes.overflow}>
- <TextField
- name="query"
- value={data.query}
- onChange={event => change(event, () => onFetch(data.query))}
- label={i18n.t("Search Categories", {
- context: "product search input label"
- })}
- placeholder={i18n.t(
- "Search by product name, attribute, product type etc...",
- {
- context: "product search input placeholder"
- }
- )}
- fullWidth
- InputProps={{
- autoComplete: "off",
- endAdornment: loading && <CircularProgress size={16} />
- }}
- />
- <FormSpacer />
- <Table>
- <TableBody>
- {categories &&
- categories.map(category => {
- const isChecked = !!data.categories.find(
- selectedCategories =>
- selectedCategories.id === category.id
- );
+ }: AssignCategoriesDialogProps) => {
+ const [query, setQuery] = React.useState("");
+ const [selectedCategories, setSelectedCategories] = React.useState<
+ SearchCategories_categories_edges_node[]
+ >([]);
- return (
- <TableRow key={category.id}>
- <TableCell
- padding="checkbox"
- className={classes.checkboxCell}
- >
- <Checkbox
- checked={isChecked}
- onChange={() =>
- isChecked
- ? change({
- target: {
- name: "categories",
- value: data.categories.filter(
- selectedCategories =>
- selectedCategories.id !==
- category.id
- )
- }
- } as any)
- : change({
- target: {
- name: "categories",
- value: [...data.categories, category]
- }
- } as any)
- }
- />
- </TableCell>
- <TableCell className={classes.wideCell}>
- {category.name}
- </TableCell>
- </TableRow>
- );
- })}
- </TableBody>
- </Table>
- </DialogContent>
- <DialogActions>
- <Button onClick={onClose}>
- {i18n.t("Cancel", { context: "button" })}
- </Button>
- <ConfirmButton
- transitionState={confirmButtonState}
- color="primary"
- variant="contained"
- type="submit"
- >
- {i18n.t("Assign categories", { context: "button" })}
- </ConfirmButton>
- </DialogActions>
- </>
- )}
- </Form>
- </Dialog>
- )
+ const handleQueryChange = (event: ChangeEvent) =>
+ onQueryChange(event, onFetch, setQuery);
+ const handleSubmit = () => onSubmit(selectedCategories);
+
+ return (
+ <Dialog
+ open={open}
+ onClose={onClose}
+ classes={{ paper: classes.overflow }}
+ fullWidth
+ maxWidth="sm"
+ >
+ <DialogTitle>{i18n.t("Assign Categories")}</DialogTitle>
+ <DialogContent className={classes.overflow}>
+ <TextField
+ name="query"
+ value={query}
+ onChange={handleQueryChange}
+ label={i18n.t("Search Categories", {
+ context: "category search input label"
+ })}
+ placeholder={i18n.t("Search by category name, etc...", {
+ context: "category search input placeholder"
+ })}
+ fullWidth
+ InputProps={{
+ autoComplete: "off",
+ endAdornment: loading && <CircularProgress size={16} />
+ }}
+ />
+ <FormSpacer />
+ <Table>
+ <TableBody>
+ {categories &&
+ categories.map(category => {
+ const isSelected = !!selectedCategories.find(
+ selectedCategories => selectedCategories.id === category.id
+ );
+
+ return (
+ <TableRow key={category.id}>
+ <TableCell
+ padding="checkbox"
+ className={classes.checkboxCell}
+ >
+ <Checkbox
+ checked={isSelected}
+ onChange={() =>
+ handleCategoryAssign(
+ category,
+ isSelected,
+ selectedCategories,
+ setSelectedCategories
+ )
+ }
+ />
+ </TableCell>
+ <TableCell className={classes.wideCell}>
+ {category.name}
+ </TableCell>
+ </TableRow>
+ );
+ })}
+ </TableBody>
+ </Table>
+ </DialogContent>
+ <DialogActions>
+ <Button onClick={onClose}>
+ {i18n.t("Cancel", { context: "button" })}
+ </Button>
+ <ConfirmButton
+ transitionState={confirmButtonState}
+ color="primary"
+ variant="contained"
+ type="submit"
+ onClick={handleSubmit}
+ >
+ {i18n.t("Assign categories", { context: "button" })}
+ </ConfirmButton>
+ </DialogActions>
+ </Dialog>
+ );
+ }
);
AssignCategoriesDialog.displayName = "AssignCategoriesDialog";
export default AssignCategoriesDialog;
diff --git a/saleor/static/dashboard-next/components/AssignCollectionDialog/AssignCollectionDialog.tsx b/saleor/static/dashboard-next/components/AssignCollectionDialog/AssignCollectionDialog.tsx
index 6d0780427b..196fd44d27 100644
--- a/saleor/static/dashboard-next/components/AssignCollectionDialog/AssignCollectionDialog.tsx
+++ b/saleor/static/dashboard-next/components/AssignCollectionDialog/AssignCollectionDialog.tsx
@@ -12,13 +12,14 @@ import TableRow from "@material-ui/core/TableRow";
import TextField from "@material-ui/core/TextField";
import React from "react";
+import { ChangeEvent } from "@saleor/hooks/useForm";
+import { onQueryChange } from "@saleor/misc";
import { SearchCollections_collections_edges_node } from "../../containers/SearchCollections/types/SearchCollections";
import i18n from "../../i18n";
import Checkbox from "../Checkbox";
import ConfirmButton, {
ConfirmButtonTransitionState
} from "../ConfirmButton/ConfirmButton";
-import Form from "../Form";
import FormSpacer from "../FormSpacer";
export interface FormData {
@@ -50,13 +51,28 @@ interface AssignCollectionDialogProps extends WithStyles<typeof styles> {
loading: boolean;
onClose: () => void;
onFetch: (value: string) => void;
- onSubmit: (data: FormData) => void;
+ onSubmit: (data: SearchCollections_collections_edges_node[]) => void;
+}
+
+function handleCollectionAssign(
+ product: SearchCollections_collections_edges_node,
+ isSelected: boolean,
+ selectedCollections: SearchCollections_collections_edges_node[],
+ setSelectedCollections: (
+ data: SearchCollections_collections_edges_node[]
+ ) => void
+) {
+ if (isSelected) {
+ setSelectedCollections(
+ selectedCollections.filter(
+ selectedProduct => selectedProduct.id !== product.id
+ )
+ );
+ } else {
+ setSelectedCollections([...selectedCollections, product]);
+ }
}
-const initialForm: FormData = {
- collections: [],
- query: ""
-};
const AssignCollectionDialog = withStyles(styles, {
name: "AssignCollectionDialog"
})(
@@ -69,104 +85,96 @@ const AssignCollectionDialog = withStyles(styles, {
onClose,
onFetch,
onSubmit
- }: AssignCollectionDialogProps) => (
- <Dialog
- onClose={onClose}
- open={open}
- classes={{ paper: classes.overflow }}
- fullWidth
- maxWidth="sm"
- >
- <Form initial={initialForm} onSubmit={onSubmit}>
- {({ data, change }) => (
- <>
- <DialogTitle>{i18n.t("Assign Collection")}</DialogTitle>
- <DialogContent className={classes.overflow}>
- <TextField
- name="query"
- value={data.query}
- onChange={event => change(event, () => onFetch(data.query))}
- label={i18n.t("Search Collection", {
- context: "product search input label"
- })}
- placeholder={i18n.t(
- "Search by product name, attribute, product type etc...",
- {
- context: "product search input placeholder"
- }
- )}
- fullWidth
- InputProps={{
- autoComplete: "off",
- endAdornment: loading && <CircularProgress size={16} />
- }}
- />
- <FormSpacer />
- <Table>
- <TableBody>
- {collections &&
- collections.map(category => {
- const isChecked = !!data.collections.find(
- selectedCollection =>
- selectedCollection.id === category.id
- );
+ }: AssignCollectionDialogProps) => {
+ const [query, setQuery] = React.useState("");
+ const [selectedCollections, setSelectedCollections] = React.useState<
+ SearchCollections_collections_edges_node[]
+ >([]);
- return (
- <TableRow key={category.id}>
- <TableCell
- padding="checkbox"
- className={classes.checkboxCell}
- >
- <Checkbox
- checked={isChecked}
- onChange={() =>
- isChecked
- ? change({
- target: {
- name: "collections",
- value: data.collections.filter(
- selectedCollection =>
- selectedCollection.id !==
- category.id
- )
- }
- } as any)
- : change({
- target: {
- name: "collections",
- value: [...data.collections, category]
- }
- } as any)
- }
- />
- </TableCell>
- <TableCell className={classes.wideCell}>
- {category.name}
- </TableCell>
- </TableRow>
- );
- })}
- </TableBody>
- </Table>
- </DialogContent>
- <DialogActions>
- <Button onClick={onClose}>
- {i18n.t("Cancel", { context: "button" })}
- </Button>
- <ConfirmButton
- transitionState={confirmButtonState}
- color="primary"
- variant="contained"
- type="submit"
- >
- {i18n.t("Assign collections", { context: "button" })}
- </ConfirmButton>
- </DialogActions>
- </>
- )}
- </Form>
- </Dialog>
- )
+ const handleQueryChange = (event: ChangeEvent) =>
+ onQueryChange(event, onFetch, setQuery);
+ const handleSubmit = () => onSubmit(selectedCollections);
+
+ return (
+ <Dialog
+ onClose={onClose}
+ open={open}
+ classes={{ paper: classes.overflow }}
+ fullWidth
+ maxWidth="sm"
+ >
+ <DialogTitle>{i18n.t("Assign Collection")}</DialogTitle>
+ <DialogContent className={classes.overflow}>
+ <TextField
+ name="query"
+ value={query}
+ onChange={handleQueryChange}
+ label={i18n.t("Search Collection", {
+ context: "product search input label"
+ })}
+ placeholder={i18n.t("Search by collection name, etc...", {
+ context: "product search input placeholder"
+ })}
+ fullWidth
+ InputProps={{
+ autoComplete: "off",
+ endAdornment: loading && <CircularProgress size={16} />
+ }}
+ />
+ <FormSpacer />
+ <Table>
+ <TableBody>
+ {collections &&
+ collections.map(collection => {
+ const isSelected = !!selectedCollections.find(
+ selectedCollection =>
+ selectedCollection.id === collection.id
+ );
+
+ return (
+ <TableRow key={collection.id}>
+ <TableCell
+ padding="checkbox"
+ className={classes.checkboxCell}
+ >
+ <Checkbox
+ checked={isSelected}
+ onChange={() =>
+ handleCollectionAssign(
+ collection,
+ isSelected,
+ selectedCollections,
+ setSelectedCollections
+ )
+ }
+ />
+ </TableCell>
+ <TableCell className={classes.wideCell}>
+ {collection.name}
+ </TableCell>
+ </TableRow>
+ );
+ })}
+ </TableBody>
+ </Table>
+ </DialogContent>
+ <DialogActions>
+ <Button onClick={onClose}>
+ {i18n.t("Cancel", { context: "button" })}
+ </Button>
+ <ConfirmButton
+ transitionState={confirmButtonState}
+ color="primary"
+ variant="contained"
+ type="submit"
+ onClick={handleSubmit}
+ >
+ {i18n.t("Assign collections", { context: "button" })}
+ </ConfirmButton>
+ </DialogActions>
+ </Dialog>
+ );
+ }
);
AssignCollectionDialog.displayName = "AssignCollectionDialog";
export default AssignCollectionDialog;
diff --git a/saleor/static/dashboard-next/discounts/views/SaleDetails.tsx b/saleor/static/dashboard-next/discounts/views/SaleDetails.tsx
index cc50292eda..d74c745319 100644
--- a/saleor/static/dashboard-next/discounts/views/SaleDetails.tsx
+++ b/saleor/static/dashboard-next/discounts/views/SaleDetails.tsx
@@ -339,13 +339,13 @@ export const SaleDetails: React.StatelessComponent<SaleDetailsProps> = ({
onFetch={searchProducts}
loading={searchProductsOpts.loading}
onClose={closeModal}
- onSubmit={formData =>
+ onSubmit={products =>
saleCataloguesAdd({
variables: {
...paginationState,
id,
input: {
- products: formData.products.map(
+ products: products.map(
product => product.id
)
}
@@ -383,13 +383,13 @@ export const SaleDetails: React.StatelessComponent<SaleDetailsProps> = ({
onFetch={searchCategories}
loading={searchCategoriesOpts.loading}
onClose={closeModal}
- onSubmit={formData =>
+ onSubmit={categories =>
saleCataloguesAdd({
variables: {
...paginationState,
id,
input: {
- categories: formData.categories.map(
+ categories: categories.map(
product => product.id
)
}
@@ -420,13 +420,13 @@ export const SaleDetails: React.StatelessComponent<SaleDetailsProps> = ({
onFetch={searchCollections}
loading={searchCollectionsOpts.loading}
onClose={closeModal}
- onSubmit={formData =>
+ onSubmit={collections =>
saleCataloguesAdd({
variables: {
...paginationState,
id,
input: {
- collections: formData.collections.map(
+ collections: collections.map(
product => product.id
)
}
diff --git a/saleor/static/dashboard-next/discounts/views/VoucherDetails.tsx b/saleor/static/dashboard-next/discounts/views/VoucherDetails.tsx
index 680d0cc36c..78b3949356 100644
--- a/saleor/static/dashboard-next/discounts/views/VoucherDetails.tsx
+++ b/saleor/static/dashboard-next/discounts/views/VoucherDetails.tsx
@@ -407,13 +407,13 @@ export const VoucherDetails: React.StatelessComponent<VoucherDetailsProps> = ({
onFetch={searchCategories}
loading={searchCategoriesOpts.loading}
onClose={closeModal}
- onSubmit={formData =>
+ onSubmit={categories =>
voucherCataloguesAdd({
variables: {
...paginationState,
id,
input: {
- categories: formData.categories.map(
+ categories: categories.map(
product => product.id
)
}
@@ -444,13 +444,13 @@ export const VoucherDetails: React.StatelessComponent<VoucherDetailsProps> = ({
onFetch={searchCollections}
loading={searchCollectionsOpts.loading}
onClose={closeModal}
- onSubmit={formData =>
+ onSubmit={collections =>
voucherCataloguesAdd({
variables: {
...paginationState,
id,
input: {
- collections: formData.collections.map(
+ collections: collections.map(
product => product.id
)
}
@@ -496,13 +496,13 @@ export const VoucherDetails: React.StatelessComponent<VoucherDetailsProps> = ({
onFetch={searchProducts}
loading={searchProductsOpts.loading}
onClose={closeModal}
- onSubmit={formData =>
+ onSubmit={products =>
voucherCataloguesAdd({
variables: {
...paginationState,
id,
input: {
- products: formData.products.map(
+ products: products.map(
product => product.id
)
}