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

Policy employees merge refactor #10395

Merged
merged 22 commits into from
Sep 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
1a1a489
initial code
chiragsalian Aug 11, 2022
72b6c06
Merge branch 'main' into chirag-Policy_Employees_Merge-refactor
chiragsalian Aug 16, 2022
36f1a72
testing addMembersToWorkspace
chiragsalian Aug 17, 2022
13c1d50
Merge branch 'main' into chirag-Policy_Employees_Merge-refactor
chiragsalian Aug 18, 2022
86055ef
cleanup and navigate back
chiragsalian Aug 18, 2022
2a2392b
updating WorkspaceMembersPage to display member list
chiragsalian Aug 18, 2022
5a11bf8
Merge branch 'main' into chirag-Policy_Employees_Merge-refactor
chiragsalian Aug 24, 2022
989eaba
using key email to future proof code so role can be passed easily in …
chiragsalian Aug 24, 2022
7755c74
removing unnecessary params
chiragsalian Aug 25, 2022
4e75f3b
Merge branch 'main' into chirag-Policy_Employees_Merge-refactor
chiragsalian Sep 6, 2022
27d9d26
Merge branch 'main' into chirag-Policy_Employees_Merge-refactor
chiragsalian Sep 13, 2022
4713df2
Adding translations
chiragsalian Sep 13, 2022
2a27ef2
Merge branch 'main' into chirag-Policy_Employees_Merge-refactor
chiragsalian Sep 16, 2022
761ef21
Update src/languages/es.js
chiragsalian Sep 16, 2022
e11e682
using const
chiragsalian Sep 16, 2022
6e30c4f
Merge branch 'chirag-Policy_Employees_Merge-refactor' of github.com:E…
chiragsalian Sep 16, 2022
33aa73e
Merge branch 'main' into chirag-Policy_Employees_Merge-refactor
chiragsalian Sep 19, 2022
f8fadfc
enabledWhenOffline prop
chiragsalian Sep 20, 2022
ec856c7
showing opacity for add
chiragsalian Sep 20, 2022
8c6de38
code revert
chiragsalian Sep 26, 2022
882fb40
clearing errors on success
chiragsalian Sep 26, 2022
2c34bc7
logic fixes
chiragsalian Sep 27, 2022
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
6 changes: 5 additions & 1 deletion src/components/FormAlertWithSubmitButton.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ const propTypes = {

/** Submit function */
onSubmit: PropTypes.func.isRequired,

/** Should the button be enabled when offline */
enabledWhenOffline: PropTypes.bool,
};

