import React, { useState } from 'react';
import { Building2 } from 'lucide-react';
import { useNavigate } from 'react-router-dom'; // Adjust import based on your routing setup
import apiService from '../../services/apiService'; // Adjust import path as needed
/**
* @typedef {Object} SetPasswordProps
* // Add any props if the component were to receive them
*/
/**
* A React component that provides a form for users to set their password using their phone number.
*/
const SetPassword = () => {
const [phoneNumber, setPhoneNumber] = useState('');
const [password, setPassword] = useState('');
const [confirmPassword, setConfirmPassword] = useState('');
const [error, setError] = useState('');
const [loading, setLoading] = useState(false);
const [success, setSuccess] = useState(false);
const navigate = useNavigate();
const handleSubmit = async (e) => {
/**
* Handles the form submission for setting the password.
* Performs client-side validation and calls the API to set the password.
* @param {React.FormEvent<HTMLFormElement>} e - The form event.
*/
e.preventDefault();
// Validation
if (!phoneNumber || !password || !confirmPassword) {
setError('Please fill in all fields');
return;
}
if (password !== confirmPassword) {
setError('Passwords do not match');
return;
}
if (password.length < 6) {
setError('Password must be at least 6 characters');
return;
}
setLoading(true);
setError('');
try {
const result = await setPasswordfunc(phoneNumber, password);
if (result.success) {
setSuccess(true);
// Optionally redirect after successful password set
// navigate('/login');
} else {
setError(result.message || 'Failed to set password');
}
} catch (err) {
setError('An error occurred. Please try again.');
} finally {
setLoading(false);
}
};
return (
<div className="min-h-screen bg-gray-100 flex items-center justify-center py-12 px-4 sm:px-6 lg:px-8">
<div className="max-w-md w-full space-y-8">
<div>
<div className="mx-auto h-12 w-12 flex items-center justify-center rounded-full bg-blue-100">
<Building2 className="h-6 w-6 text-blue-600" />
</div>
<h2 className="mt-6 text-center text-3xl font-extrabold text-gray-900">
Set Your Password
</h2>
</div>
{success ? (
<div className="bg-green-50 border border-green-200 text-green-600 px-4 py-3 rounded-md text-sm">
Password set successfully! You can now login with your new password.
</div>
) : (
<form className="mt-8 space-y-6" onSubmit={handleSubmit}>
<div className="space-y-4">
<div>
<label htmlFor="phone-number" className="block text-sm font-medium text-gray-700">
Phone Number
</label>
<input
id="phone-number"
type="tel"
value={phoneNumber}
onChange={(e) => setPhoneNumber(e.target.value)}
className="mt-1 block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500"
placeholder="Enter your phone number"
required
/>
</div>
<div>
<label htmlFor="password" className="block text-sm font-medium text-gray-700">
New Password
</label>
<input
id="password"
type="password"
value={password}
onChange={(e) => setPassword(e.target.value)}
className="mt-1 block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500"
placeholder="Enter your new password"
required
/>
</div>
<div>
<label htmlFor="confirm-password" className="block text-sm font-medium text-gray-700">
Confirm Password
</label>
<input
id="confirm-password"
type="password"
value={confirmPassword}
onChange={(e) => setConfirmPassword(e.target.value)}
className="mt-1 block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500"
placeholder="Confirm your password"
required
/>
</div>
</div>
{error && (
<div className="bg-red-50 border border-red-200 text-red-600 px-4 py-3 rounded-md text-sm">
{error}
</div>
)}
<button
type="submit"
disabled={loading}
className="group relative w-full flex justify-center py-2 px-4 border border-transparent text-sm font-medium rounded-md text-white bg-blue-600 hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500 disabled:opacity-50"
>
{loading ? 'Setting password...' : 'Set Password'}
</button>
</form>
)}
</div>
</div>
);
};
export default SetPassword;
Source