|
1 | 1 | import { useMutation } from "@tanstack/react-query"; |
2 | 2 |
|
3 | | -import { DateVO } from "@/lib/date/date"; |
4 | 3 | import { httpService } from "@/lib/http"; |
5 | 4 | import { Logger } from "@/lib/logger"; |
6 | 5 | import { UnknownError } from "@/lib/types/unknown-error"; |
7 | 6 |
|
8 | 7 | interface AddToCartPayload { |
9 | 8 | productId: number; |
10 | 9 | quantity?: number; |
11 | | - cartId: number; |
12 | | - userId: number; |
13 | 10 | } |
14 | 11 |
|
15 | 12 | interface AddToCartDto { |
16 | | - userId: number; |
17 | | - date: string; |
18 | | - products: { productId: number; quantity: number }[]; |
| 13 | + cartId: number; |
| 14 | + payload: AddToCartPayload; |
19 | 15 | } |
20 | 16 |
|
21 | 17 | export const useAddToCartMutation = () => { |
22 | | - const { mutateAsync, isPending } = useMutation< |
23 | | - void, |
24 | | - unknown, |
25 | | - AddToCartPayload |
26 | | - >({ |
| 18 | + const { mutateAsync, isPending } = useMutation<void, unknown, AddToCartDto>({ |
27 | 19 | mutationFn: (body) => |
28 | | - httpService.put<void, AddToCartDto>(`carts/${body.cartId}`, { |
29 | | - userId: body.userId, |
30 | | - date: DateVO.now(), |
31 | | - products: [{ productId: body.productId, quantity: body.quantity ?? 1 }], |
| 20 | + httpService.put<void, AddToCartPayload>(`carts/${body.cartId}`, { |
| 21 | + productId: body.payload.productId, |
| 22 | + quantity: body.payload.quantity, |
32 | 23 | }), |
33 | 24 | }); |
34 | 25 |
|
35 | | - const handler = async (body: AddToCartPayload) => { |
| 26 | + const handler = async (cartId: number, payload: AddToCartPayload) => { |
36 | 27 | try { |
37 | | - return await mutateAsync(body); |
| 28 | + return await mutateAsync({ cartId, payload }); |
38 | 29 | } catch (e) { |
39 | 30 | Logger.error( |
40 | 31 | "An error occurred during adding an item to the cart", |
|
0 commit comments