diff --git a/app/create/page.tsx b/app/create/page.tsx index 172c331..c03cd97 100644 --- a/app/create/page.tsx +++ b/app/create/page.tsx @@ -1,13 +1,13 @@ "use client"; -import { QuestionBuilder } from "@/components/survey/question-builder"; -import { Button } from "@/components/ui/button"; -import { Input } from "@/components/ui/input"; -import { Textarea } from "@/components/ui/textarea"; -import { saveSurvey } from "@/lib/survey"; -import { Question, Survey } from "@/types/survey"; +import { SurveyForm } from "@/components/survey/survey-form"; +import { useFormValidation } from "@/hooks/use-form-validation"; +import { loadDraft, saveSurvey } from "@/lib/survey"; +import { Survey, surveySchema } from "@/types/survey"; +import Link from "next/link"; import { useRouter } from "next/navigation"; -import { useState } from "react"; +import { useEffect, useState } from "react"; +import { toast } from "sonner"; export default function CreateSurvey() { const router = useRouter(); @@ -18,80 +18,46 @@ export default function CreateSurvey() { questions: [], createdAt: new Date().toISOString(), }); + const { errors, validate, resetError } = useFormValidation(surveySchema); + + useEffect(() => { + const savedDraft = loadDraft(); + if (savedDraft) { + setSurvey(savedDraft); + } + }, []); - const addQuestion = () => { - const newQuestion: Question = { - id: crypto.randomUUID(), - type: "text", - text: "", - options: [], - required: false, - }; - setSurvey({ - ...survey, - questions: [...survey.questions, newQuestion], - }); - }; - const updateQuestion = (updatedQuestion: Question) => { - setSurvey({ - ...survey, - questions: survey.questions.map((q) => - q.id === updatedQuestion.id ? updatedQuestion : q - ), - }); - }; - const deleteQuestion = (questionId: string) => { - setSurvey({ - ...survey, - questions: survey.questions.filter((q) => q.id !== questionId), - }); - }; const handleSave = () => { + const isValid = validate(survey); + if (!isValid) { + return; + } + survey.modifiedAt = new Date().toISOString(); saveSurvey(survey); + toast.success( + + Test saved successfully! You can view it{" "} + + here + + + ); router.push("/"); }; return ( -
-
-

Create New Test

- -
- setSurvey({ ...survey, title: e.target.value })} - /> -