few cosmetic fixes
This commit is contained in:
139
locationStore.js
139
locationStore.js
@@ -1,79 +1,84 @@
|
||||
import { create } from "zustand";
|
||||
import {create} from "zustand";
|
||||
import * as api from "@/api/locations";
|
||||
|
||||
const useLocationStore = create((set, get) => ({
|
||||
locations: [],
|
||||
loading: false,
|
||||
error: null,
|
||||
locations: [],
|
||||
loading: false,
|
||||
error: null,
|
||||
|
||||
fetchLocations: async () => {
|
||||
set({ loading: true, error: null });
|
||||
try {
|
||||
const data = await api.listLocations();
|
||||
set({ locations: data, loading: false });
|
||||
} catch (error) {
|
||||
set({ error, loading: false });
|
||||
}
|
||||
},
|
||||
fetchLocations: async () => {
|
||||
set({loading: true, error: null});
|
||||
try {
|
||||
const data = await api.listLocations();
|
||||
set({locations: data, loading: false});
|
||||
} catch (error) {
|
||||
set({error, loading: false});
|
||||
}
|
||||
},
|
||||
|
||||
addLocation: async (location) => {
|
||||
set({ loading: true, error: null });
|
||||
try {
|
||||
const newLoc = await api.addLocation(location);
|
||||
const normalizedLoc = {
|
||||
...newLoc,
|
||||
imageSource: api.normalizeImageSource(newLoc.image)
|
||||
};
|
||||
set((state) => ({
|
||||
locations: [...state.locations, normalizedLoc],
|
||||
loading: false,
|
||||
}));
|
||||
return normalizedLoc;
|
||||
} catch (error) {
|
||||
set({ error, loading: false });
|
||||
return null;
|
||||
}
|
||||
},
|
||||
addLocation: async (location) => {
|
||||
set({loading: true, error: null});
|
||||
try {
|
||||
const newLoc = await api.addLocation(location);
|
||||
const normalizedLoc = {
|
||||
...newLoc,
|
||||
imageSource: api.normalizeImageSource(newLoc.image)
|
||||
};
|
||||
set((state) => ({
|
||||
locations: [...state.locations, normalizedLoc],
|
||||
loading: false,
|
||||
}));
|
||||
return normalizedLoc;
|
||||
} catch (error) {
|
||||
set({error, loading: false});
|
||||
return null;
|
||||
}
|
||||
},
|
||||
|
||||
updateLocation: async (id, location) => {
|
||||
set({ loading: true, error: null });
|
||||
try {
|
||||
const updated = await api.updateLocation(id, location);
|
||||
set((state) => ({
|
||||
locations: state.locations.map((loc) =>
|
||||
loc.id === id ? updated : loc
|
||||
),
|
||||
|
||||
loading: false,
|
||||
}));
|
||||
updateLocation: async (id, location) => {
|
||||
set({loading: true, error: null});
|
||||
try {
|
||||
const updated = await api.updateLocation(id, location);
|
||||
set((state) => ({
|
||||
locations: state.locations.map((loc) =>
|
||||
loc.id === id ? updated : loc
|
||||
),
|
||||
|
||||
return updated;
|
||||
} catch (error) {
|
||||
set({ error, loading: false });
|
||||
return null;
|
||||
}
|
||||
},
|
||||
loading: false,
|
||||
}));
|
||||
|
||||
deleteLocation: async (id) => {
|
||||
set({ loading: true, error: null });
|
||||
try {
|
||||
const deleted = await api.deleteLocation(id);
|
||||
set((state) => ({
|
||||
locations: state.locations.filter((loc) => loc.id !== id),
|
||||
loading: false,
|
||||
}));
|
||||
if(deleted) {
|
||||
return true;
|
||||
}
|
||||
} catch (error) {
|
||||
set({ error, loading: false });
|
||||
return false;
|
||||
}
|
||||
},
|
||||
return updated;
|
||||
} catch (error) {
|
||||
set({error, loading: false});
|
||||
return null;
|
||||
}
|
||||
},
|
||||
|
||||
getLocation: (id) => {
|
||||
return get().locations.find((loc) => String(loc.id) === String(id));
|
||||
},
|
||||
deleteLocation: async (id) => {
|
||||
set({loading: true, error: null});
|
||||
try {
|
||||
const deleted = await api.deleteLocation(id);
|
||||
set((state) => ({
|
||||
locations: state.locations.filter((loc) => loc.id !== id),
|
||||
loading: false,
|
||||
}));
|
||||
return deleted;
|
||||
} catch (error) {
|
||||
set({error, loading: false});
|
||||
return false;
|
||||
}
|
||||
},
|
||||
|
||||
getLocation: (id) => {
|
||||
const location = get().locations.find((loc) => String(loc.id) === String(id));
|
||||
if (location && !location.imageSource) {
|
||||
return {
|
||||
...location,
|
||||
imageSource: api.normalizeImageSource(location.image)
|
||||
};
|
||||
}
|
||||
return location;
|
||||
},
|
||||
}));
|
||||
|
||||
export default useLocationStore;
|
||||
Reference in New Issue
Block a user