feat: product select
This commit is contained in:
@@ -1,10 +1,11 @@
|
|||||||
import {ProductSchema} from "../../client";
|
import {ProductSchema} from "../../client";
|
||||||
import {FC} from "react";
|
import {FC, useEffect, 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";
|
||||||
import {ComboboxItem, Image, Loader, OptionsFilter, rem, SelectProps, Text, Tooltip} from "@mantine/core";
|
import {ComboboxItem, Image, Loader, OptionsFilter, rem, SelectProps, Text, Tooltip} from "@mantine/core";
|
||||||
import {getProductFields} from "../../types/utils.ts";
|
import {getProductFields} from "../../types/utils.ts";
|
||||||
|
import {useDebouncedValue} from "@mantine/hooks";
|
||||||
|
|
||||||
type RestProps = {
|
type RestProps = {
|
||||||
clientId: number;
|
clientId: number;
|
||||||
@@ -12,9 +13,14 @@ type RestProps = {
|
|||||||
const MAX_PRODUCTS = 200;
|
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 {products, isLoading} = useProductsList({
|
const [searchValue, setSearchValue] = useState("");
|
||||||
|
const [debouncedSearch] = useDebouncedValue(searchValue, 1000);
|
||||||
|
const [itemsPerPage, setItemsPerPage] = useState(1);
|
||||||
|
const {products, isLoading, paginationInfo} = useProductsList({
|
||||||
clientId: props.clientId,
|
clientId: props.clientId,
|
||||||
searchInput: ""
|
searchInput: debouncedSearch,
|
||||||
|
page: 0,
|
||||||
|
itemsPerPage: itemsPerPage
|
||||||
});
|
});
|
||||||
const restProps = omit(props, ['clientId']);
|
const restProps = omit(props, ['clientId']);
|
||||||
const renderOption: SelectProps['renderOption'] = (item) => {
|
const renderOption: SelectProps['renderOption'] = (item) => {
|
||||||
@@ -38,10 +44,7 @@ const ProductSelect: FC<Props> = (props: Props) => {
|
|||||||
alt={product.name}
|
alt={product.name}
|
||||||
maw={rem(250)}
|
maw={rem(250)}
|
||||||
/>}
|
/>}
|
||||||
|
|
||||||
</>
|
</>
|
||||||
|
|
||||||
|
|
||||||
}>
|
}>
|
||||||
<div>
|
<div>
|
||||||
{product.name}<br/>
|
{product.name}<br/>
|
||||||
@@ -49,10 +52,7 @@ const ProductSelect: FC<Props> = (props: Props) => {
|
|||||||
</div>
|
</div>
|
||||||
</Tooltip>)
|
</Tooltip>)
|
||||||
}
|
}
|
||||||
|
|
||||||
const optionsFilter: OptionsFilter = ({options, search}) => {
|
const optionsFilter: OptionsFilter = ({options, search}) => {
|
||||||
|
|
||||||
|
|
||||||
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;
|
||||||
@@ -61,18 +61,33 @@ const ProductSelect: FC<Props> = (props: Props) => {
|
|||||||
product.article?.toLowerCase() === search.toLowerCase();
|
product.article?.toLowerCase() === search.toLowerCase();
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
filtered.sort((a, b) => a.label.localeCompare(b.label));
|
filtered.sort((a, b) => a.label.localeCompare(b.label));
|
||||||
|
|
||||||
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) => {
|
||||||
|
if (!paginationInfo) return;
|
||||||
|
if (paginationInfo.totalItems === itemsPerPage) {
|
||||||
|
setSearchValue("");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
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 ?
|
(isLoading || searchValue !== debouncedSearch) ?
|
||||||
<Loader size={"sm"}/> : null
|
<Loader size={"sm"}/> : null
|
||||||
}
|
}
|
||||||
|
onSearchChange={setSearchValueImpl}
|
||||||
limit={products.length > MAX_PRODUCTS ? MAX_PRODUCTS : Infinity}
|
limit={products.length > MAX_PRODUCTS ? MAX_PRODUCTS : Infinity}
|
||||||
renderOption={renderOption}
|
renderOption={renderOption}
|
||||||
searchable
|
searchable
|
||||||
|
|||||||
Reference in New Issue
Block a user