2021-03-31 06:44:20 -04:00
|
|
|
import React, { useState, useEffect } from 'react'
|
2021-03-12 05:23:46 -05:00
|
|
|
import PropTypes from 'prop-types'
|
|
|
|
import { Trans, useTranslation } from 'react-i18next'
|
|
|
|
import {
|
|
|
|
useProjectContext,
|
2021-04-27 03:52:58 -04:00
|
|
|
useShareProjectContext,
|
2021-03-12 05:23:46 -05:00
|
|
|
} from './share-project-modal'
|
|
|
|
import Icon from '../../../shared/components/icon'
|
|
|
|
import TransferOwnershipModal from './transfer-ownership-modal'
|
|
|
|
import {
|
|
|
|
Button,
|
|
|
|
Col,
|
|
|
|
Form,
|
|
|
|
FormControl,
|
|
|
|
FormGroup,
|
|
|
|
OverlayTrigger,
|
2021-04-27 03:52:58 -04:00
|
|
|
Tooltip,
|
2021-03-12 05:23:46 -05:00
|
|
|
} from 'react-bootstrap'
|
|
|
|
import { removeMemberFromProject, updateMember } from '../utils/api'
|
|
|
|
|
|
|
|
export default function EditMember({ member }) {
|
|
|
|
const [privileges, setPrivileges] = useState(member.privileges)
|
|
|
|
const [
|
|
|
|
confirmingOwnershipTransfer,
|
2021-04-27 03:52:58 -04:00
|
|
|
setConfirmingOwnershipTransfer,
|
2021-03-12 05:23:46 -05:00
|
|
|
] = useState(false)
|
|
|
|
|
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()
|
|
|
|
const project = useProjectContext()
|
|
|
|
|
|
|
|
function handleSubmit(event) {
|
|
|
|
event.preventDefault()
|
|
|
|
|
|
|
|
if (privileges === 'owner') {
|
|
|
|
setConfirmingOwnershipTransfer(true)
|
|
|
|
} else {
|
|
|
|
monitorRequest(() =>
|
|
|
|
updateMember(project, member, {
|
2021-04-27 03:52:58 -04:00
|
|
|
privilegeLevel: privileges,
|
2021-03-12 05:23:46 -05:00
|
|
|
})
|
|
|
|
).then(() => {
|
|
|
|
updateProject({
|
|
|
|
members: project.members.map(item =>
|
|
|
|
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>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
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()
|
|
|
|
const project = useProjectContext()
|
|
|
|
|
|
|
|
function handleClick(event) {
|
|
|
|
event.preventDefault()
|
|
|
|
|
|
|
|
monitorRequest(() => removeMemberFromProject(project, member)).then(() => {
|
|
|
|
updateProject({
|
2021-04-27 03:52:58 -04:00
|
|
|
members: project.members.filter(existing => existing !== member),
|
2021-03-12 05:23:46 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<FormControl.Static className="text-center">
|
|
|
|
<OverlayTrigger
|
|
|
|
placement="bottom"
|
|
|
|
overlay={
|
|
|
|
<Tooltip id="tooltip-remove-collaborator">
|
|
|
|
<Trans i18nKey="remove_collaborator" />
|
|
|
|
</Tooltip>
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<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>
|
|
|
|
</OverlayTrigger>
|
|
|
|
</FormControl.Static>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
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 }) {
|
|
|
|
return (
|
|
|
|
<div className="text-center">
|
|
|
|
<Button type="submit" bsSize="sm" bsStyle="success">
|
|
|
|
<Trans i18nKey="change_or_cancel-change" />
|
|
|
|
</Button>
|
|
|
|
<div className="text-sm">
|
|
|
|
<Trans i18nKey="change_or_cancel-or" />
|
|
|
|
|
|
|
|
<Button type="button" className="btn-inline-link" onClick={handleReset}>
|
|
|
|
<Trans i18nKey="change_or_cancel-cancel" />
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
ChangePrivilegesActions.propTypes = {
|
2021-04-27 03:52:58 -04:00
|
|
|
handleReset: PropTypes.func.isRequired,
|
2021-03-12 05:23:46 -05:00
|
|
|
}
|