Files
WhiteNightsAdminPanel/src/shared/store/RouteStore/index.ts

151 lines
3.5 KiB
TypeScript

import { makeAutoObservable, runInAction } from "mobx";
import { authInstance } from "@shared";
export type Route = {
carrier: string;
carrier_id: number;
center_latitude: number;
center_longitude: number;
governor_appeal: number;
id: number;
path: number[][];
rotate: number;
route_direction: boolean;
route_number: string;
route_sys_number: string;
scale_max: number;
scale_min: number;
video_preview: string;
};
class RouteStore {
routes: {
data: Route[];
loaded: boolean;
} = {
data: [],
loaded: false,
};
route: Record<string, Route> = {};
constructor() {
makeAutoObservable(this);
}
getRoutes = async () => {
if (this.routes.loaded) return;
const response = await authInstance.get("/route");
runInAction(() => {
this.routes = {
data: response.data,
loaded: true,
};
});
};
createRoute = async (route: any) => {
const response = await authInstance.post("/route", route);
const id = response.data.id;
runInAction(() => {
this.route[id] = { ...route, id };
this.routes.data = [...this.routes.data, { ...route, id }];
});
};
deleteRoute = async (id: number) => {
await authInstance.delete(`/route/${id}`);
runInAction(() => {
this.routes = {
data: this.routes.data.filter((route) => route.id !== id),
loaded: true,
};
});
};
routeStations: Record<string, any[]> = {};
getRoute = async (id: number) => {
if (this.route[id]) return this.route[id];
const response = await authInstance.get(`/route/${id}`);
const stations = await authInstance.get(`/route/${id}/station`);
runInAction(() => {
this.route[id] = response.data;
this.routeStations[id] = stations.data;
});
return response.data;
};
setRouteStations = (routeId: number, stationId: number, data: any) => {
this.routeStations[routeId] = this.routeStations[routeId]?.map((station) =>
station.id === stationId ? { ...station, ...data } : station
);
};
saveRouteStations = async (routeId: number, stationId: number) => {
await authInstance.patch(`/route/${routeId}/station`, {
...this.routeStations[routeId]?.find(
(station) => station.id === stationId
),
station_id: stationId,
});
};
editRouteData = {
carrier: "",
carrier_id: 0,
center_latitude: "",
center_longitude: "",
governor_appeal: 0,
id: 0,
path: [] as number[][],
rotate: 0,
route_direction: false,
route_number: "",
route_sys_number: "",
scale_max: 0,
scale_min: 0,
video_preview: "",
};
setEditRouteData = (data: any) => {
this.editRouteData = { ...this.editRouteData, ...data };
};
editRoute = async (id: number) => {
const response = await authInstance.patch(`/route/${id}`, {
...this.editRouteData,
center_latitude: parseFloat(this.editRouteData.center_latitude),
center_longitude: parseFloat(this.editRouteData.center_longitude),
});
runInAction(() => {
this.route[id] = response.data;
this.routes.data = this.routes.data.map((route) =>
route.id === id ? response.data : route
);
});
};
copyRouteAction = async (id: number) => {
const response = await authInstance.post(`/route/${id}/copy`);
runInAction(() => {
this.routes.data = [...this.routes.data, response.data];
});
};
selectedStationId = 0;
setSelectedStationId = (id: number) => {
this.selectedStationId = id;
};
}
export const routeStore = new RouteStore();