Browse Source

disabled the requirements for dates

Viktoriia 12 giờ trước cách đây
mục cha
commit
ea5b1613d0

+ 35 - 29
src/screens/InAppScreens/TravelsScreen/AddNewTripScreen/index.tsx

@@ -63,7 +63,7 @@ const AddNewTripScreen = ({ route }: { route: any }) => {
 
   const [showDatePicker, setShowDatePicker] = useState<DatePickerState | null>(null);
   const actionSheetRef = useRef<any>(null);
-  const [selectedYear, setSelectedYear] = useState<number>(new Date().getFullYear());
+  const [selectedYear, setSelectedYear] = useState<number | null>(new Date().getFullYear());
   const [selectedMonth, setSelectedMonth] = useState<number | null>(new Date().getMonth() + 1);
   const [selectedDay, setSelectedDay] = useState<number | null>(null);
 
@@ -197,10 +197,11 @@ const AddNewTripScreen = ({ route }: { route: any }) => {
   }, [regions, selectedDates]);
 
   const currentYear = new Date().getFullYear();
-  const years = Array.from({ length: 120 }, (_, i) => currentYear - 80 + i);
+  const years = ['-', ...Array.from({ length: 120 }, (_, i) => (currentYear - 80 + i).toString())];
 
-  const getAvailableMonths = (year: number) => {
+  const getAvailableMonths = (year: number | null) => {
     const allMonths = [
+      { label: '-', value: null },
       { label: 'Jan', value: 1 },
       { label: 'Feb', value: 2 },
       { label: 'Mar', value: 3 },
@@ -221,10 +222,10 @@ const AddNewTripScreen = ({ route }: { route: any }) => {
   const months = getAvailableMonths(selectedYear);
 
   const getDaysInMonth = (
-    year: number,
+    year: number | null,
     month: number | null
   ): Array<{ label: string; value: number | null }> => {
-    if (!month) return [{ label: '-', value: null }];
+    if (!year || !month) return [{ label: '-', value: null }];
 
     const daysCount = moment(`${year}-${month}`, 'YYYY-M').daysInMonth();
     const days = [{ label: '-', value: null }];
@@ -245,27 +246,25 @@ const AddNewTripScreen = ({ route }: { route: any }) => {
   ) => {
     setShowDatePicker({ regionId, field });
 
-    if (initialDate && initialDate.year && initialDate.month) {
-      setSelectedYear(initialDate.year);
-      setSelectedMonth(initialDate.month);
+    if (initialDate && initialDate.year) {
+      setSelectedYear(initialDate.year || null);
+      setSelectedMonth(initialDate.month || null);
       setSelectedDay(initialDate.day || null);
     } else {
-      const today = new Date();
-
-      setSelectedYear(today.getFullYear());
-      setSelectedMonth(today.getMonth() + 1);
-      setSelectedDay(today.getDate());
+      setSelectedYear(null);
+      setSelectedMonth(null);
+      setSelectedDay(null);
     }
 
     actionSheetRef.current?.show();
   };
 
   const handleDateConfirm = () => {
-    if (showDatePicker && selectedMonth) {
+    if (showDatePicker) {
       const dateValue: DateValue = {
-        year: selectedYear,
-        month: selectedMonth,
-        day: selectedDay
+        year: selectedYear || null,
+        month: selectedMonth || null,
+        day: selectedDay || null
       };
 
       setRegions(
@@ -337,10 +336,10 @@ const AddNewTripScreen = ({ route }: { route: any }) => {
           id: region.id,
           quality: region.quality ?? 3,
           hidden: region.hidden,
-          year_from: region.visitStartDate?.year || new Date().getFullYear(),
-          year_to: region.visitEndDate?.year || new Date().getFullYear(),
-          month_from: region.visitStartDate?.month || new Date().getMonth() + 1,
-          month_to: region.visitEndDate?.month || new Date().getMonth() + 1,
+          year_from: region.visitStartDate?.year || null,
+          year_to: region.visitEndDate?.year || null,
+          month_from: region.visitStartDate?.month || null,
+          month_to: region.visitEndDate?.month || null,
           day_from: region.visitStartDate?.day || null,
           day_to: region.visitEndDate?.day || null
         };
@@ -372,10 +371,10 @@ const AddNewTripScreen = ({ route }: { route: any }) => {
           id: region.id,
           quality: region.quality ?? 3,
           hidden: region.hidden,
-          year_from: region.visitStartDate?.year || new Date().getFullYear(),
-          year_to: region.visitEndDate?.year || new Date().getFullYear(),
-          month_from: region.visitStartDate?.month || new Date().getMonth() + 1,
-          month_to: region.visitEndDate?.month || new Date().getMonth() + 1,
+          year_from: region.visitStartDate?.year || null,
+          year_to: region.visitEndDate?.year || null,
+          month_from: region.visitStartDate?.month || null,
+          month_to: region.visitEndDate?.month || null,
           day_from: region.visitStartDate?.day || null,
           day_to: region.visitEndDate?.day || null
         };
@@ -542,10 +541,11 @@ const AddNewTripScreen = ({ route }: { route: any }) => {
                 fontFamily: 'montserrat-600'
               }}
               pickerData={months ? months?.map((m) => m.label) : []}
-              selectedValue={months?.find((m) => m.value === selectedMonth)?.label || 'Jan'}
+              selectedValue={months.find((m) => m.value === selectedMonth)?.label || '-'}
               onValueChange={(value: string) => {
                 const month = months?.find((m) => m.label === value);
                 setSelectedMonth(month?.value || null);
+                if (!month?.value) setSelectedDay(null);
               }}
             />
           </View>
@@ -558,9 +558,15 @@ const AddNewTripScreen = ({ route }: { route: any }) => {
               itemStyle={{ fontSize: 16, fontFamily: 'montserrat-600' }}
               isCyclic={true}
               pickerData={years}
-              selectedValue={selectedYear.toString()}
-              onValueChange={(value: number) => {
-                setSelectedYear(value);
+              selectedValue={selectedYear ? selectedYear.toString() : '-'}
+              onValueChange={(value: string) => {
+                if (value === '-') {
+                  setSelectedYear(null);
+                  setSelectedMonth(null);
+                  setSelectedDay(null);
+                } else {
+                  setSelectedYear(Number(value));
+                }
               }}
             />
           </View>

+ 3 - 3
src/screens/InAppScreens/TravelsScreen/Components/RegionItem/index.tsx

@@ -1,4 +1,4 @@
-import React, { useEffect, useState, useCallback } from 'react';
+import React, { useEffect, useState, useCallback, JSX } from 'react';
 import { View, Text, Image, TouchableOpacity } from 'react-native';
 import { MaterialCommunityIcons } from '@expo/vector-icons';
 
@@ -51,7 +51,7 @@ const RegionItem = ({
     : 'Good Visit';
 
   const formatDateForDisplay = useCallback((dateValue: DateValue | null): string => {
-    if (!dateValue || !dateValue.year || !dateValue.month) return 'Select date';
+    if (!dateValue || !dateValue.year) return 'Select date';
 
     let result = dateValue.year.toString();
 
@@ -67,7 +67,7 @@ const RegionItem = ({
   }, []);
 
   const isDateValid = useCallback((dateValue: DateValue | null): boolean => {
-    return dateValue !== null && dateValue.year !== null && dateValue.month !== null;
+    return dateValue !== null && dateValue.year !== null;
   }, []);
 
   const renderDatePicker = useCallback(

+ 27 - 20
src/screens/InAppScreens/TravelsScreen/EditNmDataScreen/index.tsx

@@ -1,4 +1,4 @@
-import React, { useState, useEffect, useCallback, useRef } from 'react';
+import React, { useState, useEffect, useCallback, useRef, JSX } from 'react';
 import {
   View,
   Text,
@@ -92,7 +92,7 @@ const EditNmDataScreen = ({ navigation, route }: { navigation: any; route: any }
   const [isLoading, setIsLoading] = useState<boolean>(false);
 
   const actionSheetRef = useRef<any>(null);
-  const [selectedYear, setSelectedYear] = useState<number>(new Date().getFullYear());
+  const [selectedYear, setSelectedYear] = useState<number | null>(new Date().getFullYear());
   const [selectedMonth, setSelectedMonth] = useState<number | null>(null);
   const [selectedDay, setSelectedDay] = useState<number | null>(null);
 
@@ -151,14 +151,14 @@ const EditNmDataScreen = ({ navigation, route }: { navigation: any; route: any }
   );
 
   const addNewVisit = useCallback((): void => {
-    const hasEmptyVisit = visits.some(
-      (visit: Visit) => !visit.startDate || !visit.endDate || !visit.quality
-    );
+    // const hasEmptyVisit = visits.some(
+    //   (visit: Visit) => !visit.startDate || !visit.endDate || !visit.quality
+    // );
 
-    if (hasEmptyVisit) {
-      Alert.alert('Please fill all fields in existing visits before adding a new one.');
-      return;
-    }
+    // if (hasEmptyVisit) {
+    //   Alert.alert('Please fill all fields in existing visits before adding a new one.');
+    //   return;
+    // }
 
     const newVisit = createEmptyVisit();
     setVisits((prev) => [newVisit, ...prev]);
@@ -425,10 +425,10 @@ const EditNmDataScreen = ({ navigation, route }: { navigation: any; route: any }
     const newVisits = visits.filter((visit: Visit) => !visit.isExisting);
 
     for (const visit of newVisits) {
-      if (!isDateValid(visit.startDate) || !isDateValid(visit.endDate) || !visit.quality) {
-        Alert.alert('Please fill all fields for each visit.');
-        return false;
-      }
+      // if (!isDateValid(visit.startDate) || !isDateValid(visit.endDate) || !visit.quality) {
+      //   Alert.alert('Please fill all fields for each visit.');
+      //   return false;
+      // }
 
       if (!compareDates(visit.startDate, visit.endDate)) {
         Alert.alert('Start date cannot be after end date.');
@@ -526,8 +526,9 @@ const EditNmDataScreen = ({ navigation, route }: { navigation: any; route: any }
   }, [validateVisits, visits, navigation]);
 
   const currentYear = new Date().getFullYear();
-  const years = Array.from({ length: 120 }, (_, i) => currentYear - 80 + i);
-  const getAvailableMonths = (year: number) => {
+  // const years = Array.from({ length: 120 }, (_, i) => currentYear - 80 + i);
+  const years = ['-', ...Array.from({ length: 120 }, (_, i) => (currentYear - 80 + i).toString())];
+  const getAvailableMonths = (year: number | null) => {
     const allMonths = [
       { label: '-', value: null },
       { label: 'Jan', value: 1 },
@@ -550,10 +551,10 @@ const EditNmDataScreen = ({ navigation, route }: { navigation: any; route: any }
   const months = getAvailableMonths(selectedYear);
 
   const getDaysInMonth = (
-    year: number,
+    year: number | null,
     month: number | null
   ): Array<{ label: string; value: number | null }> => {
-    if (!month) return [{ label: '-', value: null }];
+    if (!year || !month) return [{ label: '-', value: null }];
 
     const daysCount = moment(`${year}-${month}`, 'YYYY-M').daysInMonth();
     const days = [{ label: '-', value: null }];
@@ -820,9 +821,15 @@ const EditNmDataScreen = ({ navigation, route }: { navigation: any; route: any }
               itemStyle={{ fontSize: 16, fontFamily: 'montserrat-600' }}
               isCyclic={true}
               pickerData={years}
-              selectedValue={selectedYear.toString()}
-              onValueChange={(value: number) => {
-                setSelectedYear(value);
+              selectedValue={selectedYear ? selectedYear.toString() : '-'}
+              onValueChange={(value: string) => {
+                if (value === '-') {
+                  setSelectedYear(null);
+                  setSelectedMonth(null);
+                  setSelectedDay(null);
+                } else {
+                  setSelectedYear(Number(value));
+                }
               }}
             />
           </View>