feat: intégration de @tanstack/vue-query pour la gestion des requêtes dans l'application Vue, ajout de nouveaux composables pour les chapitres et les détails des mangas, et mise à jour du store pour une meilleure gestion des états de chargement et d'erreur
This commit is contained in:
parent
fd2d3cd640
commit
71242433e6
@@ -1,81 +1,82 @@
|
||||
import {defineStore} from 'pinia';
|
||||
import {ApiMangaRepository} from '../../infrastructure/api/apiMangaRepository';
|
||||
import { defineStore } from 'pinia';
|
||||
import { ApiMangaRepository } from '../../infrastructure/api/apiMangaRepository';
|
||||
|
||||
const mangaRepository = new ApiMangaRepository();
|
||||
|
||||
export const useMangaStore = defineStore('manga', {
|
||||
state: () => ({
|
||||
// État pour la collection
|
||||
collection: null,
|
||||
// État pour les détails
|
||||
currentManga: null,
|
||||
chapters: [],
|
||||
loading: false,
|
||||
error: null,
|
||||
isBackgroundLoading: false
|
||||
}),
|
||||
|
||||
actions: {
|
||||
// Actions pour la collection
|
||||
async loadCollection() {
|
||||
if (this.loading) return;
|
||||
|
||||
this.loading = true;
|
||||
this.error = null;
|
||||
|
||||
try {
|
||||
this.collection = await mangaRepository.getCollection();
|
||||
} catch (err) {
|
||||
this.error = err.message;
|
||||
} finally {
|
||||
this.loading = false;
|
||||
}
|
||||
},
|
||||
|
||||
async refreshCollectionInBackground() {
|
||||
if (this.isBackgroundLoading) return;
|
||||
|
||||
this.isBackgroundLoading = true;
|
||||
|
||||
try {
|
||||
this.collection = await mangaRepository.getCollection();
|
||||
} catch (err) {
|
||||
console.error('Failed to refresh collection:', err);
|
||||
} finally {
|
||||
this.isBackgroundLoading = false;
|
||||
}
|
||||
},
|
||||
|
||||
// Actions pour les détails du manga
|
||||
async fetchMangaDetails(mangaId) {
|
||||
this.loading = true;
|
||||
this.error = null;
|
||||
try {
|
||||
this.currentManga = await mangaRepository.getMangaById(mangaId);
|
||||
} catch (error) {
|
||||
this.error = error.message;
|
||||
} finally {
|
||||
this.loading = false;
|
||||
}
|
||||
},
|
||||
|
||||
async fetchMangaChapters(mangaId) {
|
||||
this.loading = true;
|
||||
this.error = null;
|
||||
try {
|
||||
const response = await mangaRepository.getChapters(mangaId);
|
||||
this.chapters = Array.isArray(response) ? response :
|
||||
(response.items ? response.items : []);
|
||||
} catch (error) {
|
||||
this.error = error.message;
|
||||
} finally {
|
||||
this.loading = false;
|
||||
}
|
||||
},
|
||||
|
||||
clearCurrentManga() {
|
||||
this.currentManga = null;
|
||||
this.chapters = [];
|
||||
// Helper pour comparer la collection (peut être supprimé si non utilisé ailleurs)
|
||||
const deepCompare = (obj1, obj2) => {
|
||||
try {
|
||||
if (obj1 == null && obj2 == null) return true;
|
||||
if (obj1 == null || obj2 == null) return false;
|
||||
return JSON.stringify(obj1) === JSON.stringify(obj2);
|
||||
} catch (e) {
|
||||
console.error("Erreur lors de la comparaison d'objets:", e);
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
export const useMangaStore = defineStore('manga', {
|
||||
state: () => ({
|
||||
// --- Collection State ---
|
||||
collection: null,
|
||||
loadingCollection: false,
|
||||
errorCollection: null,
|
||||
isBackgroundLoadingCollection: false,
|
||||
|
||||
// --- Selected Manga State ---
|
||||
// Gardé pour savoir quel manga est sélectionné dans l'UI,
|
||||
// mais les données détaillées ne sont plus stockées ici.
|
||||
currentMangaId: null
|
||||
}),
|
||||
|
||||
getters: {
|
||||
// Plus de getters spécifiques aux détails/chapitres ici
|
||||
},
|
||||
|
||||
actions: {
|
||||
// --- Collection Actions ---
|
||||
async loadCollection() {
|
||||
if (this.loadingCollection) return;
|
||||
this.loadingCollection = true;
|
||||
this.errorCollection = null;
|
||||
try {
|
||||
const newCollection = await mangaRepository.getCollection();
|
||||
// On garde la comparaison pour éviter màj inutile de la collection
|
||||
if (!deepCompare(this.collection, newCollection)) {
|
||||
this.collection = newCollection;
|
||||
}
|
||||
} catch (err) {
|
||||
this.errorCollection = err.message;
|
||||
} finally {
|
||||
this.loadingCollection = false;
|
||||
}
|
||||
},
|
||||
|
||||
async refreshCollectionInBackground() {
|
||||
if (this.isBackgroundLoadingCollection) return;
|
||||
this.isBackgroundLoadingCollection = true;
|
||||
try {
|
||||
const newCollection = await mangaRepository.getCollection();
|
||||
if (!deepCompare(this.collection, newCollection)) {
|
||||
this.collection = newCollection;
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('Failed to refresh collection:', err);
|
||||
} finally {
|
||||
this.isBackgroundLoadingCollection = false;
|
||||
}
|
||||
},
|
||||
|
||||
// --- Selected Manga Actions ---
|
||||
setCurrentMangaId(mangaId) {
|
||||
// Met simplement à jour l'ID sélectionné
|
||||
this.currentMangaId = mangaId;
|
||||
},
|
||||
|
||||
clearCurrentMangaFocus() {
|
||||
this.currentMangaId = null;
|
||||
}
|
||||
|
||||
// Plus d'actions fetchMangaDetails / fetchMangaChapters ici
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user