-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
WorkspacesListPage.js
executable file
·227 lines (205 loc) · 8.07 KB
/
WorkspacesListPage.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import React, {Component} from 'react';
import {ScrollView} from 'react-native';
import {withOnyx} from 'react-native-onyx';
import PropTypes from 'prop-types';
import _ from 'underscore';
import HeaderWithCloseButton from '../../components/HeaderWithCloseButton';
import Navigation from '../../libs/Navigation/Navigation';
import ScreenWrapper from '../../components/ScreenWrapper';
import ROUTES from '../../ROUTES';
import ONYXKEYS from '../../ONYXKEYS';
import CONST from '../../CONST';
import styles from '../../styles/styles';
import compose from '../../libs/compose';
import OfflineWithFeedback from '../../components/OfflineWithFeedback';
import withLocalize, {withLocalizePropTypes} from '../../components/withLocalize';
import * as Expensicons from '../../components/Icon/Expensicons';
import themeColors from '../../styles/themes/default';
import * as PolicyUtils from '../../libs/PolicyUtils';
import MenuItem from '../../components/MenuItem';
import * as Policy from '../../libs/actions/Policy';
import policyMemberPropType from '../policyMemberPropType';
import Permissions from '../../libs/Permissions';
import Button from '../../components/Button';
import FixedFooter from '../../components/FixedFooter';
import BlockingView from '../../components/BlockingViews/BlockingView';
import {withNetwork} from '../../components/OnyxProvider';
import * as ReportUtils from '../../libs/ReportUtils';
const propTypes = {
/* Onyx Props */
/** The list of this user's policies */
policies: PropTypes.objectOf(PropTypes.shape({
/** The ID of the policy */
ID: PropTypes.string,
/** The name of the policy */
name: PropTypes.string,
/** The type of the policy */
type: PropTypes.string,
/** The user's role in the policy */
role: PropTypes.string,
/** The current action that is waiting to happen on the policy */
pendingAction: PropTypes.oneOf(_.values(CONST.RED_BRICK_ROAD_PENDING_ACTION)),
})),
/** List of policy members */
policyMembers: PropTypes.objectOf(policyMemberPropType),
/** The user's wallet account */
userWallet: PropTypes.shape({
/** The user's current wallet balance */
currentBalance: PropTypes.number,
}),
/** List of betas available to current user */
betas: PropTypes.arrayOf(PropTypes.string),
...withLocalizePropTypes,
};
const defaultProps = {
policies: {},
policyMembers: {},
userWallet: {
currentBalance: 0,
},
betas: [],
};
/**
* Dismisses the errors on one item
*
* @param {string} policyID
* @param {string} pendingAction
*/
function dismissWorkspaceError(policyID, pendingAction) {
if (pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE) {
Policy.clearDeleteWorkspaceError(policyID);
return;
}
if (pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD) {
Policy.removeWorkspace(policyID);
return;
}
throw new Error('Not implemented');
}
class WorkspacesListPage extends Component {
constructor(props) {
super(props);
this.getWalletBalance = this.getWalletBalance.bind(this);
this.getWorkspaces = this.getWorkspaces.bind(this);
this.getMenuItem = this.getMenuItem.bind(this);
}
/**
* @param {Boolean} isPaymentItem whether the item being rendered is the payments menu item
* @returns {Number} the user wallet balance
*/
getWalletBalance(isPaymentItem) {
return (isPaymentItem && Permissions.canUseWallet(this.props.betas))
? this.props.numberFormat(
this.props.userWallet.currentBalance / 100, // Divide by 100 because balance is in cents
{style: 'currency', currency: 'USD'},
) : undefined;
}
/**
* Add free policies (workspaces) to the list of menu items and returns the list of menu items
* @returns {Array} the menu item list
*/
getWorkspaces() {
return _.chain(this.props.policies)
.filter(policy => PolicyUtils.shouldShowPolicy(policy, this.props.network.isOffline))
.map(policy => ({
title: policy.name,
icon: policy.avatar ? policy.avatar : ReportUtils.getDefaultWorkspaceAvatar(policy.name),
iconType: policy.avatar ? CONST.ICON_TYPE_AVATAR : CONST.ICON_TYPE_ICON,
action: () => Navigation.navigate(ROUTES.getWorkspaceInitialRoute(policy.id)),
iconFill: themeColors.textLight,
fallbackIcon: Expensicons.FallbackWorkspaceAvatar,
brickRoadIndicator: PolicyUtils.getPolicyBrickRoadIndicatorStatus(policy, this.props.policyMembers),
pendingAction: policy.pendingAction,
errors: policy.errors,
dismissError: () => dismissWorkspaceError(policy.id, policy.pendingAction),
disabled: policy.pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE,
}))
.sortBy(policy => policy.title)
.value();
}
/**
* Gets the menu item for each workspace
*
* @param {Object} item
* @param {Number} index
* @returns {JSX}
*/
getMenuItem(item, index) {
const keyTitle = item.translationKey ? this.props.translate(item.translationKey) : item.title;
const isPaymentItem = item.translationKey === 'common.payments';
return (
<OfflineWithFeedback
key={`${keyTitle}_${index}`}
pendingAction={item.pendingAction}
errorRowStyles={styles.offlineFeedback.menuItemErrorPadding}
onClose={item.dismissError}
errors={item.errors}
>
<MenuItem
title={keyTitle}
icon={item.icon}
iconType={CONST.ICON_TYPE_WORKSPACE}
onPress={item.action}
iconStyles={item.iconStyles}
iconFill={item.iconFill}
shouldShowRightIcon
badgeText={this.getWalletBalance(isPaymentItem)}
fallbackIcon={item.fallbackIcon}
brickRoadIndicator={item.brickRoadIndicator}
disabled={item.disabled}
/>
</OfflineWithFeedback>
);
}
render() {
const workspaces = this.getWorkspaces();
return (
<ScreenWrapper>
<HeaderWithCloseButton
title={this.props.translate('common.workspaces')}
shouldShowBackButton
onBackButtonPress={() => Navigation.navigate(ROUTES.SETTINGS)}
onCloseButtonPress={() => Navigation.dismissModal(true)}
/>
{_.isEmpty(workspaces) ? (
<BlockingView
icon={Expensicons.Building}
title={this.props.translate('workspace.emptyWorkspace.title')}
subtitle={this.props.translate('workspace.emptyWorkspace.subtitle')}
/>
) : (
<ScrollView style={styles.flex1}>
{_.map(workspaces, (item, index) => this.getMenuItem(item, index))}
</ScrollView>
)}
<FixedFooter style={[styles.flexGrow0]}>
<Button
success
text={this.props.translate('workspace.new.newWorkspace')}
onPress={() => Policy.createWorkspace()}
/>
</FixedFooter>
</ScreenWrapper>
);
}
}
WorkspacesListPage.propTypes = propTypes;
WorkspacesListPage.defaultProps = defaultProps;
export default compose(
withLocalize,
withNetwork(),
withOnyx({
policies: {
key: ONYXKEYS.COLLECTION.POLICY,
},
policyMembers: {
key: ONYXKEYS.COLLECTION.POLICY_MEMBER_LIST,
},
userWallet: {
key: ONYXKEYS.USER_WALLET,
},
betas: {
key: ONYXKEYS.BETAS,
},
}),
)(WorkspacesListPage);