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,7 +64,7 @@ class CarrierStore {
getCarriers = async (language: Language) => {
if (this.carriers[language as keyof Carriers].loaded) return;
const response = await authInstance.get("/carrier");
const response = await languageInstance(language).get("/carrier");
runInAction(() => {
this.carriers[language as keyof Carriers].data = response.data;
@@ -108,46 +108,94 @@ class CarrierStore {
return this.carrier[id];
};
createCarrier = async (
createCarrierData = {
city_id: 0,
logo: "",
ru: {
full_name: "",
short_name: "",
slogan: "",
},
en: {
full_name: "",
short_name: "",
slogan: "",
},
zh: {
full_name: "",
short_name: "",
slogan: "",
},
};
setCreateCarrierData = (
fullName: string,
shortName: string,
cityId: number,
slogan: string,
logoId: string
logoId: string,
language: Language
) => {
const { language } = languageStore;
const cityName =
cityStore.cities[language].data.find((city) => city.id === cityId)
?.name || "";
const response = await languageInstance(language).post("/carrier", {
this.createCarrierData.city_id = cityId;
this.createCarrierData.logo = logoId;
this.createCarrierData[language] = {
full_name: fullName,
short_name: shortName,
slogan: slogan,
};
};
createCarrier = async () => {
const { language } = languageStore;
const cityName =
cityStore.cities[language].data.find(
(city) => city.id === this.createCarrierData.city_id
)?.name || "";
const payload = {
full_name: this.createCarrierData[language].full_name,
short_name: this.createCarrierData[language].short_name,
city: cityName,
city_id: cityId,
slogan,
logo: logoId,
});
city_id: this.createCarrierData.city_id,
slogan: this.createCarrierData[language].slogan,
...(this.createCarrierData.logo
? { logo: this.createCarrierData.logo }
: {}),
};
const response = await languageInstance(language).post("/carrier", payload);
const carrierId = response.data.id;
runInAction(() => {
this.carriers[language].data.push(response.data);
});
// Create translations for other languages
for (const lang of ["ru", "en", "zh"].filter((l) => l !== language)) {
await languageInstance(lang as Language).patch(`/carrier/${carrierId}`, {
full_name: fullName,
short_name: shortName,
const patchPayload = {
// @ts-ignore
full_name: this.createCarrierData[lang as any].full_name as string,
// @ts-ignore
short_name: this.createCarrierData[lang as any].short_name as string,
city: cityName,
city_id: cityId,
slogan,
logo: logoId,
city_id: this.createCarrierData.city_id,
// @ts-ignore
slogan: this.createCarrierData[lang as any].slogan as string,
...(this.createCarrierData.logo
? { logo: this.createCarrierData.logo }
: {}),
};
await languageInstance(lang as Language).patch(
`/carrier/${carrierId}`,
patchPayload
);
runInAction(() => {
this.carriers[lang as keyof Carriers].data.push(response.data);
});
}
runInAction(() => {
for (const language of ["ru", "en", "zh"] as const) {
this.carriers[language].data.push(response.data);
}
});
};
editCarrierData = {
@@ -206,31 +254,29 @@ class CarrierStore {
};
editCarrier = async (id: number) => {
const { language } = languageStore;
const cityName =
cityStore.cities[languageStore.language].data.find(
cityStore.cities[language].data.find(
(city) => city.id === this.editCarrierData.city_id
)?.name || "";
for (const language of ["ru", "en", "zh"] as const) {
const response = await languageInstance(language).patch(
`/carrier/${id}`,
{
...this.editCarrierData[language],
city: cityName,
logo: this.editCarrierData.logo,
}
);
for (const lang of ["ru", "en", "zh"] as const) {
const response = await languageInstance(lang).patch(`/carrier/${id}`, {
...this.editCarrierData[lang],
city: cityName,
city_id: this.editCarrierData.city_id,
logo: this.editCarrierData.logo,
});
runInAction(() => {
if (this.carrier[id]) {
this.carrier[id][language] = response.data;
}
for (const language of ["ru", "en", "zh"] as const) {
this.carriers[language].data = this.carriers[language].data.map(
(carrier: Carrier) =>
carrier.id === id ? { ...carrier, ...response.data } : carrier
);
this.carrier[id][lang] = response.data;
}
this.carriers[lang].data = this.carriers[lang].data.map(
(carrier: Carrier) =>
carrier.id === id ? { ...carrier, ...response.data } : carrier
);
});
}
};

View File

@@ -85,7 +85,7 @@ class CityStore {
return;
}
const response = await authInstance.get(`/city`);
const response = await languageInstance(language).get(`/city`);
runInAction(() => {
this.cities[language].data = response.data;
@@ -98,7 +98,7 @@ class CityStore {
return;
}
const response = await authInstance.get(`/city/${code}`);
const response = await languageInstance(language).get(`/city/${code}`);
runInAction(() => {
if (!this.city[code]) {
@@ -170,15 +170,20 @@ class CityStore {
try {
// Create city in primary language
const cityResponse = await languageInstance(language).post("/city", {
const cityPayload = {
name,
country:
countryStore.countries[language as keyof CashedCountries]?.data.find(
(c) => c.code === country_code
)?.name || "",
country_code,
arms: arms || "",
});
...(arms ? { arms } : {}),
};
const cityResponse = await languageInstance(language).post(
"/city",
cityPayload
);
const cityId = cityResponse.data.id;
@@ -194,14 +199,16 @@ class CityStore {
(c) => c.code === country_code
)?.name || "";
const patchPayload = {
name: secondaryName || "",
country: countryName,
country_code: country_code || "",
...(arms ? { arms } : {}),
};
const patchResponse = await languageInstance(secondaryLanguage).patch(
`/city/${cityId}`,
{
name: secondaryName || "",
country: countryName,
country_code: country_code || "",
arms: arms || "",
}
patchPayload
);
runInAction(() => {

View File

@@ -91,14 +91,16 @@ class CountryStore {
return response.data;
};
deleteCountry = async (code: string, language: keyof CashedCountries) => {
deleteCountry = async (code: string) => {
await authInstance.delete(`/country/${code}`);
runInAction(() => {
this.countries[language].data = this.countries[language].data.filter(
(country) => country.code !== code
);
this.countries[language].loaded = true;
for (const lang of ["ru", "en", "zh"]) {
this.countries[lang as keyof CashedCountries].data = this.countries[
lang as keyof CashedCountries
].data.filter((country) => country.code !== code);
}
this.country[code] = {
ru: null,
en: null,

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