From adf5ac074626dac5bfec9d8fb54b28149e492e1b Mon Sep 17 00:00:00 2001 From: Dominik Guzei Date: Wed, 10 Apr 2019 17:17:02 +0200 Subject: handle deleted services that are attached to workspaces --- src/features/workspaces/store.js | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) (limited to 'src/features') diff --git a/src/features/workspaces/store.js b/src/features/workspaces/store.js index ba48022c2..ea601700e 100644 --- a/src/features/workspaces/store.js +++ b/src/features/workspaces/store.js @@ -74,6 +74,7 @@ export default class WorkspacesStore extends FeatureStore { this._setIsPremiumFeatureReaction, this._activateLastUsedWorkspaceReaction, this._openDrawerWithSettingsReaction, + this._cleanupInvalidServiceReferences, ]); getUserWorkspacesRequest.execute(); @@ -92,16 +93,17 @@ export default class WorkspacesStore extends FeatureStore { filterServicesByActiveWorkspace = (services) => { const { activeWorkspace, isFeatureActive } = this; - - if (!isFeatureActive) return services; - if (activeWorkspace) { - return services.filter(s => ( - activeWorkspace.services.includes(s.id) - )); + if (isFeatureActive && activeWorkspace) { + return this.getWorkspaceServices(activeWorkspace); } return services; }; + getWorkspaceServices(workspace) { + const { services } = this.stores; + return workspace.services.map(id => services.one(id)).filter(s => !!s); + } + // ========== PRIVATE ========= // _wasDrawerOpenBeforeSettingsRoute = null; @@ -218,7 +220,8 @@ export default class WorkspacesStore extends FeatureStore { if (this.activeWorkspace) { const services = this.stores.services.allDisplayed; const activeService = services.find(s => s.isActive); - const workspaceServices = this.filterServicesByActiveWorkspace(services); + const workspaceServices = this.getWorkspaceServices(this.activeWorkspace); + if (workspaceServices.length <= 0) return; const isActiveServiceInWorkspace = workspaceServices.includes(activeService); if (!isActiveServiceInWorkspace) { this.actions.service.setActive({ serviceId: workspaceServices[0].id }); @@ -255,4 +258,19 @@ export default class WorkspacesStore extends FeatureStore { } } }; + + _cleanupInvalidServiceReferences = () => { + const { services } = this.stores; + let invalidServiceReferencesExist = false; + this.workspaces.forEach((workspace) => { + workspace.services.forEach((serviceId) => { + if (!services.one(serviceId)) { + invalidServiceReferencesExist = true; + } + }); + }); + if (invalidServiceReferencesExist) { + getUserWorkspacesRequest.execute(); + } + }; } -- cgit v1.2.3-54-g00ecf