Khởi tạo ban đầu
This commit is contained in:
26
state/use-trip.ts
Normal file
26
state/use-trip.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import { queryTrip } from "@/controller/TripController";
|
||||
import { create } from "zustand";
|
||||
|
||||
type Trip = {
|
||||
trip: Model.Trip | null;
|
||||
getTrip: () => Promise<void>;
|
||||
error: string | null;
|
||||
loading?: boolean;
|
||||
};
|
||||
|
||||
export const useTrip = create<Trip>((set) => ({
|
||||
trip: null,
|
||||
getTrip: async () => {
|
||||
try {
|
||||
const response = await queryTrip();
|
||||
console.log("Trip fetching API");
|
||||
|
||||
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,
|
||||
}));
|
||||
Reference in New Issue
Block a user