diff --git a/src/app/components/header/header.component.ts b/src/app/components/header/header.component.ts index 1ee15e8..20f948d 100644 --- a/src/app/components/header/header.component.ts +++ b/src/app/components/header/header.component.ts @@ -9,9 +9,10 @@ import { TranslationService } from '../../services/translation.service'; import { ThemeService } from '../../services/theme.service'; import { TranslateModule } from '@ngx-translate/core'; import { ReactiveFormsModule } from '@angular/forms'; -import { RouterLink, RouterLinkActive } from '@angular/router'; +import { Router, RouterLink, RouterLinkActive } from '@angular/router'; import { NgClass, NgOptimizedImage } from '@angular/common'; import { WorkService } from '../../services/work.service'; +import { Observable } from 'rxjs'; @Component({ selector: 'app-header', @@ -32,12 +33,18 @@ export class HeaderComponent { version: string = '1.0'; isMenuOpen: boolean = false; isCheck: boolean = false; + linkLastWork: string = this.workService.getIdLastWorkByUserId(""); + @ViewChild('menuRef') menuRef!: ElementRef; @Input() themeClass!: string; @Input() themeService!: ThemeService; // Instanciation du service pour les actions de traduction - constructor(private translationService: TranslationService, private workService: WorkService) { } + constructor( + private router: Router, + private translationService: TranslationService, + private workService: WorkService) + { } // Méthode pour changer la langue onLanguageChange(event: Event) { @@ -65,8 +72,9 @@ export class HeaderComponent { } } - getLastWorkLink() : string { - return 'work/'.concat(this.workService.getIdLastWorkByUserId("")) + onLastWorkLink() : void { + const url = `/work/${this.linkLastWork}`; + this.router.navigateByUrl(url); } - + } diff --git a/src/app/components/work/work.component.ts b/src/app/components/work/work.component.ts index 00eef29..9b41323 100644 --- a/src/app/components/work/work.component.ts +++ b/src/app/components/work/work.component.ts @@ -1,5 +1,5 @@ import { Component, OnInit } from '@angular/core'; -import { Router, RouterLink } from '@angular/router'; +import { RouterLink, ActivatedRoute } from '@angular/router'; import { ThemeService } from '../../services/theme.service'; import { NgClass } from '@angular/common'; import { TranslateModule } from '@ngx-translate/core'; @@ -7,6 +7,7 @@ import {Work} from "../../models/work.model"; import {WorkService} from "../../services/work.service"; import {NgForOf} from "@angular/common"; import {FormsModule, NgForm} from "@angular/forms"; +import { Observable } from 'rxjs'; @Component({ selector: 'app-work', @@ -27,9 +28,9 @@ export class WorkComponent implements OnInit { themeClass!: string; works: Work[] = []; - + workEnCours: Observable = new Observable(); constructor( - private router: Router, + private route: ActivatedRoute, private themeService: ThemeService, protected workService: WorkService ) {} @@ -41,16 +42,13 @@ export class WorkComponent implements OnInit { : (this.themeClass = 'light-theme'); }); this.workService.getWorks().subscribe((response: Work[]) => this.works = response); + const link = String(this.route.snapshot.paramMap.get('link')); + if (link) { + this.workEnCours = this.workService.getWorkByLink(link); + } } - - // Si click sur "Work", on redirige vers la page des travaux - onContinue(): void { - this.router.navigateByUrl('/work'); - } - - onSubmit(form: NgForm) { - this.workService.postWork(form); + this.workService.postWork(form); } } diff --git a/src/app/services/work.service.ts b/src/app/services/work.service.ts index 0db2366..2081c68 100644 --- a/src/app/services/work.service.ts +++ b/src/app/services/work.service.ts @@ -20,6 +20,16 @@ export class WorkService { return this.http.get(`${this.API_URL}/works`); } + //je veux return un work en fonction d'un link passé en param + getWorkByLink(link: string): Observable { + return this.http.get(`${this.API_URL}/work/${link}`); + } + + //je veux return le link du dernier work d'un user (en l'occurence celui connecté) + getLinkLastWorkByIdUser(id_user: string): Observable { + return this.http.get(`${this.API_URL}/work/${id_user}`); + } + postWork(form: NgForm): void { let body = {link: crypto.randomUUID(), id_user: 1, id_language: 1, code: form.value.content, saveDate: new Date()} this.http.post(`${this.API_URL}/works`, body).subscribe();