forked from Expensify/App
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWorkspaceCompanyCardsList.tsx
108 lines (99 loc) · 4.48 KB
/
WorkspaceCompanyCardsList.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
import React, {useCallback, useMemo} from 'react';
import type {ListRenderItemInfo} from 'react-native';
import {FlatList, View} from 'react-native';
import {useOnyx} from 'react-native-onyx';
import type {OnyxEntry} from 'react-native-onyx';
import OfflineWithFeedback from '@components/OfflineWithFeedback';
import {PressableWithFeedback} from '@components/Pressable';
import Text from '@components/Text';
import useLocalize from '@hooks/useLocalize';
import useThemeStyles from '@hooks/useThemeStyles';
import * as CardUtils from '@libs/CardUtils';
import Navigation from '@navigation/Navigation';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import type {Card, WorkspaceCardsList} from '@src/types/onyx';
import WorkspaceCompanyCardsFeedAddedEmptyPage from './WorkspaceCompanyCardsFeedAddedEmptyPage';
import WorkspaceCompanyCardsListRow from './WorkspaceCompanyCardsListRow';
type WorkspaceCompanyCardsListProps = {
/** List of company cards */
cardsList: OnyxEntry<WorkspaceCardsList>;
/** Current policy id */
policyID: string;
};
function WorkspaceCompanyCardsList({cardsList, policyID}: WorkspaceCompanyCardsListProps) {
const styles = useThemeStyles();
const {translate} = useLocalize();
const [personalDetails] = useOnyx(ONYXKEYS.PERSONAL_DETAILS_LIST);
const [customCardNames] = useOnyx(ONYXKEYS.NVP_EXPENSIFY_COMPANY_CARDS_CUSTOM_NAMES);
const sortedCards = useMemo(() => CardUtils.sortCardsByCardholderName(cardsList, personalDetails), [cardsList, personalDetails]);
const renderItem = useCallback(
({item, index}: ListRenderItemInfo<Card>) => {
const cardID = Object.keys(cardsList ?? {}).find((id) => cardsList?.[id].cardID === item.cardID);
const cardName = CardUtils.getCompanyCardNumber(cardsList?.cardList ?? {}, item.lastFourPAN);
const isCardDeleted = item.pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE;
return (
<OfflineWithFeedback
key={`${item.nameValuePairs?.cardTitle}_${index}`}
errorRowStyles={styles.ph5}
errors={item.errors}
pendingAction={item.pendingAction}
>
<PressableWithFeedback
role={CONST.ROLE.BUTTON}
style={[styles.mh5, styles.br3, styles.mb3, styles.highlightBG]}
accessibilityLabel="row"
hoverStyle={styles.hoveredComponentBG}
disabled={isCardDeleted}
onPress={() => {
if (!cardID || !item?.accountID) {
return;
}
Navigation.navigate(ROUTES.WORKSPACE_COMPANY_CARD_DETAILS.getRoute(policyID, cardID, item.bank));
}}
>
<WorkspaceCompanyCardsListRow
cardholder={personalDetails?.[item.accountID ?? '-1']}
cardNumber={CardUtils.maskCardNumber(cardName)}
name={customCardNames?.[item.cardID] ?? ''}
/>
</PressableWithFeedback>
</OfflineWithFeedback>
);
},
[cardsList, customCardNames, personalDetails, policyID, styles],
);
const renderListHeader = useCallback(
() => (
<View style={[styles.flexRow, styles.appBG, styles.justifyContentBetween, styles.mh5, styles.gap5, styles.p4]}>
<Text
numberOfLines={1}
style={[styles.textLabelSupporting, styles.lh16]}
>
{translate('common.name')}
</Text>
<Text
numberOfLines={1}
style={[styles.textLabelSupporting, styles.lh16]}
>
{translate('workspace.companyCards.cardNumber')}
</Text>
</View>
),
[styles, translate],
);
if (sortedCards.length === 0) {
return <WorkspaceCompanyCardsFeedAddedEmptyPage />;
}
return (
<FlatList
contentContainerStyle={styles.flexGrow1}
data={sortedCards}
renderItem={renderItem}
ListHeaderComponent={renderListHeader}
stickyHeaderIndices={[0]}
/>
);
}
export default WorkspaceCompanyCardsList;