Skip to content

Commit

Permalink
fix2
Browse files Browse the repository at this point in the history
  • Loading branch information
Иван Лайер committed Oct 29, 2024
1 parent 80695cb commit aa2c369
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 10 deletions.
9 changes: 8 additions & 1 deletion src/entities/ViewProductList/ViewProductList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,14 @@ export const ViewProductList = ({ isEditMode }: IViewProductListProps) => {
setIsOpenAddCategoryModal(true)
}}
onClose={() => {
setIsOpenAddProductModal(false)
dispatcher(productGet(
{
pageSize: pagination.maxOnPage,
pageNumber: pagination.currentPage,
sorting: pagination.sort,
}
));
setIsOpenAddProductModal(false);
}}
/>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ import { z } from 'zod';
import s from './AddProductModal.modal.sass';
import { zodResolver } from '@hookform/resolvers/zod';
import axiosInstance from "../../../../shared/axiosHelper/axiosHelper";
import { useAppDispatch } from "../../../../store/hooks";
import { useAppDispatch, useAppSelector } from "../../../../store/hooks";
import { productAdd } from "../../../../store/slices/saga/addProductSaga";
import { useNavigate } from "react-router-dom";
import { productGet } from "../../../../store/slices/saga/getProductSaga";

const schema = z.object({
name: z.string().min(3, { message: 'Минимальная длина имени товара 3 символа' }),
photo: z.string().nullable(),
photo: z.string({ message: 'Укажите фотографию' }),
desc: z.string().nullable(),
oldPrice: z.preprocess((p) => {
const res = p === '' ? undefined : Number(p);
Expand All @@ -32,7 +34,7 @@ export interface IAddProductModalProps {

export const AddProductModal = ({ isOpen, category, onAddCategory, onClose }: IAddProductModalProps) => {
const dispatcher = useAppDispatch();

const {
register,
handleSubmit,
Expand All @@ -55,7 +57,7 @@ export const AddProductModal = ({ isOpen, category, onAddCategory, onClose }: IA
}
}, [category]);

const onSubmit: SubmitHandler<TAddProductParams> = (data) => {
const onSubmit: SubmitHandler<TAddProductParams> = async (data) => {
const newProduct: TAddProductParams = {
name: data.name,
price: data.price,
Expand All @@ -64,7 +66,7 @@ export const AddProductModal = ({ isOpen, category, onAddCategory, onClose }: IA
desc: data.desc,
oldPrice: data.oldPrice,
}
dispatcher(productAdd(newProduct))
await dispatcher(productAdd(newProduct));

onClose();
}
Expand Down
5 changes: 2 additions & 3 deletions src/store/slices/saga/addProductSaga.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ import { UNKNOWN_ERROR_MESSAGE } from "./constant";
import { Category, Product, TAddProductParams, TUpdateProductParams } from "../../../entities/ViewProductList/model/types/types";
import { addProductApi, putProductApi } from "../../../entities/ViewProductList/api/request";
import { setError, cleanProductList } from "../productSlice";
import { productGet } from "./getProductSaga";
import { getProductSaga, productGet } from "./getProductSaga";

// Saga Effects. Add product
export function* addProductSaga(data: { type: string, payload: TAddProductParams }): any {
try {
yield addProductApi(data.payload);
yield put(cleanProductList())
yield call(productGet, { pageSize: 10, pageNumber: 1, sorting: { type: 'ASC', field: 'id' } });

} catch (error: unknown) {
if (isTErrorResponse(error)) {
let allErrors = "";
Expand Down
1 change: 1 addition & 0 deletions src/store/slices/saga/getProductSaga.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export type TGetProductSagaProps = {

// Saga Effects. get product
export function* getProductSaga(data: { type: string, payload: TGetProductSagaProps }): any {
console.log('getProductSaga: ')
try {
yield put(setError({ isError: false, errorMessage: "" }));
yield put(setIsLoading(true));
Expand Down
4 changes: 3 additions & 1 deletion src/store/slices/saga/updateProductSaga.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { setError, updateProductList } from "../productSlice";
// Saga Effects. Update product
export function* updateProductSaga(data: { type: string, payload: Product }): any {
try {

const updateProduct: TUpdateProductParams = {
id: data.payload.id,
name: data.payload.name,
Expand All @@ -18,6 +17,9 @@ export function* updateProductSaga(data: { type: string, payload: Product }): an
price: data.payload.price,
categoryId: data.payload.category.id,
commandId: data.payload.commandId,
photo: data.payload.photo,
desc: data.payload.desc,
oldPrice: data.payload.oldPrice,
}

yield putProductApi(updateProduct);
Expand Down

0 comments on commit aa2c369

Please sign in to comment.