init
This commit is contained in:
37
app/strength/import/actions.ts
Normal file
37
app/strength/import/actions.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
"use server";
|
||||
|
||||
import { redirect } from "next/navigation";
|
||||
import { revalidatePath } from "next/cache";
|
||||
import { parseStrongShareText } from "@/lib/strong/parser";
|
||||
import { upsertStrengthWorkout } from "@/lib/models/strength";
|
||||
|
||||
export type ImportStrongWorkoutState = { error: string } | null;
|
||||
|
||||
export async function importStrongWorkout(
|
||||
_prevState: ImportStrongWorkoutState,
|
||||
formData: FormData
|
||||
): Promise<ImportStrongWorkoutState> {
|
||||
const text = formData.get("text");
|
||||
if (typeof text !== "string" || text.trim().length === 0) {
|
||||
return { error: "Wklej tekst wygenerowany przez funkcję 'Share workout' w Strong." };
|
||||
}
|
||||
|
||||
let workouts;
|
||||
try {
|
||||
workouts = parseStrongShareText(text);
|
||||
} catch (error) {
|
||||
return { error: error instanceof Error ? error.message : "Nie udało się przetworzyć tekstu." };
|
||||
}
|
||||
|
||||
if (workouts.length === 0) {
|
||||
return { error: "Nie znaleziono żadnego treningu w podanym tekście." };
|
||||
}
|
||||
|
||||
for (const workout of workouts) {
|
||||
await upsertStrengthWorkout(workout);
|
||||
}
|
||||
|
||||
revalidatePath("/strength");
|
||||
revalidatePath("/");
|
||||
redirect("/strength");
|
||||
}
|
||||
Reference in New Issue
Block a user