From 5bb3be0aa48cd97f96f0b5ceea8f9de8b14bab87 Mon Sep 17 00:00:00 2001 From: Tariq Jamal A Date: Tue, 13 May 2025 05:57:34 +0530 Subject: [PATCH] refactor: add country field in registration form --- app/accounts/login/page.jsx | 143 +++++++++++++++++++++++++++++------- app/contexts/mainContext.js | 2 +- 2 files changed, 119 insertions(+), 26 deletions(-) diff --git a/app/accounts/login/page.jsx b/app/accounts/login/page.jsx index a357598..1cdfb77 100644 --- a/app/accounts/login/page.jsx +++ b/app/accounts/login/page.jsx @@ -4,6 +4,8 @@ import MainContext from "@/app/contexts/mainContext"; import { backendUrl, serAxios } from "@/utils/axios"; import Image from "next/image"; import React, { useContext, useEffect, useState } from "react"; +import { ChevronDown, Search } from "lucide-react"; +import { useCurrency } from "@/app/contexts/currencyContext"; const getAuthPageData = async () => { // Fetch data from external API @@ -26,7 +28,15 @@ const LoginSignup = () => { email: "", password: "", confirmPassword: "", + country: "", }); + const [countries, setCountries] = useState([]); + const [isCountryDropdownOpen, setIsCountryDropdownOpen] = useState(false); + const [isLoading, setIsLoading] = useState(false); + const [searchTerm, setSearchTerm] = useState(""); + const [filteredCountries, setFilteredCountries] = useState([]); + const { setUserCountry } = useCurrency(); + const handleInputChange = (e) => { const { name, value } = e.target; setFormData((prevData) => ({ @@ -35,25 +45,69 @@ const LoginSignup = () => { })); }; + const handleCountrySelect = (country) => { + setFormData((prevData) => ({ + ...prevData, + country: country, + })); + setIsCountryDropdownOpen(false); + setSearchTerm(""); + }; + + const fetchCountries = async () => { + setIsLoading(true); + try { + const response = await fetch('https://countriesnow.space/api/v0.1/countries/'); + const data = await response.json(); + if (!data.error) { + setCountries(data.data); + setFilteredCountries(data.data); + } + } catch (error) { + console.error("Error fetching countries:", error); + } finally { + setIsLoading(false); + } + }; + const handleSubmit = async (e) => { e.preventDefault(); if (isLogin) { loginUser(formData); } else { + // If registering, also set the user's country for currency purposes + if (formData.country) { + setUserCountry(formData.country); + } registerUser(formData); } }; const toggleMode = () => { setIsLogin(!isLogin); - setFormData({ email: "", password: "", confirmPassword: "" }); + setFormData({ email: "", password: "", confirmPassword: "", country: "" }); + setSearchTerm(""); }; useEffect(() => { getAuthPageData().then((data) => { setLoginPageData(data); }); - }, []); + + if (!isLogin) { + fetchCountries(); + } + }, [isLogin]); + + // Filter countries based on search term + useEffect(() => { + if (countries.length > 0) { + const filtered = countries.filter(country => + country.country.toLowerCase().includes(searchTerm.toLowerCase()) + ); + setFilteredCountries(filtered); + } + }, [searchTerm, countries]); return (
@@ -126,22 +180,68 @@ const LoginSignup = () => { />
{!isLogin && ( -
- - -
+ <> +
+ + +
+
+ + + {isCountryDropdownOpen && ( +
+
+
+ + setSearchTerm(e.target.value)} + className="w-full pl-8 pr-4 py-2 text-sm border border-gray-200 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500" + /> +
+
+ + {isLoading ? ( +
Loading countries...
+ ) : filteredCountries.length > 0 ? ( + filteredCountries.map((countryData) => ( + + )) + ) : ( +
No countries found
+ )} +
+ )} +
+ )} @@ -155,13 +255,6 @@ const LoginSignup = () => {
- {/*

- Manage subscriptions -

*/} - {/*

or

-

- Continue with google -

*/}