118 lines
3.4 KiB
JavaScript
118 lines
3.4 KiB
JavaScript
|
|
// @ts-check
|
||
|
|
import { defineStore } from 'pinia';
|
||
|
|
import api from 'src/services/api';
|
||
|
|
|
||
|
|
export const useAdminStore = defineStore('admin', {
|
||
|
|
state: () => ({
|
||
|
|
home: null,
|
||
|
|
loadingHome: false,
|
||
|
|
savingHome: false,
|
||
|
|
events: [],
|
||
|
|
posts: [],
|
||
|
|
loadingEvents: false,
|
||
|
|
loadingPosts: false,
|
||
|
|
saving: false,
|
||
|
|
error: null
|
||
|
|
}),
|
||
|
|
|
||
|
|
actions: {
|
||
|
|
async loadHome() {
|
||
|
|
this.loadingHome = true; this.error = null;
|
||
|
|
try {
|
||
|
|
const { data } = await api.get('/home');
|
||
|
|
this.home = data;
|
||
|
|
} catch (e) {
|
||
|
|
this.error = e?.message || 'Errore caricamento Home';
|
||
|
|
} finally {
|
||
|
|
this.loadingHome = false;
|
||
|
|
}
|
||
|
|
},
|
||
|
|
async saveHome(partial) {
|
||
|
|
this.savingHome = true; this.error = null;
|
||
|
|
try {
|
||
|
|
const payload = { ...(this.home || {}), ...(partial || {}) };
|
||
|
|
const { data } = await api.put('/home', payload);
|
||
|
|
this.home = data;
|
||
|
|
return data;
|
||
|
|
} catch (e) {
|
||
|
|
this.error = e?.message || 'Errore salvataggio Home';
|
||
|
|
throw e;
|
||
|
|
} finally {
|
||
|
|
this.savingHome = false;
|
||
|
|
}
|
||
|
|
},
|
||
|
|
|
||
|
|
async loadEvents(params = { limit: 50 }) {
|
||
|
|
this.loadingEvents = true; this.error = null;
|
||
|
|
try {
|
||
|
|
const { data } = await api.get('/events', { params });
|
||
|
|
this.events = Array.isArray(data?.items) ? data.items : data; // supporta entrambi i formati
|
||
|
|
} catch (e) {
|
||
|
|
this.error = e?.message || 'Errore caricamento Eventi';
|
||
|
|
} finally {
|
||
|
|
this.loadingEvents = false;
|
||
|
|
}
|
||
|
|
},
|
||
|
|
async createEvent(item) {
|
||
|
|
this.saving = true;
|
||
|
|
try {
|
||
|
|
const { data } = await api.post('/events', item);
|
||
|
|
this.events.unshift(data);
|
||
|
|
return data;
|
||
|
|
} finally { this.saving = false; }
|
||
|
|
},
|
||
|
|
async updateEvent(id, partial) {
|
||
|
|
this.saving = true;
|
||
|
|
try {
|
||
|
|
const { data } = await api.put(`/events/${id}`, partial);
|
||
|
|
const i = this.events.findIndex(e => e._id === id);
|
||
|
|
if (i >= 0) this.events[i] = data;
|
||
|
|
return data;
|
||
|
|
} finally { this.saving = false; }
|
||
|
|
},
|
||
|
|
async deleteEvent(id) {
|
||
|
|
this.saving = true;
|
||
|
|
try {
|
||
|
|
await api.delete(`/events/${id}`);
|
||
|
|
this.events = this.events.filter(e => e._id !== id);
|
||
|
|
} finally { this.saving = false; }
|
||
|
|
},
|
||
|
|
|
||
|
|
async loadPosts(params = { limit: 50, sort: '-date' }) {
|
||
|
|
this.loadingPosts = true; this.error = null;
|
||
|
|
try {
|
||
|
|
const { data } = await api.get('/posts', { params });
|
||
|
|
this.posts = Array.isArray(data?.items) ? data.items : data;
|
||
|
|
} catch (e) {
|
||
|
|
this.error = e?.message || 'Errore caricamento Post';
|
||
|
|
} finally {
|
||
|
|
this.loadingPosts = false;
|
||
|
|
}
|
||
|
|
},
|
||
|
|
async createPost(item) {
|
||
|
|
this.saving = true;
|
||
|
|
try {
|
||
|
|
const { data } = await api.post('/posts', item);
|
||
|
|
this.posts.unshift(data);
|
||
|
|
return data;
|
||
|
|
} finally { this.saving = false; }
|
||
|
|
},
|
||
|
|
async updatePost(id, partial) {
|
||
|
|
this.saving = true;
|
||
|
|
try {
|
||
|
|
const { data } = await api.put(`/posts/${id}`, partial);
|
||
|
|
const i = this.posts.findIndex(p => p._id === id);
|
||
|
|
if (i >= 0) this.posts[i] = data;
|
||
|
|
return data;
|
||
|
|
} finally { this.saving = false; }
|
||
|
|
},
|
||
|
|
async deletePost(id) {
|
||
|
|
this.saving = true;
|
||
|
|
try {
|
||
|
|
await api.delete(`/posts/${id}`);
|
||
|
|
this.posts = this.posts.filter(p => p._id !== id);
|
||
|
|
} finally { this.saving = false; }
|
||
|
|
}
|
||
|
|
}
|
||
|
|
});
|