38 lines
1.1 KiB
TypeScript
38 lines
1.1 KiB
TypeScript
|
|
"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");
|
||
|
|
}
|