diff options
Diffstat (limited to 'src/features/workspaces')
-rw-r--r-- | src/features/workspaces/actions.js | 9 | ||||
-rw-r--r-- | src/features/workspaces/containers/EditWorkspaceScreen.js | 12 | ||||
-rw-r--r-- | src/features/workspaces/containers/WorkspacesScreen.js | 10 | ||||
-rw-r--r-- | src/features/workspaces/index.js | 40 | ||||
-rw-r--r-- | src/features/workspaces/state.js | 6 | ||||
-rw-r--r-- | src/features/workspaces/store.js | 37 |
6 files changed, 83 insertions, 31 deletions
diff --git a/src/features/workspaces/actions.js b/src/features/workspaces/actions.js index 84de2b011..25246de09 100644 --- a/src/features/workspaces/actions.js +++ b/src/features/workspaces/actions.js | |||
@@ -1,7 +1,8 @@ | |||
1 | import PropTypes from 'prop-types'; | 1 | import PropTypes from 'prop-types'; |
2 | import Workspace from './models/Workspace'; | 2 | import Workspace from './models/Workspace'; |
3 | import { createActionsFromDefinitions } from '../../actions/lib/actions'; | ||
3 | 4 | ||
4 | export default { | 5 | export default createActionsFromDefinitions({ |
5 | edit: { | 6 | edit: { |
6 | workspace: PropTypes.instanceOf(Workspace).isRequired, | 7 | workspace: PropTypes.instanceOf(Workspace).isRequired, |
7 | }, | 8 | }, |
@@ -14,4 +15,8 @@ export default { | |||
14 | update: { | 15 | update: { |
15 | workspace: PropTypes.instanceOf(Workspace).isRequired, | 16 | workspace: PropTypes.instanceOf(Workspace).isRequired, |
16 | }, | 17 | }, |
17 | }; | 18 | activate: { |
19 | workspace: PropTypes.instanceOf(Workspace).isRequired, | ||
20 | }, | ||
21 | deactivate: {}, | ||
22 | }, PropTypes.checkPropTypes); | ||
diff --git a/src/features/workspaces/containers/EditWorkspaceScreen.js b/src/features/workspaces/containers/EditWorkspaceScreen.js index 790b8a0fe..1b13bc2d4 100644 --- a/src/features/workspaces/containers/EditWorkspaceScreen.js +++ b/src/features/workspaces/containers/EditWorkspaceScreen.js | |||
@@ -4,7 +4,7 @@ import PropTypes from 'prop-types'; | |||
4 | 4 | ||
5 | import ErrorBoundary from '../../../components/util/ErrorBoundary'; | 5 | import ErrorBoundary from '../../../components/util/ErrorBoundary'; |
6 | import EditWorkspaceForm from '../components/EditWorkspaceForm'; | 6 | import EditWorkspaceForm from '../components/EditWorkspaceForm'; |
7 | import { state } from '../state'; | 7 | import { workspacesState } from '../state'; |
8 | import ServicesStore from '../../../stores/ServicesStore'; | 8 | import ServicesStore from '../../../stores/ServicesStore'; |
9 | import Workspace from '../models/Workspace'; | 9 | import Workspace from '../models/Workspace'; |
10 | 10 | ||
@@ -22,23 +22,23 @@ class EditWorkspaceScreen extends Component { | |||
22 | }; | 22 | }; |
23 | 23 | ||
24 | onDelete = () => { | 24 | onDelete = () => { |
25 | const { workspaceBeingEdited } = state; | 25 | const { workspaceBeingEdited } = workspacesState; |
26 | const { actions } = this.props; | 26 | const { actions } = this.props; |
27 | if (!workspaceBeingEdited) return null; | 27 | if (!workspaceBeingEdited) return null; |
28 | actions.workspace.delete({ workspace: workspaceBeingEdited }); | 28 | actions.workspaces.delete({ workspace: workspaceBeingEdited }); |
29 | }; | 29 | }; |
30 | 30 | ||
31 | onSave = (values) => { | 31 | onSave = (values) => { |
32 | const { workspaceBeingEdited } = state; | 32 | const { workspaceBeingEdited } = workspacesState; |
33 | const { actions } = this.props; | 33 | const { actions } = this.props; |
34 | const workspace = new Workspace( | 34 | const workspace = new Workspace( |
35 | Object.assign({}, workspaceBeingEdited, values), | 35 | Object.assign({}, workspaceBeingEdited, values), |
36 | ); | 36 | ); |
37 | actions.workspace.update({ workspace }); | 37 | actions.workspaces.update({ workspace }); |
38 | }; | 38 | }; |
39 | 39 | ||
40 | render() { | 40 | render() { |
41 | const { workspaceBeingEdited } = state; | 41 | const { workspaceBeingEdited } = workspacesState; |
42 | const { stores } = this.props; | 42 | const { stores } = this.props; |
43 | if (!workspaceBeingEdited) return null; | 43 | if (!workspaceBeingEdited) return null; |
44 | return ( | 44 | return ( |
diff --git a/src/features/workspaces/containers/WorkspacesScreen.js b/src/features/workspaces/containers/WorkspacesScreen.js index b89cbcf67..94e714255 100644 --- a/src/features/workspaces/containers/WorkspacesScreen.js +++ b/src/features/workspaces/containers/WorkspacesScreen.js | |||
@@ -1,7 +1,7 @@ | |||
1 | import React, { Component } from 'react'; | 1 | import React, { Component } from 'react'; |
2 | import { inject, observer } from 'mobx-react'; | 2 | import { inject, observer } from 'mobx-react'; |
3 | import PropTypes from 'prop-types'; | 3 | import PropTypes from 'prop-types'; |
4 | import { state } from '../state'; | 4 | import { workspacesState } from '../state'; |
5 | import WorkspacesDashboard from '../components/WorkspacesDashboard'; | 5 | import WorkspacesDashboard from '../components/WorkspacesDashboard'; |
6 | import ErrorBoundary from '../../../components/util/ErrorBoundary'; | 6 | import ErrorBoundary from '../../../components/util/ErrorBoundary'; |
7 | 7 | ||
@@ -20,10 +20,10 @@ class WorkspacesScreen extends Component { | |||
20 | return ( | 20 | return ( |
21 | <ErrorBoundary> | 21 | <ErrorBoundary> |
22 | <WorkspacesDashboard | 22 | <WorkspacesDashboard |
23 | workspaces={state.workspaces} | 23 | workspaces={workspacesState.workspaces} |
24 | isLoading={state.isLoading} | 24 | isLoading={workspacesState.isLoading} |
25 | onCreateWorkspaceSubmit={data => actions.workspace.create(data)} | 25 | onCreateWorkspaceSubmit={data => actions.workspaces.create(data)} |
26 | onWorkspaceClick={w => actions.workspace.edit({ workspace: w })} | 26 | onWorkspaceClick={w => actions.workspaces.edit({ workspace: w })} |
27 | /> | 27 | /> |
28 | </ErrorBoundary> | 28 | </ErrorBoundary> |
29 | ); | 29 | ); |
diff --git a/src/features/workspaces/index.js b/src/features/workspaces/index.js index 50ac3b414..8091f49fc 100644 --- a/src/features/workspaces/index.js +++ b/src/features/workspaces/index.js | |||
@@ -1,14 +1,28 @@ | |||
1 | import { reaction } from 'mobx'; | 1 | import { reaction, runInAction } from 'mobx'; |
2 | import WorkspacesStore from './store'; | 2 | import WorkspacesStore from './store'; |
3 | import api from './api'; | 3 | import api from './api'; |
4 | import { state, resetState } from './state'; | 4 | import { workspacesState, resetState } from './state'; |
5 | 5 | ||
6 | const debug = require('debug')('Franz:feature:workspaces'); | 6 | const debug = require('debug')('Franz:feature:workspaces'); |
7 | 7 | ||
8 | let store = null; | 8 | let store = null; |
9 | 9 | ||
10 | export const filterServicesByActiveWorkspace = (services) => { | ||
11 | const { isFeatureActive, activeWorkspace } = workspacesState; | ||
12 | if (isFeatureActive && activeWorkspace) { | ||
13 | return services.filter(s => activeWorkspace.services.includes(s.id)); | ||
14 | } | ||
15 | return services; | ||
16 | }; | ||
17 | |||
18 | export const getActiveWorkspaceServices = (services) => { | ||
19 | return filterServicesByActiveWorkspace(services); | ||
20 | }; | ||
21 | |||
10 | export default function initWorkspaces(stores, actions) { | 22 | export default function initWorkspaces(stores, actions) { |
11 | const { features, user } = stores; | 23 | const { features, user } = stores; |
24 | |||
25 | // Toggle workspace feature | ||
12 | reaction( | 26 | reaction( |
13 | () => ( | 27 | () => ( |
14 | features.features.isWorkspaceEnabled && ( | 28 | features.features.isWorkspaceEnabled && ( |
@@ -18,10 +32,12 @@ export default function initWorkspaces(stores, actions) { | |||
18 | (isEnabled) => { | 32 | (isEnabled) => { |
19 | if (isEnabled) { | 33 | if (isEnabled) { |
20 | debug('Initializing `workspaces` feature'); | 34 | debug('Initializing `workspaces` feature'); |
21 | store = new WorkspacesStore(stores, api, actions, state); | 35 | store = new WorkspacesStore(stores, api, actions, workspacesState); |
22 | store.initialize(); | 36 | store.initialize(); |
37 | runInAction(() => { workspacesState.isFeatureActive = true; }); | ||
23 | } else if (store) { | 38 | } else if (store) { |
24 | debug('Disabling `workspaces` feature'); | 39 | debug('Disabling `workspaces` feature'); |
40 | runInAction(() => { workspacesState.isFeatureActive = false; }); | ||
25 | store.teardown(); | 41 | store.teardown(); |
26 | store = null; | 42 | store = null; |
27 | resetState(); // Reset state to default | 43 | resetState(); // Reset state to default |
@@ -31,4 +47,22 @@ export default function initWorkspaces(stores, actions) { | |||
31 | fireImmediately: true, | 47 | fireImmediately: true, |
32 | }, | 48 | }, |
33 | ); | 49 | ); |
50 | |||
51 | // Update active service on workspace switches | ||
52 | reaction(() => ({ | ||
53 | isFeatureActive: workspacesState.isFeatureActive, | ||
54 | activeWorkspace: workspacesState.activeWorkspace, | ||
55 | }), ({ isFeatureActive, activeWorkspace }) => { | ||
56 | if (!isFeatureActive) return; | ||
57 | if (activeWorkspace) { | ||
58 | const services = stores.services.allDisplayed; | ||
59 | const activeService = services.find(s => s.isActive); | ||
60 | const workspaceServices = filterServicesByActiveWorkspace(services); | ||
61 | const isActiveServiceInWorkspace = workspaceServices.includes(activeService); | ||
62 | if (!isActiveServiceInWorkspace) { | ||
63 | console.log(workspaceServices[0].id); | ||
64 | actions.service.setActive({ serviceId: workspaceServices[0].id }); | ||
65 | } | ||
66 | } | ||
67 | }); | ||
34 | } | 68 | } |
diff --git a/src/features/workspaces/state.js b/src/features/workspaces/state.js index f938c1470..963b96f81 100644 --- a/src/features/workspaces/state.js +++ b/src/features/workspaces/state.js | |||
@@ -1,13 +1,15 @@ | |||
1 | import { observable } from 'mobx'; | 1 | import { observable } from 'mobx'; |
2 | 2 | ||
3 | const defaultState = { | 3 | const defaultState = { |
4 | activeWorkspace: null, | ||
4 | isLoading: false, | 5 | isLoading: false, |
6 | isFeatureActive: false, | ||
5 | workspaces: [], | 7 | workspaces: [], |
6 | workspaceBeingEdited: null, | 8 | workspaceBeingEdited: null, |
7 | }; | 9 | }; |
8 | 10 | ||
9 | export const state = observable(defaultState); | 11 | export const workspacesState = observable(defaultState); |
10 | 12 | ||
11 | export function resetState() { | 13 | export function resetState() { |
12 | Object.assign(state, defaultState); | 14 | Object.assign(workspacesState, defaultState); |
13 | } | 15 | } |
diff --git a/src/features/workspaces/store.js b/src/features/workspaces/store.js index 5cccb2ab7..a2997a0d2 100644 --- a/src/features/workspaces/store.js +++ b/src/features/workspaces/store.js | |||
@@ -1,8 +1,9 @@ | |||
1 | import { observable, reaction } from 'mobx'; | 1 | import { observable, reaction, action } from 'mobx'; |
2 | import Store from '../../stores/lib/Store'; | 2 | import Store from '../../stores/lib/Store'; |
3 | import CachedRequest from '../../stores/lib/CachedRequest'; | 3 | import CachedRequest from '../../stores/lib/CachedRequest'; |
4 | import Workspace from './models/Workspace'; | 4 | import Workspace from './models/Workspace'; |
5 | import { matchRoute } from '../../helpers/routing-helpers'; | 5 | import { matchRoute } from '../../helpers/routing-helpers'; |
6 | import workspaceActions from './actions'; | ||
6 | 7 | ||
7 | const debug = require('debug')('Franz:feature:workspaces'); | 8 | const debug = require('debug')('Franz:feature:workspaces'); |
8 | 9 | ||
@@ -48,28 +49,30 @@ export default class WorkspacesStore extends Store { | |||
48 | }, | 49 | }, |
49 | ); | 50 | ); |
50 | 51 | ||
51 | this.actions.workspace.edit.listen(this._edit); | 52 | workspaceActions.edit.listen(this._edit); |
52 | this.actions.workspace.create.listen(this._create); | 53 | workspaceActions.create.listen(this._create); |
53 | this.actions.workspace.delete.listen(this._delete); | 54 | workspaceActions.delete.listen(this._delete); |
54 | this.actions.workspace.update.listen(this._update); | 55 | workspaceActions.update.listen(this._update); |
56 | workspaceActions.activate.listen(this._setActiveWorkspace); | ||
57 | workspaceActions.deactivate.listen(this._deactivateActiveWorkspace); | ||
55 | } | 58 | } |
56 | 59 | ||
57 | _setWorkspaces = (workspaces) => { | 60 | _getWorkspaceById = id => this.state.workspaces.find(w => w.id === id); |
61 | |||
62 | @action _setWorkspaces = (workspaces) => { | ||
58 | debug('setting user workspaces', workspaces.slice()); | 63 | debug('setting user workspaces', workspaces.slice()); |
59 | this.state.workspaces = workspaces.map(data => new Workspace(data)); | 64 | this.state.workspaces = workspaces.map(data => new Workspace(data)); |
60 | }; | 65 | }; |
61 | 66 | ||
62 | _setIsLoading = (isLoading) => { | 67 | @action _setIsLoading = (isLoading) => { |
63 | this.state.isLoading = isLoading; | 68 | this.state.isLoading = isLoading; |
64 | }; | 69 | }; |
65 | 70 | ||
66 | _getWorkspaceById = id => this.state.workspaces.find(w => w.id === id); | 71 | @action _edit = ({ workspace }) => { |
67 | |||
68 | _edit = ({ workspace }) => { | ||
69 | this.stores.router.push(`/settings/workspaces/edit/${workspace.id}`); | 72 | this.stores.router.push(`/settings/workspaces/edit/${workspace.id}`); |
70 | }; | 73 | }; |
71 | 74 | ||
72 | _create = async ({ name }) => { | 75 | @action _create = async ({ name }) => { |
73 | try { | 76 | try { |
74 | const result = await this.api.createWorkspace(name); | 77 | const result = await this.api.createWorkspace(name); |
75 | const workspace = new Workspace(result); | 78 | const workspace = new Workspace(result); |
@@ -80,7 +83,7 @@ export default class WorkspacesStore extends Store { | |||
80 | } | 83 | } |
81 | }; | 84 | }; |
82 | 85 | ||
83 | _delete = async ({ workspace }) => { | 86 | @action _delete = async ({ workspace }) => { |
84 | try { | 87 | try { |
85 | await this.api.deleteWorkspace(workspace); | 88 | await this.api.deleteWorkspace(workspace); |
86 | this.state.workspaces.remove(workspace); | 89 | this.state.workspaces.remove(workspace); |
@@ -90,7 +93,7 @@ export default class WorkspacesStore extends Store { | |||
90 | } | 93 | } |
91 | }; | 94 | }; |
92 | 95 | ||
93 | _update = async ({ workspace }) => { | 96 | @action _update = async ({ workspace }) => { |
94 | try { | 97 | try { |
95 | await this.api.updateWorkspace(workspace); | 98 | await this.api.updateWorkspace(workspace); |
96 | const localWorkspace = this.state.workspaces.find(ws => ws.id === workspace.id); | 99 | const localWorkspace = this.state.workspaces.find(ws => ws.id === workspace.id); |
@@ -100,4 +103,12 @@ export default class WorkspacesStore extends Store { | |||
100 | throw error; | 103 | throw error; |
101 | } | 104 | } |
102 | }; | 105 | }; |
106 | |||
107 | @action _setActiveWorkspace = ({ workspace }) => { | ||
108 | this.state.activeWorkspace = workspace; | ||
109 | }; | ||
110 | |||
111 | @action _deactivateActiveWorkspace = () => { | ||
112 | this.state.activeWorkspace = null; | ||
113 | }; | ||
103 | } | 114 | } |