-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathWorkspaceCompanyCardsSettingsPage.tsx
133 lines (124 loc) · 6.94 KB
/
WorkspaceCompanyCardsSettingsPage.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
import React, {useMemo, useState} from 'react';
import {View} from 'react-native';
import {useOnyx} from 'react-native-onyx';
import ConfirmModal from '@components/ConfirmModal';
import HeaderWithBackButton from '@components/HeaderWithBackButton';
import * as Expensicons from '@components/Icon/Expensicons';
import MenuItem from '@components/MenuItem';
import MenuItemWithTopDescription from '@components/MenuItemWithTopDescription';
import ScreenWrapper from '@components/ScreenWrapper';
import ScrollView from '@components/ScrollView';
import Text from '@components/Text';
import useLocalize from '@hooks/useLocalize';
import usePolicy from '@hooks/usePolicy';
import useThemeStyles from '@hooks/useThemeStyles';
import * as CardUtils from '@libs/CardUtils';
import Navigation from '@libs/Navigation/Navigation';
import type {PlatformStackScreenProps} from '@libs/Navigation/PlatformStackNavigation/types';
import type {SettingsNavigatorParamList} from '@libs/Navigation/types';
import AccessOrNotFoundWrapper from '@pages/workspace/AccessOrNotFoundWrapper';
import ToggleSettingOptionRow from '@pages/workspace/workflows/ToggleSettingsOptionRow';
import * as CompanyCards from '@userActions/CompanyCards';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import type SCREENS from '@src/SCREENS';
import type {CompanyCardFeed} from '@src/types/onyx';
type WorkspaceCompanyCardsSettingsPageProps = PlatformStackScreenProps<SettingsNavigatorParamList, typeof SCREENS.WORKSPACE.COMPANY_CARDS_SETTINGS>;
function WorkspaceCompanyCardsSettingsPage({
route: {
params: {policyID},
},
}: WorkspaceCompanyCardsSettingsPageProps) {
const styles = useThemeStyles();
const {translate} = useLocalize();
const policy = usePolicy(policyID);
const workspaceAccountID = policy?.workspaceAccountID ?? CONST.DEFAULT_NUMBER_ID;
const [deleteCompanyCardConfirmModalVisible, setDeleteCompanyCardConfirmModalVisible] = useState(false);
const [cardFeeds] = useOnyx(`${ONYXKEYS.COLLECTION.SHARED_NVP_PRIVATE_DOMAIN_MEMBER}${workspaceAccountID}`);
const [lastSelectedFeed] = useOnyx(`${ONYXKEYS.COLLECTION.LAST_SELECTED_FEED}${policyID}`);
// eslint-disable-next-line react-compiler/react-compiler, react-hooks/exhaustive-deps -- we want to run the hook only once to escape unexpected feed change
const selectedFeed = useMemo(() => CardUtils.getSelectedFeed(lastSelectedFeed, cardFeeds), []);
const [cardsList] = useOnyx(`${ONYXKEYS.COLLECTION.WORKSPACE_CARDS_LIST}${workspaceAccountID}_${selectedFeed}`);
const feedName = CardUtils.getCustomOrFormattedFeedName(selectedFeed, cardFeeds?.settings?.companyCardNicknames);
const companyFeeds = CardUtils.getCompanyFeeds(cardFeeds);
const liabilityType = selectedFeed && companyFeeds[selectedFeed]?.liabilityType;
const isPersonal = liabilityType === CONST.COMPANY_CARDS.DELETE_TRANSACTIONS.ALLOW;
const navigateToChangeFeedName = () => {
Navigation.navigate(ROUTES.WORKSPACE_COMPANY_CARDS_SETTINGS_FEED_NAME.getRoute(policyID));
};
const deleteCompanyCardFeed = () => {
if (selectedFeed) {
const {cardList, ...cards} = cardsList ?? {};
const cardIDs = Object.keys(cards);
const feedToOpen = (Object.keys(companyFeeds) as CompanyCardFeed[])
.filter((feed) => feed !== selectedFeed && companyFeeds[feed]?.pendingAction !== CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE)
.at(0);
CompanyCards.deleteWorkspaceCompanyCardFeed(policyID, workspaceAccountID, selectedFeed, cardIDs, feedToOpen);
}
setDeleteCompanyCardConfirmModalVisible(false);
Navigation.setNavigationActionToMicrotaskQueue(Navigation.goBack);
};
const onToggleLiability = (isOn: boolean) => {
if (!selectedFeed) {
return;
}
CompanyCards.setWorkspaceCompanyCardTransactionLiability(
workspaceAccountID,
policyID,
selectedFeed,
isOn ? CONST.COMPANY_CARDS.DELETE_TRANSACTIONS.ALLOW : CONST.COMPANY_CARDS.DELETE_TRANSACTIONS.RESTRICT,
);
};
return (
<AccessOrNotFoundWrapper
policyID={policyID}
featureName={CONST.POLICY.MORE_FEATURES.ARE_COMPANY_CARDS_ENABLED}
>
<ScreenWrapper
testID={WorkspaceCompanyCardsSettingsPage.displayName}
style={styles.defaultModalContainer}
>
<ScrollView contentContainerStyle={styles.flexGrow1}>
<HeaderWithBackButton title={translate('common.settings')} />
<View style={styles.flex1}>
<MenuItemWithTopDescription
shouldShowRightIcon
title={feedName}
description={translate('workspace.moreFeatures.companyCards.cardFeedName')}
style={[styles.moneyRequestMenuItem]}
titleStyle={styles.flex1}
onPress={navigateToChangeFeedName}
/>
<View style={[styles.mv3, styles.mh5]}>
<ToggleSettingOptionRow
title={translate('workspace.moreFeatures.companyCards.personal')}
switchAccessibilityLabel={translate('workspace.moreFeatures.companyCards.personal')}
onToggle={onToggleLiability}
isActive={isPersonal}
/>
<Text style={[styles.mutedTextLabel, styles.mt2]}>{translate('workspace.moreFeatures.companyCards.setTransactionLiabilityDescription')}</Text>
</View>
<MenuItem
icon={Expensicons.Trashcan}
title={translate('workspace.moreFeatures.companyCards.removeCardFeed')}
onPress={() => setDeleteCompanyCardConfirmModalVisible(true)}
/>
</View>
<ConfirmModal
isVisible={deleteCompanyCardConfirmModalVisible}
onConfirm={deleteCompanyCardFeed}
onCancel={() => setDeleteCompanyCardConfirmModalVisible(false)}
title={feedName && translate('workspace.moreFeatures.companyCards.removeCardFeedTitle', {feedName})}
prompt={translate('workspace.moreFeatures.companyCards.removeCardFeedDescription')}
confirmText={translate('common.delete')}
cancelText={translate('common.cancel')}
danger
/>
</ScrollView>
</ScreenWrapper>
</AccessOrNotFoundWrapper>
);
}
WorkspaceCompanyCardsSettingsPage.displayName = 'WorkspaceCompanyCardsSettingsPage';
export default WorkspaceCompanyCardsSettingsPage;