From 0b037f96e634b412fbaaee24747df08afdc0e565 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thea=20Sch=C3=B6bl?= Date: Mon, 5 Sep 2022 13:16:00 +0000 Subject: [PATCH] feat: assessment tree view --- .../modules/assessments/assessments.module.ts | 2 + .../assessments/assessments.provider.ts | 53 +++++++++++++++- .../detail/assessments-detail.component.ts | 24 +++---- .../list/assessments-list-item.html | 1 + .../assessments/page/assessments-page.html | 62 ++++++++++++------- .../data/list/data-list-item.component.ts | 2 + src/app/modules/data/list/data-list-item.html | 2 +- .../modules/data/list/tree-list-fragment.html | 30 ++++++--- .../modules/data/list/tree-list-fragment.scss | 49 +++++++++++++++ 9 files changed, 175 insertions(+), 50 deletions(-) diff --git a/src/app/modules/assessments/assessments.module.ts b/src/app/modules/assessments/assessments.module.ts index b625f03d..a6eb1373 100644 --- a/src/app/modules/assessments/assessments.module.ts +++ b/src/app/modules/assessments/assessments.module.ts @@ -36,6 +36,7 @@ import {AssessmentsSimpleDataListComponent} from './list/assessments-simple-data import {ProtectedRoutes} from '../auth/protected.routes'; import {AssessmentsTreeListComponent} from './list/assessments-tree-list.component'; import {IonIconModule} from '../../util/ion-icon/ion-icon.module'; +import {UtilModule} from '../../util/util.module'; const routes: ProtectedRoutes = [ { @@ -75,6 +76,7 @@ const routes: ProtectedRoutes = [ DataModule, ThingTranslateModule, MomentModule, + UtilModule, ], providers: [AssessmentsProvider], exports: [], diff --git a/src/app/modules/assessments/assessments.provider.ts b/src/app/modules/assessments/assessments.provider.ts index 78d37bf2..492ef56f 100644 --- a/src/app/modules/assessments/assessments.provider.ts +++ b/src/app/modules/assessments/assessments.provider.ts @@ -15,9 +15,43 @@ import {Injectable} from '@angular/core'; import {ConfigProvider} from '../config/config.provider'; -import {SCAssessment} from '@openstapps/core'; +import {SCAssessment, SCUuid} from '@openstapps/core'; import {DefaultAuthService} from '../auth/default-auth.service'; import {HttpClient} from '@angular/common/http'; +import {uniqBy} from '../../_helpers/collections/uniq'; +import {keyBy} from '../../_helpers/collections/key-by'; + +/** + * + */ +export function toAssessmentMap( + data: SCAssessment[], +): Record { + return keyBy( + uniqBy( + [ + ...data, + ...data.flatMap( + assessment => + [...(assessment.superAssessments ?? [])] + .reverse() + .map((superAssessment, index, array) => { + const superAssessmentCopy = { + ...superAssessment, + } as SCAssessment; + superAssessmentCopy.origin = assessment.origin; + superAssessmentCopy.superAssessments = array + .slice(index + 1) + .reverse(); + return superAssessmentCopy; + }) ?? [], + ), + ] as SCAssessment[], + it => it.uid, + ), + it => it.uid, + ); +} @Injectable({ providedIn: 'root', @@ -29,6 +63,8 @@ export class AssessmentsProvider { // is very aggressive about too many requests being made to the server cache?: Promise; + assessments: Promise>; + cacheTimestamp = 0; // 15 minutes @@ -40,6 +76,16 @@ export class AssessmentsProvider { readonly http: HttpClient, ) {} + async getAssessment( + uid: SCUuid, + accessToken?: string | null, + forceFetch = false, + ): Promise { + await this.getAssessments(accessToken, forceFetch); + + return (await this.assessments)[uid]; + } + async getAssessments( accessToken?: string | null, forceFetch = false, @@ -53,6 +99,7 @@ export class AssessmentsProvider { this.cache = import('./assessment-mock-data.json').then( it => it.data as SCAssessment[], ); + this.assessments = this.cache.then(toAssessmentMap); } if ( @@ -60,7 +107,7 @@ export class AssessmentsProvider { !forceFetch && Date.now() - this.cacheTimestamp < this.cacheMaxAge ) { - return await this.cache; + return this.cache; } const url = this.configProvider.config.app.features.extern?.hisometry.url; @@ -77,8 +124,10 @@ export class AssessmentsProvider { .toPromise() .then(it => { this.cacheTimestamp = Date.now(); + return it?.data ?? []; }); + this.assessments = this.cache.then(toAssessmentMap); return this.cache; } diff --git a/src/app/modules/assessments/detail/assessments-detail.component.ts b/src/app/modules/assessments/detail/assessments-detail.component.ts index 42696ad0..2ab460c5 100644 --- a/src/app/modules/assessments/detail/assessments-detail.component.ts +++ b/src/app/modules/assessments/detail/assessments-detail.component.ts @@ -23,6 +23,7 @@ import { import {NavController, ViewWillEnter} from '@ionic/angular'; import {Subscription} from 'rxjs'; import {DataRoutingService} from '../../data/data-routing.service'; +import {SCAssessment} from '@openstapps/core'; @Component({ selector: 'assessments-detail', @@ -47,6 +48,8 @@ export class AssessmentsDetailComponent @ViewChild(DataDetailComponent) detailComponent: DataDetailComponent; + item: SCAssessment; + ngOnInit() { if (!this.dataPathAutoRouting) return; this.subscriptions.push( @@ -69,25 +72,14 @@ export class AssessmentsDetailComponent getItem(event: ExternalDataLoadEvent) { this.assessmentsProvider - .getAssessments( + .getAssessment( + event.uid, this.route.snapshot.queryParamMap.get('token'), event.forceReload, ) - .then(assessments => { - const assessment = assessments.find(it => it.uid === event.uid); - event.resolve( - assessment ?? - assessments - .flatMap(it => - Array.isArray(it.superAssessments) - ? it.superAssessments.map(superAssessment => ({ - ...superAssessment, - origin: it.origin, - })) - : [], - ) - .find(it => it?.uid === event.uid), - ); + .then(assessment => { + this.item = assessment; + event.resolve(this.item); }); } diff --git a/src/app/modules/assessments/list/assessments-list-item.html b/src/app/modules/assessments/list/assessments-list-item.html index 1e90d68b..4c48af0a 100644 --- a/src/app/modules/assessments/list/assessments-list-item.html +++ b/src/app/modules/assessments/list/assessments-list-item.html @@ -16,6 +16,7 @@ diff --git a/src/app/modules/assessments/page/assessments-page.html b/src/app/modules/assessments/page/assessments-page.html index 49ce639a..22c1ce24 100644 --- a/src/app/modules/assessments/page/assessments-page.html +++ b/src/app/modules/assessments/page/assessments-page.html @@ -1,3 +1,18 @@ + + @@ -7,30 +22,31 @@ - - -
- - {{ 'name' | thingTranslate: course }} ({{ - 'academicDegree' | thingTranslate: course - }}) - -
- - {{ key }} - -
-
+ + +
+ + {{ 'name' | thingTranslate: course }} ({{ + 'academicDegree' | thingTranslate: course + }}) + +
+ + {{ key }} + +
+
>; diff --git a/src/app/modules/data/list/data-list-item.html b/src/app/modules/data/list/data-list-item.html index 785eab6c..1845d8a5 100644 --- a/src/app/modules/data/list/data-list-item.html +++ b/src/app/modules/data/list/data-list-item.html @@ -16,7 +16,7 @@ diff --git a/src/app/modules/data/list/tree-list-fragment.html b/src/app/modules/data/list/tree-list-fragment.html index c9400299..bd4192c0 100644 --- a/src/app/modules/data/list/tree-list-fragment.html +++ b/src/app/modules/data/list/tree-list-fragment.html @@ -13,20 +13,34 @@ ~ this program. If not, see . --> - - - - {{ header.name }} - - - + + +
+ +
+ +
- +