From 82d88f82bb0acc5d37966dc24c73cbdb7cade789 Mon Sep 17 00:00:00 2001 From: a-honey Date: Sat, 24 Feb 2024 03:07:43 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=EC=8B=9D=EC=9E=90=EC=9E=AC=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80=20api?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/molecules/Counter.tsx | 5 +- src/components/organisms/FridgeInfoBox.tsx | 4 +- .../organisms/IngredientAddModal.tsx | 65 ++++++++++++------- .../queries/fridge/useGetIngredientById.ts | 2 +- src/hooks/queries/fridge/usePostIngredient.ts | 9 +-- 5 files changed, 55 insertions(+), 30 deletions(-) diff --git a/src/components/molecules/Counter.tsx b/src/components/molecules/Counter.tsx index 251de57..b09fd97 100644 --- a/src/components/molecules/Counter.tsx +++ b/src/components/molecules/Counter.tsx @@ -1,4 +1,4 @@ -import React from 'react'; +import React, { useEffect } from 'react'; import type { CountState } from '@/hooks/useCount'; const Counter: React.FC = ({ @@ -6,12 +6,13 @@ const Counter: React.FC = ({ handleDecreaseCount, handleIncreaseCount, }) => { + useEffect(() => {}, [currentCount]); return (
- + diff --git a/src/components/organisms/FridgeInfoBox.tsx b/src/components/organisms/FridgeInfoBox.tsx index 3514384..17041fa 100644 --- a/src/components/organisms/FridgeInfoBox.tsx +++ b/src/components/organisms/FridgeInfoBox.tsx @@ -32,7 +32,9 @@ const FridgeInfoBox: React.FC<{ className="rounded-6 w-[100px] p-[10px] body1-semibold bg-primary2 text-white" text="식자재 추가" onClick={() => { - void router.push(`/fridge/add?fridgeid=${fridgeid as string}`); + void router.push( + `/fridge/add?fridgeid=${fridgeid as string}&name=${name as string}`, + ); }} /> )} diff --git a/src/components/organisms/IngredientAddModal.tsx b/src/components/organisms/IngredientAddModal.tsx index 4bee752..260659e 100644 --- a/src/components/organisms/IngredientAddModal.tsx +++ b/src/components/organisms/IngredientAddModal.tsx @@ -2,7 +2,6 @@ import { BoxIcon, CalendarIcon, FreezerIcon, MemoIcon } from '@/assets/icons'; import { Button, Toggle } from '@/components/atoms'; import { Counter, IngredientAddItemContainer } from '../molecules'; import React, { useState } from 'react'; -import useCount from '@/hooks/useCount'; import useToast from '@/hooks/useToast'; import ModalContainer from '../atoms/ModalContainer'; import { @@ -11,11 +10,17 @@ import { } from '@/hooks/queries/fridge'; import Image from 'next/image'; import type { PostIngredientBodyType } from '@/hooks/queries/fridge/usePostIngredient'; +import { useRouter } from 'next/router'; const IngredientAddModal: React.FC<{ id: number; toggleIsOpenIngredientAddModal: () => void; }> = ({ id, toggleIsOpenIngredientAddModal }) => { + const router = useRouter(); + const today = new Date(); + + const { fridgeid, name } = router.query; + const { showToast } = useToast(); const onSuccess = () => { @@ -23,33 +28,43 @@ const IngredientAddModal: React.FC<{ showToast('식자재 추가가 완료되었습니다.', 'success'); }; - const postIngredient = usePostIngredient(onSuccess); + const postIngredient = usePostIngredient( + onSuccess, + fridgeid as string, + name as string, + ); + + const data = useGetIngredientById(id); + + const expirationDate = new Date(today); + expirationDate.setDate(today.getDate() + (data?.expirationDays ?? 0)); const [reqBody, setReqBody] = useState({ - refrigeratorId: 0, - ingredientId: 0, - name: '', + refrigeratorId: Number(fridgeid), + ingredientId: id, + name: data?.name ?? '', quantity: 0, location: 'FREEZING', memo: '', - addDate: '2024-01-12', - expirationDate: '2024-01-22', + addDate: today, + expirationDate, isDeleted: true, }); const [isInFreezer, setIsInFreezer] = useState(false); - const { currentCount, handleIncreaseCount, handleDecreaseCount } = useCount(); const toggleIsInFreezer: () => void = () => { setIsInFreezer((prev) => !prev); }; const handleSubmit: () => void = () => { - postIngredient.mutate({ ...reqBody, quantity: currentCount }); + console.log({ ...reqBody }); + postIngredient.mutate({ + ...reqBody, + location: isInFreezer ? 'FREEZING' : 'FREEZING', + }); }; - const data = useGetIngredientById(id); - return (
@@ -71,28 +86,24 @@ const IngredientAddModal: React.FC<{
{ - console.log(e.target.value); setReqBody((prev) => ({ ...prev, - addDate: e.target.value, + addDate: new Date(e.target.value), })); }} />
~
{ - console.log(e.target.value); setReqBody((prev) => ({ ...prev, - expirationDate: e.target.value, + expirationDate: new Date(e.target.value), })); }} /> @@ -104,9 +115,19 @@ const IngredientAddModal: React.FC<{ title="수량" > { + setReqBody((prev) => ({ + ...prev, + quantity: prev.quantity + 1, + })); + }} + handleDecreaseCount={() => { + setReqBody((prev) => ({ + ...prev, + quantity: prev.quantity - 1, + })); + }} /> { diff --git a/src/hooks/queries/fridge/usePostIngredient.ts b/src/hooks/queries/fridge/usePostIngredient.ts index cb7be2c..43b28d0 100644 --- a/src/hooks/queries/fridge/usePostIngredient.ts +++ b/src/hooks/queries/fridge/usePostIngredient.ts @@ -9,15 +9,16 @@ export interface PostIngredientBodyType { quantity: number; location: 'FREEZING'; memo: string; - addDate: string; - expirationDate: string; + addDate: Date; + expirationDate: Date; isDeleted: true; } -const usePostIngredient = (fn?: () => void) => { +const usePostIngredient = (fn: () => void, fridgeid: string, name: string) => { const router = useRouter(); const onSuccess = () => { - void router.push('/fridge'); + fn(); + void router.push(`/fridge?fridgeid=${fridgeid}&name=${name}`); }; return useBaseMutation( queryKeys.INGREDIENTS(),