"use server"; import { redirect } from "next/navigation"; import { revalidatePath } from "next/cache"; import { parseStrongShareText } from "@/lib/strong/parser"; import { upsertStrengthWorkout } from "@/lib/models/strength"; import { getCurrentUserId } from "@/lib/session"; export type ImportStrongWorkoutState = { error: string } | null; export async function importStrongWorkout( _prevState: ImportStrongWorkoutState, formData: FormData ): Promise { 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." }; } const userId = await getCurrentUserId(); for (const workout of workouts) { await upsertStrengthWorkout(userId, workout); } revalidatePath("/strength"); revalidatePath("/"); redirect("/strength"); }