-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bd32625
commit ac45566
Showing
3 changed files
with
262 additions
and
14 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
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,127 @@ | ||
import { useRef, useState } from 'react'; | ||
import ReCAPTCHA from 'react-google-recaptcha'; | ||
import styles from './cohorts.module.css'; | ||
|
||
class NotificationFormClass { | ||
name: string; | ||
email: string; | ||
token: string; | ||
|
||
constructor(name: string, email: string, token: string) { | ||
this.name = name; | ||
this.email = email; | ||
this.token = token; | ||
} | ||
} | ||
|
||
type Message = { | ||
message: string; | ||
type: 'error' | 'success'; | ||
}; | ||
|
||
const localEnv = | ||
process.env.NODE_ENV === 'development' && | ||
process.env.NEXT_PUBLIC_APPWRITE_HASKEY === 'false'; | ||
|
||
const siteKey = process.env.NEXT_PUBLIC_RECAPTCHA_SITEKEY ?? ''; | ||
|
||
export default function NotificationForm() { | ||
const [formData, setFormData] = useState( | ||
new NotificationFormClass('', '', '') | ||
); | ||
const [message, setMessage] = useState<Message | null>(null); | ||
|
||
const captchaRef = useRef<ReCAPTCHA>(null); | ||
|
||
const handleChange = (event: any) => { | ||
const { name, value } = event.target; | ||
setFormData({ ...formData, [name]: value }); | ||
}; | ||
|
||
const handleSubmit = async (event: any) => { | ||
event.preventDefault(); | ||
const token = localEnv ? 'localEnv' : captchaRef.current?.getValue(); | ||
if (!(token || localEnv)) { | ||
console.log('display robot message'); | ||
setMessage({ | ||
message: 'Are you a robot? Please complete the reCAPTCHA', | ||
type: 'error', | ||
}); | ||
return; | ||
} | ||
|
||
formData.token = token ?? ''; | ||
|
||
try { | ||
const responese = await fetch('/api/notificationForm', { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify(formData), | ||
}); | ||
|
||
if (!responese.ok) { | ||
setMessage({ | ||
message: responese.statusText, | ||
type: 'error', | ||
}); | ||
throw new Error(`Failed to submit form: ${responese.statusText}`); | ||
} | ||
|
||
setMessage({ | ||
message: 'Success! You will be notified when the next cohort opens.', | ||
type: 'success', | ||
}); | ||
|
||
setFormData(new NotificationFormClass('', '', '')); | ||
} catch (error) { | ||
console.error('error', error); | ||
} | ||
}; | ||
|
||
return ( | ||
<div className={styles.notificationFormContainer}> | ||
<form onSubmit={handleSubmit} method='post'> | ||
<div className={styles.formGroup}> | ||
<label htmlFor='name'>Name</label> | ||
<input | ||
type='text' | ||
id='name' | ||
name='name' | ||
required | ||
value={formData.name} | ||
onChange={handleChange} | ||
/> | ||
</div> | ||
<div className={styles.formGroup}> | ||
<label htmlFor='email'>Email</label> | ||
<input | ||
type='email' | ||
id='email' | ||
name='email' | ||
required | ||
value={formData.email} | ||
onChange={handleChange} | ||
/> | ||
</div> | ||
<div className={styles.recaptcha}> | ||
{!localEnv && <ReCAPTCHA sitekey={siteKey} ref={captchaRef} />} | ||
</div> | ||
<button className='mdText' type='submit'> | ||
Get Notified | ||
</button> | ||
</form> | ||
<div | ||
className={[ | ||
styles.formMessage, | ||
message?.type === 'error' | ||
? styles.errorMessage | ||
: styles.successMessage, | ||
].join(' ')} | ||
> | ||
{message?.message} | ||
</div> | ||
</div> | ||
); | ||
} |
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