-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathOnboarding.tsx
More file actions
452 lines (418 loc) · 17.1 KB
/
Onboarding.tsx
File metadata and controls
452 lines (418 loc) · 17.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
import { useState } from 'react';
import { useNavigate } from 'react-router-dom';
import { Session } from '@supabase/supabase-js';
import { supabase } from '../lib/supabase';
import { Button } from './ui/Button';
import { z } from 'zod';
interface OnboardingProps {
session: Session;
}
const checkUsernameExists = async (username: string) => {
const { data, error } = await supabase
.from('profiles')
.select('username')
.eq('username', username);
if (error) {
console.error('Error checking username:', error);
return false;
}
return data.length > 0;
};
// Validation schema
const onboardingSchema = z.object({
name: z.string().trim().min(2, 'Name must be at least 2 characters').max(50, 'Name is too long'),
username: z.string().trim().min(3, 'Username must be at least 3 characters').max(10, 'Username is too long')
.refine(async (username) => {
const exists = await checkUsernameExists(username);
return !exists;
}, {
message: 'Username already exists'
}),
role: z.string().trim().min(2, 'Role must be at least 2 characters').max(50, 'Role is too long'),
bio: z.string().max(500, 'Bio must be less than 500 characters').optional(),
skills: z.string().max(200, 'Skills list is too long')
.refine(val => !val || val.split(',').every(skill => skill.trim().length > 0), {
message: 'Skills should be comma-separated values'
})
.optional(),
github: z.string().max(100)
.refine(val => !val || val.startsWith('https://github.com/'), {
message: 'GitHub URL must start with https://github.com/'
})
.optional(),
linkedin: z.string().max(100)
.refine(val => !val || val.startsWith('https://linkedin.com/in/') || val.startsWith('https://www.linkedin.com/in/'), {
message: 'LinkedIn URL must be a valid profile URL'
})
.optional(),
company: z.string().max(50, 'Company name is too long').optional(),
website: z.string().max(100)
.refine(val => !val || val.startsWith('http://') || val.startsWith('https://'), {
message: 'Website must start with http:// or https://'
})
.optional(),
is_student: z.boolean(),
is_employed: z.boolean(),
is_freelance: z.boolean(),
followers_count: z.number().default(0),
following_count: z.number().default(0),
});
type ValidationErrors = {
[K in keyof z.infer<typeof onboardingSchema>]?: string;
};
export function Onboarding({ session }: OnboardingProps) {
const navigate = useNavigate();
const [loading, setLoading] = useState(false);
const [error, setError] = useState<string | null>(null);
const [validationErrors, setValidationErrors] = useState<ValidationErrors>({});
const [formData, setFormData] = useState({
name: '',
username: '',
role: '',
bio: '',
skills: '',
github: '',
linkedin: '',
company: '',
website: '',
is_student: false,
is_employed: false,
is_freelance: false,
});
const handleInputChange = (
e: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>
) => {
const { name, value, type } = e.target;
setFormData(prev => ({
...prev,
[name]: type === 'checkbox' ? (e.target as HTMLInputElement).checked : value
}));
// Clear validation error for this field when user starts typing
if (validationErrors[name as keyof ValidationErrors]) {
setValidationErrors(prev => ({ ...prev, [name]: undefined }));
}
};
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();
setLoading(true);
setError(null);
setValidationErrors({});
try {
const validatedData = await onboardingSchema.parseAsync(formData);
console.log('Validated data:', validatedData);
// First check if profile exists
const { data: existingProfile, error: checkError } = await supabase
.from('profiles')
.select()
.eq('user_id', session.user.id)
.maybeSingle();
if (checkError) {
console.error('Error checking existing profile:', checkError);
throw checkError;
}
console.log('Existing profile:', existingProfile);
let updateError;
if (existingProfile) {
// Update existing profile
const updateData = {
...validatedData,
onboarding_completed: true,
updated_at: new Date().toISOString(),
};
console.log('Updating profile with:', updateData);
const { error } = await supabase
.from('profiles')
.update(updateData)
.eq('user_id', session.user.id);
updateError = error;
} else {
// Create new profile
const insertData = {
user_id: session.user.id,
...validatedData,
onboarding_completed: true,
followers_count: 0,
following_count: 0,
created_at: new Date().toISOString(),
updated_at: new Date().toISOString(),
};
console.log('Creating profile with:', insertData);
const { error } = await supabase
.from('profiles')
.insert([insertData]);
updateError = error;
}
if (updateError) {
console.error('Error updating/creating profile:', {
message: updateError.message,
details: updateError.details,
hint: updateError.hint,
code: updateError.code
});
throw updateError;
}
// Redirect to community page after successful onboarding
navigate('/community');
} catch (err) {
if (err instanceof z.ZodError) {
const errors: ValidationErrors = {};
err.errors.forEach(error => {
const path = error.path[0] as keyof ValidationErrors;
errors[path] = error.message;
});
setValidationErrors(errors);
console.error('Validation errors:', errors);
} else {
console.error('Error updating profile:', {
error: err,
formData,
session: session.user.id
});
setError('Failed to save profile. Please try again.');
}
} finally {
setLoading(false);
}
};
return (
<div className="min-h-screen bg-light-900 dark:bg-dark-900 py-12 px-4 sm:px-6 lg:px-8">
<div className="max-w-2xl mx-auto">
<div className="bg-white dark:bg-dark-800 rounded-lg shadow-lg px-6 py-8 transition-colors duration-200">
<div className="text-center mb-8">
<h1 className="text-3xl font-bold text-gray-900 dark:text-white">
Welcome to Coderplex!
</h1>
<p className="mt-2 text-gray-600 dark:text-gray-400">
Let's get to know you better. Please fill out the following information to get started.
</p>
</div>
<form onSubmit={handleSubmit} className="space-y-8">
{/* Required Fields Section */}
<div className="space-y-6 pb-6 border-b border-gray-200 dark:border-gray-700">
<h2 className="text-xl font-semibold text-gray-900 dark:text-white">
Required Information
</h2>
{/* Name Field */}
<div>
<label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">
Name <span className="text-red-500">*</span>
</label>
<input
type="text"
name="name"
value={formData.name}
onChange={handleInputChange}
className="w-full px-4 py-2 text-gray-900 dark:text-gray-100
bg-white dark:bg-dark-700 border border-gray-300 dark:border-gray-600
rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500
dark:focus:ring-blue-400 transition-colors duration-200"
required
/>
{validationErrors.name && (
<p className="mt-1 text-sm text-red-600 dark:text-red-400">{validationErrors.name}</p>
)}
</div>
{/* Username Field */}
<div>
<label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">
Username <span className="text-red-500">*</span>
</label>
<input
type="text"
name="username"
value={formData.username}
onChange={handleInputChange}
className="w-full px-4 py-2 text-gray-900 dark:text-gray-100
bg-white dark:bg-dark-700 border border-gray-300 dark:border-gray-600
rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500
dark:focus:ring-blue-400 transition-colors duration-200"
required
/>
{validationErrors.username && (
<p className="mt-1 text-sm text-red-600 dark:text-red-400">{validationErrors.username}</p>
)}
</div>
{/* Role Field */}
<div>
<label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">
Role <span className="text-red-500">*</span>
</label>
<input
type="text"
name="role"
value={formData.role}
onChange={handleInputChange}
placeholder="e.g., Frontend Developer, DevOps Engineer"
className="w-full px-4 py-2 text-gray-900 dark:text-gray-100
bg-white dark:bg-dark-700 border border-gray-300 dark:border-gray-600
rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500
dark:focus:ring-blue-400 transition-colors duration-200"
required
/>
{validationErrors.role && (
<p className="mt-1 text-sm text-red-600 dark:text-red-400">{validationErrors.role}</p>
)}
</div>
</div>
{/* Status Section */}
<div className="space-y-6 pb-6 border-b border-gray-200 dark:border-gray-700">
<h2 className="text-xl font-semibold text-gray-900 dark:text-white">
Current Status
</h2>
<div className="space-y-4">
<label className="flex items-center space-x-3 cursor-pointer">
<input
type="checkbox"
name="is_student"
checked={formData.is_student}
onChange={handleInputChange}
className="w-4 h-4 text-blue-600 border-gray-300 rounded
focus:ring-blue-500 dark:border-gray-600 dark:focus:ring-blue-400"
/>
<span className="text-gray-700 dark:text-gray-300">
I am currently a student
</span>
</label>
<label className="flex items-center space-x-3 cursor-pointer">
<input
type="checkbox"
name="is_employed"
checked={formData.is_employed}
onChange={handleInputChange}
className="w-4 h-4 text-blue-600 border-gray-300 rounded
focus:ring-blue-500 dark:border-gray-600 dark:focus:ring-blue-400"
/>
<span className="text-gray-700 dark:text-gray-300">
I am currently employed
</span>
</label>
<label className="flex items-center space-x-3 cursor-pointer">
<input
type="checkbox"
name="is_freelance"
checked={formData.is_freelance}
onChange={handleInputChange}
className="w-4 h-4 text-blue-600 border-gray-300 rounded
focus:ring-blue-500 dark:border-gray-600 dark:focus:ring-blue-400"
/>
<span className="text-gray-700 dark:text-gray-300">
I am open to freelance opportunities
</span>
</label>
</div>
</div>
{/* Optional Fields Section */}
<div className="space-y-6">
<h2 className="text-xl font-semibold text-gray-900 dark:text-white">
Additional Information (Optional)
</h2>
{/* Bio Field */}
<div>
<label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">
Bio
</label>
<textarea
name="bio"
value={formData.bio}
onChange={handleInputChange}
rows={3}
className="w-full px-4 py-2 text-gray-900 dark:text-gray-100
bg-white dark:bg-dark-700 border border-gray-300 dark:border-gray-600
rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500
dark:focus:ring-blue-400 transition-colors duration-200"
/>
</div>
{/* Skills Field */}
<div>
<label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">
Skills
</label>
<input
type="text"
name="skills"
value={formData.skills}
onChange={handleInputChange}
placeholder="e.g., JavaScript, React, Node.js"
className="w-full px-4 py-2 text-gray-900 dark:text-gray-100
bg-white dark:bg-dark-700 border border-gray-300 dark:border-gray-600
rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500
dark:focus:ring-blue-400 transition-colors duration-200"
/>
</div>
{/* Social Links */}
<div className="space-y-4">
<div>
<label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">
GitHub Profile
</label>
<input
type="url"
name="github"
value={formData.github}
onChange={handleInputChange}
placeholder="https://github.com/username"
className="w-full px-4 py-2 text-gray-900 dark:text-gray-100
bg-white dark:bg-dark-700 border border-gray-300 dark:border-gray-600
rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500
dark:focus:ring-blue-400 transition-colors duration-200"
/>
</div>
<div>
<label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">
LinkedIn Profile
</label>
<input
type="url"
name="linkedin"
value={formData.linkedin}
onChange={handleInputChange}
placeholder="https://linkedin.com/in/username"
className="w-full px-4 py-2 text-gray-900 dark:text-gray-100
bg-white dark:bg-dark-700 border border-gray-300 dark:border-gray-600
rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500
dark:focus:ring-blue-400 transition-colors duration-200"
/>
</div>
<div>
<label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">
Personal Website
</label>
<input
type="url"
name="website"
value={formData.website}
onChange={handleInputChange}
placeholder="https://example.com"
className="w-full px-4 py-2 text-gray-900 dark:text-gray-100
bg-white dark:bg-dark-700 border border-gray-300 dark:border-gray-600
rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500
dark:focus:ring-blue-400 transition-colors duration-200"
/>
</div>
</div>
</div>
{error && (
<div className="text-red-600 dark:text-red-400 text-sm mt-4">{error}</div>
)}
<div className="flex justify-end pt-4">
<Button
type="submit"
disabled={loading}
className="w-full sm:w-auto"
>
{loading ? (
<div className="flex items-center justify-center">
<div className="animate-spin rounded-full h-5 w-5 border-b-2 border-white"></div>
<span className="ml-2">Saving...</span>
</div>
) : (
'Complete Profile'
)}
</Button>
</div>
</form>
</div>
</div>
</div>
);
}