[hooks] usePriceInfo 추가
Detail Notes :
This commit is contained in:
36
com.twin.app.shoptime/src/hooks/usePriceInfo.jsx
Normal file
36
com.twin.app.shoptime/src/hooks/usePriceInfo.jsx
Normal file
@@ -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 };
|
||||
}
|
||||
Reference in New Issue
Block a user