-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathWorkspacesListPage.js
executable file
·224 lines (203 loc) · 8.41 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
import React, {useMemo} from 'react';
import {ScrollView} from 'react-native';
import {withOnyx} from 'react-native-onyx';
import PropTypes from 'prop-types';
import _ from 'underscore';
import HeaderWithBackButton from '../../components/HeaderWithBackButton';
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 ReimbursementAccountProps from '../ReimbursementAccount/reimbursementAccountPropTypes';
import * as ReportUtils from '../../libs/ReportUtils';
import * as CurrencyUtils from '../../libs/CurrencyUtils';
import withPolicyAndFullscreenLoading from './withPolicyAndFullscreenLoading';
import * as App from '../../libs/actions/App';
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)),
}),
),
/** Bank account attached to free plan */
reimbursementAccount: ReimbursementAccountProps.reimbursementAccountPropTypes,
/** A collection of objects for all policies which key policy member objects by accountIDs */
allPolicyMembers: PropTypes.objectOf(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: {},
allPolicyMembers: {},
reimbursementAccount: {},
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');
}
function WorkspacesListPage({policies, allPolicyMembers, reimbursementAccount, userWallet, betas, network, translate}) {
/**
* @param {Boolean} isPaymentItem whether the item being rendered is the payments menu item
* @returns {Number} the user wallet balance
*/
function getWalletBalance(isPaymentItem) {
return isPaymentItem && Permissions.canUseWallet(betas) ? CurrencyUtils.convertToDisplayString(userWallet.currentBalance) : undefined;
}
/**
* Gets the menu item for each workspace
*
* @param {Object} item
* @param {Number} index
* @returns {JSX}
*/
function getMenuItem(item, index) {
const keyTitle = item.translationKey ? translate(item.translationKey) : item.title;
const isPaymentItem = item.translationKey === 'common.payments';
return (
<OfflineWithFeedback
key={`${keyTitle}_${index}`}
pendingAction={item.pendingAction}
errorRowStyles={styles.ph5}
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={getWalletBalance(isPaymentItem)}
fallbackIcon={item.fallbackIcon}
brickRoadIndicator={item.brickRoadIndicator}
disabled={item.disabled}
/>
</OfflineWithFeedback>
);
}
/**
* Add free policies (workspaces) to the list of menu items and returns the list of menu items
* @returns {Array} the menu item list
*/
const workspaces = useMemo(() => {
const reimbursementAccountBrickRoadIndicator = !_.isEmpty(reimbursementAccount.errors) ? CONST.BRICK_ROAD_INDICATOR_STATUS.ERROR : '';
return _.chain(policies)
.filter((policy) => PolicyUtils.shouldShowPolicy(policy, 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: reimbursementAccountBrickRoadIndicator || PolicyUtils.getPolicyBrickRoadIndicatorStatus(policy, allPolicyMembers),
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.toLowerCase())
.value();
}, [reimbursementAccount.errors, policies, network.isOffline, allPolicyMembers]);
return (
<ScreenWrapper>
<HeaderWithBackButton
title={translate('common.workspaces')}
onBackButtonPress={() => Navigation.goBack(ROUTES.SETTINGS)}
/>
{_.isEmpty(workspaces) ? (
<BlockingView
icon={Expensicons.Building}
title={translate('workspace.emptyWorkspace.title')}
subtitle={translate('workspace.emptyWorkspace.subtitle')}
/>
) : (
<ScrollView style={styles.flex1}>{_.map(workspaces, (item, index) => getMenuItem(item, index))}</ScrollView>
)}
<FixedFooter style={[styles.flexGrow0]}>
<Button
accessibilityLabel={translate('workspace.new.newWorkspace')}
success
text={translate('workspace.new.newWorkspace')}
onPress={() => App.createWorkspaceAndNavigateToIt()}
/>
</FixedFooter>
</ScreenWrapper>
);
}
WorkspacesListPage.propTypes = propTypes;
WorkspacesListPage.defaultProps = defaultProps;
export default compose(
withLocalize,
withPolicyAndFullscreenLoading,
withNetwork(),
withOnyx({
policies: {
key: ONYXKEYS.COLLECTION.POLICY,
},
allPolicyMembers: {
key: ONYXKEYS.COLLECTION.POLICY_MEMBERS,
},
reimbursementAccount: {
key: ONYXKEYS.REIMBURSEMENT_ACCOUNT,
},
userWallet: {
key: ONYXKEYS.USER_WALLET,
},
betas: {
key: ONYXKEYS.BETAS,
},
}),
)(WorkspacesListPage);