Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 36 additions & 15 deletions frontend/src/pages/auth/Signup.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const Signup = ({ onAuthSuccess = () => {} }) => {
password: '',
confirm: '',
});
const [countryCode, setCountryCode] = useState('+1');
const [touched, setTouched] = useState({
username: false,
name: false,
Expand Down Expand Up @@ -93,12 +94,15 @@ const Signup = ({ onAuthSuccess = () => {} }) => {

try {
const normalizedUsername = normalizeUsername(form.username);
const rawDigits = form.phone.replace(/\D/g, '');
const canonicalPhone = form.phone.startsWith('+') ? form.phone : `${countryCode}${rawDigits}`;

const payload = {
username: normalizedUsername,
name: form.name.trim(),
date_of_birth: form.dateOfBirth,
gender: form.gender,
phone: form.phone,
phone: canonicalPhone,
password: form.password,
};

Expand Down Expand Up @@ -237,20 +241,37 @@ const Signup = ({ onAuthSuccess = () => {} }) => {
</div>

<div>
<label className="block text-sm font-medium mb-1">{t('auth:phone')} *</label>
<input
type="tel"
className={`w-full border rounded px-3 py-2 ${
touched.phone && !phoneValid ? 'border-red-500' : 'border-gray-300'
}`}
name="phone"
value={form.phone}
onChange={onChange}
onBlur={onBlur}
placeholder={t('auth:phonePlaceholder', '10-15 digit phone number')}
autoComplete="tel"
inputMode="numeric"
/>
<label className="block text-sm font-medium mb-1" htmlFor="phone">
{t('auth:phone')} *
</label>
<div className="flex gap-2">
<select
aria-label="Country Code"
value={countryCode}
onChange={(e) => setCountryCode(e.target.value)}
className="w-28 px-2 py-2 border rounded border-gray-300 bg-gray-50 text-sm focus:outline-none focus:ring-2 focus:ring-teal-500"
>
<option value="+1">+1 (US)</option>
<option value="+34">+34 (ES)</option>
<option value="+91">+91 (IN)</option>
<option value="+86">+86 (CN)</option>
<option value="+971">+971 (AE)</option>
</select>
<input
id="phone"
type="tel"
className={`flex-1 border rounded px-3 py-2 ${
touched.phone && !phoneValid ? 'border-red-500' : 'border-gray-300'
}`}
name="phone"
value={form.phone}
onChange={onChange}
onBlur={onBlur}
placeholder={t('auth:phonePlaceholder', '10-15 digit phone number')}
autoComplete="tel"
inputMode="numeric"
/>
</div>
{touched.phone && !phoneValid && (
<p className="text-xs text-red-600 mt-1">{t('auth:errors.phoneInvalid', 'Enter a valid 10-15 digit phone number')}</p>
)}
Expand Down
30 changes: 16 additions & 14 deletions frontend/src/pages/auth/Signup.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ describe('Signup', () => {
it('renders gender dropdown with options', () => {
renderSignup();

const genderSelect = screen.getByRole('combobox');
const genderSelect = document.querySelector('select[name="gender"]');
expect(genderSelect).toBeInTheDocument();
});
});
Expand Down Expand Up @@ -126,13 +126,14 @@ describe('Signup', () => {

describe('Form Submission', () => {
const fillValidForm = async (user) => {
await user.type(screen.getByLabelText(/username/i), 'john_doe');
await user.type(screen.getByLabelText(/full name/i), 'John Doe');
fireEvent.change(screen.getByLabelText(/date of birth/i), { target: { value: '2000-01-01' } });
await user.selectOptions(screen.getByRole('combobox'), 'male');
await user.type(screen.getByPlaceholderText(/phone number/i), '1234567890');
await user.type(screen.getByLabelText(/^password$/i), 'Password123');
await user.type(screen.getByLabelText(/confirm password/i), 'Password123');
await user.type(screen.getByLabelText(/username/i), 'john_doe');
await user.type(screen.getByLabelText(/full name/i), 'John Doe');
fireEvent.change(screen.getByLabelText(/date of birth/i), { target: { value: '2000-01-01' } });
const genderSelect = document.querySelector('select[name="gender"]');
await user.selectOptions(genderSelect, 'male');
await user.type(screen.getByPlaceholderText(/phone number/i), '1234567890');
await user.type(screen.getByLabelText(/^password$/i), 'Password123');
await user.type(screen.getByLabelText(/confirm password/i), 'Password123');
};

it('enables submit button when form is valid', async () => {
Expand All @@ -159,7 +160,7 @@ describe('Signup', () => {
name: 'John Doe',
date_of_birth: '2000-01-01',
gender: 'male',
phone: '1234567890',
phone: '+11234567890',
password: 'Password123',
});
});
Expand Down Expand Up @@ -207,10 +208,11 @@ describe('Signup', () => {
mockSignup.mockResolvedValueOnce({ username: 'john_doe' });
renderSignup();

await user.type(screen.getByLabelText(/username/i), 'john_doe');
await user.type(screen.getByLabelText(/full name/i), 'John Doe');
fireEvent.change(screen.getByLabelText(/date of birth/i), { target: { value: '2000-01-01' } });
await user.selectOptions(screen.getByRole('combobox'), 'male');
await user.type(screen.getByLabelText(/username/i), 'john_doe');
await user.type(screen.getByLabelText(/full name/i), 'John Doe');
fireEvent.change(screen.getByLabelText(/date of birth/i), { target: { value: '2000-01-01' } });
const genderSelect = document.querySelector('select[name="gender"]');
await user.selectOptions(genderSelect, 'male');

// Try to type letters - they should be stripped
const phoneInput = screen.getByPlaceholderText(/phone number/i);
Expand All @@ -223,7 +225,7 @@ describe('Signup', () => {
await waitFor(() => {
expect(mockSignup).toHaveBeenCalledWith(
expect.objectContaining({
phone: '1234567890',
phone: '+11234567890',
})
);
});
Expand Down