import { Store } from 'vuex' ; import Service from '@/services/Service' ; import User from '@/models/users/User' ; import Workspace from '@/models/soukai/Workspace' ; import EventBus from '@/utils/EventBus' ; import Storage from '@/utils/Storage' ; interface State { activeWorkspace : Workspace | null ; workspaces : Workspace [ ] ; } export default class Workspaces extends Service { public get empty ( ) : boolean { return ! this . storage . workspaces || this . storage . workspaces . length === 0 ; } public get active ( ) : Workspace | null { return this . storage . activeWorkspace ; } public get all ( ) : Workspace [ ] { return this . storage . workspaces ; } public async setActive ( workspace ) { this . app . $store . commit ( 'setActiveWorkspace' , workspace ) ; if ( workspace ) Storage . set ( 'activeWorkspaceId' , workspace . id ) ; else Storage . remove ( 'activeWorkspaceId' ) ; } public add ( workspace , activate = true ) { this . app . $store . commit ( 'addWorkspace' , workspace ) ; if ( activate ) this . setActive ( workspace ) ; } public remove ( workspace ) { this . app . $store . commit ( 'removeWorkspace' , workspace ) ; this . setActive ( this . all . length > 0 ? this . all [ 0 ] : null ) ; } protected get storage ( ) : State { return this . app . $store . state . workspaces ? this . app . $store . state . workspaces : { } ; } protected async init ( ) { await super . init ( ) ; await this . app . $auth . ready ; if ( this . app . $auth . isLoggedIn ( ) ) { await this . load ( this . app . $auth . user ) ; } EventBus . on ( 'login' , this . load . bind ( this ) ) ; EventBus . on ( 'logout' , this . unload . bind ( this ) ) ; } protected registerStoreModule ( store ) { store . registerModule ( 'workspaces' , { state : { activeWorkspace : null , workspaces : [ ] , } , mutations : { setActiveWorkspace ( state , activeWorkspace ) { state . activeWorkspace = activeWorkspace ; } , setWorkspaces ( state , workspaces ) { state . workspaces = workspaces ; } , addWorkspace ( state , workspace ) { state . workspaces . push ( workspace ) ; } , removeWorkspace ( state , workspace ) { const index = state . workspaces . findIndex ( existingWorkspace => existingWorkspace === workspace ) ; if ( index !== - 1 ) { state . workspaces . splice ( index , 1 ) ; } } , } , } ) ; } protected unregisterStoreModule ( store ) { store . unregisterModule ( 'workspaces' ) ; } protected async load ( user ) { const workspaces = [ ] ; const activeWorkspaceId = Storage . get ( 'activeWorkspaceId' ) ; const activeListId = Storage . get ( 'activeListId' ) ; let activeWorkspace ; for ( const storage of user . storages ) { workspaces . push ( ... ( await Workspace . from ( storage ) . all < Workspace > ( ) ) ) ; } activeWorkspace = workspaces . find ( workspace => workspace . id === activeWorkspaceId ) ; if ( ! activeWorkspace && workspaces . length > 0 ) activeWorkspace = workspaces [ 0 ] ; if ( activeWorkspace ) await this . loadWorkspace ( activeWorkspace , activeListId ) ; this . app . $store . commit ( 'setWorkspaces' , workspaces ) ; this . app . $store . commit ( 'setActiveWorkspace' , activeWorkspace ) ; } protected async unload ( ) { this . app . $store . commit ( 'setWorkspaces' , [ ] ) ; this . app . $store . commit ( 'setActiveWorkspace' , null ) ; Storage . remove ( 'activeWorkspaceId' ) ; Storage . remove ( 'activeListId' ) ; } private async loadWorkspace ( workspace , activeListId ) { if ( ! workspace . isRelationLoaded ( 'lists' ) ) { await workspace . loadRelation ( 'lists' ) ; for ( const list of workspace . lists ! ) { list . setRelationModels ( 'workspace' , workspace ) ; } } workspace . setActiveList ( workspace . lists ! . find ( list => list . id === activeListId ) || workspace . activeList ) ; if ( workspace && ! workspace . activeList . isRelationLoaded ( 'tasks' ) ) await workspace . activeList . loadRelation ( 'tasks' ) ; } }