forked from Expensify/App
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExpensify.js
160 lines (138 loc) · 5.34 KB
/
Expensify.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
import React, {Component} from 'react';
import {View} from 'react-native';
import PropTypes from 'prop-types';
import Onyx, {withOnyx} from 'react-native-onyx';
import {recordCurrentlyViewedReportID, recordCurrentRoute} from './libs/actions/App';
import HomePage from './pages/home/HomePage';
import NotFoundPage from './pages/NotFound';
import SetPasswordPage from './pages/SetPasswordPage';
import SignInPage from './pages/signin/SignInPage';
import listenToStorageEvents from './libs/listenToStorageEvents';
import * as ActiveClientManager from './libs/ActiveClientManager';
import ONYXKEYS from './ONYXKEYS';
import styles from './styles/styles';
import Log from './libs/Log';
import {
Route,
Router,
Redirect,
Switch
} from './libs/Router';
import ROUTES from './ROUTES';
import PushNotification from './libs/Notification/PushNotification';
// Initialize the store when the app loads for the first time
Onyx.init({
keys: ONYXKEYS,
safeEvictionKeys: [ONYXKEYS.COLLECTION.REPORT_ACTIONS],
initialKeyStates: {
// Clear any loading and error messages so they do not appear on app startup
[ONYXKEYS.SESSION]: {loading: false, error: ''},
},
registerStorageEventListener: (onStorageEvent) => {
listenToStorageEvents(onStorageEvent);
},
});
Onyx.registerLogger(({level, message}) => {
if (level === 'alert') {
Log.alert(message, 0, {}, false);
} else {
Log.client(message);
}
});
const propTypes = {
/* Onyx Props */
// A route set by Onyx that we will redirect to if present. Always empty on app init.
redirectTo: PropTypes.string,
};
const defaultProps = {
redirectTo: '',
};
class Expensify extends Component {
constructor(props) {
super(props);
// Initialize this client as being an active client
ActiveClientManager.init();
this.removeLoadingState = this.removeLoadingState.bind(this);
this.state = {
isLoading: true,
authToken: null,
};
}
componentDidMount() {
Onyx.connect({
key: ONYXKEYS.SESSION,
callback: this.removeLoadingState,
});
}
componentDidUpdate(prevProps, prevState) {
if (this.state.accountID && this.state.accountID !== prevState.accountID) {
PushNotification.register(this.state.accountID);
}
}
/**
* When the authToken is updated, the app should remove the loading state and handle the authToken
*
* @param {Object} session
* @param {String} session.authToken
*/
removeLoadingState(session) {
this.setState({
authToken: session ? session.authToken : null,
accountID: session ? session.accountID : null,
isLoading: false,
});
}
render() {
// Until the authToken has been initialized from Onyx, display a blank page
if (this.state.isLoading) {
return (
<View style={styles.genericView} />
);
}
return (
<Router>
{/* If there is ever a property for redirecting, we do the redirect here */}
{/* Leave this as a ternary or else iOS throws an error about text not being wrapped in <Text> */}
{this.props.redirectTo ? <Redirect push to={this.props.redirectTo} /> : null}
<Route path="*" render={recordCurrentRoute} />
{/* We must record the currentlyViewedReportID when hitting the 404 page so */}
{/* that we do not try to redirect back to that report again */}
<Route path={[ROUTES.REPORT, ROUTES.NOT_FOUND]} exact render={recordCurrentlyViewedReportID} />
<Switch>
<Route
exact
path={ROUTES.ROOT}
render={() => (
this.state.authToken
? <Redirect to={ROUTES.HOME} />
: <Redirect to={ROUTES.SIGNIN} />
)}
/>
<Route path={[ROUTES.SET_PASSWORD]} component={SetPasswordPage} />
<Route path={[ROUTES.NOT_FOUND]} component={NotFoundPage} />
<Route path={[ROUTES.SIGNIN_WITH_EXITTO, ROUTES.SIGNIN]} component={SignInPage} />
<Route
path={[ROUTES.HOME, ROUTES.ROOT]}
render={match => (
// Need to do this for every page that the user needs to be logged in to access
this.state.authToken
? <HomePage match={match} />
: <Redirect to={ROUTES.SIGNIN} />
)}
/>
</Switch>
</Router>
);
}
}
Expensify.propTypes = propTypes;
Expensify.defaultProps = defaultProps;
export default withOnyx({
redirectTo: {
key: ONYXKEYS.APP_REDIRECT_TO,
// Prevent the prefilling of Onyx data or else the app will always redirect to what the last value was set to.
// This ends up in a situation where you go to a report, refresh the page, and then rather than seeing the
// report you are brought back to the root of the site (ie. "/").
initWithStoredValues: false,
},
})(Expensify);