101 lines
2.5 KiB
TypeScript
101 lines
2.5 KiB
TypeScript
import { Mission, Prisma } from "@repo/db";
|
|
import { MissionOptionalDefaults } from "@repo/db/zod";
|
|
import { create } from "zustand";
|
|
|
|
interface MissionStore {
|
|
missions: Mission[];
|
|
setMissions: (missions: Mission[]) => void;
|
|
getMissions: () => Promise<undefined>;
|
|
createMission: (mission: MissionOptionalDefaults) => Promise<Mission>;
|
|
setMission: (mission: Mission) => void;
|
|
}
|
|
|
|
export const useMissionsStore = create<MissionStore>((set) => ({
|
|
missions: [
|
|
{
|
|
id: 1,
|
|
type: "primär",
|
|
state: "draft",
|
|
addressCity: "Berlin",
|
|
addressStreet: "Alexanderplatz",
|
|
addressZip: "10178",
|
|
addressOSMways: [],
|
|
missionAdditionalInfo: "",
|
|
addressLat: 52.520008,
|
|
addressLng: 13.404954,
|
|
missionKeywordName: "Test",
|
|
missionKeywordCategory: "Test",
|
|
missionKeywordAbbreviation: "Test",
|
|
missionPatientInfo: "Test",
|
|
missionStationIds: [],
|
|
createdUserId: "1",
|
|
hpgMissionString: null,
|
|
missionLog: [],
|
|
missionStationUserIds: [],
|
|
hpgLocationLat: 52.520008,
|
|
hpgLocationLng: 13.404954,
|
|
hpgAmbulanceState: null,
|
|
hpgFireEngineState: null,
|
|
hpgPoliceState: null,
|
|
createdAt: new Date(),
|
|
updatedAt: new Date(),
|
|
},
|
|
],
|
|
setMissions: (missions) => set({ missions }),
|
|
createMission: async (mission) => {
|
|
const res = await fetch("/api/mission", {
|
|
method: "PUT",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
},
|
|
body: JSON.stringify(mission),
|
|
});
|
|
if (!res.ok) return new Error("Failed to create mission");
|
|
const data = await res.json();
|
|
set((state) => ({ missions: [...state.missions, data] }));
|
|
return data;
|
|
},
|
|
getMissions: async () => {
|
|
const res = await fetch("/api/mission", {
|
|
method: "POST",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
},
|
|
body: JSON.stringify({
|
|
OR: [
|
|
{
|
|
state: "draft",
|
|
},
|
|
{
|
|
state: "running",
|
|
},
|
|
],
|
|
} as Prisma.MissionWhereInput),
|
|
});
|
|
if (!res.ok) return undefined;
|
|
const data = await res.json();
|
|
set({ missions: data });
|
|
return undefined;
|
|
},
|
|
setMission: (mission) =>
|
|
set((state) => {
|
|
const existingMissionIndex = state.missions.findIndex(
|
|
(m) => m.id === mission.id,
|
|
);
|
|
if (existingMissionIndex !== -1) {
|
|
const updatedMissions = [...state.missions];
|
|
updatedMissions[existingMissionIndex] = mission;
|
|
return { missions: updatedMissions };
|
|
} else {
|
|
return { missions: [...state.missions, mission] };
|
|
}
|
|
}),
|
|
}));
|
|
|
|
useMissionsStore
|
|
.getState()
|
|
.getMissions()
|
|
.then(() => {
|
|
console.log("Missions loaded");
|
|
});
|