-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #46197 from software-mansion-labs/289Adam289/46038…
…-advanced-filters-category advanced filters category
- Loading branch information
Showing
9 changed files
with
271 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import React, {useCallback} from 'react'; | ||
import {View} from 'react-native'; | ||
import PressableWithFeedback from '@components/Pressable/PressableWithFeedback'; | ||
import SelectCircle from '@components/SelectCircle'; | ||
import TextWithTooltip from '@components/TextWithTooltip'; | ||
import useThemeStyles from '@hooks/useThemeStyles'; | ||
import CONST from '@src/CONST'; | ||
import BaseListItem from './BaseListItem'; | ||
import type {InviteMemberListItemProps, ListItem} from './types'; | ||
|
||
function SelectableListItem<TItem extends ListItem>({ | ||
item, | ||
isFocused, | ||
showTooltip, | ||
isDisabled, | ||
canSelectMultiple, | ||
onSelectRow, | ||
onCheckboxPress, | ||
onDismissError, | ||
onFocus, | ||
shouldSyncFocus, | ||
}: InviteMemberListItemProps<TItem>) { | ||
const styles = useThemeStyles(); | ||
const handleCheckboxPress = useCallback(() => { | ||
if (onCheckboxPress) { | ||
onCheckboxPress(item); | ||
} else { | ||
onSelectRow(item); | ||
} | ||
}, [item, onCheckboxPress, onSelectRow]); | ||
|
||
return ( | ||
<BaseListItem | ||
item={item} | ||
wrapperStyle={[styles.flex1, styles.justifyContentBetween, styles.sidebarLinkInner, isFocused && styles.sidebarLinkActive]} | ||
isFocused={isFocused} | ||
isDisabled={isDisabled} | ||
showTooltip={showTooltip} | ||
canSelectMultiple={canSelectMultiple} | ||
onSelectRow={onSelectRow} | ||
onDismissError={onDismissError} | ||
errors={item.errors} | ||
pendingAction={item.pendingAction} | ||
keyForList={item.keyForList} | ||
onFocus={onFocus} | ||
shouldSyncFocus={shouldSyncFocus} | ||
> | ||
<> | ||
<View style={[styles.flex1, styles.flexColumn, styles.justifyContentCenter, styles.alignItemsStretch, styles.optionRow]}> | ||
<View style={[styles.flexRow, styles.alignItemsCenter]}> | ||
<TextWithTooltip | ||
shouldShowTooltip={showTooltip} | ||
text={item.text ?? ''} | ||
style={[ | ||
styles.optionDisplayName, | ||
isFocused ? styles.sidebarLinkActiveText : styles.sidebarLinkText, | ||
item.isBold !== false && styles.sidebarLinkTextBold, | ||
styles.pre, | ||
item.alternateText ? styles.mb1 : null, | ||
]} | ||
/> | ||
</View> | ||
</View> | ||
{!!item.rightElement && item.rightElement} | ||
{canSelectMultiple && !item.isDisabled && ( | ||
<PressableWithFeedback | ||
onPress={handleCheckboxPress} | ||
disabled={isDisabled} | ||
role={CONST.ROLE.BUTTON} | ||
accessibilityLabel={item.text ?? ''} | ||
style={[styles.ml2, styles.optionSelectCircle]} | ||
> | ||
<SelectCircle | ||
isChecked={item.isSelected ?? false} | ||
selectCircleStyles={styles.ml0} | ||
/> | ||
</PressableWithFeedback> | ||
)} | ||
</> | ||
</BaseListItem> | ||
); | ||
} | ||
|
||
SelectableListItem.displayName = 'SelectableListItem'; | ||
|
||
export default SelectableListItem; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
import React, {useCallback, useMemo, useState} from 'react'; | ||
import {View} from 'react-native'; | ||
import {useOnyx} from 'react-native-onyx'; | ||
import FullPageNotFoundView from '@components/BlockingViews/FullPageNotFoundView'; | ||
import Button from '@components/Button'; | ||
import type {FormOnyxValues} from '@components/Form/types'; | ||
import HeaderWithBackButton from '@components/HeaderWithBackButton'; | ||
import ScreenWrapper from '@components/ScreenWrapper'; | ||
import SelectionList from '@components/SelectionList'; | ||
import SelectableListItem from '@components/SelectionList/SelectableListItem'; | ||
import useDebouncedState from '@hooks/useDebouncedState'; | ||
import useLocalize from '@hooks/useLocalize'; | ||
import useThemeStyles from '@hooks/useThemeStyles'; | ||
import localeCompare from '@libs/LocaleCompare'; | ||
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'; | ||
|
||
function SearchFiltersCategoryPage() { | ||
const styles = useThemeStyles(); | ||
const {translate} = useLocalize(); | ||
|
||
const [searchTerm, debouncedSearchTerm, setSearchTerm] = useDebouncedState(''); | ||
const [noResultsFound, setNoResultsFound] = useState(false); | ||
|
||
const [searchAdvancedFiltersForm] = useOnyx(ONYXKEYS.FORMS.SEARCH_ADVANCED_FILTERS_FORM); | ||
const currentCategories = searchAdvancedFiltersForm?.category; | ||
const [newCategories, setNewCategories] = useState<string[]>(currentCategories ?? []); | ||
const policyID = searchAdvancedFiltersForm?.policyID ?? '-1'; | ||
|
||
const [allPolicyIDCategories] = useOnyx(ONYXKEYS.COLLECTION.POLICY_CATEGORIES); | ||
const singlePolicyCategories = allPolicyIDCategories?.[`${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${policyID}`]; | ||
|
||
const categoryNames = useMemo(() => { | ||
let categories: string[] = []; | ||
if (!singlePolicyCategories) { | ||
categories = Object.values(allPolicyIDCategories ?? {}) | ||
.map((policyCategories) => Object.values(policyCategories ?? {}).map((category) => category.name)) | ||
.flat(); | ||
} else { | ||
categories = Object.values(singlePolicyCategories ?? {}).map((value) => value.name); | ||
} | ||
|
||
return [...new Set(categories)]; | ||
}, [allPolicyIDCategories, singlePolicyCategories]); | ||
|
||
const sections = useMemo(() => { | ||
const newSections: CategorySection[] = []; | ||
const chosenCategories = newCategories | ||
.filter((category) => category.toLowerCase().includes(debouncedSearchTerm.toLowerCase())) | ||
.sort((a, b) => localeCompare(a, b)) | ||
.map((name) => ({ | ||
text: name, | ||
keyForList: name, | ||
isSelected: newCategories?.includes(name) ?? false, | ||
})); | ||
const remainingCategories = categoryNames | ||
.filter((category) => newCategories.includes(category) === false) | ||
.filter((category) => category.toLowerCase().includes(debouncedSearchTerm.toLowerCase())) | ||
.sort((a, b) => localeCompare(a, b)) | ||
.map((name) => ({ | ||
text: name, | ||
keyForList: name, | ||
isSelected: newCategories?.includes(name) ?? false, | ||
})); | ||
if (chosenCategories.length === 0 && remainingCategories.length === 0) { | ||
setNoResultsFound(true); | ||
} else { | ||
setNoResultsFound(false); | ||
} | ||
newSections.push({ | ||
title: undefined, | ||
data: chosenCategories, | ||
shouldShow: chosenCategories.length > 0, | ||
}); | ||
newSections.push({ | ||
title: translate('common.category'), | ||
data: remainingCategories, | ||
shouldShow: remainingCategories.length > 0, | ||
}); | ||
return newSections; | ||
}, [categoryNames, newCategories, translate, debouncedSearchTerm]); | ||
|
||
const updateCategory = useCallback((values: Partial<FormOnyxValues<typeof ONYXKEYS.FORMS.SEARCH_ADVANCED_FILTERS_FORM>>) => { | ||
SearchActions.updateAdvancedFilters(values); | ||
}, []); | ||
|
||
const handleConfirmSelection = useCallback(() => { | ||
updateCategory({ | ||
category: newCategories.sort((a, b) => localeCompare(a, b)), | ||
}); | ||
Navigation.goBack(); | ||
}, [newCategories, updateCategory]); | ||
|
||
const updateNewCategories = useCallback( | ||
(item: Partial<OptionData>) => { | ||
if (!item.text) { | ||
return; | ||
} | ||
if (item.isSelected) { | ||
setNewCategories(newCategories?.filter((category) => category !== item.text)); | ||
} else { | ||
setNewCategories([...(newCategories ?? []), item.text]); | ||
} | ||
}, | ||
[newCategories], | ||
); | ||
|
||
const footerContent = useMemo( | ||
() => ( | ||
<Button | ||
success | ||
text={translate('common.save')} | ||
pressOnEnter | ||
onPress={handleConfirmSelection} | ||
large | ||
/> | ||
), | ||
[translate, handleConfirmSelection], | ||
); | ||
return ( | ||
<ScreenWrapper | ||
testID={SearchFiltersCategoryPage.displayName} | ||
shouldShowOfflineIndicatorInWideScreen | ||
offlineIndicatorStyle={styles.mtAuto} | ||
> | ||
<FullPageNotFoundView shouldShow={false}> | ||
<HeaderWithBackButton title={translate('common.category')} /> | ||
<View style={[styles.flex1]}> | ||
<SelectionList | ||
sections={sections} | ||
textInputValue={searchTerm} | ||
onChangeText={setSearchTerm} | ||
textInputLabel={translate('common.search')} | ||
onSelectRow={updateNewCategories} | ||
headerMessage={noResultsFound ? translate('common.noResultsFound') : undefined} | ||
footerContent={footerContent} | ||
shouldStopPropagation | ||
showLoadingPlaceholder={!noResultsFound} | ||
shouldShowTooltips | ||
canSelectMultiple | ||
ListItem={SelectableListItem} | ||
/> | ||
</View> | ||
</FullPageNotFoundView> | ||
</ScreenWrapper> | ||
); | ||
} | ||
|
||
SearchFiltersCategoryPage.displayName = 'SearchFiltersCategoryPage'; | ||
|
||
export default SearchFiltersCategoryPage; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters