Save and use user's locale preference

This commit is contained in:
Timothy Armes
2024-09-30 14:53:24 +02:00
parent 03f5ea3454
commit ee031588ff
13 changed files with 467 additions and 367 deletions
-12
View File
@@ -1,12 +0,0 @@
import { ObjectId } from "mongodb";
export type UserModel = {
email: string;
emailVerified?: Date;
locale: string;
};
export type VerificationModel = {
identifier: string;
expires: Date;
};
+2 -6
View File
@@ -1,7 +1,7 @@
import mongoDB, { MongoClient } from "mongodb";
import { User } from "next-auth";
import { TournamentModel } from "@/server/models/tournamentModel";
import { UserModel, VerificationModel } from "@/server/models/userModel";
import { ZoneModel } from "@/server/models/zoneModel";
import { ClubModel } from "./models/clubModel";
@@ -9,9 +9,8 @@ import { ClubModel } from "./models/clubModel";
export const collections: {
tournaments?: mongoDB.Collection<TournamentModel>;
clubs?: mongoDB.Collection<ClubModel>;
users?: mongoDB.Collection<UserModel>;
users?: mongoDB.Collection<User>;
zones?: mongoDB.Collection<ZoneModel>;
userVerificationTokens?: mongoDB.Collection<VerificationModel>;
} = {};
if (!process.env.MONGODB_URI) {
@@ -53,9 +52,6 @@ export async function dbConnect() {
const userData: mongoDB.Db = p.db("userData");
collections.users = userData.collection("users");
collections.userVerificationTokens = userData.collection(
"userVerificationTokens",
);
collections.zones = userData.collection("zones");
const tournamentData: mongoDB.Db = p.db("tournamentsFranceDB");
+33
View File
@@ -0,0 +1,33 @@
"use server";
import { ObjectId } from "mongodb";
import { z } from "zod";
import { auth } from "@/auth";
import { collections, dbConnect } from "@/server/mongodb";
import { errorLog } from "@/utils/logger";
import { actionClient } from "./safeAction";
export const setUserLocale = actionClient
.schema(z.string())
.action(async (input) => {
try {
await dbConnect();
const user = await auth();
if (!user?.user) {
throw new Error("You must be logged update your locale");
}
await collections.users!.findOneAndUpdate(
{ _id: new ObjectId(user.user.id) },
{ $set: { locale: input.parsedInput } },
);
return true;
} catch (error) {
errorLog(error);
throw error;
}
});