-
Notifications
You must be signed in to change notification settings - Fork 22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[launch week] Add submission form on the UI #3498
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
interface CustomFormData { | ||
name: string | ||
email: string | ||
phoneNumber: string | ||
} | ||
|
||
const handleSubmit = (event: Event): void => { | ||
event.preventDefault() | ||
|
||
const form = event.target as HTMLFormElement | ||
const formData: CustomFormData = { | ||
name: (form.elements.namedItem('name') as HTMLInputElement).value, | ||
email: (form.elements.namedItem('email') as HTMLInputElement).value, | ||
phoneNumber: (form.elements.namedItem('phone') as HTMLInputElement).value, | ||
} | ||
|
||
const errors = validateFormData(formData) | ||
|
||
if (errors.length > 0) { | ||
displayErrors(errors) | ||
} else { | ||
submitForm(formData) | ||
displaySuccessMessage('Form submitted successfully!') | ||
} | ||
} catch (error) { | ||
Check failure on line 25 in src/pages/FormSubmission.tsx GitHub Actions / Run Type Checker
Check failure on line 25 in src/pages/FormSubmission.tsx GitHub Actions / Upload Bundle Stats - Production
|
||
displayErrorMessage('Failed to submit the form.') | ||
} | ||
} | ||
Check failure on line 28 in src/pages/FormSubmission.tsx GitHub Actions / Run Type Checker
Check failure on line 28 in src/pages/FormSubmission.tsx GitHub Actions / Upload Bundle Stats - Production
|
||
|
||
const validateFormData = (data: CustomFormData): string[] => { | ||
const errors: string[] = [] | ||
|
||
if (!data.name.trim()) { | ||
errors.push('Name is required.') | ||
} | ||
|
||
if (!data.email.trim()) { | ||
errors.push('Email is required.') | ||
} else if (!validateEmail(data.email)) { | ||
errors.push('Email is invalid.') | ||
} | ||
|
||
return errors | ||
} | ||
|
||
const validateEmail = (email: string): boolean => { | ||
const emailPattern = /^[^\s@]+@[^\s@]+\.[^\s@]+$/ | ||
return emailPattern.test(email) | ||
} | ||
|
||
const submitForm = (data: CustomFormData): void => { | ||
console.log('Submitting form:', data) | ||
} | ||
|
||
const displayErrors = (errors: string[]): void => { | ||
console.error('Form errors:', errors) | ||
} | ||
|
||
const displaySuccessMessage = (message: string): void => { | ||
console.log(message) | ||
} | ||
|
||
const displayErrorMessage = (message: string): void => { | ||
console.error(message) | ||
} | ||
|
||
const form = document.querySelector('form') | ||
if (form) { | ||
form.addEventListener('submit', handleSubmit) | ||
} | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using
document.querySelector
directly in a React component is not recommended. Instead, use a React ref to access the form element.