refactor: convert library scan to Server Action
- Add src/app/actions/library.ts with scanLibrary - Update ScanButton to use Server Action - Remove POST from api/komga/libraries/[libraryId]/scan route
This commit is contained in:
@@ -11,6 +11,7 @@
|
|||||||
| `POST /api/komga/favorites` | `addToFavorites()` | ✅ Done |
|
| `POST /api/komga/favorites` | `addToFavorites()` | ✅ Done |
|
||||||
| `DELETE /api/komga/favorites` | `removeFromFavorites()` | ✅ Done |
|
| `DELETE /api/komga/favorites` | `removeFromFavorites()` | ✅ Done |
|
||||||
| `PUT /api/preferences` | `updatePreferences()` | ✅ Done |
|
| `PUT /api/preferences` | `updatePreferences()` | ✅ Done |
|
||||||
|
| `POST /api/komga/libraries/[libraryId]/scan` | `scanLibrary()` | ✅ Done |
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
|||||||
28
src/app/actions/library.ts
Normal file
28
src/app/actions/library.ts
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
"use server";
|
||||||
|
|
||||||
|
import { revalidatePath } from "next/cache";
|
||||||
|
import { LibraryService } from "@/lib/services/library.service";
|
||||||
|
import { ERROR_CODES } from "@/constants/errorCodes";
|
||||||
|
import { AppError } from "@/utils/errors";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Lance un scan de bibliothèque
|
||||||
|
*/
|
||||||
|
export async function scanLibrary(
|
||||||
|
libraryId: string
|
||||||
|
): Promise<{ success: boolean; message: string }> {
|
||||||
|
try {
|
||||||
|
await LibraryService.scanLibrary(libraryId, false);
|
||||||
|
|
||||||
|
// Invalider le cache de la bibliothèque
|
||||||
|
revalidatePath(`/libraries/${libraryId}`);
|
||||||
|
revalidatePath("/libraries");
|
||||||
|
|
||||||
|
return { success: true, message: "Scan lancé" };
|
||||||
|
} catch (error) {
|
||||||
|
if (error instanceof AppError) {
|
||||||
|
return { success: false, message: error.message };
|
||||||
|
}
|
||||||
|
return { success: false, message: "Erreur lors du scan" };
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,45 +0,0 @@
|
|||||||
import { NextResponse } from "next/server";
|
|
||||||
import { LibraryService } from "@/lib/services/library.service";
|
|
||||||
import { ERROR_CODES } from "@/constants/errorCodes";
|
|
||||||
import { AppError } from "@/utils/errors";
|
|
||||||
import { getErrorMessage } from "@/utils/errors";
|
|
||||||
import type { NextRequest } from "next/server";
|
|
||||||
import logger from "@/lib/logger";
|
|
||||||
|
|
||||||
export async function POST(
|
|
||||||
request: NextRequest,
|
|
||||||
{ params }: { params: Promise<{ libraryId: string }> }
|
|
||||||
) {
|
|
||||||
try {
|
|
||||||
const libraryId: string = (await params).libraryId;
|
|
||||||
|
|
||||||
// Scan library with deep=false
|
|
||||||
await LibraryService.scanLibrary(libraryId, false);
|
|
||||||
|
|
||||||
return NextResponse.json({ success: true });
|
|
||||||
} catch (error) {
|
|
||||||
logger.error({ err: error }, "API Library Scan - Erreur");
|
|
||||||
if (error instanceof AppError) {
|
|
||||||
return NextResponse.json(
|
|
||||||
{
|
|
||||||
error: {
|
|
||||||
code: error.code,
|
|
||||||
name: "Library scan error",
|
|
||||||
message: getErrorMessage(error.code),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{ status: 500 }
|
|
||||||
);
|
|
||||||
}
|
|
||||||
return NextResponse.json(
|
|
||||||
{
|
|
||||||
error: {
|
|
||||||
code: ERROR_CODES.LIBRARY.SCAN_ERROR,
|
|
||||||
name: "Library scan error",
|
|
||||||
message: getErrorMessage(ERROR_CODES.LIBRARY.SCAN_ERROR),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{ status: 500 }
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -8,6 +8,7 @@ import { cn } from "@/lib/utils";
|
|||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
import { useRouter } from "next/navigation";
|
import { useRouter } from "next/navigation";
|
||||||
import logger from "@/lib/logger";
|
import logger from "@/lib/logger";
|
||||||
|
import { scanLibrary } from "@/app/actions/library";
|
||||||
|
|
||||||
interface ScanButtonProps {
|
interface ScanButtonProps {
|
||||||
libraryId: string;
|
libraryId: string;
|
||||||
@@ -22,12 +23,10 @@ export function ScanButton({ libraryId }: ScanButtonProps) {
|
|||||||
const handleScan = async () => {
|
const handleScan = async () => {
|
||||||
setIsScanning(true);
|
setIsScanning(true);
|
||||||
try {
|
try {
|
||||||
const response = await fetch(`/api/komga/libraries/${libraryId}/scan`, {
|
const result = await scanLibrary(libraryId);
|
||||||
method: "POST",
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!result.success) {
|
||||||
throw new Error("Failed to scan library");
|
throw new Error(result.message);
|
||||||
}
|
}
|
||||||
|
|
||||||
toast({
|
toast({
|
||||||
@@ -35,14 +34,9 @@ export function ScanButton({ libraryId }: ScanButtonProps) {
|
|||||||
description: t("library.scan.success.description"),
|
description: t("library.scan.success.description"),
|
||||||
});
|
});
|
||||||
|
|
||||||
// Attendre 5 secondes pour que le scan se termine, puis invalider le cache et rafraîchir
|
// Attendre 5 secondes pour que le scan se termine, puis rafraîchir
|
||||||
setTimeout(async () => {
|
setTimeout(async () => {
|
||||||
try {
|
try {
|
||||||
// Invalider le cache
|
|
||||||
await fetch(`/api/komga/libraries/${libraryId}/series`, {
|
|
||||||
method: "DELETE",
|
|
||||||
});
|
|
||||||
|
|
||||||
// Rafraîchir la page pour voir les changements
|
// Rafraîchir la page pour voir les changements
|
||||||
router.refresh();
|
router.refresh();
|
||||||
|
|
||||||
@@ -52,7 +46,7 @@ export function ScanButton({ libraryId }: ScanButtonProps) {
|
|||||||
description: t("library.scan.complete.description"),
|
description: t("library.scan.complete.description"),
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.error({ err: error }, "Error invalidating cache after scan:");
|
logger.error({ err: error }, "Error refreshing after scan:");
|
||||||
toast({
|
toast({
|
||||||
variant: "destructive",
|
variant: "destructive",
|
||||||
title: t("library.scan.error.title"),
|
title: t("library.scan.error.title"),
|
||||||
|
|||||||
Reference in New Issue
Block a user