|
| 1 | +import React, { Component } from 'react'; |
| 2 | +import { Dimensions, AsyncStorage } from 'react-native'; |
| 3 | +import WebViewBridge from 'react-native-webview-bridge'; |
| 4 | +import { |
| 5 | + StyleProvider, |
| 6 | + Container, |
| 7 | + Text, |
| 8 | + Button, |
| 9 | + Footer, |
| 10 | + Toast |
| 11 | +} from 'native-base'; |
| 12 | +import { SourceCode } from 'NativeModules'; |
| 13 | +import PropTypes from 'prop-types'; |
| 14 | +import { bindActionCreators } from 'redux'; |
| 15 | +import { connect } from 'react-redux'; |
| 16 | +import moment from 'moment'; |
| 17 | +import _ from 'lodash'; |
| 18 | +import uuid from 'react-native-uuid'; |
| 19 | + |
| 20 | +function prepareForBridge(message) { |
| 21 | + //Replaces the singlequote character with symbol we can fix on the other side. |
| 22 | + return JSON.stringify(message).replace(/'/g, '__@@__'); |
| 23 | +} |
| 24 | + |
| 25 | +//Android/iOS specific file loading our html assets: |
| 26 | +let VIEW; |
| 27 | +const scriptURL = SourceCode.scriptURL; |
| 28 | +if (scriptURL.indexOf('file://') > -1) { |
| 29 | + const _bundleSourcePath = scriptURL.substring( |
| 30 | + 7, |
| 31 | + scriptURL.lastIndexOf('/') + 1 |
| 32 | + ); |
| 33 | + const name = require('./prodViewUri'); |
| 34 | + const fileURI = `${_bundleSourcePath}${name}`; |
| 35 | + VIEW = { uri: fileURI }; |
| 36 | +} else { |
| 37 | + VIEW = require('../webviews/surveyIndex.html'); |
| 38 | +} |
| 39 | + |
| 40 | +export default class Survey extends Component { |
| 41 | + static propTypes = { |
| 42 | + navigation: PropTypes.object, |
| 43 | + actions: PropTypes.object, |
| 44 | + data: PropTypes.object.isRequired, |
| 45 | + errorMessage: PropTypes.string, |
| 46 | + surveyJSON: PropTypes.object.isRequired, |
| 47 | + title: PropTypes.string, |
| 48 | + onSurveyComplete: PropTypes.func, |
| 49 | + surveyResponseDateString: PropTypes.string.isRequired //Pass in existing response data if you want survey to show existing data. |
| 50 | + }; |
| 51 | + |
| 52 | + constructor(props) { |
| 53 | + super(props); |
| 54 | + this.onLoad = this.onLoad.bind(this); |
| 55 | + } |
| 56 | + |
| 57 | + onLoad() { |
| 58 | + //Replace \n with <br/> to keep survey library happy: |
| 59 | + var surveyString1 = JSON.stringify(this.props.surveyJSON) |
| 60 | + .split('\\n') |
| 61 | + .join('<br/>'); |
| 62 | + var surveyObj = JSON.parse(surveyString1); |
| 63 | + |
| 64 | + //Load response data for specified survey date if it exists: |
| 65 | + var surveyResponseObj = this.getResponseForDate( |
| 66 | + this.props.surveyResponseDateString |
| 67 | + ); |
| 68 | + |
| 69 | + setTimeout( |
| 70 | + () => |
| 71 | + this.refs.webviewbridge.sendToBridge( |
| 72 | + prepareForBridge({ |
| 73 | + action: 'LOAD', |
| 74 | + survey: surveyObj, |
| 75 | + surveydata: surveyResponseObj |
| 76 | + }) |
| 77 | + ), |
| 78 | + 500 |
| 79 | + ); |
| 80 | + } |
| 81 | + |
| 82 | + onBridgeMessage = async msg => { |
| 83 | + const message = JSON.parse(msg); |
| 84 | + |
| 85 | + if (message.action && message.action === 'COMPLETESURVEY') { |
| 86 | + var singleSurveyMap = {}; |
| 87 | + singleSurveyMap[this.props.title] = [message.surveydata]; |
| 88 | + message.surveydata.CompletionDate = moment().toISOString(); |
| 89 | + |
| 90 | + if (!message.surveydata.DataDate) { |
| 91 | + message.surveydata.DataDate = message.surveydata.CompletionDate; |
| 92 | + } |
| 93 | + try { |
| 94 | + const id = uuid.v1(); |
| 95 | + |
| 96 | + // TODO: use optional chaining propsal to check for nulls |
| 97 | + const key = `@Responses:${id}${(message.surveydata && |
| 98 | + message.surveydata.email) || |
| 99 | + ''}`; |
| 100 | + Toast.show({ |
| 101 | + text: `before set, ${key}`, |
| 102 | + buttonText: 'Okay' |
| 103 | + }); |
| 104 | + await AsyncStorage.setItem( |
| 105 | + key, |
| 106 | + JSON.stringify({ ...message.surveydata, id }) |
| 107 | + ); |
| 108 | + Toast.show({ |
| 109 | + text: `after set`, |
| 110 | + buttonText: 'Okay' |
| 111 | + }); |
| 112 | + const storedResponses = await AsyncStorage.getItem(key); |
| 113 | + |
| 114 | + if (storedResponses) { |
| 115 | + const responsesJSON = JSON.parse(storedResponses); |
| 116 | + Toast.show({ |
| 117 | + text: `Successfully stored responsn with id ${ |
| 118 | + responsesJSON.id |
| 119 | + } and user email ${responsesJSON.email}`, |
| 120 | + buttonText: 'Okay' |
| 121 | + }); |
| 122 | + } |
| 123 | + } catch (error) { |
| 124 | + throw new Error('Error storing responses', error); |
| 125 | + } |
| 126 | + //TODO: Exercise left to the reader :) |
| 127 | + } |
| 128 | + }; |
| 129 | + |
| 130 | + //Get saved survey response from user data if it exists already and survey versions are the same. |
| 131 | + getResponseForDate(dateString) { |
| 132 | + //TODO: If you want the survey to start auto filled out, a SurveyJS JSON response can be placed here. |
| 133 | + |
| 134 | + return {}; |
| 135 | + } |
| 136 | + |
| 137 | + render() { |
| 138 | + const { height: screenHeight } = Dimensions.get('window'); |
| 139 | + |
| 140 | + return ( |
| 141 | + <WebViewBridge |
| 142 | + ref="webviewbridge" |
| 143 | + style={{ flex: 1, height: 500, justifyContent: 'center' }} |
| 144 | + source={VIEW} |
| 145 | + scalesPageToFit={false} |
| 146 | + scrollEnabled |
| 147 | + javaScriptEnabled |
| 148 | + onLoad={this.onLoad} |
| 149 | + onBridgeMessage={this.onBridgeMessage} |
| 150 | + /> |
| 151 | + ); |
| 152 | + } |
| 153 | +} |
0 commit comments