diff --git a/apps/client/src/shared/apis/axios.ts b/apps/client/src/shared/apis/axios.ts index ebe439ec..6d7c6500 100644 --- a/apps/client/src/shared/apis/axios.ts +++ b/apps/client/src/shared/apis/axios.ts @@ -13,6 +13,13 @@ export const postCategory = async (categoryName: string) => { return response; }; +export const putCategory = async (id: number, categoryName: string) => { + const response = await apiRequest.put(`/api/v1/categories/${id}`, { + categoryName, + }); + return response; +}; + export const getAcorns = async () => { const now = formatLocalDateTime(new Date()); const { data } = await apiRequest.get('/api/v1/users/acorns?now=', { diff --git a/apps/client/src/shared/apis/queries.ts b/apps/client/src/shared/apis/queries.ts index 1533d8ff..454d4d43 100644 --- a/apps/client/src/shared/apis/queries.ts +++ b/apps/client/src/shared/apis/queries.ts @@ -1,5 +1,11 @@ import { useMutation, useQuery, UseQueryResult } from '@tanstack/react-query'; -import { getDashboardCategories, postCategory, postSignUp, postSignUpRequest } from '@shared/apis/axios'; +import { + getDashboardCategories, + postCategory, + postSignUp, + postSignUpRequest, + putCategory, +} from '@shared/apis/axios'; import { AxiosError } from 'axios'; import { DashboardCategoriesResponse, AcornsResponse } from '@shared/types/api'; import { getAcorns } from './axios'; @@ -19,6 +25,12 @@ export const usePostCategory = () => { mutationFn: (categoryName: string) => postCategory(categoryName), }); }; +export const usePutCategory = () => { + return useMutation({ + mutationFn: ({ id, categoryName }: { id: number; categoryName: string }) => + putCategory(id, categoryName), + }); +}; export const useGetArcons = (): UseQueryResult => { return useQuery({ @@ -34,13 +46,13 @@ export const usePostSignUp = () => { const newToken = data?.data?.token || data?.token; if (newToken) { - localStorage.setItem("token", newToken); + localStorage.setItem('token', newToken); } - console.log("회원가입 성공:", data); + console.log('회원가입 성공:', data); }, onError: (error) => { - console.error("회원가입 실패:", error); + console.error('회원가입 실패:', error); }, }); }; diff --git a/apps/client/src/shared/components/sidebar/PopupPortal.tsx b/apps/client/src/shared/components/sidebar/PopupPortal.tsx index c94e82e5..a5e30df1 100644 --- a/apps/client/src/shared/components/sidebar/PopupPortal.tsx +++ b/apps/client/src/shared/components/sidebar/PopupPortal.tsx @@ -44,7 +44,8 @@ export default function PopupPortal({ title="카테고리 수정하기" left="취소" right="확인" - placeholder={popup.name} + onInputChange={onChange} + defaultValue={popup.name} onLeftClick={onClose} onRightClick={() => onEditConfirm?.(popup.id)} /> diff --git a/apps/client/src/shared/components/sidebar/Sidebar.tsx b/apps/client/src/shared/components/sidebar/Sidebar.tsx index 2b59f1cf..1458eb31 100644 --- a/apps/client/src/shared/components/sidebar/Sidebar.tsx +++ b/apps/client/src/shared/components/sidebar/Sidebar.tsx @@ -14,6 +14,7 @@ import { useGetDashboardCategories, usePostCategory, useGetArcons, + usePutCategory, } from '@shared/apis/queries'; import { useState } from 'react'; import { useQueryClient } from '@tanstack/react-query'; @@ -23,6 +24,7 @@ export function Sidebar() { const queryClient = useQueryClient(); const { data: categories } = useGetDashboardCategories(); + const { mutate: patchCategory } = usePutCategory(); const { mutate: createCategory } = usePostCategory(); const { data, isPending, isError } = useGetArcons(); @@ -66,6 +68,19 @@ export function Sidebar() { }, }); }; + const handlePatchCategory = (id: number) => { + patchCategory( + { id, categoryName: newCategoryName }, + { + onSuccess: () => { + setNewCategoryName(''); + queryClient.invalidateQueries({ queryKey: ['dashboardCategories'] }); + close(); + }, + onError: (error) => console.error('카테고리 수정 실패:', error), + } + ); + }; if (isPending) return
; if (isError) return
; @@ -156,10 +171,7 @@ export function Sidebar() { onClose={close} onChange={handleCategoryChange} onCreateConfirm={handleCreateCategory} - onEditConfirm={() => { - // TODO: 수정 API - close(); - }} + onEditConfirm={(id) => handlePatchCategory(id)} onDeleteConfirm={() => { // TODO: 삭제 API close(); diff --git a/packages/design-system/src/components/popup/Popup.tsx b/packages/design-system/src/components/popup/Popup.tsx index 2ed72ee2..c188e95b 100644 --- a/packages/design-system/src/components/popup/Popup.tsx +++ b/packages/design-system/src/components/popup/Popup.tsx @@ -11,8 +11,9 @@ interface BasePopupProps { errortext?: string; placeholder?: string; isError?: boolean; - helperText?: string; + helperText?: string; inputValue?: string; + defaultValue?: string; onInputChange?: (value: string) => void; onLeftClick?: () => void; onRightClick?: () => void; @@ -28,6 +29,7 @@ const Popup = ({ errortext, isError, inputValue, + defaultValue, onInputChange, onLeftClick, onRightClick, @@ -43,9 +45,10 @@ const Popup = ({ isError={isError} value={inputValue} onChange={(e) => onInputChange?.(e.target.value)} + defaultValue={defaultValue} /> {isError && errortext && ( -

{errortext}

+

{errortext}

)} )}