fix: Hot bug fix

This commit is contained in:
2025-06-15 20:38:48 +03:00
parent 481385c2f4
commit 32a7cb44d1
24 changed files with 900 additions and 250 deletions

View File

@@ -64,6 +64,10 @@ type Station = {
};
};
type CreateStationData = {
[key in Language]: StationLanguageData;
} & { common: StationCommonData };
class StationsStore {
stations: Station[] = [];
station: Station | null = null;
@@ -139,6 +143,51 @@ class StationsStore {
},
};
createStationData: CreateStationData = {
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);
}
@@ -172,9 +221,6 @@ class StationsStore {
};
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}`);
@@ -336,35 +382,125 @@ class StationsStore {
});
};
createStation = async (
name: string,
systemName: string,
direction: string
setCreateCommonData = (data: Partial<StationCommonData>) => {
this.createStationData.common = {
...this.createStationData.common,
...data,
};
};
setLanguageCreateStationData = (
language: Language,
data: Partial<StationLanguageData>
) => {
const response = await authInstance.post("/station", {
station_name: name,
system_name: systemName,
direction,
this.createStationData[language] = {
...this.createStationData[language],
...data,
};
};
createStation = async () => {
const { language } = languageStore;
let commonDataPayload: Partial<StationCommonData> = {
city_id: this.createStationData.common.city_id,
direction: this.createStationData.common.direction,
icon: this.createStationData.common.icon,
latitude: this.createStationData.common.latitude,
longitude: this.createStationData.common.longitude,
offset_x: this.createStationData.common.offset_x,
offset_y: this.createStationData.common.offset_y,
transfers: this.createStationData.common.transfers,
city: this.createStationData.common.city,
};
if (this.createStationData.common.icon === "") {
delete commonDataPayload.icon;
}
// First create station in Russian
const { name, description, address } = this.createStationData[language];
const response = await languageInstance(language).post("/station", {
name: name || "",
system_name: name || "", // system_name is often derived from name
description: description || "",
address: address || "",
...commonDataPayload,
});
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,
this.stationLists[language].data.push(response.data);
});
const stationId = response.data.id;
// Then update for other languages
for (const lang of ["ru", "en", "zh"].filter(
(lang) => lang !== language
) as Language[]) {
const { name, description, address } = this.createStationData[lang];
const response = await languageInstance(lang).patch(
`/station/${stationId}`,
{
name: name || "",
system_name: name || "", // system_name is often derived from name
description: description || "",
address: address || "",
...commonDataPayload,
}
);
runInAction(() => {
this.stationLists[lang].data.push(response.data);
});
}
runInAction(() => {
this.createStationData = {
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: "",
},
},
};
});
return response.data;
};
// Reset editStationData when navigating away or after saving