"use client"; import React from "react"; import {useForm, SubmitHandler, Controller} from "react-hook-form"; import Footer from "@/components/Footer"; import Navbar from "@/components/Navbar"; import Title from "@/components/Title"; import translation from "@/translation/contactus.json"; import Image from "next/image"; import {toast, ToastContainer} from "react-toastify"; import ContactPage from "@/types/cms/contact"; type FormValues = { name: string; email: string; description: string; subject: string; type: "feedback" | "bug" | "help" | ""; }; interface Props { data: ContactPage; } const ErrorMessage = ({message}: {message: string}) => (
{message}
); export default function App({data}: Props) { const selectOptions = [ { label: data.Feedback, value: "feedback", }, { label: data.Bug, value: "bug", }, { label: data.Help, value: "help", }, ]; const {register, control, handleSubmit, formState} = useForm({ defaultValues: { name: "", email: "", description: "", subject: "", type: "", }, }); const {errors, isDirty, isValid} = formState; const onSubmit: SubmitHandler = async (body) => { try { const response = await fetch(`https://platform.encoach.com/api/tickets`, { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ reporter: { name: body.name, email: body.email, }, subject: body.subject, type: body.type, reportedFrom: window?.location.toString() || "", status: "submitted", date: new Date().toISOString(), description: body.description, }), }); if (response.status === 200) { const body = await response.json(); // Pass data to the page via props if (body.ok) { toast.success(data.TicketSuccess); return; } } } catch (err) {} toast.error(data.TicketError); }; return ( <>
{data.Title}
{errors.name && errors.name.type === "required" && } {errors.email && errors.email.type === "required" && } {errors.email && errors.email.type === "pattern" && } {errors.subject && errors.subject.type === "required" && } {errors.type && errors.type.type === "required" && }