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

@@ -3,6 +3,7 @@ import { useRouter } from "next/navigation";
import { ClientOfflineBookService } from "@/lib/services/client-offlinebook.service";
import type { KomgaBook } from "@/types/komga";
import logger from "@/lib/logger";
import { updateReadProgress } from "@/app/actions/read-progress";
interface UsePageNavigationProps {
book: KomgaBook;
@@ -48,11 +49,7 @@ export function usePageNavigation({
try {
ClientOfflineBookService.setCurrentPage(bookRef.current, page);
const completed = page === pagesLengthRef.current;
await fetch(`/api/komga/books/${bookRef.current.id}/read-progress`, {
method: "PATCH",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ page, completed }),
});
await updateReadProgress(bookRef.current.id, page, completed);
} catch (error) {
logger.error({ err: error }, "Sync error:");
}

View File

@@ -7,6 +7,7 @@ import { ClientOfflineBookService } from "@/lib/services/client-offlinebook.serv
import { useState } from "react";
import { useTranslation } from "react-i18next";
import logger from "@/lib/logger";
import { updateReadProgress } from "@/app/actions/read-progress";
interface MarkAsReadButtonProps {
bookId: string;
@@ -32,16 +33,11 @@ export function MarkAsReadButton({
setIsLoading(true);
try {
ClientOfflineBookService.removeCurrentPageById(bookId);
const response = await fetch(`/api/komga/books/${bookId}/read-progress`, {
method: "PATCH",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ page: pagesCount, completed: true }),
});
const result = await updateReadProgress(bookId, pagesCount, true);
if (!response.ok) {
throw new Error(t("books.actions.markAsRead.error.update"));
if (!result.success) {
throw new Error(result.message);
}
toast({

View File

@@ -7,6 +7,7 @@ import { ClientOfflineBookService } from "@/lib/services/client-offlinebook.serv
import { useState } from "react";
import { useTranslation } from "react-i18next";
import logger from "@/lib/logger";
import { deleteReadProgress } from "@/app/actions/read-progress";
interface MarkAsUnreadButtonProps {
bookId: string;
@@ -23,12 +24,10 @@ export function MarkAsUnreadButton({ bookId, onSuccess, className }: MarkAsUnrea
e.stopPropagation(); // Empêcher la propagation au parent
setIsLoading(true);
try {
const response = await fetch(`/api/komga/books/${bookId}/read-progress`, {
method: "DELETE",
});
const result = await deleteReadProgress(bookId);
if (!response.ok) {
throw new Error(t("books.actions.markAsUnread.error.update"));
if (!result.success) {
throw new Error(result.message);
}
// On supprime la page courante du localStorage seulement après que l'API a répondu