-
Notifications
You must be signed in to change notification settings - Fork 51
feat(PM-1506): Overwrite role if the copilot is already a member of the project with "read"/"write" access #1658
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
Changes from all commits
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 |
---|---|---|
|
@@ -225,6 +225,9 @@ class Users extends Component { | |
addNewProjectMember={this.props.addNewProjectMember} | ||
onMemberInvited={this.props.addNewProjectInvite} | ||
onClose={this.resetAddUserState} | ||
projectOption={this.state.projectOption} | ||
projectMembers={projectMembers} | ||
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. Verify that |
||
updateProjectMember={updateProjectMember} | ||
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. Ensure that |
||
/> | ||
) | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,20 +6,30 @@ import Modal from '../Modal' | |
import SelectUserAutocomplete from '../SelectUserAutocomplete' | ||
import { PROJECT_ROLES } from '../../config/constants' | ||
import PrimaryButton from '../Buttons/PrimaryButton' | ||
import { addUserToProject, inviteUserToProject } from '../../services/projects' | ||
import { addUserToProject, inviteUserToProject, updateProjectMemberRole } from '../../services/projects' | ||
|
||
import styles from './Users.module.scss' | ||
|
||
const theme = { | ||
container: styles.modalContainer | ||
} | ||
|
||
const UserAddModalContent = ({ projectId, addNewProjectMember, onMemberInvited, onClose }) => { | ||
const UserAddModalContent = ({ | ||
projectMembers, | ||
projectOption, | ||
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. The |
||
projectId, | ||
addNewProjectMember, | ||
onMemberInvited, | ||
onClose, | ||
updateProjectMember | ||
}) => { | ||
const [userToAdd, setUserToAdd] = useState(null) | ||
const [userPermissionToAdd, setUserPermissionToAdd] = useState(PROJECT_ROLES.READ) | ||
const [showSelectUserError, setShowSelectUserError] = useState(false) | ||
const [addUserError, setAddUserError] = useState(null) | ||
const [isAdding, setIsAdding] = useState(false) | ||
const [isUserAddingFailed, setUserAddingFailed] = useState(false) | ||
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. The variable |
||
const [existingRole, setExistingRole] = useState('') | ||
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. The variable |
||
|
||
const onUpdateUserToAdd = (option) => { | ||
if (option && option.value) { | ||
|
@@ -52,8 +62,12 @@ const UserAddModalContent = ({ projectId, addNewProjectMember, onMemberInvited, | |
}) | ||
if (failed) { | ||
const error = get(failed, '0.message', 'User cannot be invited') | ||
const errorCode = get(failed, '0.error') | ||
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. Consider checking if |
||
const role = get(failed, '0.role') | ||
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. Consider checking if |
||
setAddUserError(error) | ||
setIsAdding(false) | ||
setUserAddingFailed(errorCode === 'ALREADY_MEMBER') | ||
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. Consider using a constant or enum for the error code 'ALREADY_MEMBER' to avoid magic strings and improve maintainability. |
||
setExistingRole(role) | ||
} else if (rest.message) { | ||
setAddUserError(rest.message) | ||
setIsAdding(false) | ||
|
@@ -74,121 +88,152 @@ const UserAddModalContent = ({ projectId, addNewProjectMember, onMemberInvited, | |
} | ||
} | ||
|
||
const onConfirmCopilotRoleChange = async () => { | ||
const member = projectMembers.find(item => item.userId === userToAdd.userId) | ||
const action = member.role === 'manager' ? 'complete-copilot-requests' : '' | ||
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. Consider checking if |
||
const response = await updateProjectMemberRole(projectId, member.id, 'copilot', action) | ||
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. The |
||
updateProjectMember(response) | ||
onClose() | ||
} | ||
|
||
const onCancelCopilotRoleChange = () => { | ||
setUserAddingFailed(false) | ||
setAddUserError('') | ||
} | ||
|
||
return ( | ||
<Modal theme={theme} onCancel={onClose}> | ||
<div className={cn(styles.contentContainer, styles.confirm)}> | ||
<div className={styles.title}>Add User</div> | ||
<div className={styles.addUserContentContainer}> | ||
<div className={styles.row}> | ||
<div className={cn(styles.field, styles.col1, styles.addUserTitle)}> | ||
Member<span className={styles.required}>*</span> : | ||
</div> | ||
<div className={cn(styles.field, styles.col2)}> | ||
<SelectUserAutocomplete | ||
value={userToAdd ? { label: userToAdd.handle, value: userToAdd.userId.toString() } : null} | ||
onChange={onUpdateUserToAdd} | ||
/> | ||
{ | ||
isUserAddingFailed && (['observer', 'customer', 'copilot', 'manager'].includes(existingRole)) && ( | ||
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. The condition |
||
<div className={cn(styles.contentContainer, styles.confirm)}> | ||
<div className={styles.textContent}>{`The copilot ${userToAdd.handle} is part of ${projectOption.label} project with ${existingRole} role.`}</div> | ||
<div className={styles.buttonWrapper}> | ||
<PrimaryButton onClick={onConfirmCopilotRoleChange} text={'Confirm'} type={'info'} /> | ||
<PrimaryButton onClick={onCancelCopilotRoleChange} text={'Cancel'} type={'disabled'} /> | ||
</div> | ||
</div> | ||
{showSelectUserError && ( | ||
<div className={styles.row}> | ||
<div className={styles.errorMesssage}>Please select a member.</div> | ||
</div> | ||
)} | ||
<div className={styles.row}> | ||
<div className={cn(styles.field, styles.col1, styles.addUserTitle)}> | ||
<label htmlFor='memberToAdd'>Role :</label> | ||
</div> | ||
<div className={cn(styles.col5)}> | ||
<div className={styles.tcRadioButton}> | ||
<input | ||
name={`add-user-radio`} | ||
type='radio' | ||
id={`read-add-user`} | ||
checked={userPermissionToAdd === PROJECT_ROLES.READ} | ||
onChange={() => setUserPermissionToAdd(PROJECT_ROLES.READ)} | ||
/> | ||
<label htmlFor={`read-add-user`}> | ||
<div>Read</div> | ||
<input type='hidden' /> | ||
</label> | ||
) | ||
} | ||
{ | ||
!isUserAddingFailed && ( | ||
<div className={cn(styles.contentContainer, styles.confirm)}> | ||
<div className={styles.title}>Add User</div> | ||
<div className={styles.addUserContentContainer}> | ||
<div className={styles.row}> | ||
<div className={cn(styles.field, styles.col1, styles.addUserTitle)}> | ||
Member<span className={styles.required}>*</span> : | ||
</div> | ||
<div className={cn(styles.field, styles.col2)}> | ||
<SelectUserAutocomplete | ||
value={userToAdd ? { label: userToAdd.handle, value: userToAdd.userId.toString() } : null} | ||
onChange={onUpdateUserToAdd} | ||
/> | ||
</div> | ||
</div> | ||
</div> | ||
<div className={cn(styles.col5)}> | ||
<div className={styles.tcRadioButton}> | ||
<input | ||
name={`add-user-radio`} | ||
type='radio' | ||
id={`write-add-user`} | ||
checked={userPermissionToAdd === PROJECT_ROLES.WRITE} | ||
onChange={() => setUserPermissionToAdd(PROJECT_ROLES.WRITE)} | ||
/> | ||
<label htmlFor={`write-add-user`}> | ||
<div>Write</div> | ||
<input type='hidden' /> | ||
</label> | ||
{showSelectUserError && ( | ||
<div className={styles.row}> | ||
<div className={styles.errorMesssage}>Please select a member.</div> | ||
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. There is a typo in the class name |
||
</div> | ||
)} | ||
<div className={styles.row}> | ||
<div className={cn(styles.field, styles.col1, styles.addUserTitle)}> | ||
<label htmlFor='memberToAdd'>Role :</label> | ||
</div> | ||
<div className={cn(styles.col5)}> | ||
<div className={styles.tcRadioButton}> | ||
<input | ||
name={`add-user-radio`} | ||
type='radio' | ||
id={`read-add-user`} | ||
checked={userPermissionToAdd === PROJECT_ROLES.READ} | ||
onChange={() => setUserPermissionToAdd(PROJECT_ROLES.READ)} | ||
/> | ||
<label htmlFor={`read-add-user`}> | ||
<div>Read</div> | ||
<input type='hidden' /> | ||
</label> | ||
</div> | ||
</div> | ||
<div className={cn(styles.col5)}> | ||
<div className={styles.tcRadioButton}> | ||
<input | ||
name={`add-user-radio`} | ||
type='radio' | ||
id={`write-add-user`} | ||
checked={userPermissionToAdd === PROJECT_ROLES.WRITE} | ||
onChange={() => setUserPermissionToAdd(PROJECT_ROLES.WRITE)} | ||
/> | ||
<label htmlFor={`write-add-user`}> | ||
<div>Write</div> | ||
<input type='hidden' /> | ||
</label> | ||
</div> | ||
</div> | ||
<div className={cn(styles.col5)}> | ||
<div className={styles.tcRadioButton}> | ||
<input | ||
name={`add-user-radio`} | ||
type='radio' | ||
id={`full-access-add-user`} | ||
checked={userPermissionToAdd === PROJECT_ROLES.MANAGER} | ||
onChange={() => setUserPermissionToAdd(PROJECT_ROLES.MANAGER)} | ||
/> | ||
<label htmlFor={`full-access-add-user`}> | ||
<div>Full Access</div> | ||
<input type='hidden' /> | ||
</label> | ||
</div> | ||
</div> | ||
<div className={cn(styles.col5)}> | ||
<div className={styles.tcRadioButton}> | ||
<input | ||
name={`add-user-radio`} | ||
type='radio' | ||
id={`copilot-add-user`} | ||
checked={userPermissionToAdd === PROJECT_ROLES.COPILOT} | ||
onChange={() => setUserPermissionToAdd(PROJECT_ROLES.COPILOT)} | ||
/> | ||
<label htmlFor={`copilot-add-user`}> | ||
<div>Copilot</div> | ||
<input type='hidden' /> | ||
</label> | ||
</div> | ||
</div> | ||
</div> | ||
{addUserError && ( | ||
<div className={styles.errorMesssage}>{addUserError}</div> | ||
)} | ||
</div> | ||
<div className={cn(styles.col5)}> | ||
<div className={styles.tcRadioButton}> | ||
<input | ||
name={`add-user-radio`} | ||
type='radio' | ||
id={`full-access-add-user`} | ||
checked={userPermissionToAdd === PROJECT_ROLES.MANAGER} | ||
onChange={() => setUserPermissionToAdd(PROJECT_ROLES.MANAGER)} | ||
<div className={styles.buttonGroup}> | ||
<div className={styles.buttonSizeA}> | ||
<PrimaryButton | ||
text={'Close'} | ||
type={'info'} | ||
onClick={onClose} | ||
/> | ||
<label htmlFor={`full-access-add-user`}> | ||
<div>Full Access</div> | ||
<input type='hidden' /> | ||
</label> | ||
</div> | ||
</div> | ||
<div className={cn(styles.col5)}> | ||
<div className={styles.tcRadioButton}> | ||
<input | ||
name={`add-user-radio`} | ||
type='radio' | ||
id={`copilot-add-user`} | ||
checked={userPermissionToAdd === PROJECT_ROLES.COPILOT} | ||
onChange={() => setUserPermissionToAdd(PROJECT_ROLES.COPILOT)} | ||
<div className={styles.buttonSizeA}> | ||
<PrimaryButton | ||
text={isAdding ? 'Adding user...' : 'Add User'} | ||
type={'info'} | ||
onClick={onAddUserConfirmClick} | ||
/> | ||
<label htmlFor={`copilot-add-user`}> | ||
<div>Copilot</div> | ||
<input type='hidden' /> | ||
</label> | ||
</div> | ||
</div> | ||
</div> | ||
{addUserError && ( | ||
<div className={styles.errorMesssage}>{addUserError}</div> | ||
)} | ||
</div> | ||
<div className={styles.buttonGroup}> | ||
<div className={styles.buttonSizeA}> | ||
<PrimaryButton | ||
text={'Close'} | ||
type={'info'} | ||
onClick={onClose} | ||
/> | ||
</div> | ||
<div className={styles.buttonSizeA}> | ||
<PrimaryButton | ||
text={isAdding ? 'Adding user...' : 'Add User'} | ||
type={'info'} | ||
onClick={onAddUserConfirmClick} | ||
/> | ||
</div> | ||
</div> | ||
</div> | ||
) | ||
} | ||
</Modal> | ||
) | ||
} | ||
UserAddModalContent.propTypes = { | ||
projectId: PropTypes.number.isRequired, | ||
addNewProjectMember: PropTypes.func.isRequired, | ||
onMemberInvited: PropTypes.func.isRequired, | ||
onClose: PropTypes.func.isRequired | ||
onClose: PropTypes.func.isRequired, | ||
projectOption: PropTypes.any.isRequired, | ||
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. Consider using a more specific PropType instead of |
||
projectMembers: PropTypes.array.isRequired, | ||
updateProjectMember: PropTypes.func.isRequired | ||
} | ||
|
||
export default UserAddModalContent |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -103,9 +103,10 @@ export async function fetchProjectPhases (id) { | |
* @param newRole the new role | ||
* @returns {Promise<*>} | ||
*/ | ||
export async function updateProjectMemberRole (projectId, memberRecordId, newRole) { | ||
export async function updateProjectMemberRole (projectId, memberRecordId, newRole, action) { | ||
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. The function |
||
const response = await axiosInstance.patch(`${PROJECTS_API_URL}/${projectId}/members/${memberRecordId}`, { | ||
role: newRole | ||
role: newRole, | ||
action | ||
}) | ||
return _.get(response, 'data') | ||
} | ||
|
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.
Consider checking if
projectOption
is correctly initialized and used within the component. Ensure that it aligns with the intended functionality described in the pull request.