const defaultProps = {
Expand All @@ -41,6 +44,7 @@ const defaultProps = {
containerStyles: [],
isLoading: false,
onFixTheErrorsPressed: () => {},
enabledWhenOffline: false,
};

const FormAlertWithSubmitButton = props => (
Expand All @@ -51,7 +55,7 @@ const FormAlertWithSubmitButton = props => (
message={props.message}
onFixTheErrorsPressed={props.onFixTheErrorsPressed}
>
{isOffline => (isOffline ? (
{isOffline => ((isOffline && !props.enabledWhenOffline) ? (
chiragsalian marked this conversation as resolved.
Show resolved Hide resolved
<Button
success
isDisabled
Expand Down
1 change: 1 addition & 0 deletions src/languages/en.js
Original file line number Diff line number Diff line change
Expand Up @@ -810,6 +810,7 @@ export default {
removeMembersTitle: 'Remove members',
selectAll: 'Select all',
error: {
genericAdd: 'There was a problem adding this workspace member.',
cannotRemove: 'You cannot remove yourself or the workspace owner.',
},
},
Expand Down
1 change: 1 addition & 0 deletions src/languages/es.js
Original file line number Diff line number Diff line change
Expand Up @@ -812,6 +812,7 @@ export default {
removeMembersTitle: 'Eliminar miembros',
selectAll: 'Seleccionar todo',
error: {
genericAdd: 'Ha ocurrido un problema al agregar el miembro al espacio de trabajo',
cannotRemove: 'No puedes eliminarte ni a ti mismo ni al dueño del espacio de trabajo.',
},
},
Expand Down
81 changes: 43 additions & 38 deletions src/libs/actions/Policy.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import Str from 'expensify-common/lib/str';
import * as DeprecatedAPI from '../deprecatedAPI';
import * as API from '../API';
import ONYXKEYS from '../../ONYXKEYS';
import * as PersonalDetails from './PersonalDetails';
import Growl from '../Growl';
import CONFIG from '../../CONFIG';
import CONST from '../../CONST';
Expand Down Expand Up @@ -244,51 +243,57 @@ function removeMembers(members, policyID) {
}

/**
* Merges the passed in login into the specified policy
* Adds members to the specified workspace/policyID
*
* @param {Array<String>} logins
* @param {Array<String>} memberLogins
* @param {String} welcomeNote
* @param {String} policyID
*/
function invite(logins, welcomeNote, policyID) {
Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`, {
alertMessage: '',
});
function addMembersToWorkspace(memberLogins, welcomeNote, policyID) {
const membersListKey = `${ONYXKEYS.COLLECTION.POLICY_MEMBER_LIST}${policyID}`;
const logins = _.map(memberLogins, memberLogin => OptionsListUtils.addSMSDomainIfPhoneNumber(memberLogin));

// Optimistically add the user to the policy
const newEmployeeLogins = _.map(logins, login => OptionsListUtils.addSMSDomainIfPhoneNumber(login));
const employeeUpdate = {};
_.each(newEmployeeLogins, login => employeeUpdate[login] = {});
Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY_MEMBER_LIST}${policyID}`, employeeUpdate);
const optimisticData = [
{
onyxMethod: CONST.ONYX.METHOD.MERGE,
key: membersListKey,

// Make the API call to merge the login into the policy
DeprecatedAPI.Policy_Employees_Merge({
// Convert to object with each key containing {pendingAction: ‘add’}
value: _.object(logins, Array(logins.length).fill({pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD})),
},
];

const successData = [
{
onyxMethod: CONST.ONYX.METHOD.MERGE,
key: membersListKey,

// Convert to object with each key clearing pendingAction. We don’t
// need to remove the members since that will be handled by onClose of OfflineWithFeedback.
value: _.object(logins, Array(logins.length).fill({pendingAction: null, errors: null})),
},
];

const failureData = [
{
onyxMethod: CONST.ONYX.METHOD.MERGE,
key: membersListKey,

// Convert to object with each key containing the error. We don’t
// need to remove the members since that is handled by onClose of OfflineWithFeedback.
value: _.object(logins, Array(logins.length).fill({
errors: {
[DateUtils.getMicroseconds()]: Localize.translateLocal('workspace.people.error.genericAdd'),
},
})),
},
];

API.write('AddMembersToWorkspace', {
employees: JSON.stringify(_.map(logins, login => ({email: login}))),
welcomeNote,
policyID,
})
.then((data) => {
// Save the personalDetails for the invited user in Onyx and fetch the latest policyExpenseChats
if (data.jsonCode === 200) {
Onyx.merge(ONYXKEYS.PERSONAL_DETAILS, PersonalDetails.formatPersonalDetails(data.personalDetails));
Navigation.goBack();
if (!_.isEmpty(data.policyExpenseChatIDs)) {
Report.fetchChatReportsByIDs(data.policyExpenseChatIDs);
}
return;
}

// If the operation failed, undo the optimistic addition
_.each(newEmployeeLogins, login => employeeUpdate[login] = null);
Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY_MEMBER_LIST}${policyID}`, employeeUpdate);

// Show the user feedback that the addition failed
let alertMessage = Localize.translateLocal('workspace.invite.genericFailureMessage');
if (data.jsonCode === 402) {
alertMessage += ` ${Localize.translateLocal('workspace.invite.pleaseEnterValidLogin')}`;
}
Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`, {alertMessage});
});
}, {optimisticData, successData, failureData});
}

/**
Expand Down Expand Up @@ -992,7 +997,7 @@ export {
getPolicyList,
loadFullPolicy,
removeMembers,
invite,
addMembersToWorkspace,
isAdminOfFreePolicy,
update,
setWorkspaceErrors,
Expand Down
4 changes: 3 additions & 1 deletion src/pages/workspace/WorkspaceInvitePage.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,8 @@ class WorkspaceInvitePage extends React.Component {

const logins = _.map(this.state.selectedOptions, option => option.login);
const filteredLogins = _.uniq(_.compact(_.map(logins, login => login.toLowerCase().trim())));
Policy.invite(filteredLogins, this.state.welcomeNote || this.getWelcomeNote(), this.props.route.params.policyID);
Policy.addMembersToWorkspace(filteredLogins, this.state.welcomeNote || this.getWelcomeNote(), this.props.route.params.policyID);
Navigation.goBack();
}

/**
Expand Down Expand Up @@ -318,6 +319,7 @@ class WorkspaceInvitePage extends React.Component {
onFixTheErrorsLinkPressed={() => {}}
message={this.props.policy.alertMessage}
containerStyles={[styles.flexReset, styles.mb0, styles.flexGrow0, styles.flexShrink0, styles.flexBasisAuto]}
enabledWhenOffline
/>
<Pressable
onPress={(e) => {
Expand Down