diff --git a/app/(tabs)/tripInfo.tsx b/app/(tabs)/tripInfo.tsx index 08dda5d..eb74ff1 100644 --- a/app/(tabs)/tripInfo.tsx +++ b/app/(tabs)/tripInfo.tsx @@ -5,10 +5,16 @@ import CrewListTable from "@/components/tripInfo/CrewListTable"; import FishingToolsTable from "@/components/tripInfo/FishingToolsList"; import NetListTable from "@/components/tripInfo/NetListTable"; import TripCostTable from "@/components/tripInfo/TripCostTable"; +import { useTrip } from "@/state/use-trip"; +import { useEffect } from "react"; import { Platform, ScrollView, StyleSheet, Text, View } from "react-native"; import { SafeAreaView } from "react-native-safe-area-context"; export default function TripInfoScreen() { + const { trip, getTrip } = useTrip(); + useEffect(() => { + getTrip(); + }, []); return ( diff --git a/components/tripInfo/TripCostTable.tsx b/components/tripInfo/TripCostTable.tsx index fa509cd..2717582 100644 --- a/components/tripInfo/TripCostTable.tsx +++ b/components/tripInfo/TripCostTable.tsx @@ -1,4 +1,5 @@ import { IconSymbol } from "@/components/ui/icon-symbol"; +import { useTrip } from "@/state/use-trip"; import React, { useRef, useState } from "react"; import { Animated, Text, TouchableOpacity, View } from "react-native"; import TripCostDetailModal from "./modal/TripCostDetailModal"; @@ -64,6 +65,7 @@ const TripCostTable: React.FC = () => { const [modalVisible, setModalVisible] = useState(false); const animatedHeight = useRef(new Animated.Value(0)).current; const tongCong = data.reduce((sum, item) => sum + item.tongChiPhi, 0); + const { trip, getTrip } = useTrip(); const handleToggle = () => { const toValue = collapsed ? contentHeight : 0; @@ -95,6 +97,8 @@ const TripCostTable: React.FC = () => { // marginBottom: 12, }} > + {/* {trip && } */} + Chi phí chuyến đi {collapsed && ( (API_GET_TRIP); +} diff --git a/controller/index.ts b/controller/index.ts index 86fbfb9..39df43a 100644 --- a/controller/index.ts +++ b/controller/index.ts @@ -1,4 +1,5 @@ import * as AuthController from "./AuthController"; import * as DeviceController from "./DeviceController"; import * as MapController from "./MapController"; -export { AuthController, DeviceController, MapController }; +import * as TripController from "./TripController"; +export { AuthController, DeviceController, MapController, TripController }; diff --git a/controller/typings.d.ts b/controller/typings.d.ts index 5948ddf..8551605 100644 --- a/controller/typings.d.ts +++ b/controller/typings.d.ts @@ -90,4 +90,75 @@ declare namespace Model { message?: string; started_at?: number; } + // Trip + interface Trip { + id: string; + ship_id: string; + ship_length: number; + vms_id: string; + name: string; + fishing_gears: FishingGear[]; // Dụng cụ đánh cá + crews?: TripCrews[]; // Thuyền viên + departure_time: string; // ISO datetime string + departure_port_id: number; + arrival_time: string; // ISO datetime string + arrival_port_id: number; + fishing_ground_codes: number[]; + total_catch_weight: number | null; + total_species_caught: number | null; + trip_cost: TripCost[]; // Chi phí chuyến đi + trip_status: number; + approved_by: string; + notes: string | null; + fishing_logs: FishingLog[] | null; // tuỳ dữ liệu chi tiết có thể định nghĩa thêm + sync: boolean; + } + + // Dụng cụ đánh cá + interface FishingGear { + name: string; + number: string; + } + // Thuyền viên + interface TripCrews { + role: string; + joined_at: Date; + left_at: Date | null; + note: string | null; + Person: TripCrewPerson; + } + // Chi phí chuyến đi + interface TripCost { + type: string; + unit: string; + amount: string; + total_cost: string; + cost_per_unit: string; + } + // Thông tin mẻ lưới + interface FishingLog { + fishing_log_id: string; + trip_id: string; + start_at: Date; // ISO datetime + end_at: Date; // ISO datetime + start_lat: number; + start_lon: number; + haul_lat: number; + haul_lon: number; + status: number; + weather_description: string; + info?: FishingLogInfo[]; // Thông tin cá + sync: boolean; + } + // Thông tin cá + interface FishingLogInfo { + fish_species_id?: number; + fish_name?: string; + catch_number?: number; + catch_unit?: string; + fish_size?: number; + fish_rarity?: number; + fish_condition?: string; + gear_usage?: string; + } } diff --git a/state/use-trip.ts b/state/use-trip.ts new file mode 100644 index 0000000..e77e643 --- /dev/null +++ b/state/use-trip.ts @@ -0,0 +1,26 @@ +import { queryTrip } from "@/controller/TripController"; +import { create } from "zustand"; + +type Trip = { + trip: Model.Trip | null; + getTrip: () => Promise; + error: string | null; + loading?: boolean; +}; + +export const useTrip = create((set) => ({ + trip: null, + getTrip: async () => { + try { + const response = await queryTrip(); + console.log("Trip fetching: ", response.data); + + set({ trip: response.data, loading: false }); + } catch (error) { + console.error("Error when fetch trip: ", error); + set({ error: "Failed to fetch trip data", loading: false }); + set({ trip: null }); + } + }, + error: null, +}));