Skip to content

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

Refactor attribute search

dominik-zeglen6b288d16842b
diff --git a/saleor/static/dashboard-next/productTypes/containers/AttributeSearch.tsx b/saleor/static/dashboard-next/productTypes/containers/AttributeSearch.tsx
new file mode 100644
index 0000000000..1b8d49eaac
--- /dev/null
+++ b/saleor/static/dashboard-next/productTypes/containers/AttributeSearch.tsx
@@ -0,0 +1,44 @@
+import * as React from "react";
+import { QueryResult } from "react-apollo";
+import {
+ SearchAttributeQuery,
+ SearchAttributeQueryVariables
+} from "../../gql-types";
+import { searchAttributeQuery, TypedSearchAttributeQuery } from "../queries";
+
+interface AttributeSearchProviderProps {
+ children:
+ | ((
+ search: (query: string) => void,
+ props: QueryResult<SearchAttributeQuery, SearchAttributeQueryVariables>
+ ) => React.ReactElement<any>)
+ | React.ReactNode;
+}
+interface AttributeSearchProviderState {
+ query: string;
+}
+
+export class AttributeSearchProvider extends React.Component<
+ AttributeSearchProviderProps,
+ AttributeSearchProviderState
+> {
+ state: AttributeSearchProviderState = { query: "" };
+
+ search = (query: string) => this.setState({ query });
+
+ render() {
+ const { children } = this.props;
+ if (typeof children === "function") {
+ return (
+ <TypedSearchAttributeQuery
+ query={searchAttributeQuery}
+ variables={{ search: this.state.query }}
+ skip={!this.state.query}
+ >
+ {props => children(this.search, props)}
+ </TypedSearchAttributeQuery>
+ );
+ }
+ return this.props.children;
+ }
+}
diff --git a/saleor/static/dashboard-next/productTypes/views/ProductTypeCreate.tsx b/saleor/static/dashboard-next/productTypes/views/ProductTypeCreate.tsx
index d908a9a4c0..40efa24c8c 100644
--- a/saleor/static/dashboard-next/productTypes/views/ProductTypeCreate.tsx
+++ b/saleor/static/dashboard-next/productTypes/views/ProductTypeCreate.tsx
@@ -8,11 +8,11 @@ import i18n from "../../i18n";
import ProductTypeDetailsPage, {
ProductTypeForm
} from "../components/ProductTypeDetailsPage";
+import { AttributeSearchProvider } from "../containers/AttributeSearch";
import {
productTypeCreateMutation,
TypedProductTypeCreateMutation
} from "../mutations";
-import { searchAttributeQuery, TypedSearchAttributeQuery } from "../queries";
const taxRates = ["standard", "electronics", "food", "apparel"]; // FIXME: delet dis
@@ -30,10 +30,8 @@ export const ProductTypeUpdate: React.StatelessComponent = () => (
{pushMessage => (
<Navigator>
{navigate => (
- <TypedSearchAttributeQuery query={searchAttributeQuery}>
- {searchAttribute => {
- const handleSearchAttribute = (search: string) =>
- search ? searchAttribute.refetch({ search }) : undefined;
+ <AttributeSearchProvider>
+ {(searchAttribute, searchState) => {
const handleCreateSuccess = (
updateData: ProductTypeCreateMutation
) => {
@@ -85,17 +83,20 @@ export const ProductTypeUpdate: React.StatelessComponent = () => (
saveButtonBarState={
loadingCreate ? "loading" : "default"
}
- searchLoading={searchAttribute.loading}
+ searchLoading={
+ searchState ? searchState.loading : false
+ }
searchResults={
- searchAttribute.data &&
- searchAttribute.data.attributes
- ? searchAttribute.data.attributes.edges.map(
+ searchState &&
+ searchState.data &&
+ searchState.data.attributes
+ ? searchState.data.attributes.edges.map(
edge => edge.node
)
: []
}
taxRates={taxRates}
- onAttributeSearch={handleSearchAttribute}
+ onAttributeSearch={searchAttribute}
onBack={() => navigate(productTypeListUrl)}
onSubmit={handleCreate}
/>
@@ -104,7 +105,7 @@ export const ProductTypeUpdate: React.StatelessComponent = () => (
</TypedProductTypeCreateMutation>
);
}}
- </TypedSearchAttributeQuery>
+ </AttributeSearchProvider>
)}
</Navigator>
)}
diff --git a/saleor/static/dashboard-next/productTypes/views/ProductTypeUpdate.tsx b/saleor/static/dashboard-next/productTypes/views/ProductTypeUpdate.tsx
index 1eb2ad025e..3a87dee341 100644
--- a/saleor/static/dashboard-next/productTypes/views/ProductTypeUpdate.tsx
+++ b/saleor/static/dashboard-next/productTypes/views/ProductTypeUpdate.tsx
@@ -12,12 +12,11 @@ import i18n from "../../i18n";
import ProductTypeDetailsPage, {
ProductTypeForm
} from "../components/ProductTypeDetailsPage";
+import { AttributeSearchProvider } from "../containers/AttributeSearch";
import ProductTypeOperations from "../containers/ProductTypeOperations";
import {
productTypeDetailsQuery,
- searchAttributeQuery,
- TypedProductTypeDetailsQuery,
- TypedSearchAttributeQuery
+ TypedProductTypeDetailsQuery
} from "../queries";
const taxRates = ["standard", "electronics", "food", "apparel"]; // FIXME: delet dis
@@ -46,10 +45,8 @@ export const ProductTypeUpdate: React.StatelessComponent<
);
}
return (
- <TypedSearchAttributeQuery query={searchAttributeQuery}>
- {searchAttribute => {
- const handleSearchAttribute = (search: string) =>
- search ? searchAttribute.refetch({ search }) : undefined;
+ <AttributeSearchProvider>
+ {(searchAttribute, searchState) => {
const handleDeleteSuccess = (
deleteData: ProductTypeDeleteMutation
) => {
@@ -131,27 +128,31 @@ export const ProductTypeUpdate: React.StatelessComponent<
saveButtonBarState={
loading ? "loading" : "default"
}
- searchLoading={searchAttribute.loading}
+ searchLoading={
+ searchState ? searchState.loading : false
+ }
searchResults={
- searchAttribute.data &&
- searchAttribute.data.attributes
- ? searchAttribute.data.attributes.edges.map(
+ searchState &&
+ searchState.data &&
+ searchState.data.attributes
+ ? searchState.data.attributes.edges.map(
edge => edge.node
)
: []
}
taxRates={taxRates}
- onAttributeSearch={handleSearchAttribute}
+ onAttributeSearch={searchAttribute}
onBack={() => navigate(productTypeListUrl)}
onDelete={handleDelete}
onSubmit={handleUpdate}
+ key={searchState ? undefined : "search"}
/>
);
}}
</ProductTypeOperations>
);
}}
- </TypedSearchAttributeQuery>
+ </AttributeSearchProvider>
);
}}
</TypedProductTypeDetailsQuery>