Skip to content

Commit

Permalink
hotfix: grades get adjusted if preferences change, and updating prefe…
Browse files Browse the repository at this point in the history
…rence works again
  • Loading branch information
noahstreller committed May 3, 2024
1 parent 744fcdd commit b3cc669
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 6 deletions.
7 changes: 3 additions & 4 deletions components/theme-switcher.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"use client"

import * as React from "react"
import { Laptop, Moon, Sun } from "lucide-react"
import { useTheme } from "next-themes"

Expand Down Expand Up @@ -31,15 +30,15 @@ export function ThemeSwitcher() {
<DropdownMenuLabel>{t("themes.title")}</DropdownMenuLabel>
<DropdownMenuItem onClick={() => setTheme("light")}>
<Sun className="h-[1rem] w-[1rem] mr-[0.5rem]" />
{t("themes.light")}
Light
</DropdownMenuItem>
<DropdownMenuItem onClick={() => setTheme("dark")}>
<Moon className="h-[1rem] w-[1rem] mr-[0.5rem]" />
{t("themes.dark")}
Dark
</DropdownMenuItem>
<DropdownMenuItem onClick={() => setTheme("system")}>
<Laptop className="h-[1rem] w-[1rem] mr-[0.5rem]" />
{t("themes.system")}
System
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
Expand Down
2 changes: 1 addition & 1 deletion lib/problem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
};
Expand Down
1 change: 1 addition & 0 deletions lib/repositories/preferences-repo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
34 changes: 33 additions & 1 deletion lib/services/preferences-service.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand Down Expand Up @@ -41,7 +42,9 @@ export async function savePreferences(
): Promise<Preferences | Problem> {
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);
Expand All @@ -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;
}
}

0 comments on commit b3cc669

Please sign in to comment.