Browse Source

auto filling dates

Viktoriia 1 tháng trước cách đây
mục cha
commit
b59676cf4c

+ 52 - 2
src/screens/InAppScreens/TravelsScreen/AddNewTripScreen/index.tsx

@@ -1,4 +1,4 @@
-import React, { useEffect, useState, useCallback, useRef } from 'react';
+import React, { useEffect, useState, useRef } from 'react';
 import { View, Text, TouchableOpacity, ScrollView } from 'react-native';
 import ReactModal from 'react-native-modal';
 import { useNavigation } from '@react-navigation/native';
@@ -71,6 +71,48 @@ const AddNewTripScreen = ({ route }: { route: any }) => {
   const { mutate: updateTrip } = usePostUpdateTripMutation();
   const { mutate: deleteTrip } = usePostDeleteTripMutation();
 
+  const fillRegionDatesFromSelectedDates = (regionsToUpdate: RegionWithDates[]) => {
+    if (!selectedDates || !regionsToUpdate) return regionsToUpdate;
+
+    const from = selectedDates.split(' - ')[0];
+    const to = selectedDates.split(' - ')[1];
+
+    const updatedRegions = regionsToUpdate.map((region) => {
+      const hasEmptyStartDate = !region.visitStartDate?.year || !region.visitStartDate?.month;
+      const hasEmptyEndDate = !region.visitEndDate?.year || !region.visitEndDate?.month;
+
+      if (hasEmptyStartDate || hasEmptyEndDate) {
+        const updatedRegion = { ...region };
+
+        if (hasEmptyStartDate) {
+          updatedRegion.visitStartDate = {
+            year: moment(from, 'YYYY-MM-DD').year(),
+            month: moment(from, 'YYYY-MM-DD').month() + 1,
+            day: null
+          };
+          updatedRegion.year_from = moment(from, 'YYYY-MM-DD').year();
+          updatedRegion.month_from = moment(from, 'YYYY-MM-DD').month() + 1;
+        }
+
+        if (hasEmptyEndDate) {
+          updatedRegion.visitEndDate = {
+            year: moment(to, 'YYYY-MM-DD').year(),
+            month: moment(to, 'YYYY-MM-DD').month() + 1,
+            day: null
+          };
+          updatedRegion.year_to = moment(to, 'YYYY-MM-DD').year();
+          updatedRegion.month_to = moment(to, 'YYYY-MM-DD').month() + 1;
+        }
+
+        return updatedRegion;
+      }
+
+      return region;
+    });
+
+    return updatedRegions;
+  };
+
   useEffect(() => {
     if (route.params?.regionsToSave) {
       setRegions((currentRegions) => {
@@ -100,7 +142,7 @@ const AddNewTripScreen = ({ route }: { route: any }) => {
           };
         });
 
-        return updatedRegions;
+        return fillRegionDatesFromSelectedDates(updatedRegions);
       });
     }
   }, [route.params?.regionsToSave]);
@@ -140,10 +182,18 @@ const AddNewTripScreen = ({ route }: { route: any }) => {
 
   useEffect(() => {
     if (regions?.length && selectedDates) {
+      setRegions((currentRegions) => {
+        if (!currentRegions) return null;
+        return fillRegionDatesFromSelectedDates(currentRegions);
+      });
       setDisabled(false);
     } else {
       setDisabled(true);
     }
+  }, [selectedDates]);
+
+  useEffect(() => {
+    setDisabled(!(regions?.length && selectedDates));
   }, [regions, selectedDates]);
 
   const currentYear = new Date().getFullYear();