-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
Copy pathIdologyQuestions.js
175 lines (152 loc) · 6.33 KB
/
IdologyQuestions.js
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
import _ from 'underscore';
import React from 'react';
import PropTypes from 'prop-types';
import {
View,
} from 'react-native';
import RadioButtons from '../../components/RadioButtons';
import withLocalize, {withLocalizePropTypes} from '../../components/withLocalize';
import styles from '../../styles/styles';
import * as BankAccounts from '../../libs/actions/BankAccounts';
import CONST from '../../CONST';
import Text from '../../components/Text';
import TextLink from '../../components/TextLink';
import FormScrollView from '../../components/FormScrollView';
import FormAlertWithSubmitButton from '../../components/FormAlertWithSubmitButton';
const MAX_SKIP = 1;
const SKIP_QUESTION_TEXT = 'Skip Question';
const propTypes = {
...withLocalizePropTypes,
/** Questions returned by Idology */
/** example: [{"answer":["1251","6253","113","None of the above","Skip Question"],"prompt":"Which number goes with your address on MASONIC AVE?","type":"street.number.b"}, ...] */
questions: PropTypes.arrayOf(PropTypes.shape({
prompt: PropTypes.string,
type: PropTypes.string,
answer: PropTypes.arrayOf(PropTypes.string),
})),
/** ID from Idology, referencing those questions */
idNumber: PropTypes.string,
};
const defaultProps = {
questions: [],
idNumber: '',
};
class IdologyQuestions extends React.Component {
constructor(props) {
super(props);
this.submitAnswers = this.submitAnswers.bind(this);
this.state = {
/** Current question index to display. */
questionNumber: 0,
/** Should we hide the "Skip question" answer? Yes if the user already skipped MAX_SKIP questions. */
hideSkip: false,
/** Answers from the user */
answers: [],
/** Any error message */
errorMessage: '',
/** Did the user just submitted all his answers? */
isLoading: false,
};
}
/**
* Put question answer in the state.
* @param {Number} questionIndex
* @param {String} answer
*/
chooseAnswer(questionIndex, answer) {
this.setState((prevState) => {
const answers = prevState.answers;
const question = this.props.questions[questionIndex];
answers[questionIndex] = {question: question.type, answer};
return {
answers,
errorMessage: '',
};
});
}
/**
* Show next question or send all answers for Idology verifications when we've answered enough
*/
submitAnswers() {
this.setState((prevState) => {
// User must pick an answer
if (!prevState.answers[prevState.questionNumber]) {
return {
errorMessage: this.props.translate('additionalDetailsStep.selectAnswer'),
};
}
// Get the number of questions that were skipped by the user.
const skippedQuestionsCount = _.filter(prevState.answers, answer => answer.answer === SKIP_QUESTION_TEXT).length;
// We have enough answers, let's call expectID KBA to verify them
if ((prevState.answers.length - skippedQuestionsCount) >= (this.props.questions.length - MAX_SKIP)) {
const answers = prevState.answers;
// Auto skip any remaining questions
if (answers.length < this.props.questions.length) {
for (let i = answers.length; i < this.props.questions.length; i++) {
answers[i] = {question: this.props.questions[i].type, answer: SKIP_QUESTION_TEXT};
}
}
BankAccounts.activateWallet(CONST.WALLET.STEP.ADDITIONAL_DETAILS, {
idologyAnswers: {
answers,
idNumber: this.props.idNumber,
},
});
return {answers, isLoading: true};
}
// Else, show next question
return {
questionNumber: prevState.questionNumber + 1,
hideSkip: skippedQuestionsCount >= MAX_SKIP,
};
});
}
render() {
const questionIndex = this.state.questionNumber;
const question = this.props.questions[questionIndex] || {};
const possibleAnswers = _.filter(_.map(question.answer, (answer) => {
if (this.state.hideSkip && answer === SKIP_QUESTION_TEXT) {
return;
}
return {
label: answer,
value: answer,
};
}));
return (
<View style={[styles.flex1]}>
<View style={[styles.ph5]}>
<Text style={styles.mb3}>{this.props.translate('additionalDetailsStep.helpTextIdologyQuestions')}</Text>
<TextLink
style={styles.mb3}
href="https://use.expensify.com/usa-patriot-act"
>
{this.props.translate('additionalDetailsStep.helpLink')}
</TextLink>
</View>
<FormScrollView ref={el => this.form = el}>
<View style={[styles.mh5, styles.mb5, styles.mt5]} key={question.type}>
<Text style={[styles.textStrong, styles.mb5]}>{question.prompt}</Text>
<RadioButtons
items={possibleAnswers}
onPress={answer => this.chooseAnswer(questionIndex, answer)}
/>
</View>
<FormAlertWithSubmitButton
isAlertVisible={Boolean(this.state.errorMessage)}
onSubmit={this.submitAnswers}
onFixTheErrorsLinkPressed={() => {
this.form.scrollTo({y: 0, animated: true});
}}
message={this.state.errorMessage}
isLoading={this.state.isLoading}
buttonText={this.props.translate('common.saveAndContinue')}
/>
</FormScrollView>
</View>
);
}
}
IdologyQuestions.propTypes = propTypes;
IdologyQuestions.defaultProps = defaultProps;
export default withLocalize(IdologyQuestions);