|
| 1 | +import { useState } from 'react'; |
| 2 | +import { registerUser } from '../../../services/api'; |
| 3 | + |
| 4 | +const s = { |
| 5 | + label: { |
| 6 | + display: 'block', |
| 7 | + fontSize: '0.85rem', |
| 8 | + fontWeight: 800, |
| 9 | + color: 'var(--color-green)', |
| 10 | + fontFamily: 'var(--font-body)', |
| 11 | + marginBottom: '0.5rem', |
| 12 | + letterSpacing: '0.5px', |
| 13 | + textTransform: 'uppercase', |
| 14 | + }, |
| 15 | + input: { |
| 16 | + width: '100%', |
| 17 | + padding: '0.75rem 1rem', |
| 18 | + borderRadius: '10px', |
| 19 | + border: '2.5px solid var(--color-green)', |
| 20 | + fontSize: '1rem', |
| 21 | + fontFamily: 'var(--font-body)', |
| 22 | + background: 'var(--color-bg-primary)', |
| 23 | + color: 'var(--color-text-primary)', |
| 24 | + outline: 'none', |
| 25 | + boxSizing: 'border-box', |
| 26 | + transition: 'border-color 0.2s', |
| 27 | + }, |
| 28 | + select: { |
| 29 | + width: '100%', |
| 30 | + padding: '0.75rem 1rem', |
| 31 | + borderRadius: '10px', |
| 32 | + border: '2.5px solid var(--color-green)', |
| 33 | + fontSize: '1rem', |
| 34 | + fontFamily: 'var(--font-body)', |
| 35 | + background: 'var(--color-bg-primary)', |
| 36 | + color: 'var(--color-text-primary)', |
| 37 | + outline: 'none', |
| 38 | + boxSizing: 'border-box', |
| 39 | + transition: 'border-color 0.2s', |
| 40 | + }, |
| 41 | + successBar: { |
| 42 | + padding: '0.75rem 1rem', |
| 43 | + borderRadius: '10px', |
| 44 | + border: '2px solid #2e7d32', |
| 45 | + background: '#e8f5e9', |
| 46 | + color: '#2e7d32', |
| 47 | + fontWeight: 700, |
| 48 | + fontSize: '0.9rem', |
| 49 | + marginBottom: '1rem', |
| 50 | + }, |
| 51 | + errorBar: { |
| 52 | + padding: '0.75rem 1rem', |
| 53 | + borderRadius: '10px', |
| 54 | + border: '2px solid var(--color-red)', |
| 55 | + background: '#ffebee', |
| 56 | + color: 'var(--color-red)', |
| 57 | + fontWeight: 700, |
| 58 | + fontSize: '0.9rem', |
| 59 | + marginBottom: '1rem', |
| 60 | + }, |
| 61 | +}; |
| 62 | + |
| 63 | +const AddUserPanel = () => { |
| 64 | + const [formData, setFormData] = useState({ |
| 65 | + username: '', |
| 66 | + password: '', |
| 67 | + role: 'participant', |
| 68 | + }); |
| 69 | + const [error, setError] = useState(''); |
| 70 | + const [success, setSuccess] = useState(''); |
| 71 | + const [loading, setLoading] = useState(false); |
| 72 | + |
| 73 | + const handleSubmit = async (e) => { |
| 74 | + e.preventDefault(); |
| 75 | + setError(''); |
| 76 | + setSuccess(''); |
| 77 | + if (!formData.username || !formData.password) { |
| 78 | + setError('Username and password are required'); |
| 79 | + return; |
| 80 | + } |
| 81 | + setLoading(true); |
| 82 | + |
| 83 | + try { |
| 84 | + // registerUser handles the standard POST /users/register call from frontend |
| 85 | + // which now requires a Bearer token (supplied by interceptor) |
| 86 | + const response = await registerUser(formData); |
| 87 | + if (response.success) { |
| 88 | + setSuccess('✅ User account created successfully!'); |
| 89 | + setFormData({ ...formData, username: '', password: '' }); |
| 90 | + } else { |
| 91 | + setError(response.message || 'Failed to create user'); |
| 92 | + } |
| 93 | + } catch { |
| 94 | + setError('An error occurred while creating the user account'); |
| 95 | + } finally { |
| 96 | + setLoading(false); |
| 97 | + } |
| 98 | + }; |
| 99 | + |
| 100 | + return ( |
| 101 | + <div style={{ maxWidth: 720, margin: '0 auto' }}> |
| 102 | + {/* Section heading */} |
| 103 | + <div style={{ display: 'flex', alignItems: 'center', gap: 10, marginBottom: '1.5rem' }}> |
| 104 | + <svg width="28" height="28" viewBox="0 0 28 28" fill="none"> |
| 105 | + <rect x="2" y="2" width="24" height="24" rx="6" fill="var(--color-bg-secondary)" stroke="var(--color-green)" strokeWidth="2.5" /> |
| 106 | + <path d="M14 13C16.2 13 18 11.2 18 9C18 6.8 16.2 5 14 5C11.8 5 10 6.8 10 9C10 11.2 11.8 13 14 13ZM14 15C11 15 5 16.5 5 19.5V21H23V19.5C23 16.5 17 15 14 15Z" fill="var(--color-green)" /> |
| 107 | + </svg> |
| 108 | + <h2 style={{ fontFamily: 'var(--font-heading)', fontSize: '1.5rem', color: 'var(--color-green)', margin: 0 }}> |
| 109 | + Create New Participant / Admin |
| 110 | + </h2> |
| 111 | + </div> |
| 112 | + |
| 113 | + <div style={{ background: '#fff', padding: '2rem', borderRadius: '16px', boxShadow: '0 4px 6px rgba(0,0,0,0.05)' }}> |
| 114 | + {error && <div style={s.errorBar}>{error}</div>} |
| 115 | + {success && <div style={s.successBar}>{success}</div>} |
| 116 | + |
| 117 | + <form onSubmit={handleSubmit} style={{ display: 'flex', flexDirection: 'column', gap: '1.5rem' }}> |
| 118 | + |
| 119 | + <div style={{ display: 'grid', gridTemplateColumns: '1fr 1fr', gap: '1.5rem' }}> |
| 120 | + <div> |
| 121 | + <label style={s.label}>Username</label> |
| 122 | + <input |
| 123 | + type="text" |
| 124 | + value={formData.username} |
| 125 | + onChange={e => setFormData({ ...formData, username: e.target.value })} |
| 126 | + placeholder="e.g. thecluemafia" |
| 127 | + style={s.input} |
| 128 | + /> |
| 129 | + </div> |
| 130 | + |
| 131 | + <div> |
| 132 | + <label style={s.label}>Password</label> |
| 133 | + <input |
| 134 | + type="text" |
| 135 | + value={formData.password} |
| 136 | + onChange={e => setFormData({ ...formData, password: e.target.value })} |
| 137 | + placeholder="Passcode..." |
| 138 | + style={s.input} |
| 139 | + /> |
| 140 | + </div> |
| 141 | + </div> |
| 142 | + |
| 143 | + <div> |
| 144 | + <label style={s.label}>Account Role</label> |
| 145 | + <select |
| 146 | + value={formData.role} |
| 147 | + onChange={e => setFormData({ ...formData, role: e.target.value })} |
| 148 | + style={s.select} |
| 149 | + > |
| 150 | + <option value="participant">Participant</option> |
| 151 | + <option value="admin">Admin</option> |
| 152 | + </select> |
| 153 | + </div> |
| 154 | + |
| 155 | + <div style={{ paddingTop: '1rem', borderTop: '2px dashed var(--color-bg-secondary)' }}> |
| 156 | + <button |
| 157 | + type="submit" |
| 158 | + disabled={loading} |
| 159 | + style={{ |
| 160 | + width: '100%', |
| 161 | + padding: '1rem', |
| 162 | + fontSize: '1.1rem', |
| 163 | + borderRadius: '12px', |
| 164 | + background: 'var(--color-red)', |
| 165 | + color: '#fff', |
| 166 | + fontWeight: 800, |
| 167 | + border: 'none', |
| 168 | + cursor: loading ? 'not-allowed' : 'pointer', |
| 169 | + opacity: loading ? 0.7 : 1, |
| 170 | + boxShadow: '4px 4px 0 var(--color-brown-dim)', |
| 171 | + transition: 'transform 0.1s', |
| 172 | + }} |
| 173 | + onMouseDown={e => { if (!loading) e.currentTarget.style.transform = 'translate(2px, 2px)' }} |
| 174 | + onMouseUp={e => { if (!loading) e.currentTarget.style.transform = 'none' }} |
| 175 | + onMouseLeave={e => { if (!loading) e.currentTarget.style.transform = 'none' }} |
| 176 | + > |
| 177 | + {loading ? 'CREATING...' : 'CREATE ACCOUNT'} |
| 178 | + </button> |
| 179 | + </div> |
| 180 | + </form> |
| 181 | + </div> |
| 182 | + </div> |
| 183 | + ); |
| 184 | +}; |
| 185 | + |
| 186 | +export default AddUserPanel; |
0 commit comments