forked from doubtfire-lms/doubtfire-web
-
Notifications
You must be signed in to change notification settings - Fork 137
refactor: migrate units/index parent state to Angular #435
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
31Husain31
wants to merge
4
commits into
thoth-tech:9.x
Choose a base branch
from
31Husain31:migrate/inbox-pr1-units-index
base: 9.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+101
−112
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
cd66566
refactor: migrate units/index parent state to Angular
31Husain31 e4706bc
fix: remove stale import for deleted units/index module
31Husain31 222b285
fix: bridge Angular observables to AngularJS scope for child state co…
31Husain31 eb09aff
fix: bridge Angular observables to AngularJS scope for child state co…
31Husain31 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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 +1,8 @@ | ||
| <div ui-view="unitView"></div> | ||
| <div *ngIf="!(unit$ | async) || !(unitRole$ | async)" class="large-notice-block"> | ||
| <i class="fa fa-2x fa-spinner fa-pulse"></i> | ||
| <p>Loading unit details...</p> | ||
| </div> | ||
|
|
||
| <div *ngIf="(unit$ | async) && (unitRole$ | async)"> | ||
| <ui-view></ui-view> | ||
| </div> |
This file contains hidden or 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,90 +1,131 @@ | ||
| /* eslint-disable @typescript-eslint/no-explicit-any */ | ||
| import {CdkDragEnd, CdkDragMove, CdkDragStart} from '@angular/cdk/drag-drop'; | ||
| import {Component, Input, OnInit} from '@angular/core'; | ||
| import { | ||
| BehaviorSubject, | ||
| Observable, | ||
| Subject, | ||
| auditTime, | ||
| first, | ||
| merge, | ||
| of, | ||
| tap, | ||
| withLatestFrom, | ||
| } from 'rxjs'; | ||
| import {Unit, UnitService, UserService} from 'src/app/api/models/doubtfire-model'; | ||
| import { AppInjector } from '../app-injector'; | ||
| import { NgHybridStateDeclaration } from '@uirouter/angular-hybrid'; | ||
| import { GlobalStateService, ViewType } from '../projects/states/index/global-state.service'; | ||
| import { StateService } from '@uirouter/core'; | ||
| import { AlertService } from '../common/services/alert.service'; | ||
| import {Component, Input, OnInit, OnDestroy} from '@angular/core'; | ||
| import {Observable, Subscription} from 'rxjs'; | ||
| import {Unit, UnitRole} from 'src/app/api/models/doubtfire-model'; | ||
| import {AppInjector} from '../app-injector'; | ||
| import {NgHybridStateDeclaration} from '@uirouter/angular-hybrid'; | ||
| import {Ng2ViewDeclaration} from '@uirouter/angular'; | ||
| import {GlobalStateService, ViewType} from '../projects/states/index/global-state.service'; | ||
| import {StateService} from '@uirouter/core'; | ||
| import {AlertService} from '../common/services/alert.service'; | ||
| import {UnitService} from '../api/services/unit.service'; | ||
| import {UserService} from '../api/services/user.service'; | ||
| import {first} from 'rxjs/operators'; | ||
|
|
||
| @Component({ | ||
| selector: 'f-unit-root-state', | ||
| templateUrl: './unit-root-state.component.html', | ||
| styleUrl: './unit-root-state.component.css', | ||
| }) | ||
| export class UnitRootStateComponent { | ||
| export class UnitRootStateComponent implements OnInit, OnDestroy { | ||
| @Input() public unit$: Observable<Unit>; | ||
| @Input() public unitRole$: Observable<UnitRole>; | ||
|
|
||
| public unit: Unit; | ||
| public unitRole: UnitRole; | ||
|
|
||
| private subscriptions: Subscription[] = []; | ||
|
|
||
| ngOnInit(): void { | ||
| // Subscribe to observables and store values for Angular template | ||
| if (this.unit$) { | ||
| const unitSub = this.unit$.subscribe((unit) => { | ||
| this.unit = unit; | ||
| }); | ||
| this.subscriptions.push(unitSub); | ||
| } | ||
|
|
||
| if (this.unitRole$) { | ||
| const roleSub = this.unitRole$.subscribe((unitRole) => { | ||
| this.unitRole = unitRole; | ||
| }); | ||
| this.subscriptions.push(roleSub); | ||
| } | ||
| } | ||
|
|
||
| ngOnDestroy(): void { | ||
| // Clean up subscriptions | ||
| this.subscriptions.forEach(sub => sub.unsubscribe()); | ||
| } | ||
| } | ||
|
|
||
| export const UnitRootState: NgHybridStateDeclaration = { | ||
| name: 'unit-root-state', | ||
| url: '/units2/:unitId', | ||
| name: 'units/index', | ||
| url: '/units/:unitId', | ||
| abstract: true, | ||
| data: { | ||
| pageTitle: 'Unit Root State', | ||
| pageTitle: '_Home_', | ||
| roleWhitelist: ['Tutor', 'Convenor', 'Admin', 'Auditor'], | ||
| }, | ||
| views: { | ||
| main: { | ||
| component: UnitRootStateComponent, | ||
| }, | ||
| controller: function($scope, unit$, unitRole$) { | ||
| // Set observables on $scope for template binding to Angular component | ||
| $scope.unit$ = unit$; | ||
| $scope.unitRole$ = unitRole$; | ||
|
|
||
| // Subscribe and set values on $scope for AngularJS child states | ||
| unit$.subscribe((unit) => { | ||
| $scope.unit = unit; | ||
| }); | ||
| unitRole$.subscribe((unitRole) => { | ||
| $scope.unitRole = unitRole; | ||
| }); | ||
| }, | ||
| template: '<f-unit-root-state [unit$]="unit$" [unitRole$]="unitRole$"></f-unit-root-state>', | ||
| } as unknown as Ng2ViewDeclaration, | ||
| }, | ||
|
|
||
| resolve: { | ||
| unit$: function ($stateParams) { | ||
| const unitService = AppInjector.get(UnitService); | ||
| const stateService = AppInjector.get(StateService); | ||
| const alertService = AppInjector.get(AlertService); | ||
|
|
||
| const unitId = parseInt($stateParams.unitId); | ||
| if (!unitId) { | ||
| stateService.go('home'); | ||
| return; | ||
| } | ||
|
|
||
| return unitService.get(unitId).pipe( | ||
| first() | ||
| ); | ||
| }, | ||
| unitRole$: function ($stateParams, unit$) { | ||
| const globalState = AppInjector.get(GlobalStateService); | ||
| const userService = AppInjector.get(UserService); | ||
| const stateService = AppInjector.get(StateService); | ||
| const alertService = AppInjector.get(AlertService); | ||
|
|
||
| return new Observable<Unit>((observer) => { | ||
| return new Observable<UnitRole>((observer) => { | ||
| globalState.onLoad(() => { | ||
| const unitId: number = parseInt($stateParams.unitId); | ||
| let unitRole = globalState.loadedUnitRoles.currentValues.find( | ||
| (unitRole) => unitRole.unit.id === unitId, | ||
| const unitId = parseInt($stateParams.unitId); | ||
|
|
||
| let role = globalState.loadedUnitRoles.currentValues.find( | ||
| (ur) => ur.unit.id === unitId | ||
| ); | ||
|
|
||
| if ( | ||
| !unitRole && | ||
| (userService.currentUser.role == 'Admin' || userService.currentUser.role == 'Auditor') | ||
| ) { | ||
| unitRole = userService.adminOrAuditorRoleFor( | ||
| if (!role && (userService.currentUser.role === 'Admin' || userService.currentUser.role === 'Auditor')) { | ||
| role = userService.adminOrAuditorRoleFor( | ||
| userService.currentUser.role, | ||
| unitId, | ||
| userService.currentUser, | ||
| userService.currentUser | ||
| ); | ||
| } | ||
|
|
||
| // Go home if no unit role was found | ||
| if (!unitRole) { | ||
| console.log('No unit role found for unit', unitId); | ||
| return stateService.go('home'); | ||
| if (!role) { | ||
| alertService.error('You do not have access to this unit', 6000); | ||
| stateService.go('home'); | ||
| observer.complete(); | ||
| return; | ||
| } | ||
|
|
||
| unitService.get(unitId).subscribe({ | ||
| next: (unit: Unit) => { | ||
| observer.next(unit); | ||
| globalState.setView(ViewType.UNIT, unitRole); | ||
| observer.complete(); | ||
| }, | ||
| error: (err) => { | ||
| AppInjector.get(AlertService).error('Error loading unit: ' + err, 8000); | ||
| setTimeout(() => stateService.go('home'), 5000); | ||
| } | ||
| }); | ||
| globalState.setView(ViewType.UNIT, role); | ||
| observer.next(role); | ||
| observer.complete(); | ||
| }); | ||
| }).pipe(first()); | ||
| }, | ||
| }, | ||
| }; | ||
| }; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.