فهرست منبع

trips updates

Viktoriia 1 هفته پیش
والد
کامیت
ec6126f03b

+ 12 - 10
src/modules/api/trips/trips-api.tsx

@@ -75,20 +75,21 @@ export interface RegionData {
   quality: number;
   // status: 0 | 1;
   hidden: boolean;
-  year_from: number;
-  year_to: number;
-  month_from: number;
-  month_to: number;
-  day_from: number | null;
-  day_to: number | null;
+  year_from: number | null;
+  year_to: number | null;
+  month_from?: number | null;
+  month_to?: number | null;
+  day_from?: number | null;
+  day_to?: number | null;
 }
 
 export interface PostSetNewTrip {
   token: string;
-  date_from: string | null;
-  date_to: string | null;
+  date_from?: string | null;
+  date_to?: string | null;
   description: string;
   regions: RegionData[];
+  year?: number | null;
 }
 
 export interface PostSetNewTripReturn extends ResponseType {
@@ -98,10 +99,11 @@ export interface PostSetNewTripReturn extends ResponseType {
 export interface PostUpdateTrip {
   token: string;
   trip_id: number;
-  date_from: string | null;
-  date_to: string | null;
+  date_from?: string | null;
+  date_to?: string | null;
   description: string;
   regions: RegionData[];
+  year?: number | null;
 }
 
 export interface PostGetTripReturn extends ResponseType {

+ 74 - 56
src/screens/InAppScreens/TravelsScreen/AddNewTrip2025Screen/index.tsx

@@ -58,7 +58,7 @@ const AddNewTripScreen = ({ route }: { route: any }) => {
   const editTripId = route.params?.editTripId ?? null;
   const token = storage.get('token', StoreType.STRING) as string;
   const { data: editData } = useGetTripQuery(token, editTripId, Boolean(editTripId));
-  const navigation = useNavigation();
+  const navigation: any = useNavigation();
   const { defaultRegion, defaultYear } = route.params;
   const [allRegions, setAllRegions] = useState<RegionAddData[] | null>([]);
   const { data } = useGetRegionsForTripsQuery(true);
@@ -437,7 +437,9 @@ const AddNewTripScreen = ({ route }: { route: any }) => {
         }
       }
     } else {
-      sorted = updatedBeforeSort;
+      sorted = [...updatedBeforeSort].sort(
+        (a, b) => (a.visitStartDate?.year ?? 9999) - (b.visitStartDate?.year ?? 9999)
+      );
 
       const editedRegionId = regions?.[clickedInstanceIndex]?.id;
       const propagatedIdSet = new Set<number | string>();
@@ -492,31 +494,39 @@ const AddNewTripScreen = ({ route }: { route: any }) => {
     setPendingDelete(true);
   };
 
-  const computePayloadDates = (regionsList: RegionWithDates[]) => {
-    if (!regionsList || regionsList.length === 0) return { date_from: null, date_to: null };
-
-    const starts = regionsList
-      .map((r) => dateValueToISO(r.visitStartDate))
-      .filter((v): v is string => v !== null);
-    const ends = regionsList
-      .map((r) => dateValueToISO(r.visitEndDate))
-      .filter((v): v is string => v !== null);
-
-    if (!starts.length || !ends.length) return { date_from: null, date_to: null };
-
-    const toComparableStart = (v: string) =>
-      v.length === 4 ? `${v}-01-01` : v;
-    const toComparableEnd = (v: string) =>
-      v.length === 4 ? `${v}-12-31` : v;
+  const computePayloadDates = (
+    regionsList: RegionWithDates[]
+  ): { date_from?: string | null; date_to?: string | null; year?: number | null } => {
+    if (!regionsList || regionsList.length === 0) return {};
 
-    const minStart = starts.reduce((acc, cur) =>
-      toComparableStart(cur) < toComparableStart(acc) ? cur : acc
+    const allFullDates = regionsList.every(
+      (r) => isFullDate(r.visitStartDate) && isFullDate(r.visitEndDate)
     );
-    const maxEnd = ends.reduce((acc, cur) =>
-      toComparableEnd(cur) > toComparableEnd(acc) ? cur : acc
+    const allYearOnly = regionsList.every(
+      (r) => r.visitStartDate?.year && !r.visitStartDate?.month
     );
 
-    return { date_from: minStart, date_to: maxEnd };
+    if (allFullDates) {
+      const starts = regionsList
+        .map((r) => dateValueToISO(r.visitStartDate))
+        .filter((v): v is string => v !== null);
+      const ends = regionsList
+        .map((r) => dateValueToISO(r.visitEndDate))
+        .filter((v): v is string => v !== null);
+      const minStart = starts.reduce((acc, cur) => (cur < acc ? cur : acc));
+      const maxEnd = ends.reduce((acc, cur) => (cur > acc ? cur : acc));
+      return { date_from: minStart, date_to: maxEnd };
+    }
+
+    if (allYearOnly) {
+      const years = regionsList.map((r) => r.visitStartDate!.year!);
+      const uniqueYears = new Set(years);
+      if (uniqueYears.size === 1) {
+        return { year: years[0] };
+      }
+    }
+
+    return {};
   };
 
   const performSaveNewTrip = async () => {
@@ -524,24 +534,28 @@ const AddNewTripScreen = ({ route }: { route: any }) => {
 
     try {
       setIsLoading('save');
-      const regionsData = regions.map((region) => ({
-        id: region.id,
-        quality: 3,
-        hidden: region.hidden,
-        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
-      }));
-      const { date_from, date_to } = computePayloadDates(regions);
+      const regionsData = regions.map((region) => {
+        const yearOnly = region.visitStartDate?.year && !region.visitStartDate?.day;
+        return {
+          id: region.id,
+          quality: 3,
+          hidden: region.hidden,
+          year_from: region.visitStartDate?.year || null,
+          year_to: region.visitEndDate?.year || null,
+          ...(yearOnly ? {} : {
+            month_from: region.visitStartDate?.month || null,
+            month_to: region.visitEndDate?.month || null,
+            day_from: region.visitStartDate?.day || null,
+            day_to: region.visitEndDate?.day || null
+          })
+        };
+      });
+      const payload = computePayloadDates(regions);
 
       saveNewTrip(
-        { token, date_from, date_to, description, regions: regionsData },
+        { token, ...payload, description, regions: regionsData },
         {
           onSuccess: (res) => {
-            console.log('res', res)
             if (res && res.result === 'OK') {
               navigation.popTo(NAVIGATION_PAGES.TRIPS_2025, { saved: true });
             }
@@ -562,20 +576,26 @@ const AddNewTripScreen = ({ route }: { route: any }) => {
     if (!regions) return;
     try {
       setIsLoading('update');
-      const regionsData = regions.map((region) => ({
-        id: region.id,
-        quality: region.quality ?? 3,
-        hidden: region.hidden,
-        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
-      }));
-      const { date_from, date_to } = computePayloadDates(regions);
+      const regionsData = regions.map((region) => {
+        const yearOnly = region.visitStartDate?.year && !region.visitStartDate?.day;
+        return {
+          id: region.id,
+          quality: region.quality ?? 3,
+          hidden: region.hidden,
+          year_from: region.visitStartDate?.year || null,
+          year_to: region.visitEndDate?.year || null,
+          ...(yearOnly ? {} : {
+            month_from: region.visitStartDate?.month || null,
+            month_to: region.visitEndDate?.month || null,
+            day_from: region.visitStartDate?.day || null,
+            day_to: region.visitEndDate?.day || null
+          })
+        };
+      });
+      const payload = computePayloadDates(regions);
+
       updateTrip(
-        { token, trip_id: editTripId, date_from, date_to, description, regions: regionsData },
+        { token, trip_id: editTripId, ...payload, description, regions: regionsData },
         {
           onSuccess: (res) => {
             if (res && res.result === 'OK') {
@@ -597,7 +617,7 @@ const AddNewTripScreen = ({ route }: { route: any }) => {
       scrollToError(regionErrors);
       return;
     }
-    const summary = computeTripSummary(regions);
+    const summary: { totalDays: number, perRegion: any } = computeTripSummary(regions);
     if (summary.totalDays === 0) {
       performSaveNewTrip();
       return;
@@ -614,7 +634,7 @@ const AddNewTripScreen = ({ route }: { route: any }) => {
       scrollToError(regionErrors);
       return;
     }
-    const summary = computeTripSummary(regions);
+    const summary: { totalDays: number, perRegion: any } = computeTripSummary(regions);
     if (summary.totalDays === 0) {
       performUpdateTrip();
       return;
@@ -688,10 +708,8 @@ const AddNewTripScreen = ({ route }: { route: any }) => {
           style={[styles.addRegionBtn]}
           onPress={() =>
             navigation.navigate(
-              ...([
-                NAVIGATION_PAGES.ADD_REGIONS_NEW,
-                { regionsParams: regions, editId: editTripId, defaultYear, allRegions }
-              ] as never)
+              NAVIGATION_PAGES.ADD_REGIONS_NEW,
+              { regionsParams: regions, editId: editTripId, defaultYear, allRegions }
             )
           }
         >

+ 2 - 2
src/screens/InAppScreens/TravelsScreen/AddRegionsNewScreen/index.tsx

@@ -52,9 +52,9 @@ const AddRegionsScreen = ({ route }: { route: any }) => {
     regionsParams,
     allRegions
   }: { regionsParams: RegionAddData[]; allRegions: RegionAddData[] } = route.params;
-  const { data } = useGetRegionsForTripsQuery(allRegions && allRegions.length ? false : true);
+  const { data } = useGetRegionsForTripsQuery(true);
   const { data: regionsList } = useGetListRegionsQuery(true);
-  const navigation = useNavigation();
+  const navigation: any = useNavigation();
 
   const [regions, setRegions] = useState<RegionAddData[] | null>(allRegions);
   const [isModalVisible, setIsModalVisible] = useState(false);

+ 8 - 1
src/screens/InAppScreens/TravelsScreen/Components/TripItem/index.tsx

@@ -72,6 +72,13 @@ const getTripDateLabel = (item: TripsData) => {
   const validFrom = getValidDate(item.date_from);
   const validTo = getValidDate(item.date_to);
 
+  if (!validFrom && !validTo) {
+    if (item.year) {
+      return <Text style={[styles.tripDateText, { marginLeft: 8 }]}>{item.year}</Text>;
+    }
+    return null;
+  }
+
   const fromYear = validFrom ? validFrom.format('YYYY') : null;
   const toYear = validTo ? validTo.format('YYYY') : null;
 
@@ -144,7 +151,7 @@ const TripItem = ({ item }: { item: TripsData }) => {
   const validFrom = getValidDate(item.date_from);
   const validTo = getValidDate(item.date_to);
 
-  const hasAnyValidDate = !!validFrom || !!validTo;
+  const hasAnyValidDate = !!validFrom || !!validTo || !!item.year;;
   const shouldRenderWarningAbove = item.dates_missing === 1 && hasAnyValidDate;
   const shouldRenderWarningInsteadOfDate = item.dates_missing === 1 && !hasAnyValidDate;
 

+ 1 - 0
src/screens/InAppScreens/TravelsScreen/utils/types.ts

@@ -57,6 +57,7 @@ export interface TripsData {
   description: string;
   regions: RegionTripsData[];
   dates_missing: 0 | 1;
+  year?: number | null;
 }
 
 export interface RegionTripsData {