forked from doubtfire-lms/doubtfire-deploy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2f49d58
commit 99aa0dd
Showing
1 changed file
with
74 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,85 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { Observable, Observer, Subject } from 'rxjs'; | ||
import { Inject, Injectable } from '@angular/core'; | ||
import { ReplaySubject } from 'rxjs'; | ||
import { projectService, unitService } from 'src/app/ajs-upgraded-providers'; | ||
|
||
export class DoubtfireViewState { | ||
public EntityObject: any; // Unit | Project | undefined | ||
public EntityType: 'unit' | 'project' | 'other' = 'other'; | ||
} | ||
|
||
export enum ViewType { | ||
UNIT = 'UNIT', | ||
PROJECT = 'PROJECT', | ||
OTHER = 'OTHER', | ||
} | ||
@Injectable({ | ||
providedIn: 'root', | ||
}) | ||
/** | ||
* The global state for the current user. This uses replay subjects, which acts as subjects, but allow | ||
* for subscribers to request the previously emitted value. | ||
* | ||
* This maintains two sets of values: | ||
* - Units taught and subjects studied | ||
* - Current view and selected entity | ||
*/ | ||
export class GlobalStateService { | ||
public unitRole: Subject<any> = new Subject<any>(); | ||
public project: Subject<any> = new Subject<any>(); | ||
/** | ||
* The current view and entity, indicating what kind of page is being shown. | ||
*/ | ||
public currentViewAndEntitySubject: ReplaySubject<{ viewType: ViewType; entity: {} }> = new ReplaySubject<{ | ||
viewType: ViewType; | ||
entity: {}; | ||
}>(); | ||
|
||
/** | ||
* A Unit Role for when a tutor is viewing a Project. | ||
*/ | ||
public unitRoleSubject: ReplaySubject<any> = new ReplaySubject<any>(); | ||
|
||
/** | ||
* The list of all of the units taught by the current user | ||
*/ | ||
public unitRolesSubject: ReplaySubject<any> = new ReplaySubject<any>(); | ||
|
||
constructor() {} | ||
/** | ||
* The list of all of the units studied by the current user | ||
*/ | ||
public projectsSubject: ReplaySubject<any> = new ReplaySubject<any>(); | ||
|
||
constructor( | ||
@Inject(unitService) private UnitService: any, | ||
@Inject(projectService) private ProjectService: any | ||
) { | ||
this.loadUnitsAndProjects(); | ||
} | ||
|
||
/** | ||
* Query the API for the units taught and studied by the current user. | ||
*/ | ||
public loadUnitsAndProjects() { | ||
//TODO: Consider sequence here? Can we adjust to fail once. | ||
this.UnitService.getUnitRoles((roles: any) => { | ||
this.unitRolesSubject.next(roles); | ||
}); | ||
|
||
this.ProjectService.getProjects(false, (projects: any) => { | ||
this.projectsSubject.next(projects); | ||
}); | ||
} | ||
|
||
public setUnitRole(unitRole: any) { | ||
this.unitRole.next(unitRole); | ||
/** | ||
* Clear all of the project and unit role data on sign out | ||
*/ | ||
public clearUnitsAndProjects() { | ||
this.unitRolesSubject.next(null); | ||
this.projectsSubject.next(null); | ||
} | ||
|
||
public setProject(project: any) { | ||
this.project.next(project); | ||
/** | ||
* Switch to a new view, and its associated entity object | ||
*/ | ||
public setView(kind: ViewType, entity?: any) { | ||
this.currentViewAndEntitySubject.next({ viewType: kind, entity: entity }); | ||
} | ||
} |