64 lines
1.7 KiB
TypeScript
64 lines
1.7 KiB
TypeScript
'use server';
|
|
|
|
import { notesService } from '@/services/notes';
|
|
import { revalidatePath } from 'next/cache';
|
|
import { getServerSession } from 'next-auth';
|
|
import { authOptions } from '@/lib/auth';
|
|
|
|
/**
|
|
* Réordonne les notes
|
|
*/
|
|
export async function reorderNotes(
|
|
noteOrders: Array<{ id: string; order: number }>
|
|
) {
|
|
try {
|
|
const session = await getServerSession(authOptions);
|
|
if (!session?.user?.id) {
|
|
return { success: false, error: 'Non autorisé' };
|
|
}
|
|
|
|
await notesService.reorderNotes(session.user.id, noteOrders);
|
|
|
|
revalidatePath('/notes');
|
|
return { success: true };
|
|
} catch (error) {
|
|
console.error('Error reordering notes:', error);
|
|
return {
|
|
success: false,
|
|
error: error instanceof Error ? error.message : 'Erreur inconnue',
|
|
};
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Bascule l'état favori d'une note
|
|
*/
|
|
export async function toggleNoteFavorite(noteId: string) {
|
|
try {
|
|
const session = await getServerSession(authOptions);
|
|
if (!session?.user?.id) {
|
|
return { success: false, error: 'Non autorisé' };
|
|
}
|
|
|
|
// Récupérer la note actuelle pour connaître son état favori
|
|
const currentNote = await notesService.getNoteById(noteId, session.user.id);
|
|
if (!currentNote) {
|
|
return { success: false, error: 'Note non trouvée' };
|
|
}
|
|
|
|
// Basculer l'état favori
|
|
const updatedNote = await notesService.updateNote(noteId, session.user.id, {
|
|
isFavorite: !currentNote.isFavorite,
|
|
});
|
|
|
|
revalidatePath('/notes');
|
|
return { success: true, note: updatedNote };
|
|
} catch (error) {
|
|
console.error('Error toggling note favorite:', error);
|
|
return {
|
|
success: false,
|
|
error: error instanceof Error ? error.message : 'Erreur inconnue',
|
|
};
|
|
}
|
|
}
|