---|qcm| Ajout du component 'QCM'
This commit is contained in:
@@ -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<Question> {
|
||||
constructor(
|
||||
private _jumpsService: JumpsService,
|
||||
private router: Router
|
||||
) { }
|
||||
|
||||
resolve(
|
||||
route: ActivatedRouteSnapshot,
|
||||
state: RouterStateSnapshot
|
||||
): Observable<any> {
|
||||
|
||||
return this._jumpsService.get(route.params['type'])
|
||||
.pipe(catchError(() => this.router.navigateByUrl('/')));
|
||||
}
|
||||
}
|
||||
@@ -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 { }
|
||||
@@ -0,0 +1 @@
|
||||
<p>qcm works!</p>
|
||||
@@ -0,0 +1,21 @@
|
||||
import { ComponentFixture, TestBed } from '@angular/core/testing';
|
||||
|
||||
import { QcmComponent } from './qcm.component';
|
||||
|
||||
describe('QcmComponent', () => {
|
||||
let component: QcmComponent;
|
||||
let fixture: ComponentFixture<QcmComponent>;
|
||||
|
||||
beforeEach(() => {
|
||||
TestBed.configureTestingModule({
|
||||
imports: [QcmComponent]
|
||||
});
|
||||
fixture = TestBed.createComponent(QcmComponent);
|
||||
component = fixture.componentInstance;
|
||||
fixture.detectChanges();
|
||||
});
|
||||
|
||||
it('should create', () => {
|
||||
expect(component).toBeTruthy();
|
||||
});
|
||||
});
|
||||
@@ -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<any> = this.route.data;
|
||||
this._data = data$.subscribe((data: { qcm: QCM }) => {
|
||||
this.qcm = data.qcm;
|
||||
});
|
||||
// Load the current user's data
|
||||
const currentUser$: Observable<User> = this._userService.currentUser;
|
||||
this._currentUser = currentUser$.subscribe((userData: User) => {
|
||||
this.currentUser = userData;
|
||||
this.canModify = userData.role === 'Admin';
|
||||
});
|
||||
}
|
||||
|
||||
ngOnDestroy() {
|
||||
this._data.unsubscribe();
|
||||
this._currentUser.unsubscribe();
|
||||
}
|
||||
}
|
||||
@@ -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 { }
|
||||
Reference in New Issue
Block a user