From 07d10ad573d36d460acabe282d6020487e95c090 Mon Sep 17 00:00:00 2001 From: Dominik Guzei Date: Wed, 3 Apr 2019 17:53:50 +0200 Subject: add open last used workspace logic --- src/features/workspaces/store.js | 40 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 3 deletions(-) (limited to 'src/features/workspaces/store.js') 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'; import { createWorkspaceRequest, deleteWorkspaceRequest, - getUserWorkspacesRequest, + getUserWorkspacesRequest, getWorkspaceSettingsRequest, setWorkspaceSettingsRequest, updateWorkspaceRequest, } from './api'; @@ -37,6 +37,14 @@ export default class WorkspacesStore extends FeatureStore { return getUserWorkspacesRequest.result || []; } + @computed get settings() { + return getWorkspaceSettingsRequest.result; + } + + @computed get userHasWorkspaces() { + return getUserWorkspacesRequest.wasExecuted && this.workspaces.length > 0; + } + @computed get isPremiumUpgradeRequired() { return this.isFeatureEnabled && !this.isFeatureActive; } @@ -62,9 +70,11 @@ export default class WorkspacesStore extends FeatureStore { this._setActiveServiceOnWorkspaceSwitchReaction, this._setFeatureEnabledReaction, this._setIsPremiumFeatureReaction, + this._activateLastUsedWorkspaceReaction, ]); getUserWorkspacesRequest.execute(); + getWorkspaceSettingsRequest.execute(); this.isFeatureActive = true; } @@ -94,6 +104,13 @@ export default class WorkspacesStore extends FeatureStore { _getWorkspaceById = id => this.workspaces.find(w => w.id === id); + _updateSettings = (changes) => { + setWorkspaceSettingsRequest.execute({ + ...this.settings, + ...changes, + }); + }; + // Actions @action _edit = ({ workspace }) => { @@ -137,7 +154,10 @@ export default class WorkspacesStore extends FeatureStore { this.isSwitchingWorkspace = true; this.nextWorkspace = workspace; // Delay switching to next workspace so that the services loading does not drag down UI - setTimeout(() => { this.activeWorkspace = workspace; }, 100); + setTimeout(() => { + this.activeWorkspace = workspace; + this._updateSettings({ lastActiveWorkspace: workspace.id }); + }, 100); // Indicate that we are done switching to the next workspace setTimeout(() => { this.isSwitchingWorkspace = false; @@ -149,8 +169,12 @@ export default class WorkspacesStore extends FeatureStore { // Indicate that we are switching to default workspace this.isSwitchingWorkspace = true; this.nextWorkspace = null; + this._updateSettings({ lastActiveWorkspace: null }); + getWorkspaceSettingsRequest.execute(); // Delay switching to next workspace so that the services loading does not drag down UI - setTimeout(() => { this.activeWorkspace = null; }, 100); + setTimeout(() => { + this.activeWorkspace = null; + }, 100); // Indicate that we are done switching to the default workspace setTimeout(() => { this.isSwitchingWorkspace = false; }, 1000); }; @@ -195,4 +219,14 @@ export default class WorkspacesStore extends FeatureStore { } } }; + + _activateLastUsedWorkspaceReaction = () => { + if (!this.activeWorkspace && this.userHasWorkspaces) { + const { lastActiveWorkspace } = this.settings; + if (lastActiveWorkspace) { + const workspace = this._getWorkspaceById(lastActiveWorkspace); + if (workspace) this._setActiveWorkspace({ workspace }); + } + } + }; } -- cgit v1.2.3-54-g00ecf