-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[launch week] Add submission form on the UI
- Loading branch information
Showing
1 changed file
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
interface CustomFormData { | ||
Check failure on line 1 in src/pages/FormSubmission.tsx GitHub Actions / Upload Bundle Stats - Production
Check failure on line 1 in src/pages/FormSubmission.tsx GitHub Actions / Run Type Checker
Check failure on line 1 in src/pages/FormSubmission.tsx GitHub Actions / Upload Bundle Stats - Staging
Check failure on line 1 in src/pages/FormSubmission.tsx GitHub Actions / Build App
|
||
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) | ||
|
||
try { | ||
submitForm(formData) | ||
if (errors.length > 0) { | ||
displayErrors(errors) | ||
} else { | ||
displaySuccessMessage('Form submitted successfully!') | ||
} | ||
} catch (error) { | ||
displayErrorMessage('Failed to submit the form.') | ||
} | ||
} | ||
|
||
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) | ||
} |