Skip to content
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

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions src/pages/FormSubmission.tsx
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

View workflow job for this annotation

GitHub Actions / Run Type Checker

',' expected.

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

View workflow job for this annotation

GitHub Actions / Upload Bundle Stats - Production

',' expected.

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

View workflow job for this annotation

GitHub Actions / Upload Bundle Stats - Staging

',' expected.
displayErrorMessage('Failed to submit the form.')
}
}

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

View workflow job for this annotation

GitHub Actions / Run Type Checker

Declaration or statement expected.

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

View workflow job for this annotation

GitHub Actions / Upload Bundle Stats - Production

Declaration or statement expected.

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

View workflow job for this annotation

GitHub Actions / Upload Bundle Stats - Staging

Declaration or statement expected.

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)
}
Comment on lines +67 to +70
Copy link

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.

Suggested change
const form = document.querySelector('form')
if (form) {
form.addEventListener('submit', handleSubmit)
}
Y29uc3QgZm9ybVJlZiA9IFJlYWN0LnVzZVJlZigpOw0KDQp1c2VFZmZlY3QoKCkgPT4gew0KICBpZiAoaGFuZGxlU3VibWl0KSB7DQogICAgZm9ybVJlZi5jdXJyZW50LmFkZEV2ZW50TGlzdGVuZXIoJ3N1Ym1pdCcsIGhhbmRsZVN1Ym1pdCk7DQogIH0NCn0sIFtbZm9ybVJlZl0pOw0KDQpyZXR1cm4gKA0KICA8Zm9ybSByZWY9e2Zvcm1SZWZ9Pg0KICAgIC4uLg0KICA8L2Zvcm0+DQopOw==

Loading