Skip to content

Commit

Permalink
Merge pull request #12117 from Expensify/justice-refactor-create-work…
Browse files Browse the repository at this point in the history
…space

Show errors after workspace creation failure
  • Loading branch information
Justice Arthur authored Nov 1, 2022
2 parents 00f2766 + d0408fb commit 5ebbde0
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/libs/PolicyUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function hasCustomUnitsError(policy) {
*/
function getPolicyBrickRoadIndicatorStatus(policy, policyMembers) {
const policyMemberList = lodashGet(policyMembers, `${ONYXKEYS.COLLECTION.POLICY_MEMBER_LIST}${policy.id}`, {});
if (hasPolicyMemberError(policyMemberList) || hasCustomUnitsError(policy)) {
if (hasPolicyMemberError(policyMemberList) || hasCustomUnitsError(policy) || hasPolicyError(policy)) {
return CONST.BRICK_ROAD_INDICATOR_STATUS.ERROR;
}
return '';
Expand Down
15 changes: 10 additions & 5 deletions src/libs/actions/Policy.js
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,15 @@ function clearDeleteWorkspaceError(policyID) {
});
}

/**
* Removes the workspace after failure to create.
*
* @param {String} policyID
*/
function removeWorkspace(policyID) {
Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`, null);
}

/**
* Generate a policy name based on an email and policy list.
* @param {String} [email] the email to base the workspace name on. If not passed, will use the logged in user's email instead
Expand Down Expand Up @@ -845,11 +854,6 @@ function createWorkspace(ownerEmail = '', makeMeAdmin = false) {
},
}],
failureData: [{
onyxMethod: CONST.ONYX.METHOD.SET,
key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`,
value: null,
},
{
onyxMethod: CONST.ONYX.METHOD.SET,
key: `${ONYXKEYS.COLLECTION.POLICY_MEMBER_LIST}${policyID}`,
value: null,
Expand Down Expand Up @@ -952,4 +956,5 @@ export {
createWorkspace,
openWorkspaceMembersPage,
openWorkspaceInvitePage,
removeWorkspace,
};
25 changes: 24 additions & 1 deletion src/pages/workspace/WorkspaceInitialPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ class WorkspaceInitialPage extends React.Component {
this.openEditor = this.openEditor.bind(this);
this.toggleDeleteModal = this.toggleDeleteModal.bind(this);
this.confirmDeleteAndHideModal = this.confirmDeleteAndHideModal.bind(this);
this.hasPolicyCreationError = this.hasPolicyCreationError.bind(this);
this.dismissError = this.dismissError.bind(this);

this.state = {
isDeleteModalOpen: false,
Expand Down Expand Up @@ -80,6 +82,18 @@ class WorkspaceInitialPage extends React.Component {
Navigation.navigate(ROUTES.SETTINGS);
}

/**
* @returns {Boolean}
*/
hasPolicyCreationError() {
return Boolean(this.props.policy.pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD && this.props.policy.errors);
}

dismissError() {
Navigation.navigate(ROUTES.SETTINGS_WORKSPACES);
Policy.removeWorkspace(this.props.policy.id);
}

render() {
const policy = this.props.policy;
const hasMembersError = PolicyUtils.hasPolicyMemberError(this.props.policyMemberList);
Expand Down Expand Up @@ -159,11 +173,17 @@ class WorkspaceInitialPage extends React.Component {
styles.justifyContentBetween,
]}
>
<OfflineWithFeedback pendingAction={this.props.policy.pendingAction}>
<OfflineWithFeedback
pendingAction={this.props.policy.pendingAction}
onClose={this.dismissError}
errors={this.props.policy.errors}
errorRowStyles={[styles.ph6, styles.pv2]}
>
<View style={[styles.flex1]}>
<View style={styles.pageWrapper}>
<View style={[styles.settingsPageBody, styles.alignItemsCenter]}>
<Pressable
disabled={this.hasPolicyCreationError()}
style={[styles.pRelative, styles.avatarLarge]}
onPress={this.openEditor}
>
Expand All @@ -188,6 +208,7 @@ class WorkspaceInitialPage extends React.Component {
</Pressable>
{!_.isEmpty(this.props.policy.name) && (
<Pressable
disabled={this.hasPolicyCreationError()}
style={[
styles.alignSelfCenter,
styles.mt4,
Expand All @@ -214,6 +235,8 @@ class WorkspaceInitialPage extends React.Component {
{_.map(menuItems, item => (
<MenuItem
key={item.translationKey}
disabled={this.hasPolicyCreationError()}
interactive={!this.hasPolicyCreationError()}
title={this.props.translate(item.translationKey)}
icon={item.icon}
iconRight={item.iconRight}
Expand Down
6 changes: 5 additions & 1 deletion src/pages/workspace/WorkspacesListPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ function dismissWorkspaceError(policyID, pendingAction) {
Policy.clearDeleteWorkspaceError(policyID);
return;
}

if (pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD) {
Policy.removeWorkspace(policyID);
return;
}
throw new Error('Not implemented');
}

Expand Down Expand Up @@ -122,7 +127,6 @@ class WorkspacesListPage extends Component {
brickRoadIndicator: PolicyUtils.getPolicyBrickRoadIndicatorStatus(policy, this.props.policyMembers),
pendingAction: policy.pendingAction,
isPolicy: true,
errors: policy.errors,
dismissError: () => dismissWorkspaceError(policy.id, policy.pendingAction),
disabled: policy.pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE,
}))
Expand Down

0 comments on commit 5ebbde0

Please sign in to comment.