Skip to content

Commit

Permalink
[launch week] Add submission form on the UI
Browse files Browse the repository at this point in the history
  • Loading branch information
drguthals committed Nov 14, 2024
1 parent fe14916 commit ccfbfb7
Showing 1 changed file with 71 additions and 0 deletions.
71 changes: 71 additions & 0 deletions src/pages/FormSubmission.tsx
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

View workflow job for this annotation

GitHub Actions / Upload Bundle Stats - Production

'FormSubmission.tsx' cannot be compiled under '--isolatedModules' because it is considered a global script file. Add an import, export, or an empty 'export {}' statement to make it a module.

Check failure on line 1 in src/pages/FormSubmission.tsx

View workflow job for this annotation

GitHub Actions / Run Type Checker

'FormSubmission.tsx' cannot be compiled under '--isolatedModules' because it is considered a global script file. Add an import, export, or an empty 'export {}' statement to make it a module.

Check failure on line 1 in src/pages/FormSubmission.tsx

View workflow job for this annotation

GitHub Actions / Upload Bundle Stats - Staging

'FormSubmission.tsx' cannot be compiled under '--isolatedModules' because it is considered a global script file. Add an import, export, or an empty 'export {}' statement to make it a module.

Check failure on line 1 in src/pages/FormSubmission.tsx

View workflow job for this annotation

GitHub Actions / Build App

'FormSubmission.tsx' cannot be compiled under '--isolatedModules' because it is considered a global script file. Add an import, export, or an empty 'export {}' statement to make it a module.
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)
}

0 comments on commit ccfbfb7

Please sign in to comment.