From b16ff7b5c75bcbed42f3a30c45b0219d6603dc91 Mon Sep 17 00:00:00 2001 From: userjmmm Date: Fri, 2 Aug 2024 20:53:02 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20=EA=B2=B0=EC=A0=9C=ED=95=98?= =?UTF-8?q?=EA=B8=B0=20=EB=88=84=EB=A5=BC=20=EB=95=8C,=20API=20=EC=9A=94?= =?UTF-8?q?=EC=B2=AD=EC=9D=84=20=EC=A7=81=EB=A0=AC=EB=A1=9C=20=EB=B3=B4?= =?UTF-8?q?=EB=82=B4=20=ED=8F=AC=EC=9D=B8=ED=8A=B8=EA=B0=80=20=EC=B0=A8?= =?UTF-8?q?=EA=B0=90=EB=90=98=EB=8F=84=EB=A1=9D=20=EB=A1=9C=EC=A7=81=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Order/OrderForm/OrderInfo/index.tsx | 2 +- .../features/Order/OrderForm/index.tsx | 76 ++++++++----------- 2 files changed, 34 insertions(+), 44 deletions(-) diff --git a/src/components/features/Order/OrderForm/OrderInfo/index.tsx b/src/components/features/Order/OrderForm/OrderInfo/index.tsx index a7c92c490..c4cad9f6d 100644 --- a/src/components/features/Order/OrderForm/OrderInfo/index.tsx +++ b/src/components/features/Order/OrderForm/OrderInfo/index.tsx @@ -32,7 +32,7 @@ export const OrderFormOrderInfo = ({ orderHistory }: Props) => { return total + (selectedOption ? detail.price * order.quantity : 0); }, 0); - const discountedPrice = Math.round(totalPrice * 0.9); + const discountedPrice = Math.round(totalPrice * 0.95); return ( diff --git a/src/components/features/Order/OrderForm/index.tsx b/src/components/features/Order/OrderForm/index.tsx index f11872092..b9bddf6d1 100644 --- a/src/components/features/Order/OrderForm/index.tsx +++ b/src/components/features/Order/OrderForm/index.tsx @@ -40,7 +40,8 @@ export const OrderForm = ({ orderHistory }: Props) => { const orders = orderHistorySessionStorage.get() as OrderHistory[]; - const requests = orders.map(order => { + const results = []; + for (const order of orders) { const requestBody = { optionId: order.optionId, quantity: order.quantity, @@ -49,54 +50,43 @@ export const OrderForm = ({ orderHistory }: Props) => { console.log('서버로 전송하는 요청 데이터:', JSON.stringify(requestBody, null, 2)); - return fetch(`${getBaseUrl()}/api/orders`, { - method: 'POST', - headers: { - Authorization: `Bearer ${authInfo?.token}`, - 'Content-Type': 'application/json', - }, - body: JSON.stringify(requestBody), - credentials: 'include', - }) - .then(async response => { - const status = response.status; + try { + const response = await fetch(`${getBaseUrl()}/api/orders`, { + method: 'POST', + headers: { + Authorization: `Bearer ${authInfo?.token}`, + 'Content-Type': 'application/json', + }, + body: JSON.stringify(requestBody), + credentials: 'include', + }); + if (!response.ok) { const errorBody = await response.text(); - console.error(`옵션 ID ${order.optionId} 주문 실패:`, status, errorBody); - return { success: false, optionId: order.optionId, status, error: errorBody }; + console.error(`옵션 ID ${order.optionId} 주문 실패:`, response.status, errorBody); + results.push({ success: false, optionId: order.optionId, error: errorBody }); + } else { + const data = await response.json(); + console.log(`옵션 ID ${order.optionId} 주문 성공:`, data); + results.push({ success: true, optionId: order.optionId, data }); } - const data = await response.json(); - console.log(`옵션 ID ${order.optionId} 주문 성공:`, data); - return { success: true, optionId: order.optionId, status, data }; - }) - .catch(error => { + } catch (error) { console.error(`옵션 ID ${order.optionId} 주문 에러:`, error); - return { success: false, optionId: order.optionId, status: 500, error: error.message }; - }); - }); - - try { - const results = await Promise.all(requests); - const successfulOrders = results.filter(result => result.success); - const failedOrders = results.filter(result => !result.success); - - if (successfulOrders.length > 0) { - console.log('성공한 주문:', successfulOrders); - alert(`${successfulOrders.length}개의 주문이 완료되었습니다.`); + results.push({ success: false, optionId: order.optionId, error: error}); } + } - if (failedOrders.length > 0) { - console.error('실패한 주문:', failedOrders); - const insufficientPoints = failedOrders.some(order => order.status === 400); - if (insufficientPoints) { - alert('포인트가 부족합니다.'); - } else { - alert(`${failedOrders.length}개의 주문 중 오류가 발생했습니다.`); - } - } - } catch (error) { - console.error('주문 처리 중 예외 발생:', error); - alert('주문 처리 중 예기치 않은 오류가 발생했습니다.'); + const successfulOrders = results.filter(result => result.success); + const failedOrders = results.filter(result => !result.success); + + if (successfulOrders.length > 0) { + console.log('성공한 주문:', successfulOrders); + alert(`${successfulOrders.length}개의 주문이 완료되었습니다.`); + } + + if (failedOrders.length > 0) { + console.error('실패한 주문:', failedOrders); + alert(`${failedOrders.length}개의 주문 중 오류가 발생했습니다.`); } };