|
| 1 | +import { Store } from 'vuex'; |
| 2 | + |
| 3 | +import Service from '@/services/Service'; |
| 4 | + |
| 5 | +import User from '@/models/users/User'; |
| 6 | +import Workspace from '@/models/soukai/Workspace'; |
| 7 | + |
| 8 | +import EventBus from '@/utils/EventBus'; |
| 9 | +import Storage from '@/utils/Storage'; |
| 10 | + |
| 11 | +interface State { |
| 12 | + activeWorkspace: Workspace | null; |
| 13 | + workspaces: Workspace[]; |
| 14 | +} |
| 15 | + |
| 16 | +export default class Workspaces extends Service { |
| 17 | + |
| 18 | + public get empty(): boolean { |
| 19 | + return !this.storage.workspaces || this.storage.workspaces.length === 0; |
| 20 | + } |
| 21 | + |
| 22 | + public get active(): Workspace | null { |
| 23 | + return this.storage.activeWorkspace; |
| 24 | + } |
| 25 | + |
| 26 | + public get all(): Workspace[] { |
| 27 | + return this.storage.workspaces; |
| 28 | + } |
| 29 | + |
| 30 | + public async setActive(workspace: Workspace | null): Promise<void> { |
| 31 | + this.app.$store.commit('setActiveWorkspace', workspace); |
| 32 | + |
| 33 | + if (workspace) |
| 34 | + Storage.set('activeWorkspaceId', workspace.id); |
| 35 | + else |
| 36 | + Storage.remove('activeWorkspaceId'); |
| 37 | + } |
| 38 | + |
| 39 | + public add(workspace: Workspace, activate: boolean = true): void { |
| 40 | + this.app.$store.commit('addWorkspace', workspace); |
| 41 | + |
| 42 | + if (activate) |
| 43 | + this.setActive(workspace); |
| 44 | + } |
| 45 | + |
| 46 | + public remove(workspace: Workspace): void { |
| 47 | + this.app.$store.commit('removeWorkspace', workspace); |
| 48 | + |
| 49 | + this.setActive(this.all.length > 0 ? this.all[0] : null); |
| 50 | + } |
| 51 | + |
| 52 | + protected get storage(): State { |
| 53 | + return this.app.$store.state.workspaces |
| 54 | + ? this.app.$store.state.workspaces |
| 55 | + : {}; |
| 56 | + } |
| 57 | + |
| 58 | + protected async init(): Promise<void> { |
| 59 | + await super.init(); |
| 60 | + await this.app.$auth.ready; |
| 61 | + |
| 62 | + if (this.app.$auth.isLoggedIn()) { |
| 63 | + await this.load(this.app.$auth.user); |
| 64 | + } |
| 65 | + |
| 66 | + EventBus.on('login', this.load.bind(this)); |
| 67 | + EventBus.on('logout', this.unload.bind(this)); |
| 68 | + } |
| 69 | + |
| 70 | + protected registerStoreModule(store: Store<State>): void { |
| 71 | + store.registerModule('workspaces', { |
| 72 | + state: { |
| 73 | + activeWorkspace: null, |
| 74 | + workspaces: [], |
| 75 | + }, |
| 76 | + mutations: { |
| 77 | + setActiveWorkspace(state: State, activeWorkspace: Workspace | null) { |
| 78 | + state.activeWorkspace = activeWorkspace; |
| 79 | + }, |
| 80 | + setWorkspaces(state: State, workspaces: Workspace[]) { |
| 81 | + state.workspaces = workspaces; |
| 82 | + }, |
| 83 | + addWorkspace(state: State, workspace: Workspace) { |
| 84 | + state.workspaces.push(workspace); |
| 85 | + }, |
| 86 | + removeWorkspace(state: State, workspace: Workspace) { |
| 87 | + const index = state.workspaces.findIndex(existingWorkspace => existingWorkspace === workspace); |
| 88 | + |
| 89 | + if (index !== -1) { |
| 90 | + state.workspaces.splice(index, 1); |
| 91 | + } |
| 92 | + }, |
| 93 | + }, |
| 94 | + }); |
| 95 | + } |
| 96 | + |
| 97 | + protected unregisterStoreModule(store: Store<State>): void { |
| 98 | + store.unregisterModule('workspaces'); |
| 99 | + } |
| 100 | + |
| 101 | + protected async load(user: User): Promise<void> { |
| 102 | + const workspaces: Workspace[] = []; |
| 103 | + const activeWorkspaceId = Storage.get('activeWorkspaceId'); |
| 104 | + const activeListId = Storage.get('activeListId'); |
| 105 | + |
| 106 | + let activeWorkspace; |
| 107 | + |
| 108 | + for (const storage of user.storages) { |
| 109 | + workspaces.push( |
| 110 | + ...(await Workspace.from(storage).all<Workspace>()), |
| 111 | + ); |
| 112 | + } |
| 113 | + |
| 114 | + activeWorkspace = workspaces.find(workspace => workspace.id === activeWorkspaceId); |
| 115 | + |
| 116 | + if (!activeWorkspace && workspaces.length > 0) |
| 117 | + activeWorkspace = workspaces[0]; |
| 118 | + |
| 119 | + if (activeWorkspace) |
| 120 | + await this.loadWorkspace(activeWorkspace, activeListId); |
| 121 | + |
| 122 | + this.app.$store.commit('setWorkspaces', workspaces); |
| 123 | + this.app.$store.commit('setActiveWorkspace', activeWorkspace); |
| 124 | + } |
| 125 | + |
| 126 | + protected async unload(): Promise<void> { |
| 127 | + this.app.$store.commit('setWorkspaces', []); |
| 128 | + this.app.$store.commit('setActiveWorkspace', null); |
| 129 | + |
| 130 | + Storage.remove('activeWorkspaceId'); |
| 131 | + Storage.remove('activeListId'); |
| 132 | + } |
| 133 | + |
| 134 | + private async loadWorkspace(workspace: Workspace, activeListId: string): Promise<void> { |
| 135 | + if (!workspace.isRelationLoaded('lists')) { |
| 136 | + await workspace.loadRelation('lists'); |
| 137 | + |
| 138 | + // TODO this could be done automatically in Soukai |
| 139 | + for (const list of workspace.lists!) { |
| 140 | + list.setRelationModels('workspace', workspace); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + workspace.setActiveList( |
| 145 | + workspace.lists!.find(list => list.id === activeListId) || |
| 146 | + workspace.activeList, |
| 147 | + ); |
| 148 | + |
| 149 | + if (workspace && !workspace.activeList.isRelationLoaded('tasks')) |
| 150 | + await workspace.activeList.loadRelation('tasks'); |
| 151 | + } |
| 152 | +} |
0 commit comments