From 16e7eede75a3e1f6add759ce210c2165d23d8b37 Mon Sep 17 00:00:00 2001 From: "younghoon100.park" Date: Tue, 13 Feb 2024 13:28:38 +0900 Subject: [PATCH] =?UTF-8?q?[hooks]=20usePriceInfo=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Detail Notes : --- .../src/hooks/usePriceInfo.jsx | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 com.twin.app.shoptime/src/hooks/usePriceInfo.jsx diff --git a/com.twin.app.shoptime/src/hooks/usePriceInfo.jsx b/com.twin.app.shoptime/src/hooks/usePriceInfo.jsx new file mode 100644 index 00000000..49fb377f --- /dev/null +++ b/com.twin.app.shoptime/src/hooks/usePriceInfo.jsx @@ -0,0 +1,36 @@ +import React, { useCallback } from "react"; + +export default function usePriceInfo(priceInfo) { + const parsePriceInfo = useCallback( + (priceInfo) => { + const priceParts = priceInfo + .split("|") + .filter((part) => part !== "N") + .map((item) => item.trim()); + + let originalPrice, discountedPrice, discountRate, discountNumeric; + + if (priceParts.length === 4) { + [originalPrice, discountedPrice, , discountRate] = priceParts; + discountNumeric = Number(discountRate.slice(0, -1)); + } else if (priceParts.length === 2) { + [originalPrice, discountedPrice] = priceParts; + discountRate = null; + discountNumeric = null; + } else { + originalPrice = null; + discountedPrice = null; + discountRate = null; + discountNumeric = null; + } + + return { originalPrice, discountedPrice, discountRate, discountNumeric }; + }, + [priceInfo] + ); + + const { originalPrice, discountedPrice, discountRate, discountNumeric } = + parsePriceInfo(priceInfo); + + return { originalPrice, discountedPrice, discountRate, discountNumeric }; +}