Use selects with loadMore option

This commit is contained in:
dominik-zeglen 2019-10-15 15:11:38 +02:00
parent 7ebf8fc254
commit 035271adf0
4 changed files with 58 additions and 4 deletions

View file

@ -117,6 +117,7 @@ const SingleAutocompleteSelectFieldComponent = withStyles(styles, {
} }
const displayCustomValue = const displayCustomValue =
inputValue &&
inputValue.length > 0 && inputValue.length > 0 &&
allowCustomValues && allowCustomValues &&
!choices.find( !choices.find(

View file

@ -27,7 +27,7 @@ import {
} from "@saleor/products/utils/data"; } from "@saleor/products/utils/data";
import createMultiAutocompleteSelectHandler from "@saleor/utils/handlers/multiAutocompleteSelectChangeHandler"; import createMultiAutocompleteSelectHandler from "@saleor/utils/handlers/multiAutocompleteSelectChangeHandler";
import createSingleAutocompleteSelectHandler from "@saleor/utils/handlers/singleAutocompleteSelectChangeHandler"; import createSingleAutocompleteSelectHandler from "@saleor/utils/handlers/singleAutocompleteSelectChangeHandler";
import { UserError } from "../../../types"; import { FetchMoreProps, UserError } from "../../../types";
import { import {
createAttributeChangeHandler, createAttributeChangeHandler,
createAttributeMultiChangeHandler, createAttributeMultiChangeHandler,
@ -67,6 +67,9 @@ interface ProductCreatePageProps {
categories: SearchCategories_search_edges_node[]; categories: SearchCategories_search_edges_node[];
currency: string; currency: string;
disabled: boolean; disabled: boolean;
fetchMoreCategories: FetchMoreProps;
fetchMoreCollections: FetchMoreProps;
fetchMoreProductTypes: FetchMoreProps;
productTypes?: Array<{ productTypes?: Array<{
id: string; id: string;
name: string; name: string;
@ -92,6 +95,9 @@ export const ProductCreatePage: React.StatelessComponent<
errors: userErrors, errors: userErrors,
fetchCategories, fetchCategories,
fetchCollections, fetchCollections,
fetchMoreCategories,
fetchMoreCollections,
fetchMoreProductTypes,
header, header,
productTypes: productTypeChoiceList, productTypes: productTypeChoiceList,
saveButtonBarState, saveButtonBarState,
@ -271,6 +277,9 @@ export const ProductCreatePage: React.StatelessComponent<
errors={errors} errors={errors}
fetchCategories={fetchCategories} fetchCategories={fetchCategories}
fetchCollections={fetchCollections} fetchCollections={fetchCollections}
fetchMoreCategories={fetchMoreCategories}
fetchMoreCollections={fetchMoreCollections}
fetchMoreProductTypes={fetchMoreProductTypes}
fetchProductTypes={fetchProductTypes} fetchProductTypes={fetchProductTypes}
productType={productType} productType={productType}
productTypeInputDisplayValue={productType.name} productTypeInputDisplayValue={productType.name}

View file

@ -22,7 +22,7 @@ import SingleAutocompleteSelectField, {
} from "@saleor/components/SingleAutocompleteSelectField"; } from "@saleor/components/SingleAutocompleteSelectField";
import { ChangeEvent } from "@saleor/hooks/useForm"; import { ChangeEvent } from "@saleor/hooks/useForm";
import { maybe } from "@saleor/misc"; import { maybe } from "@saleor/misc";
import { FormErrors } from "@saleor/types"; import { FetchMoreProps, FormErrors } from "@saleor/types";
interface ProductType { interface ProductType {
hasVariants: boolean; hasVariants: boolean;
@ -62,6 +62,9 @@ interface ProductOrganizationProps extends WithStyles<typeof styles> {
productTypes?: SingleAutocompleteChoiceType[]; productTypes?: SingleAutocompleteChoiceType[];
fetchCategories: (query: string) => void; fetchCategories: (query: string) => void;
fetchCollections: (query: string) => void; fetchCollections: (query: string) => void;
fetchMoreCategories: FetchMoreProps;
fetchMoreCollections: FetchMoreProps;
fetchMoreProductTypes?: FetchMoreProps;
fetchProductTypes?: (data: string) => void; fetchProductTypes?: (data: string) => void;
onCategoryChange: (event: ChangeEvent) => void; onCategoryChange: (event: ChangeEvent) => void;
onCollectionChange: (event: ChangeEvent) => void; onCollectionChange: (event: ChangeEvent) => void;
@ -81,6 +84,9 @@ const ProductOrganization = withStyles(styles, { name: "ProductOrganization" })(
errors, errors,
fetchCategories, fetchCategories,
fetchCollections, fetchCollections,
fetchMoreCategories,
fetchMoreCollections,
fetchMoreProductTypes,
fetchProductTypes, fetchProductTypes,
productType, productType,
productTypeInputDisplayValue, productTypeInputDisplayValue,
@ -115,6 +121,7 @@ const ProductOrganization = withStyles(styles, { name: "ProductOrganization" })(
onChange={onProductTypeChange} onChange={onProductTypeChange}
fetchChoices={fetchProductTypes} fetchChoices={fetchProductTypes}
data-tc="product-type" data-tc="product-type"
{...fetchMoreProductTypes}
/> />
) : ( ) : (
<> <>
@ -160,6 +167,7 @@ const ProductOrganization = withStyles(styles, { name: "ProductOrganization" })(
onChange={onCategoryChange} onChange={onCategoryChange}
fetchChoices={fetchCategories} fetchChoices={fetchCategories}
data-tc="category" data-tc="category"
{...fetchMoreCategories}
/> />
<FormSpacer /> <FormSpacer />
<Hr /> <Hr />

View file

@ -33,11 +33,20 @@ export const ProductUpdate: React.StatelessComponent<
return ( return (
<SearchCategories variables={DEFAULT_INITIAL_SEARCH_DATA}> <SearchCategories variables={DEFAULT_INITIAL_SEARCH_DATA}>
{({ search: searchCategory, result: searchCategoryOpts }) => ( {({
loadMore: loadMoreCategories,
search: searchCategory,
result: searchCategoryOpts
}) => (
<SearchCollections variables={DEFAULT_INITIAL_SEARCH_DATA}> <SearchCollections variables={DEFAULT_INITIAL_SEARCH_DATA}>
{({ search: searchCollection, result: searchCollectionOpts }) => ( {({
loadMore: loadMoreCollections,
search: searchCollection,
result: searchCollectionOpts
}) => (
<SearchProductTypes variables={DEFAULT_INITIAL_SEARCH_DATA}> <SearchProductTypes variables={DEFAULT_INITIAL_SEARCH_DATA}>
{({ {({
loadMore: loadMoreProductTypes,
search: searchProductTypes, search: searchProductTypes,
result: searchProductTypesOpts result: searchProductTypesOpts
}) => { }) => {
@ -148,6 +157,33 @@ export const ProductUpdate: React.StatelessComponent<
onBack={handleBack} onBack={handleBack}
onSubmit={handleSubmit} onSubmit={handleSubmit}
saveButtonBarState={formTransitionState} saveButtonBarState={formTransitionState}
fetchMoreCategories={{
hasMore: maybe(
() =>
searchCategoryOpts.data.search.pageInfo
.hasNextPage
),
loading: searchCategoryOpts.loading,
onFetchMore: loadMoreCategories
}}
fetchMoreCollections={{
hasMore: maybe(
() =>
searchCollectionOpts.data.search.pageInfo
.hasNextPage
),
loading: searchCollectionOpts.loading,
onFetchMore: loadMoreCollections
}}
fetchMoreProductTypes={{
hasMore: maybe(
() =>
searchProductTypesOpts.data.search.pageInfo
.hasNextPage
),
loading: searchProductTypesOpts.loading,
onFetchMore: loadMoreProductTypes
}}
/> />
</> </>
); );