2021-06-23 05:37:08 -04:00
|
|
|
import { useState, useEffect } from 'react'
|
2021-03-12 05:23:46 -05:00
|
|
|
import PropTypes from 'prop-types'
|
2023-10-19 04:33:26 -04:00
|
|
|
import { useTranslation } from 'react-i18next'
|
2021-06-25 04:13:17 -04:00
|
|
|
import { useShareProjectContext } from './share-project-modal'
|
2021-03-12 05:23:46 -05:00
|
|
|
import TransferOwnershipModal from './transfer-ownership-modal'
|
|
|
|
import { removeMemberFromProject, updateMember } from '../utils/api'
|
2022-05-18 09:46:10 -04:00
|
|
|
import { Button, Col, Form, FormControl, FormGroup } from 'react-bootstrap'
|
|
|
|
import Tooltip from '../../../shared/components/tooltip'
|
|
|
|
import Icon from '../../../shared/components/icon'
|
2021-06-25 04:13:17 -04:00
|
|
|
import { useProjectContext } from '../../../shared/context/project-context'
|
2023-07-31 11:27:12 -04:00
|
|
|
import { sendMB } from '../../../infrastructure/event-tracking'
|
2021-03-12 05:23:46 -05:00
|
|
|
|
|
|
|
export default function EditMember({ member }) {
|
|
|
|
const [privileges, setPrivileges] = useState(member.privileges)
|
2022-01-10 05:23:05 -05:00
|
|
|
const [confirmingOwnershipTransfer, setConfirmingOwnershipTransfer] =
|
|
|
|
useState(false)
|
2021-03-12 05:23:46 -05:00
|
|
|
|
2021-03-31 06:44:20 -04:00
|
|
|
// update the local state if the member's privileges change externally
|
|
|
|
useEffect(() => {
|
|
|
|
setPrivileges(member.privileges)
|
|
|
|
}, [member.privileges])
|
|
|
|
|
2021-03-12 05:23:46 -05:00
|
|
|
const { updateProject, monitorRequest } = useShareProjectContext()
|
2022-01-10 10:47:10 -05:00
|
|
|
const { _id: projectId, members } = useProjectContext()
|
2021-03-12 05:23:46 -05:00
|
|
|
|
|
|
|
function handleSubmit(event) {
|
|
|
|
event.preventDefault()
|
|
|
|
|
|
|
|
if (privileges === 'owner') {
|
|
|
|
setConfirmingOwnershipTransfer(true)
|
|
|
|
} else {
|
|
|
|
monitorRequest(() =>
|
2022-01-10 10:47:10 -05:00
|
|
|
updateMember(projectId, member, {
|
2021-04-27 03:52:58 -04:00
|
|
|
privilegeLevel: privileges,
|
2021-03-12 05:23:46 -05:00
|
|
|
})
|
|
|
|
).then(() => {
|
|
|
|
updateProject({
|
2022-01-10 10:47:10 -05:00
|
|
|
members: members.map(item =>
|
2021-03-12 05:23:46 -05:00
|
|
|
item._id === member._id ? { ...item, privileges } : item
|
2021-04-27 03:52:58 -04:00
|
|
|
),
|
2021-03-12 05:23:46 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (confirmingOwnershipTransfer) {
|
|
|
|
return (
|
|
|
|
<TransferOwnershipModal
|
|
|
|
member={member}
|
|
|
|
cancel={() => setConfirmingOwnershipTransfer(false)}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Form horizontal id="share-project-form" onSubmit={handleSubmit}>
|
|
|
|
<FormGroup className="project-member">
|
|
|
|
<Col xs={7}>
|
|
|
|
<FormControl.Static>{member.email}</FormControl.Static>
|
|
|
|
</Col>
|
|
|
|
|
|
|
|
<Col xs={3}>
|
|
|
|
<SelectPrivilege
|
|
|
|
value={privileges}
|
|
|
|
handleChange={event => setPrivileges(event.target.value)}
|
|
|
|
/>
|
|
|
|
</Col>
|
|
|
|
|
|
|
|
<Col xs={2}>
|
|
|
|
{privileges === member.privileges ? (
|
|
|
|
<RemoveMemberAction member={member} />
|
|
|
|
) : (
|
|
|
|
<ChangePrivilegesActions
|
|
|
|
handleReset={() => setPrivileges(member.privileges)}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</Col>
|
|
|
|
</FormGroup>
|
|
|
|
</Form>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
EditMember.propTypes = {
|
|
|
|
member: PropTypes.shape({
|
|
|
|
_id: PropTypes.string.isRequired,
|
|
|
|
email: PropTypes.string.isRequired,
|
2021-04-27 03:52:58 -04:00
|
|
|
privileges: PropTypes.string.isRequired,
|
|
|
|
}),
|
2021-03-12 05:23:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function SelectPrivilege({ value, handleChange }) {
|
|
|
|
const { t } = useTranslation()
|
|
|
|
|
|
|
|
return (
|
|
|
|
<FormControl
|
|
|
|
componentClass="select"
|
|
|
|
className="privileges"
|
|
|
|
bsSize="sm"
|
|
|
|
value={value}
|
|
|
|
onChange={handleChange}
|
|
|
|
>
|
|
|
|
<option value="owner">{t('owner')}</option>
|
|
|
|
<option value="readAndWrite">{t('can_edit')}</option>
|
|
|
|
<option value="readOnly">{t('read_only')}</option>
|
|
|
|
</FormControl>
|
|
|
|
)
|
|
|
|
}
|
2022-05-18 09:46:10 -04:00
|
|
|
|
2021-03-12 05:23:46 -05:00
|
|
|
SelectPrivilege.propTypes = {
|
|
|
|
value: PropTypes.string.isRequired,
|
2021-04-27 03:52:58 -04:00
|
|
|
handleChange: PropTypes.func.isRequired,
|
2021-03-12 05:23:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function RemoveMemberAction({ member }) {
|
2021-05-12 06:28:03 -04:00
|
|
|
const { t } = useTranslation()
|
2021-03-12 05:23:46 -05:00
|
|
|
const { updateProject, monitorRequest } = useShareProjectContext()
|
2023-07-31 11:27:12 -04:00
|
|
|
const { _id: projectId, members, invites } = useProjectContext()
|
2021-03-12 05:23:46 -05:00
|
|
|
|
|
|
|
function handleClick(event) {
|
|
|
|
event.preventDefault()
|
|
|
|
|
2022-01-10 10:47:10 -05:00
|
|
|
monitorRequest(() => removeMemberFromProject(projectId, member)).then(
|
|
|
|
() => {
|
2023-07-31 11:27:12 -04:00
|
|
|
const updatedMembers = members.filter(existing => existing !== member)
|
2022-01-10 10:47:10 -05:00
|
|
|
updateProject({
|
2023-07-31 11:27:12 -04:00
|
|
|
members: updatedMembers,
|
|
|
|
})
|
|
|
|
sendMB('collaborator-removed', {
|
|
|
|
project_id: projectId,
|
|
|
|
current_collaborators_amount: updatedMembers.length,
|
|
|
|
current_invites_amount: invites.length,
|
2022-01-10 10:47:10 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
)
|
2021-03-12 05:23:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<FormControl.Static className="text-center">
|
2022-05-18 09:46:10 -04:00
|
|
|
<Tooltip
|
|
|
|
id="remove-collaborator"
|
2023-10-19 04:33:26 -04:00
|
|
|
description={t('remove_collaborator')}
|
2022-05-18 09:46:10 -04:00
|
|
|
overlayProps={{ placement: 'bottom' }}
|
2021-03-12 05:23:46 -05:00
|
|
|
>
|
|
|
|
<Button
|
|
|
|
type="button"
|
|
|
|
bsStyle="link"
|
|
|
|
onClick={handleClick}
|
|
|
|
className="remove-button"
|
2021-05-12 06:28:03 -04:00
|
|
|
aria-label={t('remove_collaborator')}
|
2021-03-12 05:23:46 -05:00
|
|
|
>
|
|
|
|
<Icon type="times" />
|
|
|
|
</Button>
|
2022-05-18 09:46:10 -04:00
|
|
|
</Tooltip>
|
2021-03-12 05:23:46 -05:00
|
|
|
</FormControl.Static>
|
|
|
|
)
|
|
|
|
}
|
2022-05-18 09:46:10 -04:00
|
|
|
|
2021-03-12 05:23:46 -05:00
|
|
|
RemoveMemberAction.propTypes = {
|
|
|
|
member: PropTypes.shape({
|
|
|
|
_id: PropTypes.string.isRequired,
|
|
|
|
email: PropTypes.string.isRequired,
|
2021-04-27 03:52:58 -04:00
|
|
|
privileges: PropTypes.string.isRequired,
|
|
|
|
}),
|
2021-03-12 05:23:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function ChangePrivilegesActions({ handleReset }) {
|
2023-10-19 04:33:26 -04:00
|
|
|
const { t } = useTranslation()
|
|
|
|
|
2021-03-12 05:23:46 -05:00
|
|
|
return (
|
|
|
|
<div className="text-center">
|
2022-12-07 05:51:08 -05:00
|
|
|
<Button type="submit" bsSize="sm" bsStyle="primary">
|
2023-10-19 04:33:26 -04:00
|
|
|
{t('change_or_cancel-change')}
|
2021-03-12 05:23:46 -05:00
|
|
|
</Button>
|
|
|
|
<div className="text-sm">
|
2023-10-19 04:33:26 -04:00
|
|
|
{t('change_or_cancel-or')}
|
2021-03-12 05:23:46 -05:00
|
|
|
|
|
|
|
<Button type="button" className="btn-inline-link" onClick={handleReset}>
|
2023-10-19 04:33:26 -04:00
|
|
|
{t('change_or_cancel-cancel')}
|
2021-03-12 05:23:46 -05:00
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
2022-05-18 09:46:10 -04:00
|
|
|
|
2021-03-12 05:23:46 -05:00
|
|
|
ChangePrivilegesActions.propTypes = {
|
2021-04-27 03:52:58 -04:00
|
|
|
handleReset: PropTypes.func.isRequired,
|
2021-03-12 05:23:46 -05:00
|
|
|
}
|