diff --git a/src/app/components/qcm/qcm-resolver.service.ts b/src/app/components/qcm/qcm-resolver.service.ts new file mode 100644 index 0000000..31d9a6a --- /dev/null +++ b/src/app/components/qcm/qcm-resolver.service.ts @@ -0,0 +1,24 @@ +import { Injectable, } from '@angular/core'; +import { ActivatedRouteSnapshot, Resolve, Router, RouterStateSnapshot } from '@angular/router'; +import { Observable } from 'rxjs'; + +import { Question } from 'src/app/core/models'; +import { JumpsService } from 'src/app/core/services'; +import { catchError } from 'rxjs/operators'; + +@Injectable() +export class QCMResolver implements Resolve { + constructor( + private _jumpsService: JumpsService, + private router: Router + ) { } + + resolve( + route: ActivatedRouteSnapshot, + state: RouterStateSnapshot + ): Observable { + + return this._jumpsService.get(route.params['type']) + .pipe(catchError(() => this.router.navigateByUrl('/'))); + } +} diff --git a/src/app/components/qcm/qcm-routing.module.ts b/src/app/components/qcm/qcm-routing.module.ts new file mode 100644 index 0000000..736cd74 --- /dev/null +++ b/src/app/components/qcm/qcm-routing.module.ts @@ -0,0 +1,22 @@ +import { NgModule } from '@angular/core'; +import { Routes, RouterModule } from '@angular/router'; +import { QCMComponent } from './qcm.component'; +import { QCMResolver } from './qcm-resolver.service'; +import { AuthGuard } from 'src/app/core/services'; + +const routes: Routes = [ + { + path: ':type', + component: QCMComponent, + canActivate: [AuthGuard], + resolve: { + qcm: QCMResolver + } + } +]; + +@NgModule({ + imports: [RouterModule.forChild(routes)], + exports: [RouterModule] +}) +export class QCMRoutingModule { } diff --git a/src/app/components/qcm/qcm.component.html b/src/app/components/qcm/qcm.component.html new file mode 100644 index 0000000..f52e6b6 --- /dev/null +++ b/src/app/components/qcm/qcm.component.html @@ -0,0 +1 @@ +

qcm works!

diff --git a/src/app/components/qcm/qcm.component.scss b/src/app/components/qcm/qcm.component.scss new file mode 100644 index 0000000..e69de29 diff --git a/src/app/components/qcm/qcm.component.spec.ts b/src/app/components/qcm/qcm.component.spec.ts new file mode 100644 index 0000000..5e4c639 --- /dev/null +++ b/src/app/components/qcm/qcm.component.spec.ts @@ -0,0 +1,21 @@ +import { ComponentFixture, TestBed } from '@angular/core/testing'; + +import { QcmComponent } from './qcm.component'; + +describe('QcmComponent', () => { + let component: QcmComponent; + let fixture: ComponentFixture; + + beforeEach(() => { + TestBed.configureTestingModule({ + imports: [QcmComponent] + }); + fixture = TestBed.createComponent(QcmComponent); + component = fixture.componentInstance; + fixture.detectChanges(); + }); + + it('should create', () => { + expect(component).toBeTruthy(); + }); +}); diff --git a/src/app/components/qcm/qcm.component.ts b/src/app/components/qcm/qcm.component.ts new file mode 100644 index 0000000..ba8bdd0 --- /dev/null +++ b/src/app/components/qcm/qcm.component.ts @@ -0,0 +1,47 @@ +import { Component, OnInit, OnDestroy } from '@angular/core'; +import { ActivatedRoute } from '@angular/router'; +import { CommonModule } from '@angular/common'; +import { Observable, Subscription } from 'rxjs'; + +import { Errors, QCM, User } from 'src/app/core/models'; +import { QCMService, UserService } from 'src/app/core/services'; + +@Component({ + selector: 'huapp-qcm', + standalone: true, + imports: [CommonModule], + templateUrl: './qcm.component.html', + styleUrls: ['./qcm.component.scss'] +}) +export class QCMComponent implements OnInit, OnDestroy { + private _currentUser: Subscription = new Subscription(); + private _data: Subscription = new Subscription(); + public qcm: QCM = {} as QCM; + public currentUser: User = {} as User; + public canModify = false; + + constructor( + private route: ActivatedRoute, + private _qcmService: QCMService, + private _userService: UserService + ) { } + + ngOnInit() { + // Retreive the prefetched jump + const data$: Observable = this.route.data; + this._data = data$.subscribe((data: { qcm: QCM }) => { + this.qcm = data.qcm; + }); + // Load the current user's data + const currentUser$: Observable = this._userService.currentUser; + this._currentUser = currentUser$.subscribe((userData: User) => { + this.currentUser = userData; + this.canModify = userData.role === 'Admin'; + }); + } + + ngOnDestroy() { + this._data.unsubscribe(); + this._currentUser.unsubscribe(); + } +} diff --git a/src/app/components/qcm/qcm.module.ts b/src/app/components/qcm/qcm.module.ts new file mode 100644 index 0000000..8a9e508 --- /dev/null +++ b/src/app/components/qcm/qcm.module.ts @@ -0,0 +1,19 @@ +import { NgModule, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core'; + +import { QCMComponent } from './qcm.component'; +import { QCMResolver } from './qcm-resolver.service'; + +import { QCMRoutingModule } from './qcm-routing.module'; + + +@NgModule({ + imports: [ + QCMRoutingModule, + QCMComponent + ], + providers: [ + QCMResolver + ], + schemas: [CUSTOM_ELEMENTS_SCHEMA] +}) +export class QCMModule { }