diff --git a/components/tripInfo/NetListTable.tsx b/components/tripInfo/NetListTable.tsx index d64bf2d..2b4b5f6 100644 --- a/components/tripInfo/NetListTable.tsx +++ b/components/tripInfo/NetListTable.tsx @@ -1,6 +1,7 @@ import { IconSymbol } from "@/components/ui/icon-symbol"; +import { useFishes } from "@/state/use-fish"; import { useTrip } from "@/state/use-trip"; -import React, { useRef, useState } from "react"; +import React, { useEffect, useRef, useState } from "react"; import { Animated, Text, TouchableOpacity, View } from "react-native"; import NetDetailModal from "./modal/NetDetailModal/NetDetailModal"; import styles from "./style/NetListTable.styles"; @@ -12,6 +13,10 @@ const NetListTable: React.FC = () => { const [modalVisible, setModalVisible] = useState(false); const [selectedNet, setSelectedNet] = useState(null); const { trip } = useTrip(); + const { fishSpecies, getFishSpecies } = useFishes(); + useEffect(() => { + getFishSpecies(); + }, []); const data: Model.FishingLog[] = trip?.fishing_logs ?? []; const tongSoMe = data.length; diff --git a/components/tripInfo/modal/NetDetailModal/components/FishCardForm.tsx b/components/tripInfo/modal/NetDetailModal/components/FishCardForm.tsx index 61e52bf..515bf62 100644 --- a/components/tripInfo/modal/NetDetailModal/components/FishCardForm.tsx +++ b/components/tripInfo/modal/NetDetailModal/components/FishCardForm.tsx @@ -1,3 +1,4 @@ +import { useFishes } from "@/state/use-fish"; import React from "react"; import { Text, TextInput, View } from "react-native"; import styles from "../../style/NetDetailModal.styles"; @@ -7,8 +8,8 @@ interface FishCardFormProps { fish: Model.FishingLogInfo; index: number; isEditing: boolean; - fishNameOptions: string[]; - unitOptions: string[]; + fishNameOptions: string[]; // Bỏ gọi API cá + unitOptions: string[]; // Bỏ render ở trong này // conditionOptions: string[]; // gearOptions: string[]; selectedFishIndex: number | null; @@ -30,7 +31,6 @@ export const FishCardForm: React.FC = ({ fish, index, isEditing, - fishNameOptions, unitOptions, // conditionOptions, // gearOptions, @@ -44,6 +44,18 @@ export const FishCardForm: React.FC = ({ // setSelectedGearIndex, onUpdateCatchItem, }) => { + const { fishSpecies } = useFishes(); + const [fishNameOptions, setFishNameOptions] = React.useState([]); + + React.useEffect(() => { + console.log("Length: ", fishSpecies?.length); + + if (Array.isArray(fishSpecies)) { + const names = fishSpecies.map((item) => item.name).filter(Boolean); + setFishNameOptions(names); + } + }, [fishSpecies]); + return ( <> {/* Tên cá - Select */} @@ -53,15 +65,17 @@ export const FishCardForm: React.FC = ({ Tên cá {isEditing ? ( setSelectedFishIndex(selectedFishIndex === index ? null : index) } - onSelect={(value: string) => { - onUpdateCatchItem(index, "fish_name", value); - setSelectedFishIndex(null); + onSelect={(value: Model.FishSpeciesResponse) => { + onUpdateCatchItem(index, "fish_name", value.name); + // setSelectedFishIndex(null); + + console.log("Fish Selected: ", fish); }} zIndex={1000 - index} styleOverride={styles.fishNameDropdown} @@ -96,7 +110,7 @@ export const FishCardForm: React.FC = ({ ]} > Đơn vị - {isEditing ? ( + {/* {isEditing ? ( = ({ /> ) : ( {fish.catch_unit} - )} + )} */} diff --git a/components/tripInfo/modal/NetDetailModal/components/FishSelectDropdown.tsx b/components/tripInfo/modal/NetDetailModal/components/FishSelectDropdown.tsx index 5327539..80980e8 100644 --- a/components/tripInfo/modal/NetDetailModal/components/FishSelectDropdown.tsx +++ b/components/tripInfo/modal/NetDetailModal/components/FishSelectDropdown.tsx @@ -4,11 +4,11 @@ import { ScrollView, Text, TouchableOpacity, View } from "react-native"; import styles from "../../style/NetDetailModal.styles"; interface FishSelectDropdownProps { - options: string[]; - selectedValue: string; + options: Model.FishSpeciesResponse[]; + selectedValue: Model.FishSpeciesResponse | null; isOpen: boolean; onToggle: () => void; - onSelect: (value: string) => void; + onSelect: (value: Model.FishSpeciesResponse) => void; zIndex: number; styleOverride?: any; } @@ -27,7 +27,9 @@ export const FishSelectDropdown: React.FC = ({ return ( - {selectedValue} + + {selectedValue ? selectedValue.name : "Chọn cá"} + = ({ {options.map((option, optIndex) => ( onSelect(option)} > - {option} + {option.name} ))} diff --git a/controller/FishController.ts b/controller/FishController.ts new file mode 100644 index 0000000..2b878a0 --- /dev/null +++ b/controller/FishController.ts @@ -0,0 +1,6 @@ +import { api } from "@/config"; +import { API_GET_FISH } from "@/constants"; + +export async function queryFish() { + return api.get(API_GET_FISH); +} diff --git a/controller/typings.d.ts b/controller/typings.d.ts index b9d00e7..1500520 100644 --- a/controller/typings.d.ts +++ b/controller/typings.d.ts @@ -186,4 +186,27 @@ declare namespace Model { status: number; note?: string; } + //Fish + interface FishSpeciesResponse { + id: number; + name: string; + scientific_name: string; + group_name: string; + species_code: string; + note: string; + default_unit: string; + rarity_level: number; + created_at: string; + updated_at: string; + is_deleted: boolean; + } + interface FishRarity { + id: number; + code: string; + label: string; + description: string; + iucn_code: any; + cites_appendix: any; + vn_law: boolean; + } } diff --git a/state/use-fish.ts b/state/use-fish.ts new file mode 100644 index 0000000..86f6257 --- /dev/null +++ b/state/use-fish.ts @@ -0,0 +1,26 @@ +import { queryFish } from "@/controller/FishController"; +import { create } from "zustand"; + +type Fish = { + fishSpecies: Model.FishSpeciesResponse | null; + getFishSpecies: () => Promise; + error: string | null; + loading?: boolean; +}; + +export const useFishes = create((set) => ({ + fishSpecies: null, + getFishSpecies: async () => { + try { + const response = await queryFish(); + console.log("Fish fetching API"); + + set({ fishSpecies: response.data, loading: false }); + } catch (error) { + console.error("Error when fetch fish: ", error); + set({ error: "Failed to fetch fish data", loading: false }); + set({ fishSpecies: null }); + } + }, + error: null, +}));