diff --git a/components/theme-switcher.tsx b/components/theme-switcher.tsx index 9befc77..9d8286e 100644 --- a/components/theme-switcher.tsx +++ b/components/theme-switcher.tsx @@ -1,6 +1,5 @@ "use client" -import * as React from "react" import { Laptop, Moon, Sun } from "lucide-react" import { useTheme } from "next-themes" @@ -31,15 +30,15 @@ export function ThemeSwitcher() { {t("themes.title")} setTheme("light")}> - {t("themes.light")} + Light setTheme("dark")}> - {t("themes.dark")} + Dark setTheme("system")}> - {t("themes.system")} + System diff --git a/lib/problem.ts b/lib/problem.ts index 95ec927..f8a0702 100644 --- a/lib/problem.ts +++ b/lib/problem.ts @@ -19,7 +19,7 @@ enum ErrorCode { } const errorMessages: { [key in ErrorCode]?: string } = { - [ErrorCode.UniqueConstraintViolation]: "You already added this subject", + [ErrorCode.UniqueConstraintViolation]: "You already added this item", [ErrorCode.ForeignKeyConstraintViolation]: "Some related data is missing", [ErrorCode.UnauthorizedViolation]: "You are not logged in", }; diff --git a/lib/repositories/preferences-repo.ts b/lib/repositories/preferences-repo.ts index 9f44949..83404b6 100644 --- a/lib/repositories/preferences-repo.ts +++ b/lib/repositories/preferences-repo.ts @@ -29,6 +29,7 @@ export async function updatePreferencesInDb( const result = await db .update(preferences) .set(newPreferences) + .where(eq(preferences.id, newPreferences.id!)) .returning() .execute(); return result[0] satisfies Preferences; diff --git a/lib/services/preferences-service.ts b/lib/services/preferences-service.ts index b13ac11..c4557fe 100644 --- a/lib/services/preferences-service.ts +++ b/lib/services/preferences-service.ts @@ -1,7 +1,8 @@ "use server"; -import { NewPreferences, Preferences } from "@/db/schema"; +import { Grade, NewPreferences, Preferences } from "@/db/schema"; import { Problem, catchProblem, getProblem } from "@/lib/problem"; import { addPreferencesToDb, getPreferencesFromDb, updatePreferencesInDb } from "@/lib/repositories/preferences-repo"; +import { getAllGrades, updateGrade } from "@/lib/services/grade-service"; import { getUserId, setUserId } from "@/lib/services/service-util"; import { getDefaultPreferences } from "@/lib/utils"; @@ -41,7 +42,9 @@ export async function savePreferences( ): Promise { try { newPreferences = await setUserId(newPreferences); + adjustGradesToPreferences(newPreferences as Preferences); let existing: Preferences[] = catchProblem(await getPreferences()); + console.log(existing.length); if (existing.length > 0) { newPreferences.id = existing[0].id; return await updatePreferencesInDb(newPreferences); @@ -55,3 +58,32 @@ export async function savePreferences( }) satisfies Problem; } } + +export async function adjustGradesToPreferences( + preferences: Preferences +){ + try { + let grades: Grade[] = catchProblem(await getAllGrades()); + grades.map((grade) => { + let modifiedGrade = grade; + let wasModified = false; + if(grade.value! > preferences.maximumGrade!){ + modifiedGrade.value = preferences.maximumGrade; + wasModified = true; + } + if(grade.value! < preferences.minimumGrade!){ + modifiedGrade.value = preferences.minimumGrade; + wasModified = true; + } + if(wasModified){ + updateGrade(modifiedGrade); + } + }); + } catch (e: any) { + return getProblem({ + errorMessage: e.message, + errorCode: e.code, + detail: e.detail, + }) satisfies Problem; + } +}