Skip to content

Commit

Permalink
feat: add new global state service
Browse files Browse the repository at this point in the history
  • Loading branch information
jakerenzella committed Jan 12, 2022
1 parent 2f49d58 commit 99aa0dd
Showing 1 changed file with 74 additions and 9 deletions.
83 changes: 74 additions & 9 deletions src/app/projects/states/index/global-state.service.ts
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 });
}
}

0 comments on commit 99aa0dd

Please sign in to comment.