import React, { useEffect, useState } from "react"; import { Dialog, DialogClose, DialogContent, DialogDescription, DialogHeader, DialogTitle, DialogTrigger, } from "../ui/dialog"; import FormSurvey from "./survey"; import { Controller, useForm } from "react-hook-form"; import { Textarea } from "../ui/textarea"; import { Button } from "../ui/button"; import { z } from "zod"; import { zodResolver } from "@hookform/resolvers/zod"; import { Checkbox } from "../ui/checkbox"; import { createSurveyData } from "@/service/survey/survey"; import { useTranslations } from "next-intl"; const surveySchema = z.object({ accessFrequency: z.string(), uiExperienceDesign: z.string(), uiExperienceNavigation: z.string(), uiExperienceSpeed: z.string(), infoAccuracy: z.string(), infoCompleteness: z.string(), usefulness: z.string(), suggestion: z.string().optional(), }); type SurveySchema = z.infer; const UserSurveyBox = () => { const [openPolda, setOpenPolda] = useState(); const [showSurvey, setShowSurvey] = useState(true); const [isLoading, setIsLoading] = useState(false); const t = useTranslations("LandingPage"); const { control, handleSubmit, formState: { errors }, } = useForm({ resolver: zodResolver(surveySchema), mode: "all", defaultValues: { accessFrequency: "", uiExperienceDesign: "", uiExperienceNavigation: "", uiExperienceSpeed: "", infoAccuracy: "", infoCompleteness: "", usefulness: "", suggestion: "", }, }); const options = { accessFrequency: [t("everyDay"), t("week"), t("month"), t("firstTime")], uiExperienceDesign: [ t("veryGood"), t("good"), t("enough"), t("notEnough"), t("bad"), ], uiExperienceNavigation: [ t("veryEasy"), t("easy"), t("enough"), t("difficult"), t("veryDifficult"), ], uiExperienceSpeed: [ t("veryFast"), t("fast"), t("enough"), t("slow"), t("verySlow"), ], infoAccuracy: [ t("verySatisfied"), t("satisfied"), t("enough"), t("lessSatisfied"), t("notSatisfied"), ], infoCompleteness: [ t("veryComplete"), t("completely"), t("enough"), t("incomplete"), t("notComplete"), ], usefulness: [ t("veryHelpful"), t("helping"), t("quiteHelpful"), t("lessHelpful"), t("notHelpful"), ], }; const renderControllerGroup = ( name: keyof SurveySchema, question: string, choices: string[] ) => (

{question}

{choices.map((choice, i) => ( ( )} /> ))}
{errors[name] && (

{errors[name]?.message as string}

)}
); const onSubmit = async (data: SurveySchema) => { setIsLoading(true); try { const response = await createSurveyData(data); console.log("API Response:", response); setShowSurvey(false); setOpenPolda(false); } catch (error) { console.error("Error submitting survey:", error); } finally { setIsLoading(false); } }; return (
{/* Garis menyerong */}
line
line
line
{/* Grid konten */}
{/* Kiri: Teks dan tombol */}

{t("survey1", { defaultValue: "Survey1" })}

{t("survey2", { defaultValue: "Survey2" })}

{t("survey4", { defaultValue: "Survey4" })} {t("survey5", { defaultValue: "Survey5" })}
{renderControllerGroup( "accessFrequency", t("survey6", { defaultValue: "Survey6" }), options.accessFrequency )}

{t("survey7", { defaultValue: "Survey7" })}

{renderControllerGroup( "uiExperienceDesign", t("websiteDesign"), options.uiExperienceDesign )} {renderControllerGroup( "uiExperienceNavigation", t("easeNavigation"), options.uiExperienceNavigation )} {renderControllerGroup( "uiExperienceSpeed", t("speed"), options.uiExperienceSpeed )}

{t("survey8", { defaultValue: "Survey8" })}

{renderControllerGroup( "infoAccuracy", t("accurate"), options.infoAccuracy )} {renderControllerGroup( "infoCompleteness", t("complete"), options.infoCompleteness )}
{renderControllerGroup( "usefulness", t("survey9", { defaultValue: "Survey9" }), options.usefulness )}

{t("survey10", { defaultValue: "Survey10" })}

(