-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(sharing): Replace old sharing link menu with the new modal
- Loading branch information
Showing
1 changed file
with
28 additions
and
137 deletions.
There are no files selected for viewing
165 changes: 28 additions & 137 deletions
165
packages/cozy-sharing/src/components/Recipient/LinkRecipientPermissions.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,156 +1,47 @@ | ||
import PropTypes from 'prop-types' | ||
import React, { useState, useRef } from 'react' | ||
|
||
import DropdownButton from 'cozy-ui/transpiled/react/DropdownButton' | ||
import Icon from 'cozy-ui/transpiled/react/Icon' | ||
import TrashIcon from 'cozy-ui/transpiled/react/Icons/Trash' | ||
import Spinner from 'cozy-ui/transpiled/react/Spinner' | ||
import Typography from 'cozy-ui/transpiled/react/Typography' | ||
import ActionMenu, { | ||
ActionMenuItem, | ||
ActionMenuRadio | ||
} from 'cozy-ui/transpiled/react/deprecated/ActionMenu' | ||
import Alerter from 'cozy-ui/transpiled/react/deprecated/Alerter' | ||
import { useI18n } from 'cozy-ui/transpiled/react/providers/I18n' | ||
|
||
import { isOnlyReadOnlyLinkAllowed } from '../../helpers/link' | ||
import { checkIsReadOnlyPermissions } from '../../helpers/permissions' | ||
import logger from '../../logger' | ||
import { ShareRestrictionModal } from '../ShareRestrictionModal/ShareRestrictionModal' | ||
|
||
const LinkRecipientPermissions = ({ | ||
className, | ||
document, | ||
documentType, | ||
permissions, | ||
onChangePermissions, | ||
onDisable | ||
}) => { | ||
const LinkRecipientPermissions = ({ className, document, permissions }) => { | ||
const { t } = useI18n() | ||
const buttonRef = useRef() | ||
const [menuIsOpen, setMenuIsOpen] = useState(false) | ||
const [loading, setLoading] = useState(false) | ||
const [openShareRestrictionModal, setOpenShareRestrictionModal] = | ||
useState(false) | ||
|
||
const isReadOnlyPermissions = checkIsReadOnlyPermissions(permissions) | ||
|
||
const toggleMenu = () => { | ||
setMenuIsOpen(!menuIsOpen) | ||
} | ||
|
||
const deleteShareLink = async () => { | ||
try { | ||
setLoading(true) | ||
await onDisable(document) | ||
} catch (e) { | ||
Alerter.error(t(`${documentType}.share.error.revoke`)) | ||
logger.log(e) | ||
} finally { | ||
setLoading(false) | ||
} | ||
} | ||
|
||
const updateLinkPermissions = ({ isReadOnly }) => { | ||
const verbs = isReadOnly ? ['GET'] : ['GET', 'POST', 'PUT', 'PATCH'] | ||
try { | ||
onChangePermissions(document, verbs) | ||
} catch (err) { | ||
Alerter.error(t(`${documentType}.share.shareByLink.permserror`)) | ||
logger.log(err) | ||
} | ||
} | ||
return ( | ||
<div className={className}> | ||
{loading && <Spinner />} | ||
{!loading && ( | ||
<> | ||
<DropdownButton | ||
onClick={toggleMenu} | ||
ref={buttonRef} | ||
textVariant="body2" | ||
> | ||
{t( | ||
`Share.type.${isReadOnlyPermissions ? 'one-way' : 'two-way'}` | ||
).toLowerCase()} | ||
</DropdownButton> | ||
{menuIsOpen && ( | ||
<ActionMenu | ||
onClose={toggleMenu} | ||
popperOptions={{ | ||
placement: 'bottom-end', | ||
strategy: 'fixed' | ||
}} | ||
anchorElRef={buttonRef} | ||
> | ||
<ActionMenuItem | ||
left={ | ||
<ActionMenuRadio | ||
name="permissionLinkMenu" | ||
value="true" | ||
checked={isReadOnlyPermissions} | ||
/> | ||
} | ||
onClick={() => { | ||
toggleMenu() | ||
updateLinkPermissions({ isReadOnly: true }) | ||
}} | ||
> | ||
<> | ||
{document?.type === 'directory' | ||
? t(`Share.permissionLink.seeFolder`) | ||
: t(`Share.permissionLink.seeFile`)} | ||
<Typography | ||
className="u-mt-half" | ||
variant="caption" | ||
color="textSecondary" | ||
> | ||
{t('Share.permissionLink.readDescription')} | ||
</Typography> | ||
</> | ||
</ActionMenuItem> | ||
{!isOnlyReadOnlyLinkAllowed({ documentType }) && ( | ||
<ActionMenuItem | ||
left={ | ||
<ActionMenuRadio | ||
name="permissionLinkMenu" | ||
value="false" | ||
checked={!isReadOnlyPermissions} | ||
/> | ||
} | ||
onClick={() => { | ||
toggleMenu() | ||
updateLinkPermissions({ isReadOnly: false }) | ||
}} | ||
> | ||
<> | ||
{document?.type === 'directory' | ||
? t(`Share.permissionLink.modifyFolder`) | ||
: t(`Share.permissionLink.modifyFile`)} | ||
<Typography | ||
className="u-mt-half" | ||
variant="caption" | ||
color="textSecondary" | ||
> | ||
{t('Share.permissionLink.writeDescription')} | ||
</Typography> | ||
</> | ||
</ActionMenuItem> | ||
)} | ||
<hr /> | ||
<ActionMenuItem | ||
left={<Icon icon={TrashIcon} color="var(--errorColor)" />} | ||
onClick={() => { | ||
toggleMenu() | ||
deleteShareLink() | ||
}} | ||
> | ||
<Typography className="u-error" variant="body1"> | ||
{t('Share.permissionLink.deactivate')} | ||
</Typography> | ||
</ActionMenuItem> | ||
</ActionMenu> | ||
)} | ||
</> | ||
)} | ||
<> | ||
<DropdownButton | ||
ref={buttonRef} | ||
textVariant="body2" | ||
onClick={() => setOpenShareRestrictionModal(true)} | ||
> | ||
{t( | ||
`Share.type.${isReadOnlyPermissions ? 'one-way' : 'two-way'}` | ||
).toLowerCase()} | ||
</DropdownButton> | ||
{openShareRestrictionModal && ( | ||
<ShareRestrictionModal | ||
file={document} | ||
onClose={() => setOpenShareRestrictionModal(false)} | ||
/> | ||
)} | ||
</> | ||
</div> | ||
) | ||
} | ||
|
||
LinkRecipientPermissions.propTypes = { | ||
className: PropTypes.string, | ||
document: PropTypes.object.isRequired, | ||
permissions: PropTypes.object.isRequired | ||
} | ||
|
||
export default LinkRecipientPermissions |