-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathPolicyDistanceRateDetailsPage.tsx
171 lines (158 loc) · 8.09 KB
/
PolicyDistanceRateDetailsPage.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
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
import type {StackScreenProps} from '@react-navigation/stack';
import React, {useState} from 'react';
import {View} from 'react-native';
import {withOnyx} from 'react-native-onyx';
import type {OnyxEntry} from 'react-native-onyx';
import ConfirmModal from '@components/ConfirmModal';
import HeaderWithBackButton from '@components/HeaderWithBackButton';
import * as Expensicons from '@components/Icon/Expensicons';
import MenuItemWithTopDescription from '@components/MenuItemWithTopDescription';
import OfflineWithFeedback from '@components/OfflineWithFeedback';
import ScreenWrapper from '@components/ScreenWrapper';
import Switch from '@components/Switch';
import Text from '@components/Text';
import useLocalize from '@hooks/useLocalize';
import useThemeStyles from '@hooks/useThemeStyles';
import useWindowDimensions from '@hooks/useWindowDimensions';
import * as CurrencyUtils from '@libs/CurrencyUtils';
import * as ErrorUtils from '@libs/ErrorUtils';
import Navigation from '@libs/Navigation/Navigation';
import type {SettingsNavigatorParamList} from '@navigation/types';
import AccessOrNotFoundWrapper from '@pages/workspace/AccessOrNotFoundWrapper';
import * as Policy from '@userActions/Policy';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import type SCREENS from '@src/SCREENS';
import type * as OnyxTypes from '@src/types/onyx';
import type {Rate} from '@src/types/onyx/Policy';
type PolicyDistanceRateDetailsPageOnyxProps = {
/** Policy details */
policy: OnyxEntry<OnyxTypes.Policy>;
};
type PolicyDistanceRateDetailsPageProps = PolicyDistanceRateDetailsPageOnyxProps & StackScreenProps<SettingsNavigatorParamList, typeof SCREENS.WORKSPACE.DISTANCE_RATE_DETAILS>;
function PolicyDistanceRateDetailsPage({policy, route}: PolicyDistanceRateDetailsPageProps) {
const styles = useThemeStyles();
const {translate} = useLocalize();
const {windowWidth} = useWindowDimensions();
const [isWarningModalVisible, setIsWarningModalVisible] = useState(false);
const [isDeleteModalVisible, setIsDeleteModalVisible] = useState(false);
const policyID = route.params.policyID;
const rateID = route.params.rateID;
const customUnits = policy?.customUnits ?? {};
const customUnit = customUnits[Object.keys(customUnits)[0]];
const rate = customUnit.rates[rateID];
const currency = rate.currency ?? CONST.CURRENCY.USD;
const canDeleteRate = Object.values(customUnit.rates).filter((distanceRate) => distanceRate.enabled).length > 1 || !rate.enabled;
const canDisableRate = Object.values(customUnit.rates).filter((distanceRate) => distanceRate.enabled).length > 1;
const errorFields = rate.errorFields;
const editRateValue = () => {
Navigation.navigate(ROUTES.WORKSPACE_DISTANCE_RATE_EDIT.getRoute(policyID, rateID));
};
const toggleRate = () => {
if (!rate.enabled || canDisableRate) {
Policy.setPolicyDistanceRatesEnabled(policyID, customUnit, [{...rate, enabled: !rate.enabled}]);
} else {
setIsWarningModalVisible(true);
}
};
const deleteRate = () => {
Navigation.goBack();
Policy.deletePolicyDistanceRates(policyID, customUnit, [rateID]);
setIsDeleteModalVisible(false);
};
const rateValueToDisplay = CurrencyUtils.convertAmountToDisplayString(rate.rate, currency);
const unitToDisplay = translate(`common.${customUnit?.attributes?.unit ?? CONST.CUSTOM_UNITS.DISTANCE_UNIT_MILES}`);
const threeDotsMenuItems = [
{
icon: Expensicons.Trashcan,
text: translate('workspace.distanceRates.deleteDistanceRate'),
onSelected: () => {
if (canDeleteRate) {
setIsDeleteModalVisible(true);
return;
}
setIsWarningModalVisible(true);
},
},
];
const clearErrorFields = (fieldName: keyof Rate) => {
Policy.clearPolicyDistanceRateErrorFields(policyID, customUnit.customUnitID, rateID, {...errorFields, [fieldName]: null});
};
return (
<AccessOrNotFoundWrapper
accessVariants={[CONST.POLICY.ACCESS_VARIANTS.ADMIN, CONST.POLICY.ACCESS_VARIANTS.PAID]}
policyID={policyID}
featureName={CONST.POLICY.MORE_FEATURES.ARE_DISTANCE_RATES_ENABLED}
>
<ScreenWrapper
testID={PolicyDistanceRateDetailsPage.displayName}
includeSafeAreaPaddingBottom={false}
style={[styles.defaultModalContainer]}
shouldShowOfflineIndicatorInWideScreen
>
<HeaderWithBackButton
title={`${rateValueToDisplay} / ${translate(`common.${customUnit?.attributes?.unit ?? CONST.CUSTOM_UNITS.DISTANCE_UNIT_MILES}`)}`}
shouldShowThreeDotsButton
threeDotsMenuItems={threeDotsMenuItems}
threeDotsAnchorPosition={styles.threeDotsPopoverOffset(windowWidth)}
/>
<View style={styles.flexGrow1}>
<OfflineWithFeedback
errors={ErrorUtils.getLatestErrorField(rate, 'enabled')}
pendingAction={rate?.pendingFields?.enabled}
errorRowStyles={styles.mh5}
onClose={() => clearErrorFields('enabled')}
>
<View style={[styles.flexRow, styles.justifyContentBetween, styles.p5]}>
<Text>{translate('workspace.distanceRates.enableRate')}</Text>
<Switch
isOn={rate.enabled ?? false}
onToggle={toggleRate}
accessibilityLabel={translate('workspace.distanceRates.enableRate')}
/>
</View>
</OfflineWithFeedback>
<OfflineWithFeedback
errors={ErrorUtils.getLatestErrorField(rate, 'rate')}
pendingAction={rate?.pendingFields?.rate ?? rate?.pendingFields?.currency}
errorRowStyles={styles.mh5}
onClose={() => clearErrorFields('rate')}
>
<MenuItemWithTopDescription
shouldShowRightIcon
title={`${rateValueToDisplay} / ${unitToDisplay}`}
description={translate('workspace.distanceRates.rate')}
descriptionTextStyle={styles.textNormal}
onPress={editRateValue}
/>
</OfflineWithFeedback>
<ConfirmModal
onConfirm={() => setIsWarningModalVisible(false)}
isVisible={isWarningModalVisible}
title={translate('workspace.distanceRates.oopsNotSoFast')}
prompt={translate('workspace.distanceRates.workspaceNeeds')}
confirmText={translate('common.buttonConfirm')}
shouldShowCancelButton={false}
/>
<ConfirmModal
title={translate('workspace.distanceRates.deleteDistanceRate')}
isVisible={isDeleteModalVisible}
onConfirm={deleteRate}
onCancel={() => setIsDeleteModalVisible(false)}
prompt={translate('workspace.distanceRates.areYouSureDelete', {count: 1})}
confirmText={translate('common.delete')}
cancelText={translate('common.cancel')}
danger
/>
</View>
</ScreenWrapper>
</AccessOrNotFoundWrapper>
);
}
PolicyDistanceRateDetailsPage.displayName = 'PolicyDistanceRateDetailsPage';
export default withOnyx<PolicyDistanceRateDetailsPageProps, PolicyDistanceRateDetailsPageOnyxProps>({
policy: {
key: ({route}) => `${ONYXKEYS.COLLECTION.POLICY}${route.params.policyID}`,
},
})(PolicyDistanceRateDetailsPage);