-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
[CRITICAL] Implement <WorkspaceWorkflowsApprovalsExpensesFromPage /> for Member Selection #46484
Changes from 6 commits
2133d72
b8d64d7
a0e00fa
725c887
5c43fbd
0e3601c
877bad1
82a8725
c05863a
a991ef7
0f42bf8
f9c1a6e
17dbd84
9da70e4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,159 @@ | ||
import type {StackScreenProps} from '@react-navigation/stack'; | ||
import React from 'react'; | ||
import React, {useCallback, useEffect, useMemo, useState} from 'react'; | ||
import type {SectionListData} from 'react-native'; | ||
import {useOnyx} from 'react-native-onyx'; | ||
import Badge from '@components/Badge'; | ||
import FullPageNotFoundView from '@components/BlockingViews/FullPageNotFoundView'; | ||
import FormAlertWithSubmitButton from '@components/FormAlertWithSubmitButton'; | ||
import HeaderWithBackButton from '@components/HeaderWithBackButton'; | ||
import ScreenWrapper from '@components/ScreenWrapper'; | ||
import SelectionList from '@components/SelectionList'; | ||
import InviteMemberListItem from '@components/SelectionList/InviteMemberListItem'; | ||
import type {Section} from '@components/SelectionList/types'; | ||
import Text from '@components/Text'; | ||
import useDebouncedState from '@hooks/useDebouncedState'; | ||
import useLocalize from '@hooks/useLocalize'; | ||
import useThemeStyles from '@hooks/useThemeStyles'; | ||
import * as DeviceCapabilities from '@libs/DeviceCapabilities'; | ||
import Navigation from '@libs/Navigation/Navigation'; | ||
import type {FullScreenNavigatorParamList} from '@libs/Navigation/types'; | ||
import * as OptionsListUtils from '@libs/OptionsListUtils'; | ||
import * as PolicyUtils from '@libs/PolicyUtils'; | ||
import AccessOrNotFoundWrapper from '@pages/workspace/AccessOrNotFoundWrapper'; | ||
import withPolicyAndFullscreenLoading from '@pages/workspace/withPolicyAndFullscreenLoading'; | ||
import type {WithPolicyAndFullscreenLoadingProps} from '@pages/workspace/withPolicyAndFullscreenLoading'; | ||
import * as Workflow from '@userActions/Workflow'; | ||
import CONST from '@src/CONST'; | ||
import ONYXKEYS from '@src/ONYXKEYS'; | ||
import ROUTES from '@src/ROUTES'; | ||
import type SCREENS from '@src/SCREENS'; | ||
import type {Member} from '@src/types/onyx/ApprovalWorkflow'; | ||
import type {Icon} from '@src/types/onyx/OnyxCommon'; | ||
import {isEmptyObject} from '@src/types/utils/EmptyObject'; | ||
|
||
type SelectionListMember = { | ||
text: string; | ||
alternateText: string; | ||
keyForList: string; | ||
isSelected: boolean; | ||
login: string; | ||
rightElement?: React.ReactNode; | ||
icons?: Icon[]; | ||
}; | ||
|
||
type MembersSection = SectionListData<SelectionListMember, Section<SelectionListMember>>; | ||
|
||
type WorkspaceWorkflowsApprovalsExpensesFromPageProps = WithPolicyAndFullscreenLoadingProps & | ||
StackScreenProps<FullScreenNavigatorParamList, typeof SCREENS.WORKSPACE.WORKFLOWS_APPROVALS_EXPENSES_FROM>; | ||
|
||
function WorkspaceWorkflowsApprovalsExpensesFromPage({policy, isLoadingReportData = true, route}: WorkspaceWorkflowsApprovalsExpensesFromPageProps) { | ||
const styles = useThemeStyles(); | ||
const {translate} = useLocalize(); | ||
const [didScreenTransitionEnd, setDidScreenTransitionEnd] = useState(false); | ||
const [searchTerm, debouncedSearchTerm, setSearchTerm] = useDebouncedState(''); | ||
const [approvalWorkflow, approvalWorkflowMetadata] = useOnyx(ONYXKEYS.APPROVAL_WORKFLOW); | ||
const [selectedMembers, setSelectedMembers] = useState<SelectionListMember[]>([]); | ||
|
||
// eslint-disable-next-line rulesdir/no-negated-variables | ||
const shouldShowNotFoundView = (isEmptyObject(policy) && !isLoadingReportData) || !PolicyUtils.isPolicyAdmin(policy) || PolicyUtils.isPendingDeletePolicy(policy); | ||
|
||
useEffect(() => { | ||
if (!approvalWorkflow?.members) { | ||
return; | ||
} | ||
|
||
setSelectedMembers( | ||
approvalWorkflow.members.map((member) => { | ||
return { | ||
text: member.displayName, | ||
alternateText: member.email, | ||
keyForList: member.email, | ||
isSelected: true, | ||
login: member.email, | ||
icons: member.avatar ? [{source: member.avatar, type: CONST.ICON_TYPE_AVATAR, name: member.displayName}] : [], | ||
}; | ||
}), | ||
); | ||
}, [approvalWorkflow?.members]); | ||
|
||
const sections: MembersSection[] = useMemo(() => { | ||
const members: SelectionListMember[] = [...selectedMembers]; | ||
|
||
if (approvalWorkflow?.availableMembers) { | ||
const availableMembers = approvalWorkflow.availableMembers | ||
.map((member) => { | ||
const isAdmin = policy?.employeeList?.[member.email].role === CONST.REPORT.ROLE.ADMIN; | ||
|
||
return { | ||
text: member.displayName, | ||
alternateText: member.email, | ||
keyForList: member.email, | ||
isSelected: false, | ||
login: member.email, | ||
icons: member.avatar ? [{source: member.avatar, type: CONST.ICON_TYPE_AVATAR, name: member.displayName}] : [], | ||
rightElement: isAdmin ? <Badge text={translate('common.admin')} /> : undefined, | ||
}; | ||
}) | ||
.filter((member) => !selectedMembers.some((selectedOption) => selectedOption.login === member.login)); | ||
|
||
members.push(...availableMembers); | ||
} | ||
|
||
const filteredMembers = | ||
debouncedSearchTerm !== '' | ||
? members.filter((option) => { | ||
const searchValue = OptionsListUtils.getSearchValueForPhoneOrEmail(debouncedSearchTerm); | ||
const isPartOfSearchTerm = !!option.text?.toLowerCase().includes(searchValue) || !!option.login?.toLowerCase().includes(searchValue); | ||
return isPartOfSearchTerm; | ||
}) | ||
: members; | ||
|
||
return [ | ||
{ | ||
title: undefined, | ||
data: filteredMembers, | ||
shouldShow: true, | ||
}, | ||
]; | ||
}, [approvalWorkflow?.availableMembers, debouncedSearchTerm, policy?.employeeList, selectedMembers, translate]); | ||
|
||
const nextStep = useCallback(() => { | ||
const members: Member[] = selectedMembers.map((member) => ({displayName: member.text, avatar: member.icons?.[0]?.source, email: member.login})); | ||
Workflow.setApprovalWorkflowMembers(members); | ||
|
||
const firstApprover = approvalWorkflow?.approvers?.[0]?.email; | ||
if (approvalWorkflow?.isBeingEdited && firstApprover) { | ||
Navigation.navigate(ROUTES.WORKSPACE_WORKFLOWS_APPROVALS_EDIT.getRoute(route.params.policyID, firstApprover)); | ||
} else { | ||
Navigation.navigate(ROUTES.WORKSPACE_WORKFLOWS_APPROVALS_APPROVER.getRoute(route.params.policyID)); | ||
} | ||
}, [approvalWorkflow?.approvers, approvalWorkflow?.isBeingEdited, route.params.policyID, selectedMembers]); | ||
|
||
const goBack = useCallback(() => { | ||
if (!approvalWorkflow?.isBeingEdited) { | ||
Workflow.clearApprovalWorkflow(); | ||
} | ||
Navigation.goBack(); | ||
}, [approvalWorkflow?.isBeingEdited]); | ||
Comment on lines
+140
to
+145
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Clearing the approval workflow on going back caused the not found view to appear briefly while hiding the modal (#50094). Also this We went with a different approach that is to clear the workflow (from |
||
|
||
const nextButton = useMemo( | ||
() => ( | ||
<FormAlertWithSubmitButton | ||
isDisabled={!selectedMembers.length} | ||
buttonText={translate('common.next')} | ||
onSubmit={nextStep} | ||
containerStyles={[styles.flexReset, styles.flexGrow0, styles.flexShrink0, styles.flexBasisAuto]} | ||
enabledWhenOffline | ||
/> | ||
), | ||
[nextStep, selectedMembers.length, styles.flexBasisAuto, styles.flexGrow0, styles.flexReset, styles.flexShrink0, translate], | ||
); | ||
|
||
const toggleMember = (member: SelectionListMember) => { | ||
const isAlreadySelected = selectedMembers.some((selectedOption) => selectedOption.login === member.login); | ||
setSelectedMembers(isAlreadySelected ? selectedMembers.filter((selectedOption) => selectedOption.login !== member.login) : [...selectedMembers, {...member, isSelected: true}]); | ||
}; | ||
|
||
return ( | ||
<AccessOrNotFoundWrapper | ||
policyID={route.params.policyID} | ||
|
@@ -31,6 +162,7 @@ function WorkspaceWorkflowsApprovalsExpensesFromPage({policy, isLoadingReportDat | |
<ScreenWrapper | ||
includeSafeAreaPaddingBottom={false} | ||
testID={WorkspaceWorkflowsApprovalsExpensesFromPage.displayName} | ||
onEntryTransitionEnd={() => setDidScreenTransitionEnd(true)} | ||
> | ||
<FullPageNotFoundView | ||
shouldShow={shouldShowNotFoundView} | ||
|
@@ -40,7 +172,21 @@ function WorkspaceWorkflowsApprovalsExpensesFromPage({policy, isLoadingReportDat | |
> | ||
<HeaderWithBackButton | ||
title={translate('workflowsExpensesFromPage.title')} | ||
onBackButtonPress={Navigation.goBack} | ||
onBackButtonPress={goBack} | ||
/> | ||
<Text style={[styles.textHeadlineH1, styles.mh5, styles.mv3]}>{translate('workflowsExpensesFromPage.header')}</Text> | ||
<SelectionList | ||
canSelectMultiple | ||
sections={sections} | ||
ListItem={InviteMemberListItem} | ||
textInputLabel={translate('selectionList.findMember')} | ||
textInputValue={searchTerm} | ||
onChangeText={setSearchTerm} | ||
onSelectRow={toggleMember} | ||
showScrollIndicator | ||
showLoadingPlaceholder={!didScreenTransitionEnd || approvalWorkflowMetadata.status === 'loading'} | ||
shouldPreventDefaultFocusOnSelectRow={!DeviceCapabilities.canUseTouchScreen()} | ||
footerContent={nextButton} | ||
/> | ||
</FullPageNotFoundView> | ||
</ScreenWrapper> | ||
|
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.
Translation already approved here