-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
Copy pathFeedbackSurvey.tsx
126 lines (107 loc) · 4.76 KB
/
FeedbackSurvey.tsx
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
import React, {useState} from 'react';
import type {StyleProp, ViewStyle} from 'react-native';
import {View} from 'react-native';
import useLocalize from '@hooks/useLocalize';
import useTheme from '@hooks/useTheme';
import useThemeStyles from '@hooks/useThemeStyles';
import CONST from '@src/CONST';
import type {FeedbackSurveyOptionID} from '@src/CONST';
import type {TranslationPaths} from '@src/languages/types';
import FixedFooter from './FixedFooter';
import FormAlertWithSubmitButton from './FormAlertWithSubmitButton';
import SingleOptionSelector from './SingleOptionSelector';
import Text from './Text';
import TextInput from './TextInput';
type FeedbackSurveyProps = {
/** Title of the survey */
title: string;
/** Description of the survey */
description: string;
/** Callback to be called when the survey is submitted */
onSubmit: (reason: FeedbackSurveyOptionID, note?: string) => void;
/** Styles for the option row element */
optionRowStyles?: StyleProp<ViewStyle>;
/** Optional text to render over the submit button */
footerText?: React.ReactNode;
/** Indicates whether note field is required */
isNoteRequired?: boolean;
/** Indicates whether a loading indicator should be shown */
isLoading?: boolean;
};
type Option = {
key: FeedbackSurveyOptionID;
label: TranslationPaths;
};
const OPTIONS: Option[] = [
{key: CONST.FEEDBACK_SURVEY_OPTIONS.TOO_LIMITED.ID, label: CONST.FEEDBACK_SURVEY_OPTIONS.TOO_LIMITED.TRANSLATION_KEY},
{key: CONST.FEEDBACK_SURVEY_OPTIONS.TOO_EXPENSIVE.ID, label: CONST.FEEDBACK_SURVEY_OPTIONS.TOO_EXPENSIVE.TRANSLATION_KEY},
{key: CONST.FEEDBACK_SURVEY_OPTIONS.INADEQUATE_SUPPORT.ID, label: CONST.FEEDBACK_SURVEY_OPTIONS.INADEQUATE_SUPPORT.TRANSLATION_KEY},
{key: CONST.FEEDBACK_SURVEY_OPTIONS.BUSINESS_CLOSING.ID, label: CONST.FEEDBACK_SURVEY_OPTIONS.BUSINESS_CLOSING.TRANSLATION_KEY},
];
function FeedbackSurvey({title, description, onSubmit, optionRowStyles, footerText, isNoteRequired, isLoading}: FeedbackSurveyProps) {
const {translate} = useLocalize();
const styles = useThemeStyles();
const theme = useTheme();
const selectCircleStyles: StyleProp<ViewStyle> = {borderColor: theme.border};
const [reason, setReason] = useState<Option>();
const [note, setNote] = useState('');
const [shouldShowReasonError, setShouldShowReasonError] = useState(false);
const handleOptionSelect = (option: Option) => {
setShouldShowReasonError(false);
setReason(option);
};
const handleSubmit = () => {
if (!reason || (isNoteRequired && !note.trim())) {
setShouldShowReasonError(true);
return;
}
onSubmit(reason.key, note.trim());
};
const handleSetNote = (text: string) => {
setNote(text);
if (isNoteRequired && shouldShowReasonError) {
setShouldShowReasonError(false);
}
};
return (
<View style={[styles.flexGrow1, styles.justifyContentBetween]}>
<View style={styles.mh5}>
<Text style={styles.textHeadline}>{title}</Text>
<Text style={[styles.mt1, styles.mb3, styles.textNormalThemeText]}>{description}</Text>
<SingleOptionSelector
options={OPTIONS}
optionRowStyles={[styles.mb7, optionRowStyles]}
selectCircleStyles={selectCircleStyles}
selectedOptionKey={reason?.key}
onSelectOption={handleOptionSelect}
/>
{!!reason && (
<>
<Text style={[styles.textNormalThemeText, styles.mb3]}>{translate('feedbackSurvey.additionalInfoTitle')}</Text>
<TextInput
label={translate('feedbackSurvey.additionalInfoInputLabel')}
accessibilityLabel={translate('feedbackSurvey.additionalInfoInputLabel')}
role={CONST.ROLE.PRESENTATION}
onChangeText={handleSetNote}
value={note}
/>
</>
)}
</View>
<FixedFooter>
{!!footerText && footerText}
<FormAlertWithSubmitButton
isAlertVisible={shouldShowReasonError}
onSubmit={handleSubmit}
message={translate('common.error.pleaseCompleteForm')}
buttonText={translate('common.submit')}
enabledWhenOffline
containerStyles={styles.mt3}
isLoading={isLoading}
/>
</FixedFooter>
</View>
);
}
FeedbackSurvey.displayName = 'FeedbackSurvey';
export default FeedbackSurvey;