-
Notifications
You must be signed in to change notification settings - Fork 3k
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
Showing
9 changed files
with
298 additions
and
90 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,170 @@ | ||
import _ from 'underscore'; | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { | ||
View, | ||
} from 'react-native'; | ||
import withLocalize, {withLocalizePropTypes} from './withLocalize'; | ||
import styles from '../styles/styles'; | ||
import * as BankAccounts from '../libs/actions/BankAccounts'; | ||
import CONST from '../CONST'; | ||
import Text from './Text'; | ||
import TextLink from './TextLink'; | ||
import FormScrollView from './FormScrollView'; | ||
import FormAlertWithSubmitButton from './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), | ||
})), | ||
}; | ||
|
||
const defaultProps = { | ||
questions: [], | ||
}; | ||
|
||
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: '', | ||
}; | ||
} | ||
|
||
/** | ||
* 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 Onfido 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 IQ 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}); | ||
return {answers}; | ||
} | ||
|
||
// 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] || {}; | ||
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]} key={question.type}> | ||
<View className="chooseText marginBottom30"> | ||
<strong>{question.prompt}</strong> | ||
</View> | ||
<View className="control-group"> | ||
<ul className="chooseList"> | ||
{_.map(question.answer, (answer, answerIndex) => { | ||
if (this.state.hideSkip && answer === SKIP_QUESTION_TEXT) { | ||
return; | ||
} | ||
|
||
return ( | ||
<li key={answer}> | ||
<input | ||
type="radio" | ||
name={`answer_${questionIndex}`} | ||
id={`answer_${questionIndex}_${answerIndex}`} | ||
onChange={() => this.chooseAnswer(questionIndex, answer)} | ||
/> | ||
<label htmlFor={`answer_${questionIndex}_${answerIndex}`} className="marginLeft"> | ||
{answer} | ||
</label> | ||
</li> | ||
); | ||
})} | ||
</ul> | ||
</View> | ||
</View> | ||
|
||
<FormAlertWithSubmitButton | ||
isAlertVisible={Boolean(this.state.errorMessage)} | ||
onSubmit={this.submitAnswers} | ||
onFixTheErrorsLinkPressed={() => { | ||
this.form.scrollTo({y: 0, animated: true}); | ||
}} | ||
message={this.state.errorMessage} | ||
buttonText={this.props.translate('common.saveAndContinue')} | ||
/> | ||
</FormScrollView> | ||
</View> | ||
); | ||
} | ||
} | ||
|
||
IdologyQuestions.propTypes = propTypes; | ||
IdologyQuestions.defaultProps = defaultProps; | ||
export default withLocalize(IdologyQuestions); |
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
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
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
Oops, something went wrong.