refactor: convert preferences to Server Action

- Add src/app/actions/preferences.ts with updatePreferences
- Update PreferencesContext to use Server Action
- Remove PUT from api/preferences route (keep GET)
This commit is contained in:
2026-02-28 10:50:32 +01:00
parent 7308c0aa63
commit d56b0fd7ae
4 changed files with 51 additions and 85 deletions

View File

@@ -0,0 +1,30 @@
"use server";
import { revalidatePath } from "next/cache";
import { PreferencesService } from "@/lib/services/preferences.service";
import { ERROR_CODES } from "@/constants/errorCodes";
import { AppError } from "@/utils/errors";
import type { UserPreferences } from "@/types/preferences";
/**
* Met à jour les préférences utilisateur
*/
export async function updatePreferences(
newPreferences: Partial<UserPreferences>
): Promise<{ success: boolean; message: string; data?: UserPreferences }> {
try {
const updatedPreferences = await PreferencesService.updatePreferences(newPreferences);
// Invalider les pages qui utilisent les préférences
revalidatePath("/");
revalidatePath("/libraries");
revalidatePath("/series");
return { success: true, message: "Préférences mises à jour", data: updatedPreferences };
} catch (error) {
if (error instanceof AppError) {
return { success: false, message: error.message };
}
return { success: false, message: "Erreur lors de la mise à jour des préférences" };
}
}

View File

@@ -7,6 +7,7 @@ import type { UserPreferences } from "@/types/preferences";
import { getErrorMessage } from "@/utils/errors";
import logger from "@/lib/logger";
// GET reste utilisé par PreferencesContext pour récupérer les préférences
export async function GET() {
try {
const preferences: UserPreferences = await PreferencesService.getPreferences();
@@ -37,36 +38,3 @@ export async function GET() {
);
}
}
export async function PUT(request: NextRequest) {
try {
const preferences: UserPreferences = await request.json();
const updatedPreferences: UserPreferences =
await PreferencesService.updatePreferences(preferences);
return NextResponse.json(updatedPreferences);
} catch (error) {
logger.error({ err: error }, "Erreur lors de la mise à jour des préférences:");
if (error instanceof AppError) {
return NextResponse.json(
{
error: {
name: "Preferences update error",
code: error.code,
message: getErrorMessage(error.code),
},
},
{ status: 500 }
);
}
return NextResponse.json(
{
error: {
name: "Preferences update error",
code: ERROR_CODES.PREFERENCES.UPDATE_ERROR,
message: getErrorMessage(ERROR_CODES.PREFERENCES.UPDATE_ERROR),
},
},
{ status: 500 }
);
}
}