diff options
Diffstat (limited to 'src/features/todos')
-rw-r--r-- | src/features/todos/actions.js | 10 | ||||
-rw-r--r-- | src/features/todos/components/TodosWebview.js | 149 | ||||
-rw-r--r-- | src/features/todos/containers/TodosScreen.js | 45 | ||||
-rw-r--r-- | src/features/todos/index.js | 33 | ||||
-rw-r--r-- | src/features/todos/store.js | 86 |
5 files changed, 323 insertions, 0 deletions
diff --git a/src/features/todos/actions.js b/src/features/todos/actions.js new file mode 100644 index 000000000..673ce8531 --- /dev/null +++ b/src/features/todos/actions.js | |||
@@ -0,0 +1,10 @@ | |||
1 | import PropTypes from 'prop-types'; | ||
2 | import { createActionsFromDefinitions } from '../../actions/lib/actions'; | ||
3 | |||
4 | export const todoActions = createActionsFromDefinitions({ | ||
5 | resize: { | ||
6 | width: PropTypes.number.isRequired, | ||
7 | }, | ||
8 | }, PropTypes.checkPropTypes); | ||
9 | |||
10 | export default todoActions; | ||
diff --git a/src/features/todos/components/TodosWebview.js b/src/features/todos/components/TodosWebview.js new file mode 100644 index 000000000..1d99b9388 --- /dev/null +++ b/src/features/todos/components/TodosWebview.js | |||
@@ -0,0 +1,149 @@ | |||
1 | import React, { Component } from 'react'; | ||
2 | import PropTypes from 'prop-types'; | ||
3 | import { observer } from 'mobx-react'; | ||
4 | import injectSheet from 'react-jss'; | ||
5 | import Webview from 'react-electron-web-view'; | ||
6 | import * as environment from '../../../environment'; | ||
7 | |||
8 | const styles = theme => ({ | ||
9 | root: { | ||
10 | background: theme.colorBackground, | ||
11 | position: 'relative', | ||
12 | }, | ||
13 | webview: { | ||
14 | height: '100%', | ||
15 | }, | ||
16 | resizeHandler: { | ||
17 | position: 'absolute', | ||
18 | left: 0, | ||
19 | marginLeft: -5, | ||
20 | width: 10, | ||
21 | zIndex: 400, | ||
22 | cursor: 'col-resize', | ||
23 | }, | ||
24 | dragIndicator: { | ||
25 | position: 'absolute', | ||
26 | left: 0, | ||
27 | width: 5, | ||
28 | zIndex: 400, | ||
29 | background: theme.todos.dragIndicator.background, | ||
30 | }, | ||
31 | }); | ||
32 | |||
33 | @injectSheet(styles) @observer | ||
34 | class TodosWebview extends Component { | ||
35 | static propTypes = { | ||
36 | classes: PropTypes.object.isRequired, | ||
37 | authToken: PropTypes.string.isRequired, | ||
38 | resize: PropTypes.func.isRequired, | ||
39 | width: PropTypes.number.isRequired, | ||
40 | minWidth: PropTypes.number.isRequired, | ||
41 | }; | ||
42 | |||
43 | state = { | ||
44 | isDragging: false, | ||
45 | width: 300, | ||
46 | } | ||
47 | |||
48 | componentWillMount() { | ||
49 | const { width } = this.props; | ||
50 | |||
51 | this.setState({ | ||
52 | width, | ||
53 | }); | ||
54 | } | ||
55 | |||
56 | componentDidMount() { | ||
57 | this.node.addEventListener('mousemove', this.resizePanel.bind(this)); | ||
58 | this.node.addEventListener('mouseup', this.stopResize.bind(this)); | ||
59 | this.node.addEventListener('mouseleave', this.stopResize.bind(this)); | ||
60 | } | ||
61 | |||
62 | startResize = (event) => { | ||
63 | this.setState({ | ||
64 | isDragging: true, | ||
65 | initialPos: event.clientX, | ||
66 | delta: 0, | ||
67 | }); | ||
68 | } | ||
69 | |||
70 | resizePanel(e) { | ||
71 | const { minWidth } = this.props; | ||
72 | |||
73 | const { | ||
74 | isDragging, | ||
75 | initialPos, | ||
76 | } = this.state; | ||
77 | |||
78 | if (isDragging && Math.abs(e.clientX - window.innerWidth) > minWidth) { | ||
79 | const delta = e.clientX - initialPos; | ||
80 | |||
81 | this.setState({ | ||
82 | delta, | ||
83 | }); | ||
84 | } | ||
85 | } | ||
86 | |||
87 | stopResize() { | ||
88 | const { | ||
89 | resize, | ||
90 | minWidth, | ||
91 | } = this.props; | ||
92 | |||
93 | const { | ||
94 | isDragging, | ||
95 | delta, | ||
96 | width, | ||
97 | } = this.state; | ||
98 | |||
99 | if (isDragging) { | ||
100 | let newWidth = width + (delta < 0 ? Math.abs(delta) : -Math.abs(delta)); | ||
101 | |||
102 | if (newWidth < minWidth) { | ||
103 | newWidth = minWidth; | ||
104 | } | ||
105 | |||
106 | this.setState({ | ||
107 | isDragging: false, | ||
108 | delta: 0, | ||
109 | width: newWidth, | ||
110 | }); | ||
111 | |||
112 | resize(newWidth); | ||
113 | } | ||
114 | } | ||
115 | |||
116 | render() { | ||
117 | const { authToken, classes } = this.props; | ||
118 | const { width, delta, isDragging } = this.state; | ||
119 | |||
120 | return ( | ||
121 | <> | ||
122 | <div | ||
123 | className={classes.root} | ||
124 | style={{ width }} | ||
125 | onMouseUp={() => this.stopResize()} | ||
126 | ref={(node) => { this.node = node; }} | ||
127 | > | ||
128 | <div | ||
129 | className={classes.resizeHandler} | ||
130 | style={Object.assign({ left: delta }, isDragging ? { width: 600, marginLeft: -200 } : {})} // This hack is required as resizing with webviews beneath behaves quite bad | ||
131 | onMouseDown={e => this.startResize(e)} | ||
132 | /> | ||
133 | {isDragging && ( | ||
134 | <div | ||
135 | className={classes.dragIndicator} | ||
136 | style={{ left: delta }} // This hack is required as resizing with webviews beneath behaves quite bad | ||
137 | /> | ||
138 | )} | ||
139 | <Webview | ||
140 | className={classes.webview} | ||
141 | src={`${environment.TODOS_FRONTEND}?authToken=${authToken}`} | ||
142 | /> | ||
143 | </div> | ||
144 | </> | ||
145 | ); | ||
146 | } | ||
147 | } | ||
148 | |||
149 | export default TodosWebview; | ||
diff --git a/src/features/todos/containers/TodosScreen.js b/src/features/todos/containers/TodosScreen.js new file mode 100644 index 000000000..0759c22db --- /dev/null +++ b/src/features/todos/containers/TodosScreen.js | |||
@@ -0,0 +1,45 @@ | |||
1 | import React, { Component } from 'react'; | ||
2 | import { inject, observer } from 'mobx-react'; | ||
3 | import PropTypes from 'prop-types'; | ||
4 | |||
5 | import TodosWebview from '../components/TodosWebview'; | ||
6 | import ErrorBoundary from '../../../components/util/ErrorBoundary'; | ||
7 | import UserStore from '../../../stores/UserStore'; | ||
8 | import TodoStore from '../store'; | ||
9 | import { TODOS_MIN_WIDTH } from '..'; | ||
10 | |||
11 | @inject('stores', 'actions') @observer | ||
12 | class TodosScreen extends Component { | ||
13 | static propTypes = { | ||
14 | stores: PropTypes.shape({ | ||
15 | user: PropTypes.instanceOf(UserStore).isRequired, | ||
16 | todos: PropTypes.instanceOf(TodoStore).isRequired, | ||
17 | }).isRequired, | ||
18 | actions: PropTypes.shape({ | ||
19 | todos: PropTypes.shape({ | ||
20 | resize: PropTypes.func.isRequired, | ||
21 | }), | ||
22 | }).isRequired, | ||
23 | }; | ||
24 | |||
25 | render() { | ||
26 | const { stores, actions } = this.props; | ||
27 | |||
28 | if (!stores.todos || !stores.todos.isFeatureActive) { | ||
29 | return null; | ||
30 | } | ||
31 | |||
32 | return ( | ||
33 | <ErrorBoundary> | ||
34 | <TodosWebview | ||
35 | authToken={stores.user.authToken} | ||
36 | width={stores.todos.width} | ||
37 | minWidth={TODOS_MIN_WIDTH} | ||
38 | resize={width => actions.todos.resize({ width })} | ||
39 | /> | ||
40 | </ErrorBoundary> | ||
41 | ); | ||
42 | } | ||
43 | } | ||
44 | |||
45 | export default TodosScreen; | ||
diff --git a/src/features/todos/index.js b/src/features/todos/index.js new file mode 100644 index 000000000..0dfd35c78 --- /dev/null +++ b/src/features/todos/index.js | |||
@@ -0,0 +1,33 @@ | |||
1 | import { reaction } from 'mobx'; | ||
2 | import TodoStore from './store'; | ||
3 | |||
4 | const debug = require('debug')('Franz:feature:todos'); | ||
5 | |||
6 | export const GA_CATEGORY_TODOS = 'Todos'; | ||
7 | |||
8 | export const DEFAULT_TODOS_WIDTH = 300; | ||
9 | export const TODOS_MIN_WIDTH = 200; | ||
10 | |||
11 | export const todoStore = new TodoStore(); | ||
12 | |||
13 | export default function initTodos(stores, actions) { | ||
14 | stores.todos = todoStore; | ||
15 | const { features } = stores; | ||
16 | |||
17 | // Toggle todos feature | ||
18 | reaction( | ||
19 | () => features.features.isTodosEnabled, | ||
20 | (isEnabled) => { | ||
21 | if (isEnabled) { | ||
22 | debug('Initializing `todos` feature'); | ||
23 | todoStore.start(stores, actions); | ||
24 | } else if (todoStore.isFeatureActive) { | ||
25 | debug('Disabling `todos` feature'); | ||
26 | todoStore.stop(); | ||
27 | } | ||
28 | }, | ||
29 | { | ||
30 | fireImmediately: true, | ||
31 | }, | ||
32 | ); | ||
33 | } | ||
diff --git a/src/features/todos/store.js b/src/features/todos/store.js new file mode 100644 index 000000000..e7e13b37f --- /dev/null +++ b/src/features/todos/store.js | |||
@@ -0,0 +1,86 @@ | |||
1 | import { | ||
2 | computed, | ||
3 | action, | ||
4 | observable, | ||
5 | } from 'mobx'; | ||
6 | import localStorage from 'mobx-localstorage'; | ||
7 | |||
8 | import { todoActions } from './actions'; | ||
9 | import { FeatureStore } from '../utils/FeatureStore'; | ||
10 | import { createReactions } from '../../stores/lib/Reaction'; | ||
11 | import { createActionBindings } from '../utils/ActionBinding'; | ||
12 | import { DEFAULT_TODOS_WIDTH, TODOS_MIN_WIDTH } from '.'; | ||
13 | |||
14 | const debug = require('debug')('Franz:feature:todos:store'); | ||
15 | |||
16 | export default class TodoStore extends FeatureStore { | ||
17 | @observable isFeatureEnabled = false; | ||
18 | |||
19 | @observable isFeatureActive = false; | ||
20 | |||
21 | @computed get width() { | ||
22 | const width = this.settings.width || DEFAULT_TODOS_WIDTH; | ||
23 | |||
24 | return width < TODOS_MIN_WIDTH ? TODOS_MIN_WIDTH : width; | ||
25 | } | ||
26 | |||
27 | @computed get settings() { | ||
28 | return localStorage.getItem('todos') || {}; | ||
29 | } | ||
30 | |||
31 | // ========== PUBLIC API ========= // | ||
32 | |||
33 | @action start(stores, actions) { | ||
34 | debug('TodoStore::start'); | ||
35 | this.stores = stores; | ||
36 | this.actions = actions; | ||
37 | |||
38 | // ACTIONS | ||
39 | |||
40 | this._registerActions(createActionBindings([ | ||
41 | [todoActions.resize, this._resize], | ||
42 | ])); | ||
43 | |||
44 | // REACTIONS | ||
45 | |||
46 | this._allReactions = createReactions([ | ||
47 | this._setFeatureEnabledReaction, | ||
48 | ]); | ||
49 | |||
50 | this._registerReactions(this._allReactions); | ||
51 | |||
52 | this.isFeatureActive = true; | ||
53 | } | ||
54 | |||
55 | @action stop() { | ||
56 | super.stop(); | ||
57 | debug('TodoStore::stop'); | ||
58 | this.reset(); | ||
59 | this.isFeatureActive = false; | ||
60 | } | ||
61 | |||
62 | // ========== PRIVATE METHODS ========= // | ||
63 | |||
64 | _updateSettings = (changes) => { | ||
65 | localStorage.setItem('todos', { | ||
66 | ...this.settings, | ||
67 | ...changes, | ||
68 | }); | ||
69 | }; | ||
70 | |||
71 | // Actions | ||
72 | |||
73 | @action _resize = ({ width }) => { | ||
74 | this._updateSettings({ | ||
75 | width, | ||
76 | }); | ||
77 | }; | ||
78 | |||
79 | // Reactions | ||
80 | |||
81 | _setFeatureEnabledReaction = () => { | ||
82 | const { isTodosEnabled } = this.stores.features.features; | ||
83 | |||
84 | this.isFeatureEnabled = isTodosEnabled; | ||
85 | }; | ||
86 | } | ||