Skip to content

Commit

Permalink
Merge pull request #47845 from software-mansion-labs/approval-workflo…
Browse files Browse the repository at this point in the history
…ws/navigation-fix

[Advanced Approval Workflows]: The Back button in the confirm page close RHP
  • Loading branch information
tylerkaraszewski authored Aug 26, 2024
2 parents e3e9f3c + 3172be1 commit d4d1c0c
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,13 @@ function ApprovalWorkflowEditor({approvalWorkflow, removeApprovalWorkflow, polic

const editMembers = useCallback(() => {
const backTo = approvalWorkflow.action === CONST.APPROVAL_WORKFLOW.ACTION.CREATE ? ROUTES.WORKSPACE_WORKFLOWS_APPROVALS_NEW.getRoute(policyID) : undefined;
Navigation.navigate(ROUTES.WORKSPACE_WORKFLOWS_APPROVALS_EXPENSES_FROM.getRoute(policyID, backTo));
Navigation.navigate(ROUTES.WORKSPACE_WORKFLOWS_APPROVALS_EXPENSES_FROM.getRoute(policyID, backTo), CONST.NAVIGATION.ACTION_TYPE.PUSH);
}, [approvalWorkflow.action, policyID]);

const editApprover = useCallback(
(approverIndex: number) => {
const backTo = approvalWorkflow.action === CONST.APPROVAL_WORKFLOW.ACTION.CREATE ? ROUTES.WORKSPACE_WORKFLOWS_APPROVALS_NEW.getRoute(policyID) : undefined;
Navigation.navigate(ROUTES.WORKSPACE_WORKFLOWS_APPROVALS_APPROVER.getRoute(policyID, approverIndex, backTo));
Navigation.navigate(ROUTES.WORKSPACE_WORKFLOWS_APPROVALS_APPROVER.getRoute(policyID, approverIndex, backTo), CONST.NAVIGATION.ACTION_TYPE.PUSH);
},
[approvalWorkflow.action, policyID],
);
Expand All @@ -101,7 +101,10 @@ function ApprovalWorkflowEditor({approvalWorkflow, removeApprovalWorkflow, polic
Navigation.navigate(ROUTES.WORKSPACE_UPGRADE.getRoute(policyID, CONST.UPGRADE_FEATURE_INTRO_MAPPING.approvals.alias, Navigation.getActiveRoute()));
return;
}
Navigation.navigate(ROUTES.WORKSPACE_WORKFLOWS_APPROVALS_APPROVER.getRoute(policyID, approvalWorkflow.approvers.length, ROUTES.WORKSPACE_WORKFLOWS_APPROVALS_NEW.getRoute(policyID)));
Navigation.navigate(
ROUTES.WORKSPACE_WORKFLOWS_APPROVALS_APPROVER.getRoute(policyID, approvalWorkflow.approvers.length, ROUTES.WORKSPACE_WORKFLOWS_APPROVALS_NEW.getRoute(policyID)),
CONST.NAVIGATION.ACTION_TYPE.PUSH,
);
}, [approvalWorkflow.approvers.length, policy, policyID]);

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,7 @@ function WorkspaceWorkflowsApprovalsApproverPageBeta({policy, personalDetails, i
}

if (approvalWorkflow?.action === CONST.APPROVAL_WORKFLOW.ACTION.CREATE) {
Navigation.goBack();
Navigation.navigate(ROUTES.WORKSPACE_WORKFLOWS_APPROVALS_NEW.getRoute(route.params.policyID), CONST.NAVIGATION.TYPE.UP);
Navigation.navigate(ROUTES.WORKSPACE_WORKFLOWS_APPROVALS_NEW.getRoute(route.params.policyID));
} else {
const firstApprover = approvalWorkflow?.approvers?.[0]?.email ?? '';
Navigation.goBack(ROUTES.WORKSPACE_WORKFLOWS_APPROVALS_EDIT.getRoute(route.params.policyID, firstApprover));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import type {WithPolicyAndFullscreenLoadingProps} from '@pages/workspace/withPol
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 {isEmptyObject} from '@src/types/utils/EmptyObject';
import ApprovalWorkflowEditor from './ApprovalWorkflowEditor';
Expand All @@ -45,7 +44,7 @@ function WorkspaceWorkflowsApprovalsCreatePage({policy, isLoadingReportData = tr
}

Workflow.createApprovalWorkflow(route.params.policyID, approvalWorkflow);
Navigation.goBack(ROUTES.WORKSPACE_WORKFLOWS.getRoute(route.params.policyID));
Navigation.dismissModal();
}, [approvalWorkflow, route.params.policyID]);

return (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type {StackScreenProps} from '@react-navigation/stack';
import React, {useCallback, useEffect, useRef, useState} from 'react';
import React, {useCallback, useEffect, useMemo, useRef, useState} from 'react';
// eslint-disable-next-line no-restricted-imports
import type {ScrollView} from 'react-native';
import {useOnyx} from 'react-native-onyx';
Expand Down Expand Up @@ -37,9 +37,6 @@ function WorkspaceWorkflowsApprovalsEditPage({policy, isLoadingReportData = true
const [isDeleteModalVisible, setIsDeleteModalVisible] = useState(false);
const formRef = useRef<ScrollView>(null);

// eslint-disable-next-line rulesdir/no-negated-variables
const shouldShowNotFoundView = (isEmptyObject(policy) && !isLoadingReportData) || !PolicyUtils.isPolicyAdmin(policy) || PolicyUtils.isPendingDeletePolicy(policy) || !approvalWorkflow;

const updateApprovalWorkflow = useCallback(() => {
if (!approvalWorkflow || !initialApprovalWorkflow) {
return;
Expand All @@ -51,7 +48,7 @@ function WorkspaceWorkflowsApprovalsEditPage({policy, isLoadingReportData = true

const membersToRemove = initialApprovalWorkflow.members.filter((initialMember) => !approvalWorkflow.members.some((member) => member.email === initialMember.email));
Workflow.updateApprovalWorkflow(route.params.policyID, approvalWorkflow, membersToRemove);
Navigation.goBack();
Navigation.dismissModal();
}, [approvalWorkflow, initialApprovalWorkflow, route.params.policyID]);

const removeApprovalWorkflow = useCallback(() => {
Expand All @@ -61,36 +58,51 @@ function WorkspaceWorkflowsApprovalsEditPage({policy, isLoadingReportData = true

// Remove the approval workflow using the initial data as it could be already edited
Workflow.removeApprovalWorkflow(route.params.policyID, initialApprovalWorkflow);
Navigation.goBack();
setIsDeleteModalVisible(false);
Navigation.dismissModal();
}, [initialApprovalWorkflow, route.params.policyID]);

// Set the initial approval workflow when the page is loaded
useEffect(() => {
if (!!initialApprovalWorkflow || !policy || !personalDetails) {
return;
const {currentApprovalWorkflow, defaultWorkflowMembers} = useMemo(() => {
if (!policy || !personalDetails) {
return {};
}

const defaultApprover = policy?.approver ?? policy.owner;
const workflows = convertPolicyEmployeesToApprovalWorkflows({
const allApprovalWorkflows = convertPolicyEmployeesToApprovalWorkflows({
employees: policy.employeeList ?? {},
defaultApprover,
personalDetails,
});
const currentApprovalWorkflow = workflows.find((workflow) => workflow.approvers.at(0)?.email === route.params.firstApproverEmail);

return {
defaultWorkflowMembers: allApprovalWorkflows.at(0)?.members ?? [],
currentApprovalWorkflow: allApprovalWorkflows.find((workflow) => workflow.approvers.at(0)?.email === route.params.firstApproverEmail),
};
}, [personalDetails, policy, route.params.firstApproverEmail]);

// eslint-disable-next-line rulesdir/no-negated-variables
const shouldShowNotFoundView =
(isEmptyObject(policy) && !isLoadingReportData) || !PolicyUtils.isPolicyAdmin(policy) || PolicyUtils.isPendingDeletePolicy(policy) || !currentApprovalWorkflow;

// Set the initial approval workflow when the page is loaded
useEffect(() => {
if (initialApprovalWorkflow) {
return;
}

if (!currentApprovalWorkflow) {
return Workflow.clearApprovalWorkflow();
}

Workflow.setApprovalWorkflow({
...currentApprovalWorkflow,
availableMembers: [...currentApprovalWorkflow.members, ...(workflows.at(0)?.members ?? [])],
availableMembers: [...currentApprovalWorkflow.members, ...defaultWorkflowMembers],
action: CONST.APPROVAL_WORKFLOW.ACTION.EDIT,
isLoading: false,
errors: null,
});
setInitialApprovalWorkflow(currentApprovalWorkflow);
}, [initialApprovalWorkflow, personalDetails, policy, route.params.firstApproverEmail, route.params.policyID]);
}, [currentApprovalWorkflow, defaultWorkflowMembers, initialApprovalWorkflow]);

return (
<AccessOrNotFoundWrapper
Expand Down

0 comments on commit d4d1c0c

Please sign in to comment.