-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathWorkspaceUpgradePage.tsx
124 lines (112 loc) · 4.45 KB
/
WorkspaceUpgradePage.tsx
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
import {useNavigation} from '@react-navigation/native';
import type {StackScreenProps} from '@react-navigation/stack';
import React, {useCallback, useEffect} from 'react';
import {useOnyx} from 'react-native-onyx';
import HeaderWithBackButton from '@components/HeaderWithBackButton';
import ScreenWrapper from '@components/ScreenWrapper';
import useLocalize from '@hooks/useLocalize';
import useNetwork from '@hooks/useNetwork';
import useThemeStyles from '@hooks/useThemeStyles';
import Navigation from '@libs/Navigation/Navigation';
import type {SettingsNavigatorParamList} from '@libs/Navigation/types';
import * as PolicyUtils from '@libs/PolicyUtils';
import NotFoundPage from '@pages/ErrorPage/NotFoundPage';
import CONST from '@src/CONST';
import * as Policy from '@src/libs/actions/Policy/Policy';
import ROUTES from '@src/ROUTES';
import type SCREENS from '@src/SCREENS';
import UpgradeConfirmation from './UpgradeConfirmation';
import UpgradeIntro from './UpgradeIntro';
type WorkspaceUpgradePageProps = StackScreenProps<SettingsNavigatorParamList, typeof SCREENS.WORKSPACE.UPGRADE>;
function WorkspaceUpgradePage({route}: WorkspaceUpgradePageProps) {
const navigation = useNavigation();
const styles = useThemeStyles();
const policyID = route.params.policyID;
const feature = Object.values(CONST.UPGRADE_FEATURE_INTRO_MAPPING).find((f) => f.alias === route.params.featureName);
const {translate} = useLocalize();
const [policy] = useOnyx(`policy_${policyID}`);
const {isOffline} = useNetwork();
const canPerformUpgrade = !!feature && !!policy && PolicyUtils.isPolicyAdmin(policy);
const isUpgraded = React.useMemo(() => PolicyUtils.isControlPolicy(policy), [policy]);
const goBack = useCallback(() => {
if (!feature) {
return;
}
switch (feature.id) {
case CONST.UPGRADE_FEATURE_INTRO_MAPPING.reportFields.id:
case CONST.UPGRADE_FEATURE_INTRO_MAPPING.rules.id:
return Navigation.navigate(ROUTES.WORKSPACE_MORE_FEATURES.getRoute(policyID));
default:
return route.params.backTo ? Navigation.navigate(route.params.backTo) : Navigation.goBack();
}
}, [feature, policyID, route.params.backTo]);
const upgradeToCorporate = () => {
if (!canPerformUpgrade) {
return;
}
Policy.upgradeToCorporate(policy.id, feature.name);
};
const confirmUpgrade = useCallback(() => {
if (!feature) {
return;
}
switch (feature.id) {
case CONST.UPGRADE_FEATURE_INTRO_MAPPING.reportFields.id:
Policy.enablePolicyReportFields(policyID, true, true);
break;
case CONST.UPGRADE_FEATURE_INTRO_MAPPING.rules.id:
Policy.enablePolicyRules(policyID, true, true);
break;
default:
}
}, [feature, policyID]);
useEffect(() => {
const unsubscribeListener = navigation.addListener('blur', () => {
if (!isUpgraded || !canPerformUpgrade) {
return;
}
confirmUpgrade();
});
return unsubscribeListener;
}, [isUpgraded, canPerformUpgrade, confirmUpgrade, navigation]);
if (!canPerformUpgrade) {
return <NotFoundPage />;
}
return (
<ScreenWrapper
shouldShowOfflineIndicator
testID="workspaceUpgradePage"
offlineIndicatorStyle={styles.mtAuto}
>
<HeaderWithBackButton
title={translate('common.upgrade')}
onBackButtonPress={() => {
if (isUpgraded) {
Navigation.dismissModal();
} else {
Navigation.goBack();
}
goBack();
}}
/>
{isUpgraded && (
<UpgradeConfirmation
onConfirmUpgrade={() => {
Navigation.dismissModal();
goBack();
}}
policyName={policy.name}
/>
)}
{!isUpgraded && (
<UpgradeIntro
feature={feature}
onUpgrade={upgradeToCorporate}
buttonDisabled={isOffline}
loading={policy.isPendingUpgrade}
/>
)}
</ScreenWrapper>
);
}
export default WorkspaceUpgradePage;