diff options
author | 2019-04-03 17:53:50 +0200 | |
---|---|---|
committer | 2019-04-03 17:54:57 +0200 | |
commit | 07d10ad573d36d460acabe282d6020487e95c090 (patch) | |
tree | 6517572b502c05bf355bdd9fcdedf76a6e41b536 | |
parent | give upgrade account button a bit more padding (diff) | |
download | ferdium-app-07d10ad573d36d460acabe282d6020487e95c090.tar.gz ferdium-app-07d10ad573d36d460acabe282d6020487e95c090.tar.zst ferdium-app-07d10ad573d36d460acabe282d6020487e95c090.zip |
add open last used workspace logic
-rw-r--r-- | src/features/workspaces/api.js | 11 | ||||
-rw-r--r-- | src/features/workspaces/store.js | 40 |
2 files changed, 48 insertions, 3 deletions
diff --git a/src/features/workspaces/api.js b/src/features/workspaces/api.js index 0ec20c9ea..0a3e2bfa4 100644 --- a/src/features/workspaces/api.js +++ b/src/features/workspaces/api.js | |||
@@ -1,4 +1,5 @@ | |||
1 | import { pick } from 'lodash'; | 1 | import { pick } from 'lodash'; |
2 | import localStorage from 'mobx-localstorage'; | ||
2 | import { sendAuthRequest } from '../../api/utils/auth'; | 3 | import { sendAuthRequest } from '../../api/utils/auth'; |
3 | import { API, API_VERSION } from '../../environment'; | 4 | import { API, API_VERSION } from '../../environment'; |
4 | import Request from '../../stores/lib/Request'; | 5 | import Request from '../../stores/lib/Request'; |
@@ -51,12 +52,22 @@ export const workspaceApi = { | |||
51 | if (!result.ok) throw result; | 52 | if (!result.ok) throw result; |
52 | return new Workspace(await result.json()); | 53 | return new Workspace(await result.json()); |
53 | }, | 54 | }, |
55 | |||
56 | getWorkspaceSettings: async () => ( | ||
57 | localStorage.getItem('workspaces') || {} | ||
58 | ), | ||
59 | |||
60 | setWorkspaceSettings: async settings => ( | ||
61 | localStorage.setItem('workspaces', settings) | ||
62 | ), | ||
54 | }; | 63 | }; |
55 | 64 | ||
56 | export const getUserWorkspacesRequest = new Request(workspaceApi, 'getUserWorkspaces'); | 65 | export const getUserWorkspacesRequest = new Request(workspaceApi, 'getUserWorkspaces'); |
57 | export const createWorkspaceRequest = new Request(workspaceApi, 'createWorkspace'); | 66 | export const createWorkspaceRequest = new Request(workspaceApi, 'createWorkspace'); |
58 | export const deleteWorkspaceRequest = new Request(workspaceApi, 'deleteWorkspace'); | 67 | export const deleteWorkspaceRequest = new Request(workspaceApi, 'deleteWorkspace'); |
59 | export const updateWorkspaceRequest = new Request(workspaceApi, 'updateWorkspace'); | 68 | export const updateWorkspaceRequest = new Request(workspaceApi, 'updateWorkspace'); |
69 | export const getWorkspaceSettingsRequest = new Request(workspaceApi, 'getWorkspaceSettings'); | ||
70 | export const setWorkspaceSettingsRequest = new Request(workspaceApi, 'setWorkspaceSettings'); | ||
60 | 71 | ||
61 | export const resetApiRequests = () => { | 72 | export const resetApiRequests = () => { |
62 | getUserWorkspacesRequest.reset(); | 73 | getUserWorkspacesRequest.reset(); |
diff --git a/src/features/workspaces/store.js b/src/features/workspaces/store.js index 712945bdc..2abb91c22 100644 --- a/src/features/workspaces/store.js +++ b/src/features/workspaces/store.js | |||
@@ -9,7 +9,7 @@ import { FeatureStore } from '../utils/FeatureStore'; | |||
9 | import { | 9 | import { |
10 | createWorkspaceRequest, | 10 | createWorkspaceRequest, |
11 | deleteWorkspaceRequest, | 11 | deleteWorkspaceRequest, |
12 | getUserWorkspacesRequest, | 12 | getUserWorkspacesRequest, getWorkspaceSettingsRequest, setWorkspaceSettingsRequest, |
13 | updateWorkspaceRequest, | 13 | updateWorkspaceRequest, |
14 | } from './api'; | 14 | } from './api'; |
15 | 15 | ||
@@ -37,6 +37,14 @@ export default class WorkspacesStore extends FeatureStore { | |||
37 | return getUserWorkspacesRequest.result || []; | 37 | return getUserWorkspacesRequest.result || []; |
38 | } | 38 | } |
39 | 39 | ||
40 | @computed get settings() { | ||
41 | return getWorkspaceSettingsRequest.result; | ||
42 | } | ||
43 | |||
44 | @computed get userHasWorkspaces() { | ||
45 | return getUserWorkspacesRequest.wasExecuted && this.workspaces.length > 0; | ||
46 | } | ||
47 | |||
40 | @computed get isPremiumUpgradeRequired() { | 48 | @computed get isPremiumUpgradeRequired() { |
41 | return this.isFeatureEnabled && !this.isFeatureActive; | 49 | return this.isFeatureEnabled && !this.isFeatureActive; |
42 | } | 50 | } |
@@ -62,9 +70,11 @@ export default class WorkspacesStore extends FeatureStore { | |||
62 | this._setActiveServiceOnWorkspaceSwitchReaction, | 70 | this._setActiveServiceOnWorkspaceSwitchReaction, |
63 | this._setFeatureEnabledReaction, | 71 | this._setFeatureEnabledReaction, |
64 | this._setIsPremiumFeatureReaction, | 72 | this._setIsPremiumFeatureReaction, |
73 | this._activateLastUsedWorkspaceReaction, | ||
65 | ]); | 74 | ]); |
66 | 75 | ||
67 | getUserWorkspacesRequest.execute(); | 76 | getUserWorkspacesRequest.execute(); |
77 | getWorkspaceSettingsRequest.execute(); | ||
68 | this.isFeatureActive = true; | 78 | this.isFeatureActive = true; |
69 | } | 79 | } |
70 | 80 | ||
@@ -94,6 +104,13 @@ export default class WorkspacesStore extends FeatureStore { | |||
94 | 104 | ||
95 | _getWorkspaceById = id => this.workspaces.find(w => w.id === id); | 105 | _getWorkspaceById = id => this.workspaces.find(w => w.id === id); |
96 | 106 | ||
107 | _updateSettings = (changes) => { | ||
108 | setWorkspaceSettingsRequest.execute({ | ||
109 | ...this.settings, | ||
110 | ...changes, | ||
111 | }); | ||
112 | }; | ||
113 | |||
97 | // Actions | 114 | // Actions |
98 | 115 | ||
99 | @action _edit = ({ workspace }) => { | 116 | @action _edit = ({ workspace }) => { |
@@ -137,7 +154,10 @@ export default class WorkspacesStore extends FeatureStore { | |||
137 | this.isSwitchingWorkspace = true; | 154 | this.isSwitchingWorkspace = true; |
138 | this.nextWorkspace = workspace; | 155 | this.nextWorkspace = workspace; |
139 | // Delay switching to next workspace so that the services loading does not drag down UI | 156 | // Delay switching to next workspace so that the services loading does not drag down UI |
140 | setTimeout(() => { this.activeWorkspace = workspace; }, 100); | 157 | setTimeout(() => { |
158 | this.activeWorkspace = workspace; | ||
159 | this._updateSettings({ lastActiveWorkspace: workspace.id }); | ||
160 | }, 100); | ||
141 | // Indicate that we are done switching to the next workspace | 161 | // Indicate that we are done switching to the next workspace |
142 | setTimeout(() => { | 162 | setTimeout(() => { |
143 | this.isSwitchingWorkspace = false; | 163 | this.isSwitchingWorkspace = false; |
@@ -149,8 +169,12 @@ export default class WorkspacesStore extends FeatureStore { | |||
149 | // Indicate that we are switching to default workspace | 169 | // Indicate that we are switching to default workspace |
150 | this.isSwitchingWorkspace = true; | 170 | this.isSwitchingWorkspace = true; |
151 | this.nextWorkspace = null; | 171 | this.nextWorkspace = null; |
172 | this._updateSettings({ lastActiveWorkspace: null }); | ||
173 | getWorkspaceSettingsRequest.execute(); | ||
152 | // Delay switching to next workspace so that the services loading does not drag down UI | 174 | // Delay switching to next workspace so that the services loading does not drag down UI |
153 | setTimeout(() => { this.activeWorkspace = null; }, 100); | 175 | setTimeout(() => { |
176 | this.activeWorkspace = null; | ||
177 | }, 100); | ||
154 | // Indicate that we are done switching to the default workspace | 178 | // Indicate that we are done switching to the default workspace |
155 | setTimeout(() => { this.isSwitchingWorkspace = false; }, 1000); | 179 | setTimeout(() => { this.isSwitchingWorkspace = false; }, 1000); |
156 | }; | 180 | }; |
@@ -195,4 +219,14 @@ export default class WorkspacesStore extends FeatureStore { | |||
195 | } | 219 | } |
196 | } | 220 | } |
197 | }; | 221 | }; |
222 | |||
223 | _activateLastUsedWorkspaceReaction = () => { | ||
224 | if (!this.activeWorkspace && this.userHasWorkspaces) { | ||
225 | const { lastActiveWorkspace } = this.settings; | ||
226 | if (lastActiveWorkspace) { | ||
227 | const workspace = this._getWorkspaceById(lastActiveWorkspace); | ||
228 | if (workspace) this._setActiveWorkspace({ workspace }); | ||
229 | } | ||
230 | } | ||
231 | }; | ||
198 | } | 232 | } |