[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) {
|
export default function usePriceInfo(priceInfo) {
|
||||||
const parsePriceInfo = useCallback(
|
const parsedPriceInfo = useMemo(() => {
|
||||||
(priceInfo) => {
|
const splittedPriceInfo = priceInfo.split("|").map((item) => item.trim());
|
||||||
const priceParts = priceInfo
|
|
||||||
.split("|")
|
|
||||||
.filter((part) => part !== "N")
|
|
||||||
.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) {
|
const {
|
||||||
[originalPrice, discountedPrice, , discountRate] = priceParts;
|
originalPrice,
|
||||||
discountNumeric = Number(discountRate.slice(0, -1));
|
discountedPrice,
|
||||||
} else if (priceParts.length === 2) {
|
rewardFlag,
|
||||||
[originalPrice, discountedPrice] = priceParts;
|
discountAmount,
|
||||||
discountRate = null;
|
discountRate,
|
||||||
discountNumeric = null;
|
} = processedPriceInfo;
|
||||||
} else {
|
|
||||||
originalPrice = null;
|
|
||||||
discountedPrice = null;
|
|
||||||
discountRate = null;
|
|
||||||
discountNumeric = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
return { originalPrice, discountedPrice, discountRate, discountNumeric };
|
const discountRateNumeric =
|
||||||
},
|
discountRate || Number(discountRate.slice(0, -1));
|
||||||
[priceInfo]
|
|
||||||
);
|
|
||||||
|
|
||||||
const { originalPrice, discountedPrice, discountRate, discountNumeric } =
|
return {
|
||||||
parsePriceInfo(priceInfo);
|
originalPrice,
|
||||||
|
discountedPrice,
|
||||||
|
rewardFlag,
|
||||||
|
discountAmount,
|
||||||
|
discountRate,
|
||||||
|
discountRateNumeric,
|
||||||
|
};
|
||||||
|
}, [priceInfo]);
|
||||||
|
|
||||||
return { originalPrice, discountedPrice, discountRate, discountNumeric };
|
return parsedPriceInfo;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user