Repository navigation
Refactor Approver Selection pages #72283
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+377
−441
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a0b826c
Add ApproverSelection List common component
parasharrajat 1fc3f22
Refactor ReporAddApproverpage to use common component
parasharrajat d5a135c
Refactor Add approver workflow page to use common component
parasharrajat 54203f6
Refactor WorkspaceWorkflowsApprovalsExpensesFromPage to use common ap…
parasharrajat 4d5ae25
Merge branch 'main' of github.com:Expensify/App into approver-cleanup
parasharrajat File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,191 @@ | ||
| import React, {useMemo, useState} from 'react'; | ||
| import type {SectionListData} from 'react-native'; | ||
| import useDebouncedState from '@hooks/useDebouncedState'; | ||
| import useLocalize from '@hooks/useLocalize'; | ||
| import useOnyx from '@hooks/useOnyx'; | ||
| import useThemeStyles from '@hooks/useThemeStyles'; | ||
| import {canUseTouchScreen} from '@libs/DeviceCapabilities'; | ||
| import {getSearchValueForPhoneOrEmail, sortAlphabetically} from '@libs/OptionsListUtils'; | ||
| import {goBackFromInvalidPolicy, isPendingDeletePolicy, isPolicyAdmin} from '@libs/PolicyUtils'; | ||
| import tokenizedSearch from '@libs/tokenizedSearch'; | ||
| import variables from '@styles/variables'; | ||
| import CONST from '@src/CONST'; | ||
| import ONYXKEYS from '@src/ONYXKEYS'; | ||
| import type {Policy} from '@src/types/onyx'; | ||
| import type {Icon} from '@src/types/onyx/OnyxCommon'; | ||
| import {isEmptyObject} from '@src/types/utils/EmptyObject'; | ||
| import BlockingView from './BlockingViews/BlockingView'; | ||
| import FullPageNotFoundView from './BlockingViews/FullPageNotFoundView'; | ||
| import HeaderWithBackButton from './HeaderWithBackButton'; | ||
| import * as Illustrations from './Icon/Illustrations'; | ||
| import ScreenWrapper from './ScreenWrapper'; | ||
| import SelectionList from './SelectionListWithSections'; | ||
| import InviteMemberListItem from './SelectionListWithSections/InviteMemberListItem'; | ||
| import type {Section} from './SelectionListWithSections/types'; | ||
|
|
||
| type ApproverSelectionListPageProps = { | ||
| testID: string; | ||
| headerTitle: string; | ||
| policy?: Policy; | ||
| isLoadingReportData?: boolean; | ||
| onBackButtonPress: () => void; | ||
| initiallyFocusedOptionKey?: string; | ||
| shouldShowNotFoundView?: boolean; | ||
| shouldShowNotFoundViewLink?: boolean; | ||
| listEmptyContentSubtitle?: string; | ||
| footerContent?: React.ReactNode; | ||
| subtitle?: React.ReactNode; | ||
| shouldShowTextInput?: boolean; | ||
| allApprovers: SelectionListApprover[]; | ||
| shouldShowListEmptyContent?: boolean; | ||
| allowMultipleSelection?: boolean; | ||
| onSelectApprover?: (approvers: SelectionListApprover[]) => void; | ||
| shouldShowLoadingPlaceholder?: boolean; | ||
| shouldEnableHeaderMaxHeight?: boolean; | ||
| }; | ||
|
|
||
| type SelectionListApprover = { | ||
| text: string; | ||
| alternateText: string; | ||
| keyForList: string; | ||
| isSelected: boolean; | ||
| login: string; | ||
| rightElement?: React.ReactNode; | ||
| icons: Icon[]; | ||
| value?: number; | ||
| }; | ||
|
|
||
| type ApproverSection = SectionListData<SelectionListApprover, Section<SelectionListApprover>>; | ||
|
|
||
| function ApproverSelectionList({ | ||
| testID, | ||
| headerTitle, | ||
| subtitle, | ||
| isLoadingReportData, | ||
| policy, | ||
| onBackButtonPress, | ||
| initiallyFocusedOptionKey, | ||
| shouldShowTextInput: shouldShowTextInputProp, | ||
| shouldShowNotFoundView: shouldShowNotFoundViewProp = false, | ||
| shouldShowNotFoundViewLink = true, | ||
| listEmptyContentSubtitle, | ||
| footerContent = null, | ||
| allApprovers, | ||
| shouldShowListEmptyContent: shouldShowListEmptyContentProp = true, | ||
| allowMultipleSelection = false, | ||
| onSelectApprover, | ||
| shouldShowLoadingPlaceholder, | ||
| shouldEnableHeaderMaxHeight, | ||
| }: ApproverSelectionListPageProps) { | ||
| const styles = useThemeStyles(); | ||
| const {translate, localeCompare} = useLocalize(); | ||
| const [searchTerm, debouncedSearchTerm, setSearchTerm] = useDebouncedState(''); | ||
| const [countryCode] = useOnyx(ONYXKEYS.COUNTRY_CODE, {canBeMissing: false}); | ||
| const shouldShowTextInput = shouldShowTextInputProp ?? allApprovers?.length >= CONST.STANDARD_LIST_ITEM_LIMIT; | ||
|
|
||
| const [selectedMembers, setSelectedMembers] = useState<SelectionListApprover[]>([]); | ||
|
|
||
| // eslint-disable-next-line rulesdir/no-negated-variables | ||
| const shouldShowNotFoundView = (isEmptyObject(policy) && !isLoadingReportData) || !isPolicyAdmin(policy) || isPendingDeletePolicy(policy) || shouldShowNotFoundViewProp; | ||
|
|
||
| const sections: ApproverSection[] = useMemo(() => { | ||
| const filteredApprovers = | ||
| debouncedSearchTerm !== '' | ||
| ? tokenizedSearch(allApprovers, getSearchValueForPhoneOrEmail(debouncedSearchTerm, countryCode), (option) => [option.text ?? '', option.login ?? '']) | ||
| : allApprovers; | ||
|
|
||
| const data = sortAlphabetically(filteredApprovers, 'text', localeCompare); | ||
| return [ | ||
| { | ||
| title: undefined, | ||
| data, | ||
| shouldShow: true, | ||
| }, | ||
| ]; | ||
| }, [allApprovers, debouncedSearchTerm, countryCode, localeCompare]); | ||
|
|
||
| const shouldShowListEmptyContent = !debouncedSearchTerm && !sections.at(0)?.data.length && shouldShowListEmptyContentProp; | ||
|
|
||
| const toggleApprover = (member: SelectionListApprover) => { | ||
| const isAlreadySelected = selectedMembers.some((selectedOption) => selectedOption.login === member.login); | ||
| let newSelectedApprovers = []; | ||
| if (!allowMultipleSelection) { | ||
| newSelectedApprovers = isAlreadySelected ? [] : [{...member, isSelected: true}]; | ||
| } else { | ||
| newSelectedApprovers = isAlreadySelected | ||
| ? selectedMembers.filter((selectedOption) => selectedOption.login !== member.login) | ||
| : [...selectedMembers, {...member, isSelected: true}]; | ||
| } | ||
| setSelectedMembers(newSelectedApprovers); | ||
| if (onSelectApprover) { | ||
| onSelectApprover(newSelectedApprovers); | ||
| } | ||
| }; | ||
|
|
||
| const headerMessage = useMemo(() => (searchTerm && !sections.at(0)?.data?.length ? translate('common.noResultsFound') : ''), [searchTerm, sections, translate]); | ||
|
|
||
| const listEmptyContent = useMemo( | ||
| () => ( | ||
| <BlockingView | ||
| icon={Illustrations.TurtleInShell} | ||
| iconWidth={variables.emptyListIconWidth} | ||
| iconHeight={variables.emptyListIconHeight} | ||
| title={translate('workflowsPage.emptyContent.title')} | ||
| subtitle={listEmptyContentSubtitle} | ||
| subtitleStyle={styles.textSupporting} | ||
| containerStyle={styles.pb10} | ||
| contentFitImage="contain" | ||
| /> | ||
| ), | ||
| [translate, listEmptyContentSubtitle, styles.textSupporting, styles.pb10], | ||
| ); | ||
|
|
||
| return ( | ||
| <ScreenWrapper | ||
| testID={testID} | ||
| enableEdgeToEdgeBottomSafeAreaPadding | ||
| shouldEnableMaxHeight={shouldEnableHeaderMaxHeight} | ||
| > | ||
| <FullPageNotFoundView | ||
| shouldShow={shouldShowNotFoundView} | ||
| subtitleKey={isEmptyObject(policy) ? undefined : 'workspace.common.notAuthorized'} | ||
| shouldShowLink={shouldShowNotFoundViewLink} | ||
| onBackButtonPress={goBackFromInvalidPolicy} | ||
| onLinkPress={goBackFromInvalidPolicy} | ||
| addBottomSafeAreaPadding | ||
| > | ||
| <HeaderWithBackButton | ||
| title={headerTitle} | ||
| onBackButtonPress={onBackButtonPress} | ||
| /> | ||
| {subtitle} | ||
| <SelectionList | ||
| canSelectMultiple={allowMultipleSelection} | ||
| sections={sections} | ||
| ListItem={InviteMemberListItem} | ||
| textInputLabel={shouldShowListEmptyContent ? undefined : translate('selectionList.findMember')} | ||
| textInputValue={searchTerm} | ||
| onChangeText={setSearchTerm} | ||
| headerMessage={headerMessage} | ||
| onSelectRow={toggleApprover} | ||
| showScrollIndicator | ||
| shouldPreventDefaultFocusOnSelectRow={!canUseTouchScreen()} | ||
| listEmptyContent={listEmptyContent} | ||
| shouldShowListEmptyContent={shouldShowListEmptyContent} | ||
| initiallyFocusedOptionKey={initiallyFocusedOptionKey} | ||
| shouldUpdateFocusedIndex | ||
| shouldShowTextInput={shouldShowTextInput} | ||
| addBottomSafeAreaPadding | ||
| showLoadingPlaceholder={shouldShowLoadingPlaceholder} | ||
| footerContent={footerContent} | ||
| /> | ||
| </FullPageNotFoundView> | ||
| </ScreenWrapper> | ||
| ); | ||
| } | ||
|
|
||
| ApproverSelectionList.displayName = 'ApproverSelectionList'; | ||
|
|
||
| export default ApproverSelectionList; | ||
|
|
||
| export type {SelectionListApprover}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a bug in this component where the previous selection is cleared when selecting another member in Expenses From ... got fixed in this PR #76096