Skip to content

Commit

Permalink
MODIFY: revalidatePath and redirect now lead to the proper page, smal…
Browse files Browse the repository at this point in the history
…l changes since TSQ is adopted to better understand the code and maintain
  • Loading branch information
srkuleo committed Aug 28, 2024
1 parent 240d18d commit c03b945
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions src/util/actions/workout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { revalidatePath } from "next/cache";
import { and, eq, ilike, ne } from "drizzle-orm";
import { db } from "@/db";
import { workouts, type QueriedByIdWorkoutType } from "@/db/schema";
import { workouts } from "@/db/schema";
import { getAuth } from "./auth";

import {
Expand Down Expand Up @@ -63,7 +63,7 @@ export async function createWorkout(

console.log(`${title} workout created!`);

revalidatePath("/workouts");
revalidatePath("/home");

return {
status: "success",
Expand Down Expand Up @@ -132,7 +132,7 @@ export async function editWorkout(

console.log(`${initTitle} workout edited.`);

revalidatePath("/workouts");
revalidatePath("/home");

return {
status: "success-redirect",
Expand All @@ -156,7 +156,7 @@ export async function removeWorkout(

console.log("Workout deleted!");

revalidatePath("/workouts");
revalidatePath("/home");

return {
status: "success",
Expand All @@ -183,7 +183,7 @@ export async function archiveWorkout(

console.log("Workout archived!");

revalidatePath("/workouts");
revalidatePath("/home");

return {
status: "success",
Expand Down Expand Up @@ -250,7 +250,7 @@ export async function submitDoneWorkout(
}

export async function updateCurrentWorkout(
updatedCurrentWorkout: Omit<QueriedByIdWorkoutType, "id">,
updatedCurrentWorkout: CreateWorkoutType,
workoutId: number,
): Promise<WorkoutActionResponse> {
const { user } = await getAuth();
Expand All @@ -259,6 +259,16 @@ export async function updateCurrentWorkout(
throw new Error("Unauthorized action. Please login.");
}

const isValidWorkout = CreateWorkoutSchema.safeParse(updatedCurrentWorkout);

if (!isValidWorkout.success) {
return {
status: "error",
errors: isValidWorkout.error.flatten().fieldErrors,
message: `${updatedCurrentWorkout.title} workout was not updated`,
};
}

try {
await db
.update(workouts)
Expand All @@ -267,7 +277,7 @@ export async function updateCurrentWorkout(

console.log("Workout successfully updated.");

revalidatePath("/workouts");
revalidatePath("/home");

return {
status: "success-redirect",
Expand Down

0 comments on commit c03b945

Please sign in to comment.