refactor: simplification du store de lecteur en supprimant les logs de débogage et en optimisant les getters pour une meilleure lisibilité
This commit is contained in:
parent
5e0fc96cd1
commit
d123166dcb
@@ -12,70 +12,39 @@ export const useReaderStore = defineStore('reader', {
|
||||
isLoading: false,
|
||||
error: null,
|
||||
pages: [],
|
||||
totalPages: 0,
|
||||
_debug: {
|
||||
lastUpdate: Date.now(),
|
||||
lastAction: null
|
||||
}
|
||||
totalPages: 0
|
||||
}),
|
||||
|
||||
getters: {
|
||||
isFirstPage: state => state.currentPage === 0,
|
||||
isLastPage: state => state.currentPage === state.totalPages - 1,
|
||||
currentPageData: state => {
|
||||
const data = state.pages[state.currentPage];
|
||||
console.log('Getting currentPageData:', {
|
||||
currentPage: state.currentPage,
|
||||
hasData: !!data,
|
||||
totalPages: state.totalPages,
|
||||
pagesLength: state.pages.length,
|
||||
lastUpdate: state._debug.lastUpdate,
|
||||
lastAction: state._debug.lastAction
|
||||
});
|
||||
return data;
|
||||
}
|
||||
currentPageData: state => state.pages[state.currentPage]
|
||||
},
|
||||
|
||||
actions: {
|
||||
async loadChapter(chapterId) {
|
||||
console.log('Loading chapter:', chapterId);
|
||||
this.isLoading = true;
|
||||
this.error = null;
|
||||
this._debug.lastAction = 'loadChapter';
|
||||
try {
|
||||
const repository = new ApiChapterRepository();
|
||||
|
||||
// Charger les informations du chapitre
|
||||
console.log('Fetching chapter info...');
|
||||
const chapterData = await repository.getChapter(chapterId);
|
||||
console.log('Chapter data received:', chapterData);
|
||||
this.currentChapter = Chapter.create(chapterData);
|
||||
|
||||
// Charger la liste des pages
|
||||
console.log('Fetching pages info...');
|
||||
const pagesData = await repository.getChapterPages(chapterId);
|
||||
console.log('Pages data received:', pagesData);
|
||||
|
||||
// Initialiser le tableau avec des placeholders
|
||||
this.pages = new Array(pagesData.totalItems).fill(null);
|
||||
this.totalPages = pagesData.totalItems;
|
||||
this._debug.lastUpdate = Date.now();
|
||||
|
||||
console.log('Pages array initialized:', {
|
||||
length: this.pages.length,
|
||||
totalPages: this.totalPages
|
||||
});
|
||||
|
||||
// Charger la première page
|
||||
if (this.totalPages > 0) {
|
||||
console.log('Loading first page...');
|
||||
this.currentPage = 0;
|
||||
await this.loadCurrentPageData();
|
||||
} else {
|
||||
console.warn('No pages available for this chapter');
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error loading chapter:', error);
|
||||
this.error = error.message;
|
||||
} finally {
|
||||
this.isLoading = false;
|
||||
@@ -84,38 +53,24 @@ export const useReaderStore = defineStore('reader', {
|
||||
|
||||
async loadCurrentPageData() {
|
||||
if (!this.currentChapter) {
|
||||
console.error('No current chapter loaded');
|
||||
return;
|
||||
}
|
||||
|
||||
if (this.currentPage < 0 || this.currentPage >= this.totalPages) {
|
||||
console.error('Invalid page index:', this.currentPage);
|
||||
return;
|
||||
}
|
||||
|
||||
const pageNumber = this.currentPage + 1; // Convertir en 1-based pour l'API
|
||||
console.log('Loading page data:', {
|
||||
pageNumber,
|
||||
chapterId: this.currentChapter.id
|
||||
});
|
||||
|
||||
if (this.pages[this.currentPage]?.base64Content) {
|
||||
console.log('Page already loaded, skipping fetch');
|
||||
return;
|
||||
}
|
||||
|
||||
this.isLoading = true;
|
||||
this.error = null;
|
||||
this._debug.lastAction = 'loadCurrentPageData';
|
||||
try {
|
||||
const repository = new ApiChapterRepository();
|
||||
console.log('Fetching page from API...');
|
||||
const pageData = await repository.getChapterPage(this.currentChapter.id, pageNumber);
|
||||
console.log('Page data received:', {
|
||||
hasContent: !!pageData?.base64Content,
|
||||
mimeType: pageData?.mimeType,
|
||||
pageNumber: pageData?.pageNumber
|
||||
});
|
||||
|
||||
// Vérifier que les données sont valides
|
||||
if (!pageData || !pageData.base64Content) {
|
||||
@@ -132,14 +87,7 @@ export const useReaderStore = defineStore('reader', {
|
||||
dimensions: pageData.dimensions
|
||||
};
|
||||
this.pages = newPages;
|
||||
this._debug.lastUpdate = Date.now();
|
||||
|
||||
console.log('Page data updated in store:', {
|
||||
pageIndex: this.currentPage,
|
||||
hasContent: !!this.pages[this.currentPage]?.base64Content
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Error loading page:', error);
|
||||
this.error = error.message;
|
||||
} finally {
|
||||
this.isLoading = false;
|
||||
@@ -148,40 +96,28 @@ export const useReaderStore = defineStore('reader', {
|
||||
|
||||
async nextPage() {
|
||||
if (!this.isLastPage) {
|
||||
console.log('Moving to next page');
|
||||
this.currentPage++;
|
||||
this._debug.lastAction = 'nextPage';
|
||||
this._debug.lastUpdate = Date.now();
|
||||
await this.loadCurrentPageData();
|
||||
}
|
||||
},
|
||||
|
||||
async previousPage() {
|
||||
if (!this.isFirstPage) {
|
||||
console.log('Moving to previous page');
|
||||
this.currentPage--;
|
||||
this._debug.lastAction = 'previousPage';
|
||||
this._debug.lastUpdate = Date.now();
|
||||
await this.loadCurrentPageData();
|
||||
}
|
||||
},
|
||||
|
||||
setReadingMode(mode) {
|
||||
this.readingMode = mode;
|
||||
this._debug.lastAction = 'setReadingMode';
|
||||
this._debug.lastUpdate = Date.now();
|
||||
},
|
||||
|
||||
setReadingDirection(direction) {
|
||||
this.readingDirection = direction;
|
||||
this._debug.lastAction = 'setReadingDirection';
|
||||
this._debug.lastUpdate = Date.now();
|
||||
},
|
||||
|
||||
setZoom(level) {
|
||||
this.zoom = level;
|
||||
this._debug.lastAction = 'setZoom';
|
||||
this._debug.lastUpdate = Date.now();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user