-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
PolicyDistanceRatesSettingsPage.tsx
181 lines (168 loc) · 9.67 KB
/
PolicyDistanceRatesSettingsPage.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
172
173
174
175
176
177
178
179
180
181
import type {StackScreenProps} from '@react-navigation/stack';
import React, {useState} from 'react';
import {View} from 'react-native';
import {useOnyx} from 'react-native-onyx';
import FullPageOfflineBlockingView from '@components/BlockingViews/FullPageOfflineBlockingView';
import HeaderWithBackButton from '@components/HeaderWithBackButton';
import OfflineWithFeedback from '@components/OfflineWithFeedback';
import ScreenWrapper from '@components/ScreenWrapper';
import ScrollView from '@components/ScrollView';
import type {ListItem} from '@components/SelectionList/types';
import Switch from '@components/Switch';
import Text from '@components/Text';
import TextLink from '@components/TextLink';
import type {UnitItemType} from '@components/UnitPicker';
import useLocalize from '@hooks/useLocalize';
import useThemeStyles from '@hooks/useThemeStyles';
import * as ErrorUtils from '@libs/ErrorUtils';
import Navigation from '@libs/Navigation/Navigation';
import * as OptionsListUtils from '@libs/OptionsListUtils';
import {getDistanceRateCustomUnit} from '@libs/PolicyUtils';
import type {SettingsNavigatorParamList} from '@navigation/types';
import AccessOrNotFoundWrapper from '@pages/workspace/AccessOrNotFoundWrapper';
import * as Category from '@userActions/Policy/Category';
import * as DistanceRate from '@userActions/Policy/DistanceRate';
import * as Policy from '@userActions/Policy/Policy';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import type SCREENS from '@src/SCREENS';
import type {CustomUnit} from '@src/types/onyx/Policy';
import CategorySelector from './CategorySelector';
import UnitSelector from './UnitSelector';
type PolicyDistanceRatesSettingsPageProps = StackScreenProps<SettingsNavigatorParamList, typeof SCREENS.WORKSPACE.DISTANCE_RATES_SETTINGS>;
function PolicyDistanceRatesSettingsPage({route}: PolicyDistanceRatesSettingsPageProps) {
const policyID = route.params.policyID;
const [policy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`);
const [policyCategories] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${policyID}`);
const styles = useThemeStyles();
const [isCategoryPickerVisible, setIsCategoryPickerVisible] = useState(false);
const {translate} = useLocalize();
const customUnit = getDistanceRateCustomUnit(policy);
const customUnitID = customUnit?.customUnitID ?? '';
const isDistanceTrackTaxEnabled = !!customUnit?.attributes?.taxEnabled;
const isPolicyTrackTaxEnabled = !!policy?.tax?.trackingEnabled;
const defaultCategory = customUnit?.defaultCategory;
const defaultUnit = customUnit?.attributes?.unit;
const errorFields = customUnit?.errorFields;
const FullPageBlockingView = !customUnit ? FullPageOfflineBlockingView : View;
const setNewUnit = (unit: UnitItemType) => {
if (!customUnit) {
return;
}
const attributes = {...customUnit?.attributes, unit: unit.value};
DistanceRate.setPolicyDistanceRatesUnit(policyID, customUnit, {...customUnit, attributes});
};
const setNewCategory = (category: ListItem) => {
if (!category.searchText || !customUnit) {
return;
}
Category.setPolicyDistanceRatesDefaultCategory(policyID, customUnit, {
...customUnit,
defaultCategory: defaultCategory === category.searchText ? '' : category.searchText,
});
};
const clearErrorFields = (fieldName: keyof CustomUnit) => {
DistanceRate.clearPolicyDistanceRatesErrorFields(policyID, customUnitID, {...errorFields, [fieldName]: null});
};
const onToggleTrackTax = (isOn: boolean) => {
if (!customUnit || !customUnit.attributes) {
return;
}
const attributes = {...customUnit?.attributes, taxEnabled: isOn};
Policy.enableDistanceRequestTax(policyID, customUnit?.name, customUnitID, attributes);
};
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
includeSafeAreaPaddingBottom={false}
style={[styles.defaultModalContainer]}
testID={PolicyDistanceRatesSettingsPage.displayName}
>
<HeaderWithBackButton title={translate('workspace.common.settings')} />
<FullPageBlockingView style={customUnit ? styles.flexGrow1 : []}>
<ScrollView
contentContainerStyle={styles.flexGrow1}
keyboardShouldPersistTaps="always"
>
<View>
{!!defaultUnit && (
<OfflineWithFeedback
errors={ErrorUtils.getLatestErrorField(customUnit ?? {}, 'attributes')}
pendingAction={customUnit?.pendingFields?.attributes}
errorRowStyles={styles.mh5}
onClose={() => clearErrorFields('attributes')}
>
<UnitSelector
label={translate('workspace.distanceRates.unit')}
defaultValue={defaultUnit}
wrapperStyle={[styles.ph5, styles.mt3]}
setNewUnit={setNewUnit}
/>
</OfflineWithFeedback>
)}
{!!policy?.areCategoriesEnabled && OptionsListUtils.hasEnabledOptions(policyCategories ?? {}) && (
<OfflineWithFeedback
errors={ErrorUtils.getLatestErrorField(customUnit ?? {}, 'defaultCategory')}
pendingAction={customUnit?.pendingFields?.defaultCategory}
errorRowStyles={styles.mh5}
onClose={() => clearErrorFields('defaultCategory')}
>
<CategorySelector
policyID={policyID}
label={translate('workspace.distanceRates.defaultCategory')}
defaultValue={defaultCategory}
wrapperStyle={[styles.ph5, styles.mt3]}
setNewCategory={setNewCategory}
isPickerVisible={isCategoryPickerVisible}
showPickerModal={() => setIsCategoryPickerVisible(true)}
hidePickerModal={() => setIsCategoryPickerVisible(false)}
/>
</OfflineWithFeedback>
)}
<OfflineWithFeedback
errors={ErrorUtils.getLatestErrorField(customUnit ?? {}, 'taxEnabled')}
errorRowStyles={styles.mh5}
pendingAction={customUnit?.pendingFields?.taxEnabled}
>
<View style={[styles.mt2, styles.mh5]}>
<View style={[styles.flexRow, styles.mb2, styles.mr2, styles.alignItemsCenter, styles.justifyContentBetween]}>
<Text style={[styles.textNormal, styles.colorMuted]}>{translate('workspace.distanceRates.trackTax')}</Text>
<Switch
isOn={isDistanceTrackTaxEnabled && isPolicyTrackTaxEnabled}
accessibilityLabel={translate('workspace.distanceRates.trackTax')}
onToggle={onToggleTrackTax}
disabled={!isPolicyTrackTaxEnabled}
/>
</View>
</View>
{!isPolicyTrackTaxEnabled && (
<View style={[styles.mh5]}>
<Text style={styles.colorMuted}>
{translate('workspace.distanceRates.taxFeatureNotEnabledMessage')}
<TextLink
onPress={() => {
Navigation.dismissModal();
Navigation.goBack(ROUTES.WORKSPACE_MORE_FEATURES.getRoute(policyID));
}}
>
{translate('workspace.common.moreFeatures')}
</TextLink>
{translate('workspace.distanceRates.changePromptMessage')}
</Text>
</View>
)}
</OfflineWithFeedback>
</View>
</ScrollView>
</FullPageBlockingView>
</ScreenWrapper>
</AccessOrNotFoundWrapper>
);
}
PolicyDistanceRatesSettingsPage.displayName = 'PolicyDistanceRatesSettingsPage';
export default PolicyDistanceRatesSettingsPage;