diff --git a/src/pages/catalog/index.tsx b/src/pages/catalog/index.tsx index 5c734af..46702dc 100644 --- a/src/pages/catalog/index.tsx +++ b/src/pages/catalog/index.tsx @@ -1,7 +1,7 @@ import { CheckboxGroup, Checkbox, - Button, Skeleton, Accordion, AccordionItem + Button, Skeleton, Accordion, AccordionItem, Pagination, Spinner } from "@nextui-org/react"; import Link from "next/link"; @@ -16,37 +16,79 @@ import Wrapper from "@/components/reusable/wrapper"; import {useMutation, useQuery, useQueryClient} from "@tanstack/react-query"; import LocalAPI from "@/service/localAPI"; import {Img} from "react-image"; +import {Dispatch, SetStateAction, useEffect, useState} from "react"; +import {element} from "prop-types"; +type SelectedFiltersStruct = { + [key: string]: string[] +} +type SelectedFiltersDispatcher = Dispatch> +const CheckboxUI = (obj: { id: number, code: string, name: string, values: { id: number, value: string }[] } & { + dispatcher: SelectedFiltersDispatcher +}) => { -const CheckboxUI = (obj: {id: number, name: string, values: {id: number, value: string}[]}) => { return ( - {obj.values && obj.values.map(val => ( - {val.value} - ))} + {obj.values && obj.values.map(val => ( + { + obj.dispatcher((prevState) => { + + if (prevState[obj.code] && prevState[obj.code].includes(val.value)) { + return { + ...prevState, + [obj.code]: prevState[obj.code].filter(v => v !== val.value) + } + } else { + return { + ...prevState, + [obj.code]: [...(prevState[obj.code] || []), val.value] + } + } + }) + } + } value={String(val.id)} key={"VALUE_" + val.id}>{val.value} + ))} ) } -const FilterGenerator = ({filterPropertiesData}: Pick, "filterPropertiesData">) => { +const FilterGenerator = ({ + filterPropertiesData, + setSelectedFilter + }: Pick, "filterPropertiesData"> & { + setSelectedFilter: SelectedFiltersDispatcher +}) => { return ( - - {filterPropertiesData.map(obj => ( - - ))} + + {filterPropertiesData.map(obj => ( + + ))} ) } - -type CardProps = InferGetStaticPropsType['catalog'][0] & { isFavourite?: boolean } +type CatalogItemStruct = { + id: number + code: string, + name: string, + properties: { + [key: string]: string | string[] + } + detailText: string, + price: { + [key: string]: number | null + } +} +type CardProps = CatalogItemStruct & { isFavourite?: boolean } const CatalogCard = (product: CardProps) => { @@ -84,7 +126,7 @@ const CatalogCard = (product: CardProps) => { isClient && product.properties.main_image && {product.name}}/> + loader={}/> }
{ const Catalog = (props: InferGetStaticPropsType) => { const {favourites} = useSnapshot(favouritesStore) + const [selectedFilters, setSelectedFilters] = useState({}) + const [page, setPage] = useState(1); + const [pageCount, setPageCount] = useState(1) + const [showPagination, setShowPagination] = useState(true); + + + useEffect(() => { + const service = new LocalAPI() + service.getCatalogItemsCount().then(data => setPageCount(Math.ceil(data / 10))) + + + }) + + const catalogQuery = useQuery({ + queryKey: ["catalog", {selectedFilters, page}], queryFn: async () => { + const service = new LocalAPI() + return await service.getCatalogItems(selectedFilters, page) + } + }) + return ( -
- +
+ { !Object.values(selectedFilters).filter(elem => elem.length).length && + { + setPage(page) + }}/> + } +
+
+
- { - props.catalog.map(product => + {catalogQuery.isFetching && + Array(10).fill(null).map((_, index) => ) + } + + {catalogQuery.data && + catalogQuery.data.map(product => <> @@ -150,25 +225,13 @@ export const getStaticProps = async () => { const filterData = await service.getFilters() as { id: number, name: string, - values: { id: number, value: string }[] - }[] - const catalogData = await service.getCatalogItems() as { - id: number code: string, - name: string, - properties: { - [key: string]: string | string[] - } - detailText: string, - price: { - [key: string]: number | null - } + values: { id: number, value: string }[] }[] return { props: { filterPropertiesData: filterData, - catalog: catalogData } } } \ No newline at end of file diff --git a/src/pages/index.tsx b/src/pages/index.tsx index 965a6f0..4648361 100644 --- a/src/pages/index.tsx +++ b/src/pages/index.tsx @@ -12,7 +12,7 @@ const Hero = () => {

Оформите заказ на сумму от 5000₽

- До 15 января и получите бесплатную доставку + И получите бесплатную доставку Перейти к покупке diff --git a/src/service/localAPI.ts b/src/service/localAPI.ts index 4d2b159..3b01517 100644 --- a/src/service/localAPI.ts +++ b/src/service/localAPI.ts @@ -27,8 +27,40 @@ class LocalAPI { }) } - async getCatalogItems() { - const {data} = await this.instance.get('/api/v1/catalog') + async getCatalogItemsCount(){ + const {data} = await this.instance.get<{status: number, info: string}>('/api/v1/catalog/count') + return +data.info + } + + async getCatalogItems(filters: { [key: string]: string[] }, page: number = 1) { + const dataFilters: {[key: string]: string} = {} + + for (const [key, value] of Object.entries(filters)) { + if (value.length === 0) { + continue + } + dataFilters[key] = value.join(',') + } + + if (Object.keys(dataFilters).length === 0) { + const {data} = await this.instance.get('/api/v1/catalog', { + params: { + limit: 10, + page, + isFilter: 0, + } + }) + return data + } + const {data} = await this.instance.get('/api/v1/catalog', { + params: { + limit: 10, + page, + isFilter: 1, + ...dataFilters + } + }) + return data }