|
|
@@ -35,7 +35,7 @@ import {
|
|
|
findNearestAvailableVariant
|
|
|
} from "@/utils/variantTools";
|
|
|
|
|
|
-export function AddToCartModal() {
|
|
|
+export default function AddToCartModal() {
|
|
|
const dispatch = useAppDispatch();
|
|
|
const {isOpen, product} = useAppSelector((state) => state.addToCartDialog);
|
|
|
|
|
|
@@ -52,7 +52,8 @@ export function AddToCartModal() {
|
|
|
},[product?.flexibleVariants]);
|
|
|
|
|
|
const productOptions: ProductOption[] = useMemo(() => {
|
|
|
- return product?.productOptions ? JSON.parse(product.productOptions) : [];
|
|
|
+ const opts = product?.productOptions;
|
|
|
+ return opts ? JSON.parse(opts) : [];
|
|
|
},[product?.productOptions]);
|
|
|
|
|
|
// 获取所有可用变体
|
|
|
@@ -62,7 +63,7 @@ export function AddToCartModal() {
|
|
|
|
|
|
const isSaleable = product?.isSaleable;
|
|
|
|
|
|
- const [productQty, setProductQty] = useState(1);
|
|
|
+ const [productQty] = useState(1);
|
|
|
|
|
|
|
|
|
// 记录当前哪个选项组刚刚被点击(用于实现“点击组内全部可点”)
|
|
|
@@ -136,7 +137,7 @@ export function AddToCartModal() {
|
|
|
let totalLinePrice = 0;
|
|
|
let totalNowPrice = 0;
|
|
|
let save = 0;
|
|
|
- let variant = flexibleVariants.find((v) => {
|
|
|
+ const variant = flexibleVariants.find((v) => {
|
|
|
const vIds = v.optionValues.map((ov) => ov.id);
|
|
|
return selectedIds.length === vIds.length && selectedIds.every((id) => vIds.includes(id));
|
|
|
});
|
|
|
@@ -167,9 +168,9 @@ export function AddToCartModal() {
|
|
|
clickValueId: number;
|
|
|
}) => {
|
|
|
// 更新前需要判断当前选中的组合是否可以购买,如果可以购买正常更新;如果不能购买查找可以购买的组合然后更新
|
|
|
- let {clickOptionId: optionId, clickValueId: valueId} = result;
|
|
|
+ const {clickOptionId: optionId, clickValueId: valueId} = result;
|
|
|
let newSelected: Record<number, number> = {...selected,[optionId]: valueId};
|
|
|
- let selectedValueIds = Object.values(newSelected);
|
|
|
+ const selectedValueIds = Object.values(newSelected);
|
|
|
if(!isCombinationAvailable(selectedValueIds, availableVariants)) {
|
|
|
newSelected = findNearestAvailableVariant(optionId,valueId,{...newSelected},productOptions,availableVariants);
|
|
|
}
|
|
|
@@ -181,12 +182,12 @@ export function AddToCartModal() {
|
|
|
|
|
|
async function addProductToCart(action:string = 'addtocart') {
|
|
|
|
|
|
- let params = {
|
|
|
+ const params = {
|
|
|
productId: String(product?._id),
|
|
|
quantity: productQty,
|
|
|
variantId: currentVariantInfo.variant?._id,
|
|
|
};
|
|
|
- let res = await onAddToCart(params);
|
|
|
+ const res = await onAddToCart(params);
|
|
|
console.log('onAddToCart run ----- 1');
|
|
|
if(action === 'buynow') {
|
|
|
if(res) {
|