-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathFeedbackForm.tsx
More file actions
184 lines (179 loc) · 5.36 KB
/
FeedbackForm.tsx
File metadata and controls
184 lines (179 loc) · 5.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import { useContext, ReactNode, useState } from 'react';
import {
ActionList,
ActionListGroup,
ActionListItem,
Alert,
Button,
Checkbox,
Form,
FormGroup,
TextArea,
Content,
TextInput,
ContentVariants,
ValidatedOptions
} from '@patternfly/react-core';
import { LocaleContext } from '../context/LocaleContext';
export interface FeedbackFormProps {
email?: string;
onCloseModal: () => void;
onSubmit: (email: string, textAreaValue: string, checked: boolean) => void;
onClickBack: () => void;
handleFeedbackError: () => void;
modalTitle: string;
modalDescription?: string | ReactNode;
textareaLabel?: string;
feedbackType: 'Feedback' | 'Bug' | '[Research Opportunities]';
checkboxDescription: string;
textAreaHidden?: boolean;
submitTitle: string;
}
export const FeedbackForm = ({
email,
onCloseModal,
onSubmit,
onClickBack,
handleFeedbackError,
modalTitle,
modalDescription,
textareaLabel,
feedbackType,
checkboxDescription,
textAreaHidden = false,
submitTitle
}: FeedbackFormProps) => {
const intl = useContext(LocaleContext);
const [currentEmail, setCurrentEmail] = useState(email ? email : '');
const [textAreaValue, setTextAreaValue] = useState('');
const [emailValid, setEmailValid] = useState(ValidatedOptions.default);
const [checked, setChecked] = useState(false);
async function handleModalSubmission() {
try {
const emailAddress = checked && currentEmail ? currentEmail : '';
await onSubmit(emailAddress, textAreaValue, checked);
} catch (err) {
handleFeedbackError();
}
}
const validateEmail = (email: string) =>
email
.toLowerCase()
.match(
/^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|.(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
);
const isSubmitButtonDisabled = () => {
if (feedbackType !== '[Research Opportunities]') {
if (textAreaValue.length > 1 && checked && validateEmail(currentEmail)) {
return false;
} else if (textAreaValue.length > 1 && !checked) {
return false;
}
return true;
} else {
if (checked && validateEmail(currentEmail)) {
return false;
} else {
return true;
}
}
};
return (
<div className="chr-c-feedback-content">
<div className="chr-c-feedback-heading">
<Content component={ContentVariants.h1}>{modalTitle}</Content>
{modalDescription}
</div>
<Form className="chr-c-feedback-content-main">
{textAreaHidden ? (
''
) : (
<FormGroup label={textareaLabel} fieldId="feedback-description-text">
<TextArea
value={textAreaValue}
onChange={(_event, value) => setTextAreaValue(value)}
className="chr-c-feedback-text-area"
name="feedback-description-text"
id="feedback-description-text"
/>
</FormGroup>
)}
<FormGroup className="pf-u-mt-20">
<Checkbox
id="feedback-checkbox"
isChecked={checked}
onChange={() => setChecked(!checked)}
label={intl.researchOpportunities}
description={checkboxDescription}
/>
</FormGroup>
</Form>
{checked ? (
<>
<div className="pf-u-font-family-heading-sans-serif chr-c-feedback-email">{intl.email}</div>
<TextInput
value={currentEmail}
onChange={(_event, value) => setCurrentEmail(value)}
validated={emailValid}
onBlur={() =>
!validateEmail(currentEmail)
? setEmailValid(ValidatedOptions.error)
: setEmailValid(ValidatedOptions.default)
}
id="textInput-basic-2"
type="email"
aria-label="Error state username example"
/>
{emailValid === ValidatedOptions.error ? (
<Alert variant="danger" isInline isPlain title="Email address is invalid." />
) : (
<></>
)}
</>
) : (
''
)}
<ActionList className="chr-c-feedback-footer">
<ActionListGroup>
<ActionListItem>
<Button
ouiaId="submit-feedback"
className="chr-c-feedback-footer-button"
key="confirm"
variant="primary"
// eslint-disable-next-line no-nested-ternary
isDisabled={isSubmitButtonDisabled()}
onClick={handleModalSubmission}
>
{submitTitle}
</Button>
</ActionListItem>
<ActionListItem>
<Button
ouiaId="back-feedback"
className="chr-c-feedback-footer-button"
key="back"
variant="secondary"
onClick={onClickBack}
>
{intl.back}
</Button>
</ActionListItem>
</ActionListGroup>
<ActionListGroup>
<ActionListItem>
<Button
ouiaId="cancel-feedback"
className="chr-c-feedback-footer-button"
key="cancel"
variant="link"
onClick={onCloseModal}
>
{intl.cancel}
</Button>
</ActionListItem>
</ActionListGroup>
</ActionList>
</div>
);
};