- aggiunto componenti per Home Template... ma ancora da provare
- sistemato catprods - Sistemato menu
This commit is contained in:
130
src/stores/home.store.ts
Normal file
130
src/stores/home.store.ts
Normal file
@@ -0,0 +1,130 @@
|
||||
import { defineStore } from 'pinia';
|
||||
import api from 'src/services/api';
|
||||
import type { HomeCMS, EventItem, PostItem } from 'src/types/home';
|
||||
|
||||
type State = {
|
||||
data: HomeCMS | null;
|
||||
loading: boolean;
|
||||
error: string | null;
|
||||
|
||||
// sottostati per sezioni dinamiche
|
||||
loadingEvents: boolean;
|
||||
errorEvents: string | null;
|
||||
|
||||
loadingPosts: boolean;
|
||||
errorPosts: string | null;
|
||||
|
||||
_cacheAt?: number;
|
||||
_eventsAt?: number;
|
||||
_postsAt?: number;
|
||||
};
|
||||
|
||||
const FIVE_MIN = 5 * 60 * 1000;
|
||||
|
||||
export const useHomeStore = defineStore('home', {
|
||||
state: (): State => ({
|
||||
data: null,
|
||||
loading: false,
|
||||
error: null,
|
||||
|
||||
loadingEvents: false,
|
||||
errorEvents: null,
|
||||
|
||||
loadingPosts: false,
|
||||
errorPosts: null,
|
||||
|
||||
_cacheAt: undefined,
|
||||
_eventsAt: undefined,
|
||||
_postsAt: undefined
|
||||
}),
|
||||
|
||||
getters: {
|
||||
nextEvents(state): EventItem[] {
|
||||
const now = new Date();
|
||||
const list = (state.data?.events || []).filter(e => new Date(e.start) >= now);
|
||||
list.sort((a, b) => +new Date(a.start) - +new Date(b.start));
|
||||
return list.slice(0, 4);
|
||||
},
|
||||
latestPosts(state): PostItem[] {
|
||||
const list = (state.data?.posts || []).slice().sort((a, b) => +new Date(b.date) - +new Date(a.date));
|
||||
return list.slice(0, 3);
|
||||
}
|
||||
},
|
||||
|
||||
actions: {
|
||||
async fetchHome(prefetched?: HomeCMS) {
|
||||
if (prefetched) {
|
||||
this.data = prefetched;
|
||||
this._cacheAt = Date.now();
|
||||
return;
|
||||
}
|
||||
const fresh = !this._cacheAt || (Date.now() - this._cacheAt) > FIVE_MIN;
|
||||
if (!fresh && this.data) return;
|
||||
|
||||
this.loading = true; this.error = null;
|
||||
try {
|
||||
const { data } = await api.get<HomeCMS>('/home');
|
||||
this.data = data;
|
||||
this._cacheAt = Date.now();
|
||||
} catch (e: any) {
|
||||
this.error = e?.message || 'Impossibile caricare la home, uso mock locale.';
|
||||
// Fallback ai mock
|
||||
const mock = await import('src/mocks/home.sample.json');
|
||||
this.data = mock.default as HomeCMS;
|
||||
this._cacheAt = Date.now();
|
||||
} finally {
|
||||
this.loading = false;
|
||||
}
|
||||
},
|
||||
|
||||
async fetchEvents() {
|
||||
const fresh = !this._eventsAt || (Date.now() - this._eventsAt) > FIVE_MIN;
|
||||
if (!fresh && (this.data?.events?.length || 0) > 0) return;
|
||||
|
||||
this.loadingEvents = true; this.errorEvents = null;
|
||||
try {
|
||||
const { data } = await api.get<EventItem[]>('/events', { params: { limit: 6 } });
|
||||
this.data = this.data || ({} as HomeCMS);
|
||||
this.data.events = data;
|
||||
this._eventsAt = Date.now();
|
||||
} catch (e: any) {
|
||||
this.errorEvents = e?.message || 'Eventi non disponibili (mock)';
|
||||
if (!this.data?.events?.length) {
|
||||
const mock = await import('src/mocks/home.sample.json');
|
||||
this.data = this.data || ({} as HomeCMS);
|
||||
this.data.events = (mock.default as HomeCMS).events;
|
||||
}
|
||||
} finally {
|
||||
this.loadingEvents = false;
|
||||
}
|
||||
},
|
||||
|
||||
async fetchPosts() {
|
||||
const fresh = !this._postsAt || (Date.now() - this._postsAt) > FIVE_MIN;
|
||||
if (!fresh && (this.data?.posts?.length || 0) > 0) return;
|
||||
|
||||
this.loadingPosts = true; this.errorPosts = null;
|
||||
try {
|
||||
const { data } = await api.get<PostItem[]>('/posts', { params: { limit: 3 } });
|
||||
this.data = this.data || ({} as HomeCMS);
|
||||
this.data.posts = data;
|
||||
this._postsAt = Date.now();
|
||||
} catch (e: any) {
|
||||
this.errorPosts = e?.message || 'Post non disponibili (mock)';
|
||||
if (!this.data?.posts?.length) {
|
||||
const mock = await import('src/mocks/home.sample.json');
|
||||
this.data = this.data || ({} as HomeCMS);
|
||||
this.data.posts = (mock.default as HomeCMS).posts;
|
||||
}
|
||||
} finally {
|
||||
this.loadingPosts = false;
|
||||
}
|
||||
},
|
||||
|
||||
async subscribeNewsletter(email: string) {
|
||||
if (!/.+@.+\..+/.test(email)) throw new Error('Email non valida');
|
||||
await api.post('/newsletter/subscribe', { email });
|
||||
// niente stato locale: demandiamo a backend
|
||||
}
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user