|
|
@@ -117,7 +117,9 @@ const { width: screenWidth, height: screenHeight } = Dimensions.get('window');
|
|
|
|
|
|
//reminder: scan qr code page, ic call should be false
|
|
|
const ScanQrPage = () => {
|
|
|
- const { userID, currentPrice } = useUserInfoStore();
|
|
|
+ const { userID, currentPrice, setCurrentPrice } = useUserInfoStore();
|
|
|
+ const [currentPriceFetchedWhenScanQr, setCurrentPriceFetchedWhenScanQr] = useState(0);
|
|
|
+
|
|
|
const { width: screenWidth, height: screenHeight } = Dimensions.get('window');
|
|
|
const [permission, requestPermission] = useCameraPermissions();
|
|
|
const [scanned, setScanned] = useState(false);
|
|
|
@@ -229,6 +231,21 @@ const ScanQrPage = () => {
|
|
|
getWalletBalance();
|
|
|
}, []);
|
|
|
|
|
|
+ const fetchCurrentPrice = async () => {
|
|
|
+ try {
|
|
|
+ const response = await chargeStationService.getCurrentPrice();
|
|
|
+ if (response) {
|
|
|
+ setCurrentPriceFetchedWhenScanQr(response);
|
|
|
+ setCurrentPrice(response);
|
|
|
+ return response;
|
|
|
+ }
|
|
|
+ } catch (error) {
|
|
|
+ console.error('Error fetching current price:', error);
|
|
|
+ Alert.alert('錯誤', '無法獲取當前價格,請稍後再試');
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
const planMap = {
|
|
|
// 3: { duration: 10, kWh: 3, displayDuration: 5, fee: 3 * currentPrice },
|
|
|
25: { duration: 40, kWh: 20, displayDuration: 25, fee: 20 * currentPrice },
|
|
|
@@ -255,6 +272,12 @@ const ScanQrPage = () => {
|
|
|
// console.log(`type: ${type} data: ${data} typeofData ${typeof data}`);
|
|
|
|
|
|
try {
|
|
|
+ const price = await fetchCurrentPrice();
|
|
|
+ console.log('fetchedCurrentPrice in scanQrPage', price);
|
|
|
+ if (!price) {
|
|
|
+ return; // Exit if price fetch failed
|
|
|
+ }
|
|
|
+
|
|
|
const response = await chargeStationService.getTodayReservation();
|
|
|
if (response) {
|
|
|
const now = new Date();
|
|
|
@@ -264,12 +287,12 @@ const ScanQrPage = () => {
|
|
|
|
|
|
// Check availability for each duration```````````````````````````````````````````````````````````````````
|
|
|
const availability = {
|
|
|
- // 3: checkAvailability(onlyThisConnector, now, 10) && walletBalance >= 3 * currentPrice,
|
|
|
- 25: checkAvailability(onlyThisConnector, now, 40) && walletBalance >= 20 * currentPrice,
|
|
|
- 30: checkAvailability(onlyThisConnector, now, 45) && walletBalance >= 25 * currentPrice,
|
|
|
- 40: checkAvailability(onlyThisConnector, now, 55) && walletBalance >= 30 * currentPrice,
|
|
|
- 45: checkAvailability(onlyThisConnector, now, 60) && walletBalance >= 40 * currentPrice,
|
|
|
- full: checkAvailability(onlyThisConnector, now, 120) && walletBalance >= 80 * currentPrice
|
|
|
+ // 3: checkAvailability(onlyThisConnector, now, 10) && walletBalance >= 3 * price,
|
|
|
+ 25: checkAvailability(onlyThisConnector, now, 40) && walletBalance >= 20 * price,
|
|
|
+ 30: checkAvailability(onlyThisConnector, now, 45) && walletBalance >= 25 * price,
|
|
|
+ 40: checkAvailability(onlyThisConnector, now, 55) && walletBalance >= 30 * price,
|
|
|
+ 45: checkAvailability(onlyThisConnector, now, 60) && walletBalance >= 40 * price,
|
|
|
+ full: checkAvailability(onlyThisConnector, now, 120) && walletBalance >= 80 * price
|
|
|
};
|
|
|
|
|
|
setAvailableSlots(availability);
|
|
|
@@ -287,7 +310,7 @@ const ScanQrPage = () => {
|
|
|
}, 2000);
|
|
|
return;
|
|
|
}
|
|
|
-
|
|
|
+ // -----------------------------------------------------------------------------------------------------
|
|
|
const { origin, size } = bounds;
|
|
|
|
|
|
// Calculate the size of the square transparent area
|
|
|
@@ -308,6 +331,11 @@ const ScanQrPage = () => {
|
|
|
// console.log(` type: ${type} data: ${data} typeofData ${typeof data}`);
|
|
|
|
|
|
try {
|
|
|
+ const price = await fetchCurrentPrice();
|
|
|
+ console.log('fetchedCurrentPrice in scanQrPage', price);
|
|
|
+ if (!price) {
|
|
|
+ return; // Exit if price fetch failed
|
|
|
+ }
|
|
|
const response = await chargeStationService.getTodayReservation();
|
|
|
if (response) {
|
|
|
const now = new Date();
|
|
|
@@ -318,12 +346,12 @@ const ScanQrPage = () => {
|
|
|
|
|
|
// Check availability for each duration
|
|
|
const availability = {
|
|
|
- // 3: checkAvailability(onlyThisConnector, now, 10) && walletBalance >= 3 * currentPrice,
|
|
|
- 25: checkAvailability(onlyThisConnector, now, 40) && walletBalance >= 20 * currentPrice,
|
|
|
- 30: checkAvailability(onlyThisConnector, now, 45) && walletBalance >= 25 * currentPrice,
|
|
|
- 40: checkAvailability(onlyThisConnector, now, 55) && walletBalance >= 30 * currentPrice,
|
|
|
- 45: checkAvailability(onlyThisConnector, now, 60) && walletBalance >= 40 * currentPrice,
|
|
|
- full: checkAvailability(onlyThisConnector, now, 120) && walletBalance >= 80 * currentPrice
|
|
|
+ // 3: checkAvailability(onlyThisConnector, now, 10) && walletBalance >= 3 * price,
|
|
|
+ 25: checkAvailability(onlyThisConnector, now, 40) && walletBalance >= 20 * price,
|
|
|
+ 30: checkAvailability(onlyThisConnector, now, 45) && walletBalance >= 25 * price,
|
|
|
+ 40: checkAvailability(onlyThisConnector, now, 55) && walletBalance >= 30 * price,
|
|
|
+ 45: checkAvailability(onlyThisConnector, now, 60) && walletBalance >= 40 * price,
|
|
|
+ full: checkAvailability(onlyThisConnector, now, 120) && walletBalance >= 80 * price
|
|
|
};
|
|
|
// console.log('availability', availability);
|
|
|
|
|
|
@@ -390,18 +418,29 @@ const ScanQrPage = () => {
|
|
|
let fee;
|
|
|
let totalPower;
|
|
|
|
|
|
+ //i create a planMap2 because i want to move the planMap inside this component but i dont wanna move the outside one because i dont wanna make any potential disruptive changes
|
|
|
+ const planMap2 = {
|
|
|
+ // 3: { duration: 10, kWh: 3, displayDuration: 5, fee: 3 * currentPriceFetchedWhenScanQr },
|
|
|
+ 25: { duration: 40, kWh: 20, displayDuration: 25, fee: 20 * currentPriceFetchedWhenScanQr },
|
|
|
+ 30: { duration: 45, kWh: 25, displayDuration: 30, fee: 25 * currentPriceFetchedWhenScanQr },
|
|
|
+ 40: { duration: 55, kWh: 30, displayDuration: 40, fee: 30 * currentPriceFetchedWhenScanQr },
|
|
|
+ 45: { duration: 60, kWh: 40, displayDuration: 45, fee: 40 * currentPriceFetchedWhenScanQr },
|
|
|
+ full: { duration: 120, displayDuration: '充滿停機', fee: 80 * currentPriceFetchedWhenScanQr }
|
|
|
+ };
|
|
|
+
|
|
|
if (selectedDuration === 'full') {
|
|
|
endTime = new Date(now.getTime() + 2 * 60 * 60 * 1000); // 2 hours for "充滿停機"
|
|
|
- fee = planMap.full.fee;
|
|
|
+ fee = planMap2.full.fee;
|
|
|
totalPower = 80; // Set to 130 for "充滿停機"
|
|
|
} else {
|
|
|
const durationInMinutes = parseInt(selectedDuration);
|
|
|
endTime = new Date(now.getTime() + durationInMinutes * 60 * 1000);
|
|
|
// console.log('endTime', endTime);
|
|
|
- fee = planMap[selectedDuration].fee;
|
|
|
- totalPower = planMap[selectedDuration].kWh;
|
|
|
+ fee = planMap2[selectedDuration].fee;
|
|
|
+ totalPower = planMap2[selectedDuration].kWh;
|
|
|
}
|
|
|
setTotalFee(fee);
|
|
|
+ console.log('fee in scanQrPage-- this is the total_fee i send to backend', fee);
|
|
|
const dataForSubmission = {
|
|
|
stationID: '2405311022116801000',
|
|
|
connector: scannedResult,
|
|
|
@@ -734,9 +773,7 @@ const ScanQrPage = () => {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- ////////
|
|
|
const wallet = await walletService.getWalletBalance();
|
|
|
- // console.log('wallet in startCharging in scanQrPage', wallet);
|
|
|
|
|
|
if (wallet < dataForSubmission.total_fee) {
|
|
|
oneTimeCharging(dataForSubmission.total_fee);
|
|
|
@@ -760,9 +797,8 @@ const ScanQrPage = () => {
|
|
|
);
|
|
|
if (response.status === 200 || response.status === 201) {
|
|
|
setSelectedDuration(null);
|
|
|
- // console.log('Charging started from startCharging', response);
|
|
|
+
|
|
|
setIsConfirmLoading(false);
|
|
|
- // Set a flag in AsyncStorage to indicate charging has started
|
|
|
|
|
|
await AsyncStorage.setItem('chargingStarted', 'true');
|
|
|
|
|
|
@@ -784,9 +820,6 @@ const ScanQrPage = () => {
|
|
|
}
|
|
|
}
|
|
|
]);
|
|
|
-
|
|
|
- // Start the navigation attempt loop
|
|
|
- // startNavigationAttempts();
|
|
|
} else if (response.status === 400) {
|
|
|
console.log('400 error in paymentSummaryPageComponent');
|
|
|
|