-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
SearchFiltersCardPage.tsx
115 lines (104 loc) · 3.95 KB
/
SearchFiltersCardPage.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
import React, {useCallback, useMemo, useState} from 'react';
import {View} from 'react-native';
import {useOnyx} from 'react-native-onyx';
import Button from '@components/Button';
import HeaderWithBackButton from '@components/HeaderWithBackButton';
import getBankIcon from '@components/Icon/BankIcons';
import type {BankName} from '@components/Icon/BankIconsUtils';
import ScreenWrapper from '@components/ScreenWrapper';
import SelectionList from '@components/SelectionList';
import CardListItem from '@components/SelectionList/CardListItem';
import useLocalize from '@hooks/useLocalize';
import useThemeStyles from '@hooks/useThemeStyles';
import type {CategorySection} from '@libs/OptionsListUtils';
import type {OptionData} from '@libs/ReportUtils';
import Navigation from '@navigation/Navigation';
import * as SearchActions from '@userActions/Search';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
function SearchFiltersCardPage() {
const styles = useThemeStyles();
const {translate} = useLocalize();
const [cardList] = useOnyx(ONYXKEYS.CARD_LIST);
const [searchAdvancedFiltersForm] = useOnyx(ONYXKEYS.FORMS.SEARCH_ADVANCED_FILTERS_FORM);
const currentCards = searchAdvancedFiltersForm?.cardID;
const [newCards, setNewCards] = useState(currentCards ?? []);
const sections = useMemo(() => {
const newSections: CategorySection[] = [];
const cards = Object.values(cardList ?? {})
.sort((a, b) => a.bank.localeCompare(b.bank))
.map((card) => {
const icon = getBankIcon({bankName: card.bank as BankName, isCard: true, styles});
return {
text: card.bank,
keyForList: card.cardID.toString(),
isSelected: newCards.includes(card.cardID.toString()),
bankIcon: icon,
};
});
newSections.push({
title: undefined,
data: cards,
shouldShow: cards.length > 0,
});
return newSections;
}, [cardList, styles, newCards]);
const handleConfirmSelection = useCallback(() => {
SearchActions.updateAdvancedFilters({
cardID: newCards,
});
Navigation.goBack(ROUTES.SEARCH_ADVANCED_FILTERS);
}, [newCards]);
const updateNewCards = useCallback(
(item: Partial<OptionData>) => {
if (!item.keyForList) {
return;
}
if (item.isSelected) {
setNewCards(newCards.filter((card) => card !== item.keyForList));
} else {
setNewCards([...newCards, item.keyForList]);
}
},
[newCards],
);
const footerContent = useMemo(
() => (
<Button
success
text={translate('common.save')}
pressOnEnter
onPress={handleConfirmSelection}
large
/>
),
[translate, handleConfirmSelection],
);
return (
<ScreenWrapper
testID={SearchFiltersCardPage.displayName}
shouldShowOfflineIndicatorInWideScreen
offlineIndicatorStyle={styles.mtAuto}
>
<HeaderWithBackButton
title={translate('common.card')}
onBackButtonPress={() => {
Navigation.goBack(ROUTES.SEARCH_ADVANCED_FILTERS);
}}
/>
<View style={[styles.flex1]}>
<SelectionList
sections={sections}
onSelectRow={updateNewCards}
footerContent={footerContent}
shouldStopPropagation
shouldShowTooltips
canSelectMultiple
ListItem={CardListItem}
/>
</View>
</ScreenWrapper>
);
}
SearchFiltersCardPage.displayName = 'SearchFiltersCardPage';
export default SearchFiltersCardPage;