Refactor admin preferences management to use global site preferences, update UI components for better user experience, and implement image selection for background settings.
This commit is contained in:
@@ -11,20 +11,24 @@ export async function GET() {
|
||||
return NextResponse.json({ error: "Accès refusé" }, { status: 403 });
|
||||
}
|
||||
|
||||
// Récupérer toutes les préférences utilisateur
|
||||
const preferences = await prisma.userPreferences.findMany({
|
||||
include: {
|
||||
user: {
|
||||
select: {
|
||||
id: true,
|
||||
username: true,
|
||||
email: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
// Récupérer les préférences globales du site
|
||||
let sitePreferences = await prisma.sitePreferences.findUnique({
|
||||
where: { id: "global" },
|
||||
});
|
||||
|
||||
return NextResponse.json(preferences);
|
||||
// Si elles n'existent pas, créer une entrée par défaut
|
||||
if (!sitePreferences) {
|
||||
sitePreferences = await prisma.sitePreferences.create({
|
||||
data: {
|
||||
id: "global",
|
||||
homeBackground: null,
|
||||
eventsBackground: null,
|
||||
leaderboardBackground: null,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
return NextResponse.json(sitePreferences);
|
||||
} catch (error) {
|
||||
console.error("Error fetching admin preferences:", error);
|
||||
return NextResponse.json(
|
||||
@@ -43,25 +47,27 @@ export async function PUT(request: Request) {
|
||||
}
|
||||
|
||||
const body = await request.json();
|
||||
const { userId, homeBackground, eventsBackground, leaderboardBackground } =
|
||||
body;
|
||||
const { homeBackground, eventsBackground, leaderboardBackground } = body;
|
||||
|
||||
if (!userId) {
|
||||
return NextResponse.json({ error: "userId requis" }, { status: 400 });
|
||||
}
|
||||
|
||||
const preferences = await prisma.userPreferences.upsert({
|
||||
where: { userId },
|
||||
const preferences = await prisma.sitePreferences.upsert({
|
||||
where: { id: "global" },
|
||||
update: {
|
||||
homeBackground: homeBackground ?? undefined,
|
||||
eventsBackground: eventsBackground ?? undefined,
|
||||
leaderboardBackground: leaderboardBackground ?? undefined,
|
||||
homeBackground:
|
||||
homeBackground === "" ? null : homeBackground ?? undefined,
|
||||
eventsBackground:
|
||||
eventsBackground === "" ? null : eventsBackground ?? undefined,
|
||||
leaderboardBackground:
|
||||
leaderboardBackground === ""
|
||||
? null
|
||||
: leaderboardBackground ?? undefined,
|
||||
},
|
||||
create: {
|
||||
userId,
|
||||
homeBackground: homeBackground ?? null,
|
||||
eventsBackground: eventsBackground ?? null,
|
||||
leaderboardBackground: leaderboardBackground ?? null,
|
||||
id: "global",
|
||||
homeBackground: homeBackground === "" ? null : homeBackground ?? null,
|
||||
eventsBackground:
|
||||
eventsBackground === "" ? null : eventsBackground ?? null,
|
||||
leaderboardBackground:
|
||||
leaderboardBackground === "" ? null : leaderboardBackground ?? null,
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user