refactor: use Server Actions for read progress updates

- Create src/app/actions/read-progress.ts with updateReadProgress and deleteReadProgress
- Update mark-as-read-button and mark-as-unread-button to use Server Actions
- Update usePageNavigation hook to use Server Action
- Use revalidateTag with 'min' profile for cache invalidation
This commit is contained in:
2026-02-28 10:34:26 +01:00
parent ecce0a9738
commit 03cb46f81b
4 changed files with 64 additions and 19 deletions

View File

@@ -0,0 +1,53 @@
"use server";
import { revalidateTag } from "next/cache";
import { BookService } from "@/lib/services/book.service";
import { ERROR_CODES } from "@/constants/errorCodes";
import { AppError } from "@/utils/errors";
const HOME_CACHE_TAG = "home-data";
/**
* Met à jour la progression de lecture d'un livre
* Note: ne pas utiliser "use server" avec redirect - on gère manuellement
*/
export async function updateReadProgress(
bookId: string,
page: number,
completed: boolean = false
): Promise<{ success: boolean; message: string }> {
try {
await BookService.updateReadProgress(bookId, page, completed);
// Invalider le cache de la home (sans refresh auto)
revalidateTag(HOME_CACHE_TAG, "min");
return { success: true, message: "Progression mise à jour" };
} catch (error) {
if (error instanceof AppError) {
return { success: false, message: error.message };
}
return { success: false, message: "Erreur lors de la mise à jour" };
}
}
/**
* Supprime la progression de lecture d'un livre
*/
export async function deleteReadProgress(
bookId: string
): Promise<{ success: boolean; message: string }> {
try {
await BookService.deleteReadProgress(bookId);
// Invalider le cache de la home (sans refresh auto)
revalidateTag(HOME_CACHE_TAG, "min");
return { success: true, message: "Progression supprimée" };
} catch (error) {
if (error instanceof AppError) {
return { success: false, message: error.message };
}
return { success: false, message: "Erreur lors de la suppression" };
}
}