From 30d4fc670e2d1e5573ef9ca65c93db4be2b498c8 Mon Sep 17 00:00:00 2001 From: Praneeth Palugula Date: Fri, 25 Oct 2024 14:27:43 +0530 Subject: [PATCH] Formatted the code --- pages/index.js | 53 +++++++++++++++++++++++++------------------------- 1 file changed, 26 insertions(+), 27 deletions(-) diff --git a/pages/index.js b/pages/index.js index fd81c10..b87907e 100644 --- a/pages/index.js +++ b/pages/index.js @@ -1,15 +1,16 @@ -import React, { useState } from "react"; -import Script from "next/script"; -import getConstants from "../constants"; -import examConfigs from "../examConfig"; -import Dropdown from "../components/dropdown"; -import { useRouter } from "next/router"; -import Head from "next/head"; +import React, { useState } from 'react'; +import Script from 'next/script'; +import getConstants from '../constants'; +import examConfigs from '../examConfig'; +import Dropdown from '../components/dropdown'; +import { useRouter } from 'next/router'; +import Head from 'next/head'; const ExamForm = () => { - const [selectedExam, setSelectedExam] = useState(""); + const [selectedExam, setSelectedExam] = useState(''); const [formData, setFormData] = useState({}); const [config, setConfig] = useState(null); + const [formSubmitted, setFormSubmitted] = useState(false); const router = useRouter(); const handleExamChange = (selectedOption) => { @@ -20,13 +21,11 @@ const ExamForm = () => { exam: selectedOption.value, rank: 0, code: selectedOption.code, - // apiEndpoint: selectedOption.apiEndpoint, }); } else { setFormData({ exam: selectedOption.value, rank: 0, - // apiEndpoint: selectedOption.apiEndpoint, }); } }; @@ -45,17 +44,19 @@ const ExamForm = () => { rank: enteredRank, })); }; + const handleSubmit = async () => { + setFormSubmitted(true); + if (Object.values(formData).some((value) => !value)) return; + const queryString = Object.entries(formData) .map(([key, value]) => `${key}=${encodeURIComponent(value)}`) - .join("&"); + .join('&'); router.push(`/college_predictor?${queryString}`); }; - const isSubmitDisabled = Object.values(formData).some((value) => !value); - const renderFields = () => { - if (!selectedExam) return null; - if (!config) return null; + const renderFields = () => { + if (!selectedExam || !config) return null; return config.fields.map((field) => (
@@ -64,7 +65,7 @@ const ExamForm = () => { - typeof option === "string" + typeof option === 'string' ? { value: option, label: option } : option )} @@ -88,18 +89,17 @@ const ExamForm = () => { />

{getConstants().TITLE}

-
+
{ {selectedExam && ( <> - {isSubmitDisabled && ( + {formSubmitted && Object.values(formData).some((value) => !value) && (

Please fill all the fields before submitting!