feat: Route list page
This commit is contained in:
@ -1,6 +1,42 @@
|
||||
import { authInstance } from "@shared";
|
||||
import { authInstance, languageInstance, languageStore } from "@shared";
|
||||
import { makeAutoObservable, runInAction } from "mobx";
|
||||
|
||||
type Language = "ru" | "en" | "zh";
|
||||
|
||||
type StationLanguageData = {
|
||||
name: string;
|
||||
system_name: string;
|
||||
description: string;
|
||||
address: string;
|
||||
loaded: boolean; // Indicates if this language's data has been loaded/modified
|
||||
};
|
||||
|
||||
type StationCommonData = {
|
||||
city_id: number;
|
||||
direction: boolean;
|
||||
icon: string;
|
||||
latitude: number;
|
||||
longitude: number;
|
||||
offset_x: number;
|
||||
offset_y: number;
|
||||
transfers: {
|
||||
bus: string;
|
||||
metro_blue: string;
|
||||
metro_green: string;
|
||||
metro_orange: string;
|
||||
metro_purple: string;
|
||||
metro_red: string;
|
||||
train: string;
|
||||
tram: string;
|
||||
trolleybus: string;
|
||||
};
|
||||
city: string;
|
||||
};
|
||||
|
||||
type EditStationData = {
|
||||
[key in Language]: StationLanguageData;
|
||||
} & { common: StationCommonData };
|
||||
|
||||
type Station = {
|
||||
id: number;
|
||||
address: string;
|
||||
@ -32,6 +68,77 @@ class StationsStore {
|
||||
stations: Station[] = [];
|
||||
station: Station | null = null;
|
||||
|
||||
stationLists: {
|
||||
[key in Language]: {
|
||||
data: Station[];
|
||||
loaded: boolean;
|
||||
};
|
||||
} = {
|
||||
ru: {
|
||||
data: [],
|
||||
loaded: false,
|
||||
},
|
||||
en: {
|
||||
data: [],
|
||||
loaded: false,
|
||||
},
|
||||
zh: {
|
||||
data: [],
|
||||
loaded: false,
|
||||
},
|
||||
};
|
||||
|
||||
// This will store the full station data, keyed by ID and then by language
|
||||
stationPreview: Record<
|
||||
string,
|
||||
Record<string, { loaded: boolean; data: Station }>
|
||||
> = {};
|
||||
|
||||
editStationData: EditStationData = {
|
||||
ru: {
|
||||
name: "",
|
||||
system_name: "",
|
||||
description: "",
|
||||
address: "",
|
||||
loaded: false,
|
||||
},
|
||||
en: {
|
||||
name: "",
|
||||
system_name: "",
|
||||
description: "",
|
||||
address: "",
|
||||
loaded: false,
|
||||
},
|
||||
zh: {
|
||||
name: "",
|
||||
system_name: "",
|
||||
description: "",
|
||||
address: "",
|
||||
loaded: false,
|
||||
},
|
||||
common: {
|
||||
city: "",
|
||||
city_id: 0,
|
||||
direction: false,
|
||||
icon: "",
|
||||
latitude: 0,
|
||||
longitude: 0,
|
||||
offset_x: 0,
|
||||
offset_y: 0,
|
||||
transfers: {
|
||||
bus: "",
|
||||
metro_blue: "",
|
||||
metro_green: "",
|
||||
metro_orange: "",
|
||||
metro_purple: "",
|
||||
metro_red: "",
|
||||
train: "",
|
||||
tram: "",
|
||||
trolleybus: "",
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
constructor() {
|
||||
makeAutoObservable(this);
|
||||
}
|
||||
@ -44,11 +151,157 @@ class StationsStore {
|
||||
});
|
||||
};
|
||||
|
||||
getStationList = async () => {
|
||||
const { language } = languageStore;
|
||||
if (this.stationLists[language].loaded) {
|
||||
return;
|
||||
}
|
||||
const response = await authInstance.get("/station");
|
||||
|
||||
runInAction(() => {
|
||||
this.stationLists[language].data = response.data;
|
||||
this.stationLists[language].loaded = true;
|
||||
});
|
||||
};
|
||||
|
||||
setEditCommonData = (data: Partial<StationCommonData>) => {
|
||||
this.editStationData.common = {
|
||||
...this.editStationData.common,
|
||||
...data,
|
||||
};
|
||||
};
|
||||
|
||||
getEditStation = async (id: number) => {
|
||||
if (this.editStationData.ru.loaded) {
|
||||
return;
|
||||
}
|
||||
const ruResponse = await languageInstance("ru").get(`/station/${id}`);
|
||||
const enResponse = await languageInstance("en").get(`/station/${id}`);
|
||||
const zhResponse = await languageInstance("zh").get(`/station/${id}`);
|
||||
|
||||
this.editStationData = {
|
||||
ru: {
|
||||
name: ruResponse.data.name,
|
||||
system_name: ruResponse.data.system_name,
|
||||
description: ruResponse.data.description,
|
||||
address: ruResponse.data.address,
|
||||
loaded: true,
|
||||
},
|
||||
en: {
|
||||
name: enResponse.data.name,
|
||||
system_name: enResponse.data.system_name,
|
||||
description: enResponse.data.description,
|
||||
address: enResponse.data.address,
|
||||
loaded: true,
|
||||
},
|
||||
zh: {
|
||||
name: zhResponse.data.name,
|
||||
system_name: zhResponse.data.system_name,
|
||||
description: zhResponse.data.description,
|
||||
address: zhResponse.data.address,
|
||||
loaded: true,
|
||||
},
|
||||
common: {
|
||||
city: ruResponse.data.city,
|
||||
city_id: ruResponse.data.city_id,
|
||||
direction: ruResponse.data.direction,
|
||||
icon: ruResponse.data.icon,
|
||||
latitude: ruResponse.data.latitude,
|
||||
longitude: ruResponse.data.longitude,
|
||||
offset_x: ruResponse.data.offset_x,
|
||||
offset_y: ruResponse.data.offset_y,
|
||||
transfers: ruResponse.data.transfers,
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
// Sets language-specific station data
|
||||
setLanguageEditStationData = (
|
||||
language: Language,
|
||||
data: Partial<StationLanguageData>
|
||||
) => {
|
||||
this.editStationData[language] = {
|
||||
...this.editStationData[language],
|
||||
...data,
|
||||
};
|
||||
};
|
||||
|
||||
editStation = async (id: number) => {
|
||||
const commonDataPayload = {
|
||||
city_id: this.editStationData.common.city_id,
|
||||
direction: this.editStationData.common.direction,
|
||||
icon: this.editStationData.common.icon,
|
||||
latitude: this.editStationData.common.latitude,
|
||||
longitude: this.editStationData.common.longitude,
|
||||
offset_x: this.editStationData.common.offset_x,
|
||||
offset_y: this.editStationData.common.offset_y,
|
||||
transfers: this.editStationData.common.transfers,
|
||||
city: this.editStationData.common.city,
|
||||
};
|
||||
|
||||
for (const language of ["ru", "en", "zh"] as const) {
|
||||
const { name, description, address } = this.editStationData[language];
|
||||
const response = await languageInstance(language).patch(
|
||||
`/station/${id}`,
|
||||
{
|
||||
name: name || "",
|
||||
system_name: name || "", // system_name is often derived from name
|
||||
description: description || "",
|
||||
address: address || "",
|
||||
...commonDataPayload,
|
||||
}
|
||||
);
|
||||
|
||||
runInAction(() => {
|
||||
// Update the cached preview data and station lists after successful patch
|
||||
if (this.stationPreview[id]) {
|
||||
this.stationPreview[id][language] = {
|
||||
...this.stationPreview[id][language], // Preserve common fields that might not be in the language-specific patch response
|
||||
id: response.data.id,
|
||||
name: response.data.name,
|
||||
system_name: response.data.system_name,
|
||||
description: response.data.description,
|
||||
address: response.data.address,
|
||||
...commonDataPayload,
|
||||
} as Station; // Cast to Station to satisfy type
|
||||
}
|
||||
if (this.stationLists[language].data) {
|
||||
this.stationLists[language].data = this.stationLists[
|
||||
language
|
||||
].data.map((station: Station) =>
|
||||
station.id === id
|
||||
? ({
|
||||
...station,
|
||||
name: response.data.name,
|
||||
system_name: response.data.system_name,
|
||||
description: response.data.description,
|
||||
address: response.data.address,
|
||||
...commonDataPayload,
|
||||
} as Station)
|
||||
: station
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
deleteStation = async (id: number) => {
|
||||
await authInstance.delete(`/station/${id}`);
|
||||
|
||||
runInAction(() => {
|
||||
this.stations = this.stations.filter((station) => station.id !== id);
|
||||
// Also clear from stationPreview cache
|
||||
if (this.stationPreview[id]) {
|
||||
delete this.stationPreview[id];
|
||||
}
|
||||
// Clear from stationLists as well for all languages
|
||||
for (const lang of ["ru", "en", "zh"] as const) {
|
||||
if (this.stationLists[lang].data) {
|
||||
this.stationLists[lang].data = this.stationLists[lang].data.filter(
|
||||
(station) => station.id !== id
|
||||
);
|
||||
}
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
@ -57,6 +310,29 @@ class StationsStore {
|
||||
this.station = response.data;
|
||||
};
|
||||
|
||||
getStationPreview = async (id: number) => {
|
||||
const { language } = languageStore;
|
||||
|
||||
if (this.stationPreview[id]?.[language]?.loaded) {
|
||||
return;
|
||||
}
|
||||
|
||||
const response = await languageInstance(language).get(`/station/${id}`);
|
||||
runInAction(() => {
|
||||
if (!this.stationPreview[id]) {
|
||||
this.stationPreview[id] = {
|
||||
ru: { loaded: false, data: {} as Station },
|
||||
en: { loaded: false, data: {} as Station },
|
||||
zh: { loaded: false, data: {} as Station },
|
||||
};
|
||||
}
|
||||
this.stationPreview[id][language] = {
|
||||
data: response.data,
|
||||
loaded: true,
|
||||
};
|
||||
});
|
||||
};
|
||||
|
||||
createStation = async (
|
||||
name: string,
|
||||
systemName: string,
|
||||
@ -69,8 +345,72 @@ class StationsStore {
|
||||
});
|
||||
runInAction(() => {
|
||||
this.stations.push(response.data);
|
||||
const newStation = response.data as Station;
|
||||
if (!this.stationPreview[newStation.id]) {
|
||||
this.stationPreview[newStation.id] = {
|
||||
ru: { loaded: false, data: newStation },
|
||||
en: { loaded: false, data: newStation },
|
||||
zh: { loaded: false, data: newStation },
|
||||
};
|
||||
}
|
||||
this.stationPreview[newStation.id]["ru"] = {
|
||||
loaded: true,
|
||||
data: newStation,
|
||||
};
|
||||
this.stationPreview[newStation.id]["en"] = {
|
||||
loaded: true,
|
||||
data: newStation,
|
||||
};
|
||||
});
|
||||
};
|
||||
|
||||
// Reset editStationData when navigating away or after saving
|
||||
resetEditStationData = () => {
|
||||
this.editStationData = {
|
||||
ru: {
|
||||
name: "",
|
||||
system_name: "",
|
||||
description: "",
|
||||
address: "",
|
||||
loaded: false,
|
||||
},
|
||||
en: {
|
||||
name: "",
|
||||
system_name: "",
|
||||
description: "",
|
||||
address: "",
|
||||
loaded: false,
|
||||
},
|
||||
zh: {
|
||||
name: "",
|
||||
system_name: "",
|
||||
description: "",
|
||||
address: "",
|
||||
loaded: false,
|
||||
},
|
||||
common: {
|
||||
city: "",
|
||||
city_id: 0,
|
||||
direction: false,
|
||||
icon: "",
|
||||
latitude: 0,
|
||||
longitude: 0,
|
||||
offset_x: 0,
|
||||
offset_y: 0,
|
||||
transfers: {
|
||||
bus: "",
|
||||
metro_blue: "",
|
||||
metro_green: "",
|
||||
metro_orange: "",
|
||||
metro_purple: "",
|
||||
metro_red: "",
|
||||
train: "",
|
||||
tram: "",
|
||||
trolleybus: "",
|
||||
},
|
||||
},
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
export const stationsStore = new StationsStore();
|
||||
|
Reference in New Issue
Block a user