|
@@ -2,7 +2,7 @@ import React, { useState, useMemo, useEffect } from "react";
|
|
|
import { useLocation, useNavigate, Link } from "react-router-dom";
|
|
import { useLocation, useNavigate, Link } from "react-router-dom";
|
|
|
import { SelectedProduct } from "../../services/types";
|
|
import { SelectedProduct } from "../../services/types";
|
|
|
import { Area, Package } from "../../services/product/type";
|
|
import { Area, Package } from "../../services/product/type";
|
|
|
-import { useQuery } from "@tanstack/react-query";
|
|
|
|
|
|
|
+import { useMutation, useQuery } from "@tanstack/react-query";
|
|
|
import { DataCacheKey, staleTime } from "../../global/constants";
|
|
import { DataCacheKey, staleTime } from "../../global/constants";
|
|
|
import { useAppDispatch, useAppSelector } from "../../hooks/useRedux";
|
|
import { useAppDispatch, useAppSelector } from "../../hooks/useRedux";
|
|
|
import { startLoading, stopLoading } from "../../features/loading/loadingSlice";
|
|
import { startLoading, stopLoading } from "../../features/loading/loadingSlice";
|
|
@@ -33,6 +33,7 @@ const ProductDetailView: React.FC = () => {
|
|
|
});
|
|
});
|
|
|
const [simType, setSimType] = useState<"eSIM" | "Physical">("eSIM");
|
|
const [simType, setSimType] = useState<"eSIM" | "Physical">("eSIM");
|
|
|
const [quantity, setQuantity] = useState<number>(1);
|
|
const [quantity, setQuantity] = useState<number>(1);
|
|
|
|
|
+ const [packages, setPackages] = useState<Package[]>(null);
|
|
|
|
|
|
|
|
if (!area) {
|
|
if (!area) {
|
|
|
return (
|
|
return (
|
|
@@ -47,25 +48,51 @@ const ProductDetailView: React.FC = () => {
|
|
|
);
|
|
);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- const { data: loadPackage = [] } = useQuery<Package[]>({
|
|
|
|
|
- queryKey: [DataCacheKey.PACKAGES],
|
|
|
|
|
- queryFn: async (): Promise<Package[]> => {
|
|
|
|
|
- try {
|
|
|
|
|
- dispatch(startLoading({}));
|
|
|
|
|
- const res = await productApi.loadPackage({
|
|
|
|
|
- areaId: area.id,
|
|
|
|
|
- isUnlimited: "-1",
|
|
|
|
|
- isDaily: "-1",
|
|
|
|
|
- });
|
|
|
|
|
- return res.data as Package[];
|
|
|
|
|
- } catch (error) {
|
|
|
|
|
- console.error(error);
|
|
|
|
|
- return []; // 🔴 bắt buộc
|
|
|
|
|
- } finally {
|
|
|
|
|
- dispatch(stopLoading());
|
|
|
|
|
|
|
+ // const { data: loadPackage = [] } = useQuery<Package[]>({
|
|
|
|
|
+ // queryKey: [DataCacheKey.PACKAGES],
|
|
|
|
|
+ // queryFn: async (): Promise<Package[]> => {
|
|
|
|
|
+ // try {
|
|
|
|
|
+ // dispatch(startLoading({}));
|
|
|
|
|
+ // const res = await productApi.loadPackage({
|
|
|
|
|
+ // areaId: area.id,
|
|
|
|
|
+ // isUnlimited: "-1",
|
|
|
|
|
+ // isDaily: "-1",
|
|
|
|
|
+ // });
|
|
|
|
|
+ // return res.data as Package[];
|
|
|
|
|
+ // } catch (error) {
|
|
|
|
|
+ // console.error(error);
|
|
|
|
|
+ // return []; // 🔴 bắt buộc
|
|
|
|
|
+ // } finally {
|
|
|
|
|
+ // dispatch(stopLoading());
|
|
|
|
|
+ // }
|
|
|
|
|
+ // },
|
|
|
|
|
+ // staleTime: staleTime,
|
|
|
|
|
+ // });
|
|
|
|
|
+
|
|
|
|
|
+ const getAreaMutation = useMutation({
|
|
|
|
|
+ mutationFn: async () => {
|
|
|
|
|
+ dispatch(startLoading({}));
|
|
|
|
|
+ const res = await productApi.loadPackage({
|
|
|
|
|
+ areaId: area.id,
|
|
|
|
|
+ isUnlimited: "-1",
|
|
|
|
|
+ isDaily: "-1",
|
|
|
|
|
+ });
|
|
|
|
|
+ return res;
|
|
|
|
|
+ },
|
|
|
|
|
+ onSuccess: (data) => {
|
|
|
|
|
+ dispatch(stopLoading());
|
|
|
|
|
+ console.log("Get package response data:", data);
|
|
|
|
|
+ if (data && data.errorCode === "0") {
|
|
|
|
|
+ console.log("Get package successful");
|
|
|
|
|
+ setPackages(data.data as Package[]);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ console.error("Get package failed, no token received");
|
|
|
}
|
|
}
|
|
|
},
|
|
},
|
|
|
- staleTime: staleTime,
|
|
|
|
|
|
|
+ onError: (error: any) => {
|
|
|
|
|
+ dispatch(stopLoading());
|
|
|
|
|
+ console.error("Get package error:", error.response.data);
|
|
|
|
|
+ },
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
const options = useMemo(() => {
|
|
const options = useMemo(() => {
|
|
@@ -74,7 +101,7 @@ const ProductDetailView: React.FC = () => {
|
|
|
const daysSet = new Set<number>();
|
|
const daysSet = new Set<number>();
|
|
|
const dataSet = new Set<string>();
|
|
const dataSet = new Set<string>();
|
|
|
|
|
|
|
|
- loadPackage.forEach((p) => {
|
|
|
|
|
|
|
+ packages.forEach((p) => {
|
|
|
daysSet.add(p.dayDuration);
|
|
daysSet.add(p.dayDuration);
|
|
|
dataSet.add(p.amountData.toString());
|
|
dataSet.add(p.amountData.toString());
|
|
|
});
|
|
});
|
|
@@ -91,7 +118,7 @@ const ProductDetailView: React.FC = () => {
|
|
|
daysArray,
|
|
daysArray,
|
|
|
dataArray,
|
|
dataArray,
|
|
|
};
|
|
};
|
|
|
- }, [loadPackage]);
|
|
|
|
|
|
|
+ }, [packages]);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
useEffect(() => {
|
|
|
setDaysOptions(options.daysArray);
|
|
setDaysOptions(options.daysArray);
|
|
@@ -107,7 +134,7 @@ const ProductDetailView: React.FC = () => {
|
|
|
const handleSelectDay = (day: number) => {
|
|
const handleSelectDay = (day: number) => {
|
|
|
// filter data options based on selected day if needed
|
|
// filter data options based on selected day if needed
|
|
|
const dataSet = new Set<string>();
|
|
const dataSet = new Set<string>();
|
|
|
- loadPackage.forEach((p) => {
|
|
|
|
|
|
|
+ packages.forEach((p) => {
|
|
|
if (p.dayDuration === day) {
|
|
if (p.dayDuration === day) {
|
|
|
dataSet.add(p.amountData.toString());
|
|
dataSet.add(p.amountData.toString());
|
|
|
}
|
|
}
|
|
@@ -119,7 +146,7 @@ const ProductDetailView: React.FC = () => {
|
|
|
const handleSelectData = (data: string) => {
|
|
const handleSelectData = (data: string) => {
|
|
|
// filter day options based on selected data if needed
|
|
// filter day options based on selected data if needed
|
|
|
const daysSet = new Set<number>();
|
|
const daysSet = new Set<number>();
|
|
|
- loadPackage.forEach((p) => {
|
|
|
|
|
|
|
+ packages.forEach((p) => {
|
|
|
if (p.amountData.toString() === data) {
|
|
if (p.amountData.toString() === data) {
|
|
|
daysSet.add(p.dayDuration);
|
|
daysSet.add(p.dayDuration);
|
|
|
}
|
|
}
|
|
@@ -132,7 +159,7 @@ const ProductDetailView: React.FC = () => {
|
|
|
const quantityToUse =
|
|
const quantityToUse =
|
|
|
quantityParam !== undefined ? quantityParam : quantity;
|
|
quantityParam !== undefined ? quantityParam : quantity;
|
|
|
// find package based on selectedDays and selectedData
|
|
// find package based on selectedDays and selectedData
|
|
|
- const selectedPackage = loadPackage.find(
|
|
|
|
|
|
|
+ const selectedPackage = packages.find(
|
|
|
(p) =>
|
|
(p) =>
|
|
|
p.dayDuration === selectedDays &&
|
|
p.dayDuration === selectedDays &&
|
|
|
p.amountData.toString() === selectedData
|
|
p.amountData.toString() === selectedData
|
|
@@ -176,7 +203,7 @@ const ProductDetailView: React.FC = () => {
|
|
|
const handleBuyNow = async () => {
|
|
const handleBuyNow = async () => {
|
|
|
// Logic for custom order or standard buy
|
|
// Logic for custom order or standard buy
|
|
|
console.log("Buy now clicked");
|
|
console.log("Buy now clicked");
|
|
|
- const selectedPackage = loadPackage.find(
|
|
|
|
|
|
|
+ const selectedPackage = packages.find(
|
|
|
(p) =>
|
|
(p) =>
|
|
|
p.dayDuration === selectedDays &&
|
|
p.dayDuration === selectedDays &&
|
|
|
p.amountData.toString() === selectedData
|
|
p.amountData.toString() === selectedData
|