diff --git a/src/client/index.ts b/src/client/index.ts index 7a7232e..60327c4 100644 --- a/src/client/index.ts +++ b/src/client/index.ts @@ -31,6 +31,7 @@ export type { DealDeleteServiceResponse } from './models/DealDeleteServiceRespon export type { DealDeleteServicesRequest } from './models/DealDeleteServicesRequest'; export type { DealDeleteServicesResponse } from './models/DealDeleteServicesResponse'; export type { DealGetAllResponse } from './models/DealGetAllResponse'; +export type { DealProductSchema } from './models/DealProductSchema'; export type { DealQuickCreateRequest } from './models/DealQuickCreateRequest'; export type { DealQuickCreateResponse } from './models/DealQuickCreateResponse'; export type { DealSchema } from './models/DealSchema'; diff --git a/src/client/models/DealProductSchema.ts b/src/client/models/DealProductSchema.ts new file mode 100644 index 0000000..c9ec006 --- /dev/null +++ b/src/client/models/DealProductSchema.ts @@ -0,0 +1,10 @@ +/* generated using openapi-typescript-codegen -- do no edit */ +/* istanbul ignore file */ +/* tslint:disable */ +/* eslint-disable */ +import type { ProductSchema } from './ProductSchema'; +export type DealProductSchema = { + product: ProductSchema; + quantity: number; +}; + diff --git a/src/client/models/DealSchema.ts b/src/client/models/DealSchema.ts index 677e9d6..777317f 100644 --- a/src/client/models/DealSchema.ts +++ b/src/client/models/DealSchema.ts @@ -2,6 +2,7 @@ /* istanbul ignore file */ /* tslint:disable */ /* eslint-disable */ +import type { DealProductSchema } from './DealProductSchema'; import type { DealServiceSchema } from './DealServiceSchema'; export type DealSchema = { id: number; @@ -10,5 +11,6 @@ export type DealSchema = { createdAt: string; currentStatus: number; services: Array; + products: Array; }; diff --git a/src/client/services/ProductService.ts b/src/client/services/ProductService.ts index be807db..4ffeb3b 100644 --- a/src/client/services/ProductService.ts +++ b/src/client/services/ProductService.ts @@ -84,8 +84,8 @@ export class ProductService { itemsPerPage, }: { clientId: number, - page: number, - itemsPerPage: number, + page?: (number | null), + itemsPerPage?: (number | null), }): CancelablePromise { return __request(OpenAPI, { method: 'GET', diff --git a/src/components/ProductSelect/ProductSelect.tsx b/src/components/ProductSelect/ProductSelect.tsx new file mode 100644 index 0000000..d7df780 --- /dev/null +++ b/src/components/ProductSelect/ProductSelect.tsx @@ -0,0 +1,58 @@ +import {ProductSchema} from "../../client"; +import {Select, SelectProps} from "@mantine/core"; +import {FC, useEffect, useMemo, useState} from "react"; +import useProductsList from "../../pages/ProductsPage/hooks/useProductsList.tsx"; + +type ControlledValueProps = { + value: ProductSchema; + onChange: (value: ProductSchema) => void; +} +type RestProps = { + defaultValue?: ProductSchema; + onChange: (value: ProductSchema) => void; + clientId: number; +} +type Props = (RestProps & Partial) & Omit; + +const ProductSelect: FC = (props) => { + const isControlled = 'value' in props; + const [intertalValue, setInternalValue] = useState(props.defaultValue); + const value = isControlled ? props.value : intertalValue + + const {products} = useProductsList({clientId: props.clientId}); + + + const data = useMemo(() => products.reduce((acc, product) => { + acc.push({ + label: product.name, + value: product.id.toString() + }); + return acc; + }, [] as { label: string, value: string }[]), [products]); + + const handleOnChange = (event: string | null) => { + if (!event) return; + const product = products.find(product => parseInt(event) == product.id); + if (!product) return; + if (isControlled) { + props.onChange(product); + return; + } + setInternalValue(product); + } + useEffect(() => { + if (isControlled || !intertalValue) return; + props.onChange(intertalValue); + }, [intertalValue]); + return ( +