@@ -14,6 +14,10 @@ interface UseCartReturn {
1414 onSuccess ?: ( ) => void ;
1515 onError ?: ( message : string ) => void ;
1616 } ) => void ;
17+ removeFromCart : ( menuId : number , callbacks ?: {
18+ onSuccess ?: ( ) => void ;
19+ onError ?: ( message : string ) => void ;
20+ } ) => void ;
1721 isAddingToCart : boolean ;
1822}
1923
@@ -54,6 +58,7 @@ export function useCart(): UseCartReturn {
5458 /**
5559 * 메뉴 1개 추가 (기존 수량 + 1)
5660 * - 버튼 연타 방지
61+ * - 각 메뉴는 최대 9개까지
5762 */
5863 const addToCart = useCallback ( (
5964 menuId : number ,
@@ -97,12 +102,61 @@ export function useCart(): UseCartReturn {
97102 pendingRequests . current . set ( menuId , requestPromise ) ;
98103 } , [ getMenuQuantity , upsertCartMutation ] ) ;
99104
105+ /**
106+ * 메뉴 1개 감소 (기존 수량 - 1)
107+ * - 수량이 1이면 장바구니에서 삭제 (수량 0으로 설정)
108+ * - 버튼 연타 방지
109+ */
110+ const removeFromCart = useCallback ( (
111+ menuId : number ,
112+ callbacks ?: {
113+ onSuccess ?: ( ) => void ;
114+ onError ?: ( message : string ) => void ;
115+ }
116+ ) => {
117+ // 중복 요청 방지
118+ if ( pendingRequests . current . has ( menuId ) ) {
119+ return ;
120+ }
121+
122+ // 특정 메뉴 수량 조회
123+ const menuQuantity = getMenuQuantity ( menuId ) ;
124+
125+ // 수량이 0이면 실행 중지
126+ if ( menuQuantity === 0 ) {
127+ return ;
128+ }
129+
130+ const newMenuQuantity = menuQuantity - 1 ;
131+
132+ const requestPromise = new Promise < void > ( ( resolve , reject ) => {
133+ upsertCartMutation . mutate (
134+ { menuId, quantity : newMenuQuantity } ,
135+ {
136+ onSuccess : ( ) => {
137+ pendingRequests . current . delete ( menuId ) ;
138+ callbacks ?. onSuccess ?.( ) ;
139+ resolve ( ) ;
140+ } ,
141+ onError : ( error ) => {
142+ pendingRequests . current . delete ( menuId ) ;
143+ callbacks ?. onError ?.( "장바구니 수정에 실패했어요. 잠시 후 다시 시도해주세요." ) ;
144+ reject ( error ) ;
145+ } ,
146+ }
147+ ) ;
148+ } ) ;
149+
150+ pendingRequests . current . set ( menuId , requestPromise ) ;
151+ } , [ getMenuQuantity , upsertCartMutation ] ) ;
152+
100153 return {
101154 cartInfo,
102155 isLoading,
103156 error,
104157 getMenuQuantity,
105158 addToCart,
159+ removeFromCart,
106160 isAddingToCart : upsertCartMutation . isPending ,
107161 } ;
108162}
0 commit comments