diff --git a/app/components/Actions.js b/app/components/Actions.js index f2c57f45bc..ccb5a86891 100644 --- a/app/components/Actions.js +++ b/app/components/Actions.js @@ -7,6 +7,7 @@ export const Action = styled(Flex)` justify-content: center; align-items: center; padding: 0 0 0 12px; + height: 32px; font-size: 15px; flex-shrink: 0; diff --git a/app/components/DropdownMenu/DropdownMenuItem.js b/app/components/DropdownMenu/DropdownMenuItem.js index 0ea5e5a541..fa694db48a 100644 --- a/app/components/DropdownMenu/DropdownMenuItem.js +++ b/app/components/DropdownMenu/DropdownMenuItem.js @@ -8,9 +8,13 @@ type Props = { disabled?: boolean, }; -const DropdownMenuItem = ({ onClick, children, ...rest }: Props) => { +const DropdownMenuItem = ({ onClick, children, disabled, ...rest }: Props) => { return ( - + {children} ); @@ -33,9 +37,13 @@ const MenuItem = styled.a` margin-right: 8px; } + svg { + opacity: ${props => (props.disabled ? '.5' : 1)}; + } + ${props => props.disabled - ? '' + ? 'pointer-events: none;' : ` &:hover { diff --git a/app/components/Editor/Embed.js b/app/components/Editor/Embed.js index d4c227b95d..2e4bb72994 100644 --- a/app/components/Editor/Embed.js +++ b/app/components/Editor/Embed.js @@ -49,5 +49,5 @@ const Container = styled.div` border-radius: 3px; box-shadow: ${props => - props.isSelected ? `0 0 0 2px ${props.theme.primary}` : 'none'}; + props.isSelected ? `0 0 0 2px ${props.theme.selected}` : 'none'}; `; diff --git a/app/components/InputSelect.js b/app/components/InputSelect.js new file mode 100644 index 0000000000..d60689b7b2 --- /dev/null +++ b/app/components/InputSelect.js @@ -0,0 +1,72 @@ +// @flow +import * as React from 'react'; +import { observer } from 'mobx-react'; +import { observable } from 'mobx'; +import styled from 'styled-components'; +import VisuallyHidden from 'components/VisuallyHidden'; +import { Outline, LabelText } from './Input'; + +const Select = styled.select` + border: 0; + flex: 1; + padding: 8px 12px; + outline: none; + background: none; + color: ${props => props.theme.text}; + + &:disabled, + &::placeholder { + color: ${props => props.theme.placeholder}; + } +`; + +type Option = { label: string, value: string }; + +export type Props = { + value?: string, + label?: string, + className?: string, + labelHidden?: boolean, + options: Option[], +}; + +@observer +class InputSelect extends React.Component { + @observable focused: boolean = false; + + handleBlur = () => { + this.focused = false; + }; + + handleFocus = () => { + this.focused = true; + }; + + render() { + const { label, className, labelHidden, options, ...rest } = this.props; + + const wrappedLabel = {label}; + + return ( + + ); + } +} + +export default InputSelect; diff --git a/app/components/Sidebar/Main.js b/app/components/Sidebar/Main.js index 30256adf9d..22020cebcf 100644 --- a/app/components/Sidebar/Main.js +++ b/app/components/Sidebar/Main.js @@ -62,7 +62,7 @@ class MainSidebar extends React.Component { if (!user || !team) return null; const draftDocumentsCount = documents.drafts.length; - const can = policies.abilties(team.id); + const can = policies.abilities(team.id); return ( diff --git a/app/components/Sidebar/Settings.js b/app/components/Sidebar/Settings.js index c3c687ad0a..81f6661dcb 100644 --- a/app/components/Sidebar/Settings.js +++ b/app/components/Sidebar/Settings.js @@ -43,7 +43,7 @@ class SettingsSidebar extends React.Component { const { team } = auth; if (!team) return null; - const can = policies.abilties(team.id); + const can = policies.abilities(team.id); return ( diff --git a/app/components/Sidebar/components/Collections.js b/app/components/Sidebar/components/Collections.js index 1b01def476..254e7d6284 100644 --- a/app/components/Sidebar/components/Collections.js +++ b/app/components/Sidebar/components/Collections.js @@ -13,11 +13,13 @@ import CollectionLink from './CollectionLink'; import Fade from 'components/Fade'; import CollectionsStore from 'stores/CollectionsStore'; +import PoliciesStore from 'stores/PoliciesStore'; import UiStore from 'stores/UiStore'; import DocumentsStore from 'stores/DocumentsStore'; type Props = { history: RouterHistory, + policies: PoliciesStore, collections: CollectionsStore, documents: DocumentsStore, onCreateCollection: () => void, @@ -41,6 +43,9 @@ class Collections extends React.Component { const { activeCollectionId } = this.props.ui; if (!activeCollectionId) return; + const can = this.props.policies.abilities(activeCollectionId); + if (!can.update) return; + this.props.history.push(newDocumentUrl(activeCollectionId)); } @@ -75,6 +80,6 @@ class Collections extends React.Component { } } -export default inject('collections', 'ui', 'documents')( +export default inject('collections', 'ui', 'documents', 'policies')( withRouter(Collections) ); diff --git a/app/components/SocketProvider.js b/app/components/SocketProvider.js index d34a1877a6..5c9d067ecc 100644 --- a/app/components/SocketProvider.js +++ b/app/components/SocketProvider.js @@ -4,6 +4,8 @@ import { inject } from 'mobx-react'; import io from 'socket.io-client'; import DocumentsStore from 'stores/DocumentsStore'; import CollectionsStore from 'stores/CollectionsStore'; +import MembershipsStore from 'stores/MembershipsStore'; +import PoliciesStore from 'stores/PoliciesStore'; import AuthStore from 'stores/AuthStore'; import UiStore from 'stores/UiStore'; @@ -13,6 +15,8 @@ type Props = { children: React.Node, documents: DocumentsStore, collections: CollectionsStore, + memberships: MembershipsStore, + policies: PoliciesStore, auth: AuthStore, ui: UiStore, }; @@ -27,34 +31,39 @@ class SocketProvider extends React.Component { path: '/realtime', }); - const { auth, ui, documents, collections } = this.props; + const { + auth, + ui, + documents, + collections, + memberships, + policies, + } = this.props; if (!auth.token) return; this.socket.on('connect', () => { this.socket.emit('authentication', { token: auth.token, }); + this.socket.on('unauthorized', err => { ui.showToast(err.message); throw err; }); - this.socket.on('entities', event => { - if (event.documents) { - event.documents.forEach(doc => { - if (doc.deletedAt) { - documents.remove(doc.id); - } else { - documents.add(doc); - } + + this.socket.on('entities', async event => { + if (event.documentIds) { + for (const documentId of event.documentIds) { + const document = await documents.fetch(documentId); // TODO: Move this to the document scene once data loading // has been refactored to be friendlier there. if ( auth.user && - doc.id === ui.activeDocumentId && - doc.updatedBy.id !== auth.user.id + documentId === ui.activeDocumentId && + document.updatedBy.id !== auth.user.id ) { - ui.showToast(`Document updated by ${doc.updatedBy.name}`, { + ui.showToast(`Document updated by ${document.updatedBy.name}`, { timeout: 30 * 1000, action: { text: 'Refresh', @@ -62,26 +71,53 @@ class SocketProvider extends React.Component { }, }); } - }); + } } - if (event.collections) { - event.collections.forEach(collection => { - if (collection.deletedAt) { - collections.remove(collection.id); - documents.removeCollectionDocuments(collection.id); - } else { - collections.add(collection); + + if (event.collectionIds) { + for (const collectionId of event.collectionIds) { + try { + await collections.fetch(collectionId, { force: true }); + } catch (err) { + if (err.statusCode === 404 || err.statusCode === 403) { + collections.remove(collectionId); + documents.removeCollectionDocuments(collectionId); + memberships.removeCollectionMemberships(collectionId); + } } - }); + } } }); + this.socket.on('documents.star', event => { documents.starredIds.set(event.documentId, true); }); + this.socket.on('documents.unstar', event => { documents.starredIds.set(event.documentId, false); }); + this.socket.on('collections.add_user', event => { + if (auth.user && event.userId === auth.user.id) { + collections.fetch(event.collectionId, { force: true }); + } + + // Document policies might need updating as the permission changes + documents.inCollection(event.collectionId).forEach(document => { + policies.remove(document.id); + }); + }); + + this.socket.on('collections.remove_user', event => { + if (auth.user && event.userId === auth.user.id) { + collections.remove(event.collectionId); + memberships.removeCollectionMemberships(event.collectionId); + documents.removeCollectionDocuments(event.collectionId); + } else { + memberships.remove(`${event.userId}-${event.collectionId}`); + } + }); + // received a message from the API server that we should request // to join a specific room. Forward that to the ws server. this.socket.on('join', event => { @@ -105,4 +141,11 @@ class SocketProvider extends React.Component { } } -export default inject('auth', 'ui', 'documents', 'collections')(SocketProvider); +export default inject( + 'auth', + 'ui', + 'documents', + 'collections', + 'memberships', + 'policies' +)(SocketProvider); diff --git a/app/menus/CollectionMenu.js b/app/menus/CollectionMenu.js index 2597b6d92d..8a827cdf76 100644 --- a/app/menus/CollectionMenu.js +++ b/app/menus/CollectionMenu.js @@ -90,7 +90,7 @@ class CollectionMenu extends React.Component { render() { const { policies, collection, position, onOpen, onClose } = this.props; - const can = policies.abilties(collection.id); + const can = policies.abilities(collection.id); return ( @@ -114,13 +114,17 @@ class CollectionMenu extends React.Component { {collection && ( - - New document - - - Import document - - {(can.update || can.export) &&
} + {can.update && ( + + New document + + )} + {can.update && ( + + Import document + + )} + {can.update &&
} {can.update && ( Edit… )} diff --git a/app/menus/DocumentMenu.js b/app/menus/DocumentMenu.js index eb7248bfa4..f37eed36b1 100644 --- a/app/menus/DocumentMenu.js +++ b/app/menus/DocumentMenu.js @@ -8,6 +8,7 @@ import Document from 'models/Document'; import UiStore from 'stores/UiStore'; import AuthStore from 'stores/AuthStore'; import CollectionStore from 'stores/CollectionsStore'; +import PoliciesStore from 'stores/PoliciesStore'; import { documentMoveUrl, documentEditUrl, @@ -22,6 +23,7 @@ type Props = { position?: 'left' | 'right' | 'center', document: Document, collections: CollectionStore, + policies: PoliciesStore, className: string, showPrint?: boolean, showToggleEmbeds?: boolean, @@ -109,6 +111,7 @@ class DocumentMenu extends React.Component { if (this.redirectTo) return ; const { + policies, document, position, className, @@ -118,20 +121,9 @@ class DocumentMenu extends React.Component { onOpen, onClose, } = this.props; - const canShareDocuments = auth.team && auth.team.sharing; - if (document.isArchived) { - return ( - - - Restore - - - Delete… - - - ); - } + const can = policies.abilities(document.id); + const canShareDocuments = can.share && auth.team && auth.team.sharing; return ( { onOpen={onOpen} onClose={onClose} > - {!document.isDraft ? ( - - {showPin && - (document.pinned ? ( + {can.unarchive && ( + + Restore + + )} + {showPin && + (document.pinned + ? can.unpin && ( Unpin - ) : ( + ) + : can.pin && ( Pin to collection ))} - {document.isStarred ? ( + {document.isStarred + ? can.unstar && ( Unstar - ) : ( + ) + : can.star && ( Star )} - {canShareDocuments && ( - - Share link… - - )} -
- - Document history - - - New child document - - Edit - - Duplicate - - - Archive - - - Delete… - - Move… -
- ) : ( - - {canShareDocuments && ( - - Share link… - - )} - - Delete… - - + {canShareDocuments && ( + + Share link… + )}
- - Download - + {can.read && ( + + Document history + + )} + {can.update && ( + + New child document + + )} + {can.update && ( + Edit + )} + {can.update && ( + + Duplicate + + )} + {can.archive && ( + + Archive + + )} + {can.delete && ( + + Delete… + + )} + {can.move && ( + Move… + )} +
+ {can.download && ( + + Download + + )} {showPrint && ( Print )} @@ -218,4 +217,4 @@ class DocumentMenu extends React.Component { } } -export default inject('ui', 'auth', 'collections')(DocumentMenu); +export default inject('ui', 'auth', 'collections', 'policies')(DocumentMenu); diff --git a/app/menus/NewDocumentMenu.js b/app/menus/NewDocumentMenu.js index a8100cb3f7..abc1536634 100644 --- a/app/menus/NewDocumentMenu.js +++ b/app/menus/NewDocumentMenu.js @@ -7,12 +7,14 @@ import { PlusIcon, CollectionIcon, PrivateCollectionIcon } from 'outline-icons'; import { newDocumentUrl } from 'utils/routeHelpers'; import CollectionsStore from 'stores/CollectionsStore'; +import PoliciesStore from 'stores/PoliciesStore'; import { DropdownMenu, DropdownMenuItem } from 'components/DropdownMenu'; import Button from 'components/Button'; type Props = { label?: React.Node, collections: CollectionsStore, + policies: PoliciesStore, }; @observer @@ -38,7 +40,7 @@ class NewDocumentMenu extends React.Component { render() { if (this.redirectTo) return ; - const { collections, label, ...rest } = this.props; + const { collections, policies, label, ...rest } = this.props; return ( { {...rest} > Choose a collection… - {collections.orderedData.map(collection => ( - this.handleNewDocument(collection.id)} - > - {collection.private ? ( - - ) : ( - - )}{' '} - {collection.name} - - ))} + {collections.orderedData.map(collection => { + const can = policies.abilities(collection.id); + + return ( + this.handleNewDocument(collection.id)} + disabled={!can.update} + > + {collection.private ? ( + + ) : ( + + )}{' '} + {collection.name} + + ); + })} ); } } -export default inject('collections')(NewDocumentMenu); +export default inject('collections', 'policies')(NewDocumentMenu); diff --git a/app/scenes/Collection.js b/app/scenes/Collection.js index 888122db2b..b215c24148 100644 --- a/app/scenes/Collection.js +++ b/app/scenes/Collection.js @@ -17,6 +17,7 @@ import RichMarkdownEditor from 'rich-markdown-editor'; import { newDocumentUrl, collectionUrl } from 'utils/routeHelpers'; import CollectionsStore from 'stores/CollectionsStore'; import DocumentsStore from 'stores/DocumentsStore'; +import PoliciesStore from 'stores/PoliciesStore'; import UiStore from 'stores/UiStore'; import Collection from 'models/Collection'; @@ -45,6 +46,7 @@ type Props = { ui: UiStore, documents: DocumentsStore, collections: CollectionsStore, + policies: PoliciesStore, match: Object, theme: Object, }; @@ -79,7 +81,7 @@ class CollectionScene extends React.Component { this.collection = collection; await this.props.documents.fetchPinned({ - collection: id, + collectionId: id, }); } @@ -112,21 +114,27 @@ class CollectionScene extends React.Component { }; renderActions() { + const can = this.props.policies.abilities(this.props.match.params.id); + return ( - - - - - - + {can.update && ( + + + + + + + + + )} @@ -325,6 +333,6 @@ const Wrapper = styled(Flex)` margin: 10px 0; `; -export default inject('collections', 'documents', 'ui')( +export default inject('collections', 'policies', 'documents', 'ui')( withTheme(CollectionScene) ); diff --git a/app/scenes/CollectionMembers/AddPeopleToCollection.js b/app/scenes/CollectionMembers/AddPeopleToCollection.js index 0c5ca01976..5ecefcd13a 100644 --- a/app/scenes/CollectionMembers/AddPeopleToCollection.js +++ b/app/scenes/CollectionMembers/AddPeopleToCollection.js @@ -55,6 +55,7 @@ class AddPeopleToCollection extends React.Component { this.props.memberships.create({ collectionId: this.props.collection.id, userId: user.id, + permission: 'read_write', }); this.props.ui.showToast(`${user.name} was added to the collection`); } catch (err) { diff --git a/app/scenes/CollectionMembers/CollectionMembers.js b/app/scenes/CollectionMembers/CollectionMembers.js index a9d4c62308..9bee7037c3 100644 --- a/app/scenes/CollectionMembers/CollectionMembers.js +++ b/app/scenes/CollectionMembers/CollectionMembers.js @@ -50,11 +50,26 @@ class CollectionMembers extends React.Component { } }; + handleUpdateUser = (user, permission) => { + try { + this.props.memberships.create({ + collectionId: this.props.collection.id, + userId: user.id, + permission, + }); + this.props.ui.showToast(`${user.name} permissions were updated`); + } catch (err) { + this.props.ui.showToast('Could not update user'); + } + }; + render() { const { collection, users, memberships, auth } = this.props; const { user } = auth; if (!user) return null; + const key = memberships.orderedData.map(m => m.permission).join('-'); + return ( {collection.private ? ( @@ -91,7 +106,7 @@ class CollectionMembers extends React.Component { Members { this.handleRemoveUser(item)} + onUpdate={permission => this.handleUpdateUser(item, permission)} /> )} /> diff --git a/app/scenes/CollectionMembers/components/MemberListItem.js b/app/scenes/CollectionMembers/components/MemberListItem.js index dc5249de6e..38af5fdfd3 100644 --- a/app/scenes/CollectionMembers/components/MemberListItem.js +++ b/app/scenes/CollectionMembers/components/MemberListItem.js @@ -1,22 +1,38 @@ // @flow import * as React from 'react'; +import styled from 'styled-components'; import Avatar from 'components/Avatar'; import Flex from 'shared/components/Flex'; import Time from 'shared/components/Time'; import Badge from 'components/Badge'; import Button from 'components/Button'; +import InputSelect from 'components/InputSelect'; import ListItem from 'components/List/Item'; import User from 'models/User'; +import Membership from 'models/Membership'; import { DropdownMenu, DropdownMenuItem } from 'components/DropdownMenu'; +const PERMISSIONS = [ + { label: 'Read only', value: 'read' }, + { label: 'Read & Edit', value: 'read_write' }, +]; type Props = { user: User, + membership?: ?Membership, canEdit: boolean, onAdd?: () => void, onRemove?: () => void, + onUpdate?: (permission: string) => void, }; -const MemberListItem = ({ user, onRemove, onAdd, canEdit }: Props) => { +const MemberListItem = ({ + user, + membership, + onRemove, + onUpdate, + onAdd, + canEdit, +}: Props) => { return ( { image={} actions={ + {canEdit && + onUpdate && ( +