Skip to content
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

[#16935] Workspace creation with Policy Draft info #29256

Merged
merged 5 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/ONYXKEYS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,8 @@ const ONYXKEYS = {
DOWNLOAD: 'download_',
POLICY: 'policy_',
POLICY_MEMBERS: 'policyMembers_',
POLICY_DRAFTS: 'policyDrafts_',
POLICY_MEMBERS_DRAFTS: 'policyMembersDrafts_',
POLICY_CATEGORIES: 'policyCategories_',
POLICY_RECENTLY_USED_CATEGORIES: 'policyRecentlyUsedCategories_',
POLICY_TAGS: 'policyTags_',
Expand Down
21 changes: 21 additions & 0 deletions src/libs/actions/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,25 @@ function createWorkspaceAndNavigateToIt(policyOwnerEmail = '', makeMeAdmin = fal
.then(endSignOnTransition);
}

function createWorkspaceWithPolicyDraftAndNavigateToIt(policyOwnerEmail = '', policyName = '', transitionFromOldDot = false) {
gedu marked this conversation as resolved.
Show resolved Hide resolved
const policyID = Policy.generatePolicyID();
Policy.createDraftInitialWorkspace(policyOwnerEmail, policyName, policyID);

Navigation.isNavigationReady()
.then(() => {
if (transitionFromOldDot) {
// We must call goBack() to remove the /transition route from history
Navigation.goBack(ROUTES.HOME);
}
Navigation.navigate(ROUTES.WORKSPACE_INITIAL.getRoute(policyID));
})
.then(endSignOnTransition);
}

function savePolicyDraftByNewWorkspace(policyID, policyName, policyOwnerEmail = '', makeMeAdmin = false) {
gedu marked this conversation as resolved.
Show resolved Hide resolved
Policy.createWorkspace(policyOwnerEmail, makeMeAdmin, policyName, policyID);
}

/**
* This action runs when the Navigator is ready and the current route changes
*
Expand Down Expand Up @@ -513,4 +532,6 @@ export {
createWorkspaceAndNavigateToIt,
getMissingOnyxUpdates,
finalReconnectAppAfterActivatingReliableUpdates,
savePolicyDraftByNewWorkspace,
createWorkspaceWithPolicyDraftAndNavigateToIt,
};
46 changes: 46 additions & 0 deletions src/libs/actions/Policy.js
Original file line number Diff line number Diff line change
Expand Up @@ -909,6 +909,40 @@ function buildOptimisticCustomUnits() {
};
}

function createDraftInitialWorkspace(policyOwnerEmail = '', policyName = '', policyID = generatePolicyID()) {
gedu marked this conversation as resolved.
Show resolved Hide resolved
gedu marked this conversation as resolved.
Show resolved Hide resolved
const workspaceName = policyName || generateDefaultWorkspaceName(policyOwnerEmail);
const {customUnits} = buildOptimisticCustomUnits();

const optimisticData = [
{
onyxMethod: Onyx.METHOD.SET,
key: `${ONYXKEYS.COLLECTION.POLICY_DRAFTS}${policyID}`,
value: {
id: policyID,
type: CONST.POLICY.TYPE.FREE,
name: workspaceName,
role: CONST.POLICY.ROLE.ADMIN,
owner: sessionEmail,
isPolicyExpenseChatEnabled: true,
outputCurrency: lodashGet(allPersonalDetails, [sessionAccountID, 'localCurrencyCode'], CONST.CURRENCY.USD),
pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD,
customUnits,
},
},
{
onyxMethod: Onyx.METHOD.SET,
key: `${ONYXKEYS.COLLECTION.POLICY_MEMBERS_DRAFTS}${policyID}`,
value: {
[sessionAccountID]: {
role: CONST.POLICY.ROLE.ADMIN,
errors: {},
},
},
},
];
Onyx.update(optimisticData);
gedu marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Optimistically creates a new workspace and default workspace chats
*
Expand Down Expand Up @@ -957,6 +991,16 @@ function createWorkspace(policyOwnerEmail = '', makeMeAdmin = false, policyName
},
{
optimisticData: [
{
onyxMethod: Onyx.METHOD.SET,
key: `${ONYXKEYS.COLLECTION.POLICY_DRAFTS}${policyID}`,
value: null,
},
{
onyxMethod: Onyx.METHOD.SET,
key: `${ONYXKEYS.COLLECTION.POLICY_MEMBERS}${policyID}`,
value: null,
},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think updates are done in order. It seems safer to clear the draft policy at last.

{
onyxMethod: Onyx.METHOD.SET,
key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`,
Expand Down Expand Up @@ -1131,6 +1175,7 @@ function createWorkspace(policyOwnerEmail = '', makeMeAdmin = false, policyName
],
},
);

return adminsChatReportID;
}

Expand Down Expand Up @@ -1259,4 +1304,5 @@ export {
clearErrors,
openDraftWorkspaceRequest,
buildOptimisticPolicyRecentlyUsedCategories,
createDraftInitialWorkspace,
};
20 changes: 17 additions & 3 deletions src/pages/workspace/WorkspaceInitialPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import Tooltip from '../../components/Tooltip';
import Text from '../../components/Text';
import ConfirmModal from '../../components/ConfirmModal';
import * as Expensicons from '../../components/Icon/Expensicons';
import * as App from '../../libs/actions/App';
import ScreenWrapper from '../../components/ScreenWrapper';
import withLocalize, {withLocalizePropTypes} from '../../components/withLocalize';
import MenuItem from '../../components/MenuItem';
Expand All @@ -31,6 +32,7 @@ import * as ReimbursementAccountProps from '../ReimbursementAccount/reimbursemen
import * as ReportUtils from '../../libs/ReportUtils';
import withWindowDimensions from '../../components/withWindowDimensions';
import PressableWithoutFeedback from '../../components/Pressable/PressableWithoutFeedback';
import useWindowDimensions from '../../hooks/useWindowDimensions';

const propTypes = {
...policyPropTypes,
Expand Down Expand Up @@ -65,9 +67,10 @@ function dismissError(policyID) {
}

function WorkspaceInitialPage(props) {
const policy = props.policy;
const policy = props.policyDraft && props.policyDraft.id ? props.policyDraft : props.policy;
const [isDeleteModalOpen, setIsDeleteModalOpen] = useState(false);
const [isCurrencyModalOpen, setIsCurrencyModalOpen] = useState(false);
const {isSmallScreenWidth} = useWindowDimensions();
const hasPolicyCreationError = Boolean(policy.pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD && policy.errors);

/**
Expand All @@ -81,6 +84,17 @@ function WorkspaceInitialPage(props) {
Navigation.goBack(ROUTES.SETTINGS_WORKSPACES);
}, [props.reports, policy]);

useEffect(() => {
const policyDraftId = lodashGet(props.policyDraft, 'id', null);
if (!policyDraftId) {
return;
}

App.savePolicyDraftByNewWorkspace(props.policyDraft.id, props.policyDraft.name, '', false, '', !isSmallScreenWidth);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
App.savePolicyDraftByNewWorkspace(props.policyDraft.id, props.policyDraft.name, '', false, '', !isSmallScreenWidth);
App.savePolicyDraftByNewWorkspace(props.policyDraft.id, props.policyDraft.name, '', false);

// I only care when the component renders the first time
gedu marked this conversation as resolved.
Show resolved Hide resolved
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

useEffect(() => {
if (!isCurrencyModalOpen || policy.outputCurrency !== CONST.CURRENCY.USD) {
return;
Expand Down Expand Up @@ -189,8 +203,8 @@ function WorkspaceInitialPage(props) {
{({safeAreaPaddingBottomStyle}) => (
<FullPageNotFoundView
onBackButtonPress={() => Navigation.goBack(ROUTES.SETTINGS_WORKSPACES)}
shouldShow={_.isEmpty(props.policy) || !PolicyUtils.isPolicyAdmin(props.policy) || PolicyUtils.isPendingDeletePolicy(props.policy)}
subtitleKey={_.isEmpty(props.policy) ? undefined : 'workspace.common.notAuthorized'}
shouldShow={_.isEmpty(policy) || !PolicyUtils.isPolicyAdmin(policy) || PolicyUtils.isPendingDeletePolicy(policy)}
subtitleKey={_.isEmpty(policy) ? undefined : 'workspace.common.notAuthorized'}
>
<HeaderWithBackButton
title={props.translate('workspace.common.workspace')}
Expand Down
4 changes: 1 addition & 3 deletions src/pages/workspace/WorkspacesListPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import * as App from '../../libs/actions/App';
import useLocalize from '../../hooks/useLocalize';
import useNetwork from '../../hooks/useNetwork';
import usePermissions from '../../hooks/usePermissions';
import useWindowDimensions from '../../hooks/useWindowDimensions';
import IllustratedHeaderPageLayout from '../../components/IllustratedHeaderPageLayout';
import SCREENS from '../../SCREENS';
import * as LottieAnimations from '../../components/LottieAnimations';
Expand Down Expand Up @@ -112,7 +111,6 @@ function WorkspacesListPage({policies, allPolicyMembers, reimbursementAccount, u
const {translate} = useLocalize();
const {isOffline} = useNetwork();
const {canUseWallet} = usePermissions();
const {isSmallScreenWidth} = useWindowDimensions();

/**
* @param {Boolean} isPaymentItem whether the item being rendered is the payments menu item
Expand Down Expand Up @@ -194,7 +192,7 @@ function WorkspacesListPage({policies, allPolicyMembers, reimbursementAccount, u
accessibilityLabel={translate('workspace.new.newWorkspace')}
success
text={translate('workspace.new.newWorkspace')}
onPress={() => App.createWorkspaceAndNavigateToIt('', false, '', false, !isSmallScreenWidth)}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gedu Shouldn't we update all instances of createWorkspaceAndNavigateToIt ? Or is it intentionally updated only for this specific case? The lack of updating other occurrences has resulted in inconsistency. For example, when creating a new workspace from the global menu, it still navigates to the workspace #admins room.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, I think we do not need this in the other flows either.

onPress={() => App.createWorkspaceWithPolicyDraftAndNavigateToIt()}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't just do

onPress={App.createWorkspaceWithPolicyDraftAndNavigateToIt}

because the onPress send a button event, and that object is passed and it breaks the calls

/>
}
>
Expand Down
6 changes: 6 additions & 0 deletions src/pages/workspace/withPolicy.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,12 @@ export default function (WrappedComponent) {
policyMembers: {
key: (props) => `${ONYXKEYS.COLLECTION.POLICY_MEMBERS}${getPolicyIDFromRoute(props.route)}`,
},
policyDraft: {
key: (props) => `${ONYXKEYS.COLLECTION.POLICY_DRAFTS}${getPolicyIDFromRoute(props.route)}`,
},
policyMembersDraft: {
key: (props) => `${ONYXKEYS.COLLECTION.POLICY_MEMBERS_DRAFTS}${getPolicyIDFromRoute(props.route)}`,
},
})(withPolicy);
}

Expand Down
7 changes: 4 additions & 3 deletions src/pages/workspace/withPolicyAndFullscreenLoading.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import PropTypes from 'prop-types';
import React from 'react';
import {withOnyx} from 'react-native-onyx';
import _ from 'underscore';
import isEmpty from 'underscore/modules/isEmpty';
import omit from 'underscore/modules/omit';
gedu marked this conversation as resolved.
Show resolved Hide resolved
import compose from '../../libs/compose';
import ONYXKEYS from '../../ONYXKEYS';
import withPolicy, {policyPropTypes, policyDefaultProps} from './withPolicy';
Expand All @@ -27,11 +28,11 @@ export default function (WrappedComponent) {
};

function WithPolicyAndFullscreenLoading(props) {
if (props.isLoadingReportData && _.isEmpty(props.policy)) {
if (props.isLoadingReportData && isEmpty(props.policy) && isEmpty(props.policyDraft)) {
return <FullscreenLoadingIndicator />;
}

const rest = _.omit(props, ['forwardedRef']);
const rest = omit(props, ['forwardedRef']);
return (
<WrappedComponent
// eslint-disable-next-line react/jsx-props-no-spreading
Expand Down
Loading