Made Mission marker DB compatible
This commit is contained in:
@@ -11,12 +11,12 @@ interface MapStore {
|
||||
zoom: number;
|
||||
};
|
||||
openMissionMarker: {
|
||||
id: string;
|
||||
tab: "home" | "";
|
||||
id: number;
|
||||
tab: "home" | "details" | "chat" | "log";
|
||||
}[];
|
||||
setOpenMissionMarker: (mission: {
|
||||
open: MapStore["openMissionMarker"];
|
||||
close: string[];
|
||||
close: number[];
|
||||
}) => void;
|
||||
openAircraftMarker: {
|
||||
id: string;
|
||||
@@ -58,22 +58,16 @@ interface MapStore {
|
||||
aircraftId: string,
|
||||
tab: MapStore["aircraftTabs"][string],
|
||||
) => void;
|
||||
missionTabs: {
|
||||
[missionId: string]: "home" | "details" | "chat";
|
||||
};
|
||||
setMissionTab: (
|
||||
missionId: string,
|
||||
tab: MapStore["missionTabs"][string],
|
||||
) => void;
|
||||
}
|
||||
|
||||
export const useMapStore = create<MapStore>((set, get) => ({
|
||||
openMissionMarker: [],
|
||||
setOpenMissionMarker: ({ open, close }) => {
|
||||
set((state) => ({
|
||||
openMissionMarker: [...state.openMissionMarker, ...open].filter(
|
||||
(marker) => !close.includes(marker.id),
|
||||
),
|
||||
const oldMarkers = get().openMissionMarker.filter(
|
||||
(m) => !close.includes(m.id) && !open.find((o) => o.id === m.id),
|
||||
);
|
||||
set(() => ({
|
||||
openMissionMarker: [...oldMarkers, ...open],
|
||||
}));
|
||||
},
|
||||
openAircraftMarker: [],
|
||||
@@ -81,7 +75,7 @@ export const useMapStore = create<MapStore>((set, get) => ({
|
||||
const oldMarkers = get().openAircraftMarker.filter(
|
||||
(m) => !close.includes(m.id) && !open.find((o) => o.id === m.id),
|
||||
);
|
||||
|
||||
console.log("oldMarkers", oldMarkers, open);
|
||||
set(() => ({
|
||||
openAircraftMarker: [...oldMarkers, ...open],
|
||||
}));
|
||||
@@ -114,11 +108,4 @@ export const useMapStore = create<MapStore>((set, get) => ({
|
||||
},
|
||||
})),
|
||||
missionTabs: {},
|
||||
setMissionTab: (missionId, tab) =>
|
||||
set((state) => ({
|
||||
missionTabs: {
|
||||
...state.missionTabs,
|
||||
[missionId]: tab,
|
||||
},
|
||||
})),
|
||||
}));
|
||||
|
||||
@@ -1,46 +1,20 @@
|
||||
import { Mission, Prisma } from "@repo/db";
|
||||
import { MissionOptionalDefaults } from "@repo/db/zod";
|
||||
import { serverApi } from "helpers/axios";
|
||||
import { create } from "zustand";
|
||||
import { toast } from "react-hot-toast";
|
||||
|
||||
interface MissionStore {
|
||||
missions: Mission[];
|
||||
setMissions: (missions: Mission[]) => void;
|
||||
getMissions: () => Promise<undefined>;
|
||||
createMission: (mission: MissionOptionalDefaults) => Promise<Mission>;
|
||||
setMission: (mission: Mission) => void;
|
||||
deleteMission: (id: number) => Promise<void>;
|
||||
editMission: (id: number, mission: Partial<Mission>) => Promise<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: "TestKName",
|
||||
missionKeywordCategory: "TestKCategory",
|
||||
missionKeywordAbbreviation: "TestKAbbreviation",
|
||||
missionPatientInfo: "TestPatientInfo",
|
||||
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(),
|
||||
},
|
||||
],
|
||||
missions: [],
|
||||
setMissions: (missions) => set({ missions }),
|
||||
createMission: async (mission) => {
|
||||
const res = await fetch("/api/mission", {
|
||||
@@ -55,41 +29,46 @@ export const useMissionsStore = create<MissionStore>((set) => ({
|
||||
set((state) => ({ missions: [...state.missions, data] }));
|
||||
return data;
|
||||
},
|
||||
editMission: async (id, mission) => {
|
||||
const { data, status } = await serverApi.patch<Mission>(
|
||||
`/mission/${id}`,
|
||||
mission,
|
||||
);
|
||||
if (status.toString().startsWith("2") && data) {
|
||||
set((state) => ({
|
||||
missions: state.missions.map((m) => (m.id === id ? data : m)),
|
||||
}));
|
||||
toast.success("Mission updated successfully");
|
||||
} else {
|
||||
toast.error("Failed to update mission");
|
||||
}
|
||||
},
|
||||
deleteMission: async (id) => {
|
||||
serverApi
|
||||
.delete(`/mission/${id}`)
|
||||
.then((res) => {
|
||||
if (res.status.toString().startsWith("2")) {
|
||||
set((state) => ({
|
||||
missions: state.missions.filter((mission) => mission.id !== id),
|
||||
}));
|
||||
toast.success("Mission deleted successfully");
|
||||
} else {
|
||||
toast.error("Failed to delete mission");
|
||||
}
|
||||
})
|
||||
.catch((err) => {
|
||||
toast.error("Failed to delete mission");
|
||||
});
|
||||
},
|
||||
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),
|
||||
const { data } = await serverApi.post<Mission[]>("/mission", {
|
||||
filter: {
|
||||
OR: [{ state: "draft" }, { state: "running" }],
|
||||
} as Prisma.MissionWhereInput,
|
||||
});
|
||||
if (!res.ok) return undefined;
|
||||
const data = await res.json();
|
||||
//set({ missions: data });
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user