-
Notifications
You must be signed in to change notification settings - Fork 17
Form to Contribute Data #1669
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
Merged
Merged
Form to Contribute Data #1669
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
3c7391a
Modal done, some type of security for input needed
0c15769
Not working so far with google recaptcha yup attempt
e1ed16e
Updated recaptcha, not working on dev site. Added formik
3d3a450
Merge branch 'main' into task/WP-828
rstijerina 703ae19
Lat and Long input fix
928f3e8
Replaced formik+yup with antd+zod
42f9573
Fixed linting error
bdd1887
Use Google reCAPTCHA Enterprise in ContributeDataModal and backend
nathanfranklin 2f9919d
Fix client linting
nathanfranklin 39fb577
Removing change
nathanfranklin 5427ac7
Refactor to only show recapcha if not logged in
nathanfranklin 1202db7
Remove debugger statement
nathanfranklin f07e540
Merge pull request #1683 from DesignSafe-CI/task/WP-828---add-enterpr…
erikriv16 76b7396
Ticket going to main queue instead of feedback queue
bd96272
Removed unused packages
9fbc891
Merge branch 'main' into task/WP-828
sophia-massie f123cdd
Drop outdated comment
nathanfranklin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 @@ | ||
| export { DateInput } from './_fields'; |
This file contains hidden or 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 hidden or 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
276 changes: 276 additions & 0 deletions
276
client/modules/reconportal/src/ReconSidePanel/ContributeDataModal.tsx
This file contains hidden or 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,276 @@ | ||
| import React, { useState } from 'react'; | ||
| import { Button, Form, Input, Modal, Typography, notification } from 'antd'; | ||
| import { z } from 'zod'; | ||
| import ReCAPTCHA from 'react-google-recaptcha'; | ||
| import { useCreateFeedbackTicket, useAuthenticatedUser } from '@client/hooks'; | ||
| import { DateInput } from '@client/datafiles'; | ||
|
|
||
| const formSchema = z.object({ | ||
| name: z.string().min(1, 'Required'), | ||
| email: z.string().email('Invalid Email').min(1, 'Required'), | ||
| dateOfHazard: z.string().min(1, 'Required'), | ||
| eventTitle: z.string().min(1, 'Required'), | ||
| url: z.string().url('Invalid URL').min(1, 'Required'), | ||
| latitude: z.coerce | ||
| .number({ | ||
| required_error: 'Required', | ||
| invalid_type_error: 'Latitude must be a number', | ||
| }) | ||
| .min(-90, 'Latitude must be between -90 and 90') | ||
| .max(90, 'Latitude must be between -90 and 90'), | ||
| longitude: z.coerce | ||
| .number({ | ||
| required_error: 'Required', | ||
| invalid_type_error: 'Longitude must be a number', | ||
| }) | ||
| .min(-180, 'Longitude must be between -180 and 180') | ||
| .max(180, 'Longitude must be between -180 and 180'), | ||
| body: z.string().min(10, 'Description must be at least 10 characters'), | ||
| recaptchaResponse: z | ||
| .string() | ||
| .min(1, 'Please complete the reCAPTCHA') | ||
| .optional(), | ||
| }); | ||
|
|
||
| type FormValues = z.infer<typeof formSchema>; | ||
|
|
||
| export const ContributeDataModal: React.FC = () => { | ||
| const { user } = useAuthenticatedUser(); | ||
| const [isModalOpen, setIsModalOpen] = useState(false); | ||
| const [form] = Form.useForm<FormValues>(); | ||
| const { Link } = Typography; | ||
| const recaptchaSiteKey = | ||
| (window as any).__RECAPTCHA_ENTERPRISE_SITE_KEY || ''; | ||
| const isAuthenticated = !!user; | ||
|
|
||
| const showModal = () => setIsModalOpen(true); | ||
| const handleClose = () => { | ||
| form.resetFields(); | ||
| setIsModalOpen(false); | ||
| }; | ||
|
|
||
| const { mutate } = useCreateFeedbackTicket( | ||
| 'RECON-PORTAL', | ||
| 'Data Contribution for DesignSafe Recon Portal' | ||
| ); | ||
| const [notifApi, contextHolder] = notification.useNotification(); | ||
|
|
||
| const handleSubmit = (values: FormValues) => { | ||
| // Putting all extra fields in body so they're included in ticket, hook doesn't handle these extra fields | ||
| const formattedBody = ` | ||
| ${values.body} | ||
|
|
||
| --- Additional Information --- | ||
| Date of Hazard Event: ${values.dateOfHazard || ''} | ||
| Event Title: ${values.eventTitle} | ||
| URL to Data: ${values.url} | ||
| Latitude: ${values.latitude} | ||
| Longitude: ${values.longitude} | ||
| `.trim(); | ||
|
|
||
| mutate( | ||
| { | ||
| formData: { | ||
| name: values.name, | ||
| email: values.email, | ||
| body: formattedBody, | ||
| projectId: 'RECON-PORTAL', | ||
| title: 'Data Contribution', | ||
| ...(values.recaptchaResponse && { | ||
| recaptchaToken: values.recaptchaResponse, | ||
| }), // Only include recaptcha if present | ||
| }, | ||
| }, | ||
| { | ||
| onSuccess: () => { | ||
| form.resetFields(); | ||
| handleClose(); | ||
| notifApi.open({ | ||
| type: 'success', | ||
| message: '', | ||
| description: | ||
| 'Your data contribution was successfully submitted. Our team will contact you shortly to help load your data.', | ||
| placement: 'bottomLeft', | ||
| }); | ||
| }, | ||
| onError: () => { | ||
| notifApi.open({ | ||
| type: 'error', | ||
| message: 'Error', | ||
| description: 'Submission failed, please try again.', | ||
| placement: 'bottomLeft', | ||
| }); | ||
| }, | ||
| } | ||
| ); | ||
| }; | ||
|
|
||
| const validateField = (fieldName: keyof FormValues) => { | ||
| return async (_: any, value: any) => { | ||
| // Handle reCAPTCHA separately | ||
| if (fieldName === 'recaptchaResponse') { | ||
| if (isAuthenticated) { | ||
| return Promise.resolve(); // Skip for logged-in users | ||
| } | ||
| // Require for unauthenticated users | ||
| if (!value || value.trim() === '') { | ||
| return Promise.reject('Please complete the reCAPTCHA'); | ||
| } | ||
| return Promise.resolve(); | ||
| } | ||
|
|
||
| const fieldSchema = formSchema.shape[fieldName]; | ||
| try { | ||
| await fieldSchema.parseAsync(value); | ||
| } catch (error) { | ||
| if (error instanceof z.ZodError) { | ||
| return Promise.reject(error.errors[0]?.message); | ||
| } | ||
| return Promise.reject('Validation failed'); | ||
| } | ||
| }; | ||
| }; | ||
|
|
||
| return ( | ||
| <> | ||
| {contextHolder} | ||
| <Link onClick={showModal}>Email us to Contribute your Data</Link> | ||
| <Modal | ||
| destroyOnHidden | ||
| open={isModalOpen} | ||
| onCancel={handleClose} | ||
| width={900} | ||
| title={<h2>Contribute Your Data</h2>} | ||
| footer={null} | ||
| > | ||
| <Form | ||
| form={form} | ||
| layout="vertical" | ||
| onFinish={handleSubmit} | ||
| style={{ display: 'flex', flexDirection: 'column', gap: '1rem' }} | ||
| > | ||
| <Form.Item | ||
| label="Full Name" | ||
| name="name" | ||
| rules={[{ validator: validateField('name') }]} | ||
| initialValue={ | ||
| user ? `${user?.firstName} ${user?.lastName}` : undefined | ||
| } | ||
| required | ||
| > | ||
| <Input disabled={!!user} /> | ||
| </Form.Item> | ||
|
|
||
| <Form.Item | ||
| label="Email" | ||
| name="email" | ||
| rules={[{ validator: validateField('email') }]} | ||
| initialValue={user ? user.email : undefined} | ||
| required | ||
| > | ||
| <Input type="email" disabled={!!user} /> | ||
| </Form.Item> | ||
| <Form.Item | ||
| label="Date of Hazard Event" | ||
| name="dateOfHazard" | ||
| rules={[{ validator: validateField('dateOfHazard') }]} | ||
| required | ||
| > | ||
| <DateInput /> | ||
| </Form.Item> | ||
|
|
||
| <Form.Item | ||
| label="Event Title" | ||
| name="eventTitle" | ||
| rules={[{ validator: validateField('eventTitle') }]} | ||
| required | ||
| > | ||
| <Input /> | ||
| </Form.Item> | ||
|
|
||
| <Form.Item | ||
| label="URL to Data" | ||
| name="url" | ||
| rules={[{ validator: validateField('url') }]} | ||
| required | ||
| > | ||
| <Input /> | ||
| </Form.Item> | ||
|
|
||
| <Form.Item | ||
| label="Latitude" | ||
| name="latitude" | ||
| rules={[{ validator: validateField('latitude') }]} | ||
| required | ||
| > | ||
| <Input | ||
| type="number" | ||
| onChange={(e) => { | ||
| const num = Number(e.target.value); | ||
| form.setFieldValue( | ||
| 'latitude', | ||
| e.target.value === '' || isNaN(num) ? undefined : num | ||
| ); | ||
| }} | ||
| /> | ||
| </Form.Item> | ||
|
|
||
| <Form.Item | ||
| label="Longitude" | ||
| name="longitude" | ||
| rules={[{ validator: validateField('longitude') }]} | ||
| required | ||
| > | ||
| <Input | ||
| type="number" | ||
| onChange={(e) => { | ||
| const num = Number(e.target.value); | ||
| form.setFieldValue( | ||
| 'longitude', | ||
| e.target.value === '' || isNaN(num) ? undefined : num | ||
| ); | ||
| }} | ||
| /> | ||
| </Form.Item> | ||
|
|
||
| <Form.Item | ||
| label="Brief Description" | ||
| name="body" | ||
| rules={[{ validator: validateField('body') }]} | ||
| required | ||
| > | ||
| <Input.TextArea autoSize={{ minRows: 4 }} /> | ||
| </Form.Item> | ||
|
|
||
| {!isAuthenticated && ( | ||
| <Form.Item | ||
| name="recaptchaResponse" | ||
| rules={[{ validator: validateField('recaptchaResponse') }]} | ||
| required | ||
| > | ||
| {recaptchaSiteKey ? ( | ||
| <ReCAPTCHA | ||
| sitekey={recaptchaSiteKey} | ||
| onChange={(value) => | ||
| form.setFieldValue('recaptchaResponse', value || '') | ||
| } | ||
| onExpired={() => form.setFieldValue('recaptchaResponse', '')} | ||
| /> | ||
| ) : ( | ||
| <div style={{ color: 'red' }}> | ||
| RECAPTCHA site key not set yet | ||
| </div> | ||
| )} | ||
| </Form.Item> | ||
| )} | ||
|
|
||
| <Form.Item> | ||
| <Button type="primary" style={{ float: 'right' }} htmlType="submit"> | ||
| Submit | ||
| </Button> | ||
| </Form.Item> | ||
| </Form> | ||
| </Modal> | ||
| </> | ||
| ); | ||
| }; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.