refactor: add country field in registration form
This commit is contained in:
@@ -4,6 +4,8 @@ import MainContext from "@/app/contexts/mainContext";
|
|||||||
import { backendUrl, serAxios } from "@/utils/axios";
|
import { backendUrl, serAxios } from "@/utils/axios";
|
||||||
import Image from "next/image";
|
import Image from "next/image";
|
||||||
import React, { useContext, useEffect, useState } from "react";
|
import React, { useContext, useEffect, useState } from "react";
|
||||||
|
import { ChevronDown, Search } from "lucide-react";
|
||||||
|
import { useCurrency } from "@/app/contexts/currencyContext";
|
||||||
|
|
||||||
const getAuthPageData = async () => {
|
const getAuthPageData = async () => {
|
||||||
// Fetch data from external API
|
// Fetch data from external API
|
||||||
@@ -26,7 +28,15 @@ const LoginSignup = () => {
|
|||||||
email: "",
|
email: "",
|
||||||
password: "",
|
password: "",
|
||||||
confirmPassword: "",
|
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 handleInputChange = (e) => {
|
||||||
const { name, value } = e.target;
|
const { name, value } = e.target;
|
||||||
setFormData((prevData) => ({
|
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) => {
|
const handleSubmit = async (e) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
if (isLogin) {
|
if (isLogin) {
|
||||||
loginUser(formData);
|
loginUser(formData);
|
||||||
} else {
|
} else {
|
||||||
|
// If registering, also set the user's country for currency purposes
|
||||||
|
if (formData.country) {
|
||||||
|
setUserCountry(formData.country);
|
||||||
|
}
|
||||||
registerUser(formData);
|
registerUser(formData);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const toggleMode = () => {
|
const toggleMode = () => {
|
||||||
setIsLogin(!isLogin);
|
setIsLogin(!isLogin);
|
||||||
setFormData({ email: "", password: "", confirmPassword: "" });
|
setFormData({ email: "", password: "", confirmPassword: "", country: "" });
|
||||||
|
setSearchTerm("");
|
||||||
};
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
getAuthPageData().then((data) => {
|
getAuthPageData().then((data) => {
|
||||||
setLoginPageData(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 (
|
return (
|
||||||
<div className="flex h-screen bg-gray-100">
|
<div className="flex h-screen bg-gray-100">
|
||||||
@@ -126,22 +180,68 @@ const LoginSignup = () => {
|
|||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
{!isLogin && (
|
{!isLogin && (
|
||||||
<div>
|
<>
|
||||||
<label htmlFor="confirm-password" className="sr-only">
|
<div>
|
||||||
Confirm Password
|
<label htmlFor="confirm-password" className="sr-only">
|
||||||
</label>
|
Confirm Password
|
||||||
<input
|
</label>
|
||||||
id="confirm-password"
|
<input
|
||||||
name="confirmPassword"
|
id="confirm-password"
|
||||||
type="password"
|
name="confirmPassword"
|
||||||
autoComplete="new-password"
|
type="password"
|
||||||
required
|
autoComplete="new-password"
|
||||||
className="appearance-none rounded-none relative block w-full px-3 py-3 border border-gray-300 placeholder-gray-500 text-gray-900 rounded-b-md focus:outline-none focus:ring-[#C19A5B] focus:border-[#C19A5B] focus:z-10 sm:text-sm"
|
required
|
||||||
placeholder="Confirm Password"
|
className="appearance-none rounded-none relative block w-full px-3 py-3 border border-gray-300 placeholder-gray-500 text-gray-900 focus:outline-none focus:ring-[#C19A5B] focus:border-[#C19A5B] focus:z-10 sm:text-sm"
|
||||||
value={formData.confirmPassword}
|
placeholder="Confirm Password"
|
||||||
onChange={handleInputChange}
|
value={formData.confirmPassword}
|
||||||
/>
|
onChange={handleInputChange}
|
||||||
</div>
|
/>
|
||||||
|
</div>
|
||||||
|
<div className="relative">
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={() => setIsCountryDropdownOpen(!isCountryDropdownOpen)}
|
||||||
|
className="appearance-none rounded-none relative block w-full px-3 py-3 border border-gray-300 placeholder-gray-500 text-gray-900 rounded-b-md focus:outline-none focus:ring-[#C19A5B] focus:border-[#C19A5B] focus:z-10 sm:text-sm text-left flex justify-between items-center"
|
||||||
|
>
|
||||||
|
{formData.country ? formData.country : "Select your country"}
|
||||||
|
<ChevronDown className={`w-4 h-4 transition-transform ${isCountryDropdownOpen ? 'rotate-180' : ''}`} />
|
||||||
|
</button>
|
||||||
|
|
||||||
|
{isCountryDropdownOpen && (
|
||||||
|
<div className="absolute z-10 w-full mt-1 bg-white border border-gray-300 rounded-md shadow-lg max-h-60 overflow-y-auto">
|
||||||
|
<div className="p-2 border-b">
|
||||||
|
<div className="relative">
|
||||||
|
<Search className="absolute left-2 top-2.5 h-4 w-4 text-gray-400" />
|
||||||
|
<input
|
||||||
|
type="text"
|
||||||
|
placeholder="Search countries..."
|
||||||
|
value={searchTerm}
|
||||||
|
onChange={(e) => 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"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{isLoading ? (
|
||||||
|
<div className="p-3 text-center text-gray-500">Loading countries...</div>
|
||||||
|
) : filteredCountries.length > 0 ? (
|
||||||
|
filteredCountries.map((countryData) => (
|
||||||
|
<button
|
||||||
|
key={countryData.iso2}
|
||||||
|
type="button"
|
||||||
|
className="block w-full px-4 py-2 text-left hover:bg-gray-100 focus:outline-none focus:bg-gray-100"
|
||||||
|
onClick={() => handleCountrySelect(countryData.country)}
|
||||||
|
>
|
||||||
|
{countryData.country}
|
||||||
|
</button>
|
||||||
|
))
|
||||||
|
) : (
|
||||||
|
<div className="p-3 text-center text-gray-500">No countries found</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
@@ -155,13 +255,6 @@ const LoginSignup = () => {
|
|||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
<div className="text-center">
|
<div className="text-center">
|
||||||
{/* <h2 className="text-[#AC8C6B] font-semibold">
|
|
||||||
Manage subscriptions
|
|
||||||
</h2> */}
|
|
||||||
{/* <h2 className="text-xl my-3">or</h2>
|
|
||||||
<h2 className="capitalize border-2 px-5 py-3">
|
|
||||||
Continue with google
|
|
||||||
</h2> */}
|
|
||||||
<button
|
<button
|
||||||
onClick={toggleMode}
|
onClick={toggleMode}
|
||||||
className="font-medium text-[#C19A5B] mt-10"
|
className="font-medium text-[#C19A5B] mt-10"
|
||||||
|
|||||||
@@ -57,7 +57,7 @@ export const ContextProvider = ({ children }) => {
|
|||||||
setToken(response.data.token)
|
setToken(response.data.token)
|
||||||
localStorage.setItem('token', response.data.token)
|
localStorage.setItem('token', response.data.token)
|
||||||
toast.success('Registration Successful');
|
toast.success('Registration Successful');
|
||||||
router.push('/')
|
router.push('/accounts/login')
|
||||||
|
|
||||||
return response.data;
|
return response.data;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
Reference in New Issue
Block a user