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 }
);
}
}

View File

@@ -7,10 +7,11 @@ import { AppError } from "../utils/errors";
import type { UserPreferences } from "@/types/preferences";
import { defaultPreferences } from "@/types/preferences";
import logger from "@/lib/logger";
import { updatePreferences as updatePreferencesAction } from "@/app/actions/preferences";
interface PreferencesContextType {
preferences: UserPreferences;
updatePreferences: (newPreferences: Partial<UserPreferences>) => Promise<void>;
updatePreferences: (newPreferences: Partial<UserPreferences>) => Promise<UserPreferences | undefined>;
isLoading: boolean;
}
@@ -84,28 +85,22 @@ export function PreferencesProvider({
}
}, [status, fetchPreferences, hasValidInitialPreferences]);
const updatePreferences = useCallback(async (newPreferences: Partial<UserPreferences>) => {
const updatePreferences = useCallback(async (newPreferences: Partial<UserPreferences>): Promise<UserPreferences | undefined> => {
try {
const response = await fetch("/api/preferences", {
method: "PUT",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(newPreferences),
});
const result = await updatePreferencesAction(newPreferences);
if (!response.ok) {
if (!result.success) {
throw new AppError(ERROR_CODES.PREFERENCES.UPDATE_ERROR);
}
const updatedPreferences = await response.json();
if (result.data) {
setPreferences((prev) => ({
...prev,
...result.data,
}));
}
setPreferences((prev) => ({
...prev,
...updatedPreferences,
}));
return updatedPreferences;
return result.data;
} catch (error) {
logger.error({ err: error }, "Erreur lors de la mise à jour des préférences");
throw error;