[hooks] usePriceInfo 수정
Detail Notes : 1. reward 여부 값의 필요에 따른 로직 변경
This commit is contained in:
@@ -1,36 +1,42 @@
|
||||
import React, { useCallback } from "react";
|
||||
import React, { useMemo } from "react";
|
||||
|
||||
const priceInfoKeys = [
|
||||
"originalPrice",
|
||||
"discountedPrice",
|
||||
"rewardFlag",
|
||||
"discountAmount",
|
||||
"discountRate",
|
||||
];
|
||||
|
||||
export default function usePriceInfo(priceInfo) {
|
||||
const parsePriceInfo = useCallback(
|
||||
(priceInfo) => {
|
||||
const priceParts = priceInfo
|
||||
.split("|")
|
||||
.filter((part) => part !== "N")
|
||||
.map((item) => item.trim());
|
||||
const parsedPriceInfo = useMemo(() => {
|
||||
const splittedPriceInfo = priceInfo.split("|").map((item) => item.trim());
|
||||
|
||||
let originalPrice, discountedPrice, discountRate, discountNumeric;
|
||||
const processedPriceInfo = priceInfoKeys.reduce((objcet, key, index) => {
|
||||
objcet[key] = splittedPriceInfo[index];
|
||||
return objcet;
|
||||
}, {});
|
||||
|
||||
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;
|
||||
}
|
||||
const {
|
||||
originalPrice,
|
||||
discountedPrice,
|
||||
rewardFlag,
|
||||
discountAmount,
|
||||
discountRate,
|
||||
} = processedPriceInfo;
|
||||
|
||||
return { originalPrice, discountedPrice, discountRate, discountNumeric };
|
||||
},
|
||||
[priceInfo]
|
||||
);
|
||||
const discountRateNumeric =
|
||||
discountRate || Number(discountRate.slice(0, -1));
|
||||
|
||||
const { originalPrice, discountedPrice, discountRate, discountNumeric } =
|
||||
parsePriceInfo(priceInfo);
|
||||
return {
|
||||
originalPrice,
|
||||
discountedPrice,
|
||||
rewardFlag,
|
||||
discountAmount,
|
||||
discountRate,
|
||||
discountRateNumeric,
|
||||
};
|
||||
}, [priceInfo]);
|
||||
|
||||
return { originalPrice, discountedPrice, discountRate, discountNumeric };
|
||||
return parsedPriceInfo;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user