feat: product select
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
import {ProductSchema} from "../../client";
|
import {ProductSchema} from "../../client";
|
||||||
import {FC, useEffect, useState} from "react";
|
import {FC, useState} from "react";
|
||||||
import useProductsList from "../../pages/ProductsPage/hooks/useProductsList.tsx";
|
import useProductsList from "../../pages/ProductsPage/hooks/useProductsList.tsx";
|
||||||
import {omit} from "lodash";
|
import {omit} from "lodash";
|
||||||
import ObjectSelect, {ObjectSelectProps} from "../ObjectSelect/ObjectSelect.tsx";
|
import ObjectSelect, {ObjectSelectProps} from "../ObjectSelect/ObjectSelect.tsx";
|
||||||
@@ -14,13 +14,12 @@ const MAX_PRODUCTS = 200;
|
|||||||
type Props = Omit<ObjectSelectProps<ProductSchema>, 'data'> & RestProps;
|
type Props = Omit<ObjectSelectProps<ProductSchema>, 'data'> & RestProps;
|
||||||
const ProductSelect: FC<Props> = (props: Props) => {
|
const ProductSelect: FC<Props> = (props: Props) => {
|
||||||
const [searchValue, setSearchValue] = useState("");
|
const [searchValue, setSearchValue] = useState("");
|
||||||
const [debouncedSearch] = useDebouncedValue(searchValue, 1000);
|
const [debounced] = useDebouncedValue(searchValue, 500);
|
||||||
const [itemsPerPage, setItemsPerPage] = useState(1);
|
const {products, isLoading} = useProductsList({
|
||||||
const {products, isLoading, paginationInfo} = useProductsList({
|
|
||||||
clientId: props.clientId,
|
clientId: props.clientId,
|
||||||
searchInput: debouncedSearch,
|
searchInput: debounced,
|
||||||
page: 0,
|
page: 0,
|
||||||
itemsPerPage: itemsPerPage
|
itemsPerPage: MAX_PRODUCTS
|
||||||
});
|
});
|
||||||
const restProps = omit(props, ['clientId']);
|
const restProps = omit(props, ['clientId']);
|
||||||
const renderOption: SelectProps['renderOption'] = (item) => {
|
const renderOption: SelectProps['renderOption'] = (item) => {
|
||||||
@@ -53,6 +52,7 @@ const ProductSelect: FC<Props> = (props: Props) => {
|
|||||||
</Tooltip>)
|
</Tooltip>)
|
||||||
}
|
}
|
||||||
const optionsFilter: OptionsFilter = ({options, search}) => {
|
const optionsFilter: OptionsFilter = ({options, search}) => {
|
||||||
|
return options;
|
||||||
const filtered = (options as ComboboxItem[]).filter((option) => {
|
const filtered = (options as ComboboxItem[]).filter((option) => {
|
||||||
const product = products.find(product => product.id == parseInt(option.value));
|
const product = products.find(product => product.id == parseInt(option.value));
|
||||||
if (!product) return true;
|
if (!product) return true;
|
||||||
@@ -65,30 +65,19 @@ const ProductSelect: FC<Props> = (props: Props) => {
|
|||||||
return filtered.length > MAX_PRODUCTS ? filtered.slice(0, MAX_PRODUCTS) : filtered;
|
return filtered.length > MAX_PRODUCTS ? filtered.slice(0, MAX_PRODUCTS) : filtered;
|
||||||
};
|
};
|
||||||
const setSearchValueImpl = (value: string) => {
|
const setSearchValueImpl = (value: string) => {
|
||||||
if (!paginationInfo) return;
|
const names = products.map(product => product.name);
|
||||||
if (paginationInfo.totalItems === itemsPerPage) {
|
if (names.includes(value)) return;
|
||||||
setSearchValue("");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
setSearchValue(value);
|
setSearchValue(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (!paginationInfo) return;
|
|
||||||
if (paginationInfo.totalItems < MAX_PRODUCTS)
|
|
||||||
setItemsPerPage(paginationInfo.totalItems);
|
|
||||||
else
|
|
||||||
setItemsPerPage(MAX_PRODUCTS);
|
|
||||||
}, [paginationInfo]);
|
|
||||||
return (
|
return (
|
||||||
<ObjectSelect
|
<ObjectSelect
|
||||||
disabled={isLoading}
|
// disabled={isLoading}
|
||||||
rightSection={
|
rightSection={
|
||||||
(isLoading || searchValue !== debouncedSearch) ?
|
(isLoading || searchValue !== debounced) ?
|
||||||
<Loader size={"sm"}/> : null
|
<Loader size={"sm"}/> : null
|
||||||
}
|
}
|
||||||
onSearchChange={setSearchValueImpl}
|
onSearchChange={setSearchValueImpl}
|
||||||
limit={products.length > MAX_PRODUCTS ? MAX_PRODUCTS : Infinity}
|
|
||||||
renderOption={renderOption}
|
renderOption={renderOption}
|
||||||
searchable
|
searchable
|
||||||
{...restProps}
|
{...restProps}
|
||||||
|
|||||||
Reference in New Issue
Block a user