Skip to content

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

Autocomplete categories

dominik-zeglen63aa1a6935d1
diff --git a/saleor/static/dashboard-next/categories/types/RootCategories.ts b/saleor/static/dashboard-next/categories/types/RootCategories.ts
index 95ddbf743f..5f13e3ba61 100644
--- a/saleor/static/dashboard-next/categories/types/RootCategories.ts
+++ b/saleor/static/dashboard-next/categories/types/RootCategories.ts
@@ -45,3 +45,10 @@ export interface RootCategories_categories {
export interface RootCategories {
categories: RootCategories_categories | null;
}
+
+export interface RootCategoriesVariables {
+ first?: number | null;
+ after?: string | null;
+ last?: number | null;
+ before?: string | null;
+}
diff --git a/saleor/static/dashboard-next/products/components/ProductOrganization/ProductOrganization.tsx b/saleor/static/dashboard-next/products/components/ProductOrganization/ProductOrganization.tsx
index 8bdbbd2261..ebb121fae5 100644
--- a/saleor/static/dashboard-next/products/components/ProductOrganization/ProductOrganization.tsx
+++ b/saleor/static/dashboard-next/products/components/ProductOrganization/ProductOrganization.tsx
@@ -46,13 +46,16 @@ const styles = (theme: Theme) =>
interface ProductOrganizationProps extends WithStyles<typeof styles> {
categories?: Array<{ value: string; label: string }>;
collections?: Array<{ value: string; label: string }>;
- category: string;
productCollections: string[];
data: {
attributes: Array<{
slug: string;
value: string;
}>;
+ category: {
+ label: string;
+ value: string;
+ };
productType: {
label: string;
value: {
@@ -72,18 +75,19 @@ interface ProductOrganizationProps extends WithStyles<typeof styles> {
};
};
productTypes?: ProductType[];
+ fetchCategories: (query: string) => void;
onChange: (event: React.ChangeEvent<any>) => void;
}
const ProductOrganization = withStyles(styles, { name: "ProductOrganization" })(
({
- category,
categories,
classes,
collections,
data,
disabled,
errors,
+ fetchCategories,
product,
productCollections,
productTypes,
@@ -215,7 +219,6 @@ const ProductOrganization = withStyles(styles, { name: "ProductOrganization" })(
label: v.name,
value: v.slug
}))}
- key={index}
custom
/>
<FormSpacer />
@@ -226,15 +229,14 @@ const ProductOrganization = withStyles(styles, { name: "ProductOrganization" })(
<Skeleton />
)}
<hr className={classes.hr} />
- <SingleSelectField
+ <SingleAutocompleteSelectField
disabled={disabled}
- error={!!errors.category}
- hint={errors.category}
label={i18n.t("Category")}
choices={disabled ? [] : categories}
name="category"
- value={category}
+ value={data.category}
onChange={onChange}
+ fetchChoices={fetchCategories}
/>
<FormSpacer />
<hr className={classes.hr} />
diff --git a/saleor/static/dashboard-next/products/components/ProductUpdatePage/ProductUpdatePage.tsx b/saleor/static/dashboard-next/products/components/ProductUpdatePage/ProductUpdatePage.tsx
index 640d68b176..3289a58bcf 100644
--- a/saleor/static/dashboard-next/products/components/ProductUpdatePage/ProductUpdatePage.tsx
+++ b/saleor/static/dashboard-next/products/components/ProductUpdatePage/ProductUpdatePage.tsx
@@ -76,6 +76,7 @@ interface ProductUpdateProps extends WithStyles<typeof styles> {
product?: ProductDetails_product;
header: string;
saveButtonBarState: ConfirmButtonTransitionState;
+ fetchCategories: (query: string) => void;
onVariantShow: (id: string) => () => void;
onImageDelete: (id: string) => () => void;
onAttributesEdit: () => void;
@@ -97,6 +98,7 @@ export const ProductUpdate = withStyles(styles, { name: "ProductUpdate" })(
categories: categoryChoiceList,
collections: collectionChoiceList,
errors: userErrors,
+ fetchCategories,
images,
header,
placeholderImage,
@@ -126,7 +128,12 @@ export const ProductUpdate = withStyles(styles, { name: "ProductUpdate" })(
: undefined,
available: product.isPublished,
availableOn: product.availableOn,
- category: product.category ? product.category.id : undefined,
+ category: product.category
+ ? {
+ label: product.category.name,
+ value: product.category.id
+ }
+ : undefined,
chargeTaxes: product.chargeTaxes ? product.chargeTaxes : false,
collections: productCollections
? productCollections.map(node => node.id)
@@ -256,9 +263,9 @@ export const ProductUpdate = withStyles(styles, { name: "ProductUpdate" })(
</div>
<div>
<ProductOrganization
- category={data.category}
categories={categories}
errors={errors}
+ fetchCategories={fetchCategories}
productCollections={data.collections}
collections={collections}
product={product}
diff --git a/saleor/static/dashboard-next/products/containers/CategorySearch.tsx b/saleor/static/dashboard-next/products/containers/CategorySearch.tsx
new file mode 100644
index 0000000000..75bdd199af
--- /dev/null
+++ b/saleor/static/dashboard-next/products/containers/CategorySearch.tsx
@@ -0,0 +1,39 @@
+import * as React from "react";
+import { QueryResult } from "react-apollo";
+import { TypedCategorySearchQuery } from "../queries";
+import {
+ CategorySearch,
+ CategorySearchVariables
+} from "../types/CategorySearch";
+
+interface CategorySearchProviderProps {
+ children: ((
+ props: {
+ search: (query: string) => void;
+ searchOpts: QueryResult<CategorySearch, CategorySearchVariables>;
+ }
+ ) => React.ReactElement<any>);
+}
+interface CategorySearchProviderState {
+ query: string;
+}
+
+export class CategorySearchProvider extends React.Component<
+ CategorySearchProviderProps,
+ CategorySearchProviderState
+> {
+ state: CategorySearchProviderState = { query: "" };
+
+ search = (query: string) => this.setState({ query });
+
+ render() {
+ return (
+ <TypedCategorySearchQuery
+ variables={{ query: this.state.query }}
+ skip={!this.state.query}
+ >
+ {searchOpts => this.props.children({ search: this.search, searchOpts })}
+ </TypedCategorySearchQuery>
+ );
+ }
+}
diff --git a/saleor/static/dashboard-next/products/queries.ts b/saleor/static/dashboard-next/products/queries.ts
index e06780a076..f35bd5a735 100644
--- a/saleor/static/dashboard-next/products/queries.ts
+++ b/saleor/static/dashboard-next/products/queries.ts
@@ -1,6 +1,10 @@
import gql from "graphql-tag";
import { TypedQuery } from "../queries";
+import {
+ CategorySearch,
+ CategorySearchVariables
+} from "./types/CategorySearch";
import { ProductCreateData } from "./types/ProductCreateData";
import {
ProductDetails,
@@ -241,14 +245,6 @@ const productDetailsQuery = gql`
}
}
}
- categories(first: 20) {
- edges {
- node {
- id
- name
- }
- }
- }
}
`;
export const TypedProductDetailsQuery = TypedQuery<
@@ -299,14 +295,6 @@ const productCreateQuery = gql`
}
}
}
- categories(first: 20) {
- edges {
- node {
- id
- name
- }
- }
- }
}
`;
export const TypedProductCreateQuery = TypedQuery<ProductCreateData, {}>(
@@ -373,3 +361,20 @@ export const TypedProductImageQuery = TypedQuery<
ProductImageById,
ProductImageByIdVariables
>(productImageQuery);
+
+const categorySearch = gql`
+ query CategorySearch($query: String) {
+ categories(first: 5, query: $query) {
+ edges {
+ node {
+ id
+ name
+ }
+ }
+ }
+ }
+`;
+export const TypedCategorySearchQuery = TypedQuery<
+ CategorySearch,
+ CategorySearchVariables
+>(categorySearch);
diff --git a/saleor/static/dashboard-next/products/types/CategorySearch.ts b/saleor/static/dashboard-next/products/types/CategorySearch.ts
new file mode 100644
index 0000000000..169d55721b
--- /dev/null
+++ b/saleor/static/dashboard-next/products/types/CategorySearch.ts
@@ -0,0 +1,30 @@
+/* tslint:disable */
+// This file was automatically generated and should not be edited.
+
+// ====================================================
+// GraphQL query operation: CategorySearch
+// ====================================================
+
+export interface CategorySearch_categories_edges_node {
+ __typename: "Category";
+ id: string;
+ name: string;
+}
+
+export interface CategorySearch_categories_edges {
+ __typename: "CategoryCountableEdge";
+ node: CategorySearch_categories_edges_node;
+}
+
+export interface CategorySearch_categories {
+ __typename: "CategoryCountableConnection";
+ edges: CategorySearch_categories_edges[];
+}
+
+export interface CategorySearch {
+ categories: CategorySearch_categories | null;
+}
+
+export interface CategorySearchVariables {
+ query?: string | null;
+}
diff --git a/saleor/static/dashboard-next/products/views/ProductUpdate/index.tsx b/saleor/static/dashboard-next/products/views/ProductUpdate/index.tsx
index 6107bf492f..7870c9159a 100644
--- a/saleor/static/dashboard-next/products/views/ProductUpdate/index.tsx
+++ b/saleor/static/dashboard-next/products/views/ProductUpdate/index.tsx
@@ -13,6 +13,7 @@ import i18n from "../../../i18n";
import { decimal, getMutationState, maybe } from "../../../misc";
import { productTypeUrl } from "../../../productTypes/urls";
import ProductUpdatePage from "../../components/ProductUpdatePage";
+import { CategorySearchProvider } from "../../containers/CategorySearch";
import ProductUpdateOperations from "../../containers/ProductUpdateOperations";
import { TypedProductDetailsQuery } from "../../queries";
import {
@@ -37,197 +38,220 @@ export const ProductUpdate: React.StatelessComponent<ProductUpdateProps> = ({
<Navigator>
{navigate => {
return (
- <TypedProductDetailsQuery displayLoader variables={{ id }}>
- {({ data, loading, error }) => {
- if (error) {
- return <ErrorMessageCard message="Something went wrong" />;
- }
+ <CategorySearchProvider>
+ {({
+ search: searchCategories,
+ searchOpts: searchCategoriesOpts
+ }) => (
+ <TypedProductDetailsQuery displayLoader variables={{ id }}>
+ {({ data, loading, error }) => {
+ if (error) {
+ return (
+ <ErrorMessageCard message="Something went wrong" />
+ );
+ }
- const handleDelete = () => {
- pushMessage({ text: i18n.t("Product removed") });
- navigate(productListUrl());
- };
- const handleUpdate = () =>
- pushMessage({ text: i18n.t("Saved changes") });
- const handleImageCreate = () =>
- pushMessage({
- text: i18n.t("Image successfully uploaded")
- });
- const handleImageDeleteSuccess = () =>
- pushMessage({
- text: i18n.t("Image successfully deleted")
- });
- const handleVariantAdd = () =>
- navigate(productVariantAddUrl(id));
+ const handleDelete = () => {
+ pushMessage({ text: i18n.t("Product removed") });
+ navigate(productListUrl());
+ };
+ const handleUpdate = () =>
+ pushMessage({ text: i18n.t("Saved changes") });
+ const handleImageCreate = () =>
+ pushMessage({
+ text: i18n.t("Image successfully uploaded")
+ });
+ const handleImageDeleteSuccess = () =>
+ pushMessage({
+ text: i18n.t("Image successfully deleted")
+ });
+ const handleVariantAdd = () =>
+ navigate(productVariantAddUrl(id));
- const product = data ? data.product : undefined;
- const allCollections =
- data && data.collections
- ? data.collections.edges.map(edge => edge.node)
- : [];
- const allCategories =
- data && data.categories
- ? data.categories.edges.map(edge => edge.node)
- : [];
- return (
- <ProductUpdateOperations
- product={product}
- onDelete={handleDelete}
- onImageCreate={handleImageCreate}
- onImageDelete={handleImageDeleteSuccess}
- onUpdate={handleUpdate}
- >
- {({
- createProductImage,
- deleteProduct,
- deleteProductImage,
- reorderProductImages,
- updateProduct
- }) => {
- const handleImageDelete = (id: string) => () =>
- deleteProductImage.mutate({ id });
- const handleImageEdit = (imageId: string) => () =>
- navigate(productImageUrl(id, imageId));
- const handleSubmit = data => {
- if (product) {
- updateProduct.mutate({
- attributes: data.attributes,
- availableOn:
- data.availableOn !== ""
- ? data.availableOn
- : null,
- category: data.category,
- chargeTaxes: data.chargeTaxes,
- collections: data.collections,
- description: data.description,
- id: product.id,
- isPublished: data.available,
- name: data.name,
- price: decimal(data.price)
- });
- }
- };
+ const product = data ? data.product : undefined;
+ const allCollections =
+ data && data.collections
+ ? data.collections.edges.map(edge => edge.node)
+ : [];
+ return (
+ <ProductUpdateOperations
+ product={product}
+ onDelete={handleDelete}
+ onImageCreate={handleImageCreate}
+ onImageDelete={handleImageDeleteSuccess}
+ onUpdate={handleUpdate}
+ >
+ {({
+ createProductImage,
+ deleteProduct,
+ deleteProductImage,
+ reorderProductImages,
+ updateProduct
+ }) => {
+ const handleImageDelete = (id: string) => () =>
+ deleteProductImage.mutate({ id });
+ const handleImageEdit = (imageId: string) => () =>
+ navigate(productImageUrl(id, imageId));
+ const handleSubmit = data => {
+ if (product) {
+ updateProduct.mutate({
+ attributes: data.attributes,
+ availableOn:
+ data.availableOn !== ""
+ ? data.availableOn
+ : null,
+ category: data.category,
+ chargeTaxes: data.chargeTaxes,
+ collections: data.collections,
+ description: data.description,
+ id: product.id,
+ isPublished: data.available,
+ name: data.name,
+ price: decimal(data.price)
+ });
+ }
+ };
- const disableFormSave =
- createProductImage.opts.loading ||
- deleteProduct.opts.loading ||
- reorderProductImages.opts.loading ||
- updateProduct.opts.loading ||
- loading;
- const formTransitionState = getMutationState(
- updateProduct.opts.called,
- updateProduct.opts.loading,
- maybe(
- () => updateProduct.opts.data.productUpdate.errors
- )
- );
- const deleteTransitionState = getMutationState(
- deleteProduct.opts.called,
- deleteProduct.opts.loading,
- maybe(
- () => deleteProduct.opts.data.productDelete.errors
- )
- );
- return (
- <>
- <WindowTitle
- title={maybe(() => data.product.name)}
- />
- <ProductUpdatePage
- categories={allCategories}
- collections={allCollections}
- disabled={disableFormSave}
- errors={maybe(
+ const disableFormSave =
+ createProductImage.opts.loading ||
+ deleteProduct.opts.loading ||
+ reorderProductImages.opts.loading ||
+ updateProduct.opts.loading ||
+ loading;
+ const formTransitionState = getMutationState(
+ updateProduct.opts.called,
+ updateProduct.opts.loading,
+ maybe(
() =>
- updateProduct.opts.data.productUpdate.errors,
- []
- )}
- saveButtonBarState={formTransitionState}
- images={maybe(() => data.product.images)}
- header={maybe(() => product.name)}
- placeholderImage={placeholderImg}
- product={product}
- productCollections={maybe(
- () => product.collections
- )}
- variants={maybe(() => product.variants)}
- onAttributesEdit={() =>
- navigate(
- productTypeUrl(data.product.productType.id)
- )
- }
- onBack={() => {
- navigate(productListUrl());
- }}
- onDelete={() => navigate(productRemoveUrl(id))}
- onProductShow={() => {
- if (product) {
- window.open(product.url);
- }
- }}
- onImageReorder={({ newIndex, oldIndex }) => {
- if (product) {
- let ids = product.images.map(
- image => image.id
- );
- ids = arrayMove(ids, oldIndex, newIndex);
- reorderProductImages.mutate({
- imagesIds: ids,
- productId: product.id
- });
- }
- }}
- onSubmit={handleSubmit}
- onVariantAdd={handleVariantAdd}
- onVariantShow={variantId => () =>
- navigate(
- productVariantEditUrl(product.id, variantId)
- )}
- onImageUpload={event => {
- if (product) {
- createProductImage.mutate({
- alt: "",
- image: event.target.files[0],
- product: product.id
- });
- }
- }}
- onImageEdit={handleImageEdit}
- onImageDelete={handleImageDelete}
- />
- <Route
- path={productRemovePath(":id")}
- render={({ match }) => (
- <ActionDialog
- open={!!match}
- onClose={() => navigate(productUrl(id))}
- confirmButtonState={deleteTransitionState}
- onConfirm={() => deleteProduct.mutate({ id })}
- variant="delete"
- title={i18n.t("Remove product")}
- >
- <DialogContentText
- dangerouslySetInnerHTML={{
- __html: i18n.t(
- "Are you sure you want to remove <strong>{{ name }}</strong>?",
- {
- name: product
- ? product.name
- : undefined
- }
+ updateProduct.opts.data.productUpdate.errors
+ )
+ );
+ const deleteTransitionState = getMutationState(
+ deleteProduct.opts.called,
+ deleteProduct.opts.loading,
+ maybe(
+ () =>
+ deleteProduct.opts.data.productDelete.errors
+ )
+ );
+ return (
+ <>
+ <WindowTitle
+ title={maybe(() => data.product.name)}
+ />
+ <ProductUpdatePage
+ categories={maybe(
+ () =>
+ searchCategoriesOpts.data.categories
+ .edges,
+ []
+ ).map(edge => edge.node)}
+ collections={allCollections}
+ disabled={disableFormSave}
+ errors={maybe(
+ () =>
+ updateProduct.opts.data.productUpdate
+ .errors,
+ []
+ )}
+ fetchCategories={searchCategories}
+ saveButtonBarState={formTransitionState}
+ images={maybe(() => data.product.images)}
+ header={maybe(() => product.name)}
+ placeholderImage={placeholderImg}
+ product={product}
+ productCollections={maybe(
+ () => product.collections
+ )}
+ variants={maybe(() => product.variants)}
+ onAttributesEdit={() =>
+ navigate(
+ productTypeUrl(
+ data.product.productType.id
)
- }}
- />
- </ActionDialog>
- )}
- />
- </>
- );
- }}
- </ProductUpdateOperations>
- );
- }}
- </TypedProductDetailsQuery>
+ )
+ }
+ onBack={() => {
+ navigate(productListUrl());
+ }}
+ onDelete={() =>
+ navigate(productRemoveUrl(id))
+ }
+ onProductShow={() => {
+ if (product) {
+ window.open(product.url);
+ }
+ }}
+ onImageReorder={({ newIndex, oldIndex }) => {
+ if (product) {
+ let ids = product.images.map(
+ image => image.id
+ );
+ ids = arrayMove(ids, oldIndex, newIndex);
+ reorderProductImages.mutate({
+ imagesIds: ids,
+ productId: product.id
+ });
+ }
+ }}
+ onSubmit={handleSubmit}
+ onVariantAdd={handleVariantAdd}
+ onVariantShow={variantId => () =>
+ navigate(
+ productVariantEditUrl(
+ product.id,
+ variantId
+ )
+ )}
+ onImageUpload={event => {
+ if (product) {
+ createProductImage.mutate({
+ alt: "",
+ image: event.target.files[0],
+ product: product.id
+ });
+ }
+ }}
+ onImageEdit={handleImageEdit}
+ onImageDelete={handleImageDelete}
+ />
+ <Route
+ path={productRemovePath(":id")}
+ render={({ match }) => (
+ <ActionDialog
+ open={!!match}
+ onClose={() => navigate(productUrl(id))}
+ confirmButtonState={deleteTransitionState}
+ onConfirm={() =>
+ deleteProduct.mutate({ id })
+ }
+ variant="delete"
+ title={i18n.t("Remove product")}
+ >
+ <DialogContentText
+ dangerouslySetInnerHTML={{
+ __html: i18n.t(
+ "Are you sure you want to remove <strong>{{ name }}</strong>?",
+ {
+ name: product
+ ? product.name
+ : undefined
+ }
+ )
+ }}
+ />
+ </ActionDialog>
+ )}
+ />
+ </>
+ );
+ }}
+ </ProductUpdateOperations>
+ );
+ }}
+ </TypedProductDetailsQuery>
+ )}
+ </CategorySearchProvider>
);
}}
</Navigator>