From a9b5d57fc935e7bd331b9e3d36453f8b67151de8 Mon Sep 17 00:00:00 2001 From: Oleg Urin Date: Fri, 21 Nov 2025 18:32:59 +0300 Subject: [PATCH] =?UTF-8?q?=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=B8?= =?UTF-8?q?=D0=BB=20=D1=85=D1=80=D0=B0=D0=BD=D0=B8=D0=BB=D0=B8=D1=89=D0=B5?= =?UTF-8?q?=20=D0=B8=20=D1=81=D0=BB=D0=B0=D0=B9=D1=81=D1=8B=20=D0=B4=D0=BB?= =?UTF-8?q?=D1=8F=20=D1=81=D1=82=D1=80=D0=B0=D0=BD=D0=B8=D1=86=D1=8B=20?= =?UTF-8?q?=D0=BA=D0=B0=D0=B8=D0=B0=D0=BB=D0=BE=D0=B3=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- front/src/app/store.ts | 7 ++-- front/src/features/catalog/catalogSlice.ts | 35 +++++++++++-------- front/src/features/catalog/categoriesSlice.ts | 7 ++-- 3 files changed, 28 insertions(+), 21 deletions(-) diff --git a/front/src/app/store.ts b/front/src/app/store.ts index 11ebdd1..b7c77eb 100644 --- a/front/src/app/store.ts +++ b/front/src/app/store.ts @@ -3,12 +3,13 @@ import catalogReducer from "../features/catalog/catalogSlice"; export const store = configureStore({ reducer: { - catalog: catalogReducer, + categories: categoriesReducer, + products: productsReducer, }, - middleware: (getDefaultMiddleware) => + /*middleware: (getDefaultMiddleware) => getDefaultMiddleware({ serializableCheck: false, // отключаем для async thunk - }), + }),*/ }); export type RootState = ReturnType; diff --git a/front/src/features/catalog/catalogSlice.ts b/front/src/features/catalog/catalogSlice.ts index 5e0c8e2..bfa06a0 100644 --- a/front/src/features/catalog/catalogSlice.ts +++ b/front/src/features/catalog/catalogSlice.ts @@ -2,6 +2,7 @@ import { createAsyncThunk, createSlice } from "@reduxjs/toolkit"; import type {PayloadAction} from "@reduxjs/toolkit"; import { fetchCategoryProducts, fetchCategories } from "../../api/catalogApi"; import type { Product } from "../../types/product"; +import axios from "axios"; interface CatalogState { products: Product[]; @@ -21,21 +22,21 @@ const initialState: CatalogState = { currentCategory: null, }; -export const getCategories = createAsyncThunk( - "catalog/getCategories", - async (categoryid: string) => { - const data = await fetchCategories(categoryid); - return data; +export const fetchProductsByCategory = createAsyncThunk( + "products/fetchProductsByCategory", + async (categoryId: number) => { + const res = await axios.get(`/api/products?category=${categoryId}`); + return res.data; } ); -export const getCategoryProducts = createAsyncThunk( - "catalog/getCategoryProducts", - async (category: string) => { - const data = await fetchCategoryProducts(category); - return data; +export const fetchCategoryParameters = createAsyncThunk( + "products/fetchCategoryParametrs", + async (categoryId: number) => { + const res = await axios.get(`/api/category-params/${categoryId}`); + return res.data; } -); +) const catalogSlice = createSlice({ name: "catalog", @@ -58,19 +59,23 @@ const catalogSlice = createSlice({ }, extraReducers: (builder) => { builder - .addCase(getCategoryProducts.pending, (state) => { + .addCase(fetchProductsByCategory.pending, (state) => { state.loading = true; state.error = null; }) - .addCase(getCategoryProducts.fulfilled, (state, action) => { + .addCase(fetchProductsByCategory.fulfilled, (state, action) => { state.loading = false; state.products = action.payload; state.filtered = action.payload; }) - .addCase(getCategoryProducts.rejected, (state, action) => { + .addCase(fetchCategoryParameters.fulfilled, (state, action) => { + state.loading = false; + state.filters = action.payload + }) + /*.addCase(getCategoryProducts.rejected, (state, action) => { state.loading = false; state.error = action.error.message || "Ошибка загрузки данных"; - }); + });*/ }, }); diff --git a/front/src/features/catalog/categoriesSlice.ts b/front/src/features/catalog/categoriesSlice.ts index ecd87d1..4c38a87 100644 --- a/front/src/features/catalog/categoriesSlice.ts +++ b/front/src/features/catalog/categoriesSlice.ts @@ -1,11 +1,12 @@ import { createSlice, createAsyncThunk } from "@reduxjs/toolkit"; import type { Category } from "../../types/categories"; +import axios from "axios"; export const fetchCategories = createAsyncThunk( - "categories/fetch", + "categories/fetchCategories", async () => { - const res = await fetch("/api/categories"); - return await res.json(); + const res = await axios.get("/api/categories"); + return res.data; } );