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:
@@ -8,42 +8,15 @@
|
||||
|----------------|---------------|--------|
|
||||
| `PATCH /api/komga/books/[bookId]/read-progress` | `updateReadProgress()` | ✅ Done |
|
||||
| `DELETE /api/komga/books/[bookId]/read-progress` | `deleteReadProgress()` | ✅ Done |
|
||||
| `POST /api/komga/favorites` | `addToFavorites()` | ✅ Done |
|
||||
| `DELETE /api/komga/favorites` | `removeFromFavorites()` | ✅ Done |
|
||||
| `PUT /api/preferences` | `updatePreferences()` | ✅ Done |
|
||||
|
||||
---
|
||||
|
||||
## À convertir (priorité haute)
|
||||
|
||||
### 1. Favoris
|
||||
|
||||
**Route actuelle** : `api/komga/favorites/route.ts`
|
||||
|
||||
```typescript
|
||||
// Action à créer : src/app/actions/favorites.ts
|
||||
export async function addToFavorites(seriesId: string)
|
||||
export async function removeFromFavorites(seriesId: string)
|
||||
```
|
||||
|
||||
**Appelants à migrer** :
|
||||
- `components/series/SeriesHeader.tsx` (POST/DELETE fetch)
|
||||
|
||||
---
|
||||
|
||||
### 2. Préférences
|
||||
|
||||
**Route actuelle** : `api/preferences/route.ts`
|
||||
|
||||
```typescript
|
||||
// Action à créer : src/app/actions/preferences.ts
|
||||
export async function updatePreferences(preferences: UserPreferences)
|
||||
```
|
||||
|
||||
**Appelants à migrer** :
|
||||
- `components/settings/PreferencesForm.tsx` (PUT fetch)
|
||||
- `hooks/usePreferences.ts` (PUT fetch)
|
||||
|
||||
---
|
||||
|
||||
### 3. Scan de bibliothèque
|
||||
### 1. Scan de bibliothèque
|
||||
|
||||
**Route actuelle** : `api/komga/libraries/[libraryId]/scan/route.ts`
|
||||
|
||||
@@ -59,7 +32,7 @@ export async function scanLibrary(libraryId: string)
|
||||
|
||||
## À convertir (priorité moyenne)
|
||||
|
||||
### 4. Configuration Komga
|
||||
### 2. Configuration Komga
|
||||
|
||||
**Route actuelle** : `api/komga/config/route.ts`
|
||||
|
||||
@@ -71,7 +44,7 @@ export async function getKomgaConfig()
|
||||
|
||||
---
|
||||
|
||||
### 5. Mot de passe utilisateur
|
||||
### 3. Mot de passe utilisateur
|
||||
|
||||
**Route actuelle** : `api/user/password/route.ts`
|
||||
|
||||
@@ -82,7 +55,7 @@ export async function changePassword(currentPassword: string, newPassword: strin
|
||||
|
||||
---
|
||||
|
||||
### 6. Inscription
|
||||
### 4. Inscription
|
||||
|
||||
**Route actuelle** : `api/auth/register/route.ts`
|
||||
|
||||
@@ -95,7 +68,7 @@ export async function registerUser(email: string, password: string)
|
||||
|
||||
## À convertir (priorité basse - admin)
|
||||
|
||||
### 7. Gestion des utilisateurs (admin)
|
||||
### 5. Gestion des utilisateurs (admin)
|
||||
|
||||
**Routes** :
|
||||
- `api/admin/users/[userId]/route.ts` (PATCH, DELETE)
|
||||
|
||||
30
src/app/actions/preferences.ts
Normal file
30
src/app/actions/preferences.ts
Normal 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" };
|
||||
}
|
||||
}
|
||||
@@ -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 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
...updatedPreferences,
|
||||
...result.data,
|
||||
}));
|
||||
}
|
||||
|
||||
return updatedPreferences;
|
||||
return result.data;
|
||||
} catch (error) {
|
||||
logger.error({ err: error }, "Erreur lors de la mise à jour des préférences");
|
||||
throw error;
|
||||
|
||||
Reference in New Issue
Block a user