feat: Select city in top of the page for next usage in create/edit pages
This commit is contained in:
48
src/shared/store/SelectedCityStore/index.ts
Normal file
48
src/shared/store/SelectedCityStore/index.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
import { makeAutoObservable, runInAction } from "mobx";
|
||||
import { City } from "../CityStore";
|
||||
|
||||
class SelectedCityStore {
|
||||
selectedCity: City | null = null;
|
||||
|
||||
constructor() {
|
||||
makeAutoObservable(this);
|
||||
this.initialize();
|
||||
}
|
||||
|
||||
private initialize() {
|
||||
const storedCity = localStorage.getItem("selectedCity");
|
||||
if (storedCity) {
|
||||
try {
|
||||
this.selectedCity = JSON.parse(storedCity);
|
||||
} catch (error) {
|
||||
console.error("Error parsing stored city:", error);
|
||||
localStorage.removeItem("selectedCity");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
setSelectedCity = (city: City | null) => {
|
||||
runInAction(() => {
|
||||
this.selectedCity = city;
|
||||
if (city) {
|
||||
localStorage.setItem("selectedCity", JSON.stringify(city));
|
||||
} else {
|
||||
localStorage.removeItem("selectedCity");
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
clearSelectedCity = () => {
|
||||
this.setSelectedCity(null);
|
||||
};
|
||||
|
||||
get selectedCityId() {
|
||||
return this.selectedCity?.id || null;
|
||||
}
|
||||
|
||||
get selectedCityName() {
|
||||
return this.selectedCity?.name || null;
|
||||
}
|
||||
}
|
||||
|
||||
export const selectedCityStore = new SelectedCityStore();
|
||||
Reference in New Issue
Block a user