|
| 1 | +import React, {useCallback, useMemo} from 'react'; |
| 2 | +import {View} from 'react-native'; |
| 3 | +import {useOnyx} from 'react-native-onyx'; |
| 4 | +import type {ValueOf} from 'type-fest'; |
| 5 | +import HeaderWithBackButton from '@components/HeaderWithBackButton'; |
| 6 | +import ScreenWrapper from '@components/ScreenWrapper'; |
| 7 | +import SelectionList from '@components/SelectionList'; |
| 8 | +import RadioListItem from '@components/SelectionList/RadioListItem'; |
| 9 | +import type {ListItem} from '@components/SelectionList/types'; |
| 10 | +import useLocalize from '@hooks/useLocalize'; |
| 11 | +import useThemeStyles from '@hooks/useThemeStyles'; |
| 12 | +import Navigation from '@libs/Navigation/Navigation'; |
| 13 | +import {updateAdvancedFilters} from '@userActions/Search'; |
| 14 | +import CONST from '@src/CONST'; |
| 15 | +import type {TranslationPaths} from '@src/languages/types'; |
| 16 | +import ONYXKEYS from '@src/ONYXKEYS'; |
| 17 | +import ROUTES from '@src/ROUTES'; |
| 18 | +import type {SearchBooleanFilterKeys} from './types'; |
| 19 | + |
| 20 | +type BooleanFilterItem = ListItem & { |
| 21 | + value: ValueOf<typeof CONST.SEARCH.BOOLEAN>; |
| 22 | +}; |
| 23 | + |
| 24 | +type SearchBooleanFilterBaseProps = { |
| 25 | + /** Key used for the boolean filter */ |
| 26 | + booleanKey: SearchBooleanFilterKeys; |
| 27 | + |
| 28 | + /** The translation key for the page title */ |
| 29 | + titleKey: TranslationPaths; |
| 30 | +}; |
| 31 | + |
| 32 | +function SearchBooleanFilterBase({booleanKey, titleKey}: SearchBooleanFilterBaseProps) { |
| 33 | + const styles = useThemeStyles(); |
| 34 | + const {translate} = useLocalize(); |
| 35 | + |
| 36 | + const booleanValues = Object.values(CONST.SEARCH.BOOLEAN); |
| 37 | + const [searchAdvancedFiltersForm] = useOnyx(ONYXKEYS.FORMS.SEARCH_ADVANCED_FILTERS_FORM); |
| 38 | + |
| 39 | + const selectedItem = useMemo(() => { |
| 40 | + return booleanValues.find((value) => searchAdvancedFiltersForm?.[booleanKey] === value) ?? null; |
| 41 | + }, [booleanKey, searchAdvancedFiltersForm, booleanValues]); |
| 42 | + |
| 43 | + const items = useMemo(() => { |
| 44 | + return booleanValues.map((value) => ({ |
| 45 | + value, |
| 46 | + keyForList: value, |
| 47 | + text: translate(`common.${value}`), |
| 48 | + isSelected: selectedItem === value, |
| 49 | + })); |
| 50 | + }, [selectedItem, translate, booleanValues]); |
| 51 | + |
| 52 | + const updateFilter = useCallback( |
| 53 | + (selectedFilter: BooleanFilterItem) => { |
| 54 | + const newValue = selectedFilter.isSelected ? null : selectedFilter.value; |
| 55 | + |
| 56 | + updateAdvancedFilters({[booleanKey]: newValue}); |
| 57 | + Navigation.goBack(ROUTES.SEARCH_ADVANCED_FILTERS); |
| 58 | + }, |
| 59 | + [booleanKey], |
| 60 | + ); |
| 61 | + |
| 62 | + return ( |
| 63 | + <ScreenWrapper |
| 64 | + testID={SearchBooleanFilterBase.displayName} |
| 65 | + shouldShowOfflineIndicatorInWideScreen |
| 66 | + offlineIndicatorStyle={styles.mtAuto} |
| 67 | + includeSafeAreaPaddingBottom={false} |
| 68 | + shouldEnableMaxHeight |
| 69 | + > |
| 70 | + <HeaderWithBackButton |
| 71 | + title={translate(titleKey)} |
| 72 | + onBackButtonPress={() => { |
| 73 | + Navigation.goBack(ROUTES.SEARCH_ADVANCED_FILTERS); |
| 74 | + }} |
| 75 | + /> |
| 76 | + <View style={[styles.flex1]}> |
| 77 | + <SelectionList |
| 78 | + shouldSingleExecuteRowSelect |
| 79 | + sections={[{data: items}]} |
| 80 | + ListItem={RadioListItem} |
| 81 | + initiallyFocusedOptionKey={selectedItem} |
| 82 | + onSelectRow={updateFilter} |
| 83 | + /> |
| 84 | + </View> |
| 85 | + </ScreenWrapper> |
| 86 | + ); |
| 87 | +} |
| 88 | + |
| 89 | +SearchBooleanFilterBase.displayName = 'SearchBooleanFilterBase'; |
| 90 | + |
| 91 | +export default SearchBooleanFilterBase; |
0 commit comments