From 12f9285058e4dd4b4483076a90ef43da1877e441 Mon Sep 17 00:00:00 2001 From: Joao Ramos Date: Tue, 13 Feb 2024 15:17:24 +0000 Subject: [PATCH] Added toast on form submit --- package.json | 1 + src/app/layout.tsx | 1 + src/templates/ContactUs.tsx | 298 +++++++++++++++++++-------------- src/translation/contactus.json | 8 + yarn.lock | 12 ++ 5 files changed, 191 insertions(+), 129 deletions(-) diff --git a/package.json b/package.json index e548f69..259d65e 100644 --- a/package.json +++ b/package.json @@ -25,6 +25,7 @@ "react-hook-form": "^7.50.1", "react-icons": "^4.11.0", "react-string-replace": "^1.1.1", + "react-toastify": "^10.0.4", "sharp": "^0.32.6" }, "devDependencies": { diff --git a/src/app/layout.tsx b/src/app/layout.tsx index e3b998c..bf937a7 100644 --- a/src/app/layout.tsx +++ b/src/app/layout.tsx @@ -1,6 +1,7 @@ /* eslint-disable @next/next/no-page-custom-font */ import clsx from "clsx"; import "./globals.css"; +import "react-toastify/dist/ReactToastify.css"; import type {Metadata} from "next"; import {Inter} from "next/font/google"; import {Almarai} from "next/font/google"; diff --git a/src/templates/ContactUs.tsx b/src/templates/ContactUs.tsx index 337dcf7..d0eac73 100644 --- a/src/templates/ContactUs.tsx +++ b/src/templates/ContactUs.tsx @@ -1,149 +1,189 @@ "use client"; import React from "react"; -import {useForm, SubmitHandler, Controller} from "react-hook-form"; +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"; type FormValues = { - name: string; - email: string; - description: string; - subject: string; - type: "feedback" | "bug" | "help" | ""; + name: string; + email: string; + description: string; + subject: string; + type: "feedback" | "bug" | "help" | ""; }; interface Props { - language: "en" | "ar"; - page: string; + language: "en" | "ar"; + page: string; } -const ErrorMessage = ({message}: {message: string}) => ( -
- {message} -
+const ErrorMessage = ({ message }: { message: string }) => ( +
+ {message} +
); -export default function App({language, page}: Props) { - const selectOptions = [ - { - label: translation.feedback[language], - value: "feedback", - }, - { - label: translation.bug[language], - value: "bug", - }, - { - label: translation.help[language], - value: "help", - }, - ]; +export default function App({ language, page }: Props) { + const selectOptions = [ + { + label: translation.feedback[language], + value: "feedback", + }, + { + label: translation.bug[language], + value: "bug", + }, + { + label: translation.help[language], + value: "help", + }, + ]; - const {register, control, handleSubmit, formState} = useForm({ - defaultValues: { - name: "", - email: "", - description: "", - subject: "", - type: "", - }, - }); + const { register, control, handleSubmit, formState } = useForm({ + defaultValues: { + name: "", + email: "", + description: "", + subject: "", + type: "", + }, + }); - const {errors, isDirty, isValid} = formState; - console.log("formState", formState.isSubmitSuccessful); - const onSubmit: SubmitHandler = async (data) => { - try { - const response = await fetch(`https://platform.encoach.com/api/tickets`, { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify({ - reporter: { - name: data.name, - email: data.email, - }, - subject: data.subject, - type: data.type, - reportedFrom: window.location.href, - status: "submitted", - date: new Date().toISOString(), - description: data.description, - }), - }); + const { errors, isDirty, isValid } = formState; + const onSubmit: SubmitHandler = async (data) => { + try { + const response = await fetch(`https://platform.encoach.com/api/tickets`, { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ + reporter: { + name: data.name, + email: data.email, + }, + subject: data.subject, + type: data.type, + reportedFrom: window.location.href, + status: "submitted", + date: new Date().toISOString(), + description: data.description, + }), + }); - if (response.status === 200) { - const data = await response.json(); - // Pass data to the page via props - console.log(data); - return; - } - } catch (err) {} - }; + if (response.status === 200) { + const data = await response.json(); + // Pass data to the page via props + if (data.ok) { + toast.success(translation.ticketSuccess[language]); + return; + } + } + } catch (err) {} - return ( -
- -
-
- {translation.title[language]} -
-
-
-
- - {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" && } -