diff --git a/src/app/components/editor/editor.component.ts b/src/app/components/editor/editor.component.ts index a35c238..db795f9 100644 --- a/src/app/components/editor/editor.component.ts +++ b/src/app/components/editor/editor.component.ts @@ -253,16 +253,24 @@ export class EditorComponent { a.click(); } - addToDatabase(): string { - return this.workService.postWork( + addToDatabase(): void { + this.workService.postWork( this.editorContent, this.selectedLanguage.name, - 1 - ); // replace 1 by current_user's id + ).subscribe((link) => { + this.prepareClipboard(link); + }); } shareButtonClicked() { - const link = this.#work?.link || this.addToDatabase(); + if (this.#work) { + this.prepareClipboard(this.#work.link); + } else { + this.addToDatabase(); + } + } + + prepareClipboard(link: string) { const path = this.locationStrategy.prepareExternalUrl(`/work/${link}`); const url = new URL(path, window.location.href); @@ -284,11 +292,14 @@ export class EditorComponent { this.editorContent, this.selectedLanguage.name ); - } - else { - const link = this.addToDatabase(); - const url = `/work/${link}`; - this.router.navigateByUrl(url); + } else { + this.workService.postWork( + this.editorContent, + this.selectedLanguage.name, + ).subscribe((link) => { + const url = `/work/${link}`; + this.router.navigateByUrl(url); + }); } } } diff --git a/src/app/components/login/login.component.ts b/src/app/components/login/login.component.ts index 51179e1..c193401 100644 --- a/src/app/components/login/login.component.ts +++ b/src/app/components/login/login.component.ts @@ -57,7 +57,7 @@ export class LoginComponent { this.userService .loginUser(formValue.login!, formValue.password!) - .subscribe((response) => { + .then((response) => { console.log('response :', response); if (response.success) { this.successLogin = 'Vous êtes connecté.'; diff --git a/src/app/components/works-list/works-list.component.ts b/src/app/components/works-list/works-list.component.ts index 8a2af82..4c40409 100644 --- a/src/app/components/works-list/works-list.component.ts +++ b/src/app/components/works-list/works-list.component.ts @@ -1,4 +1,4 @@ -import { Component } from '@angular/core'; +import { Component, OnInit } from '@angular/core'; import { Work } from '../../models/work.model'; import { WorkService } from '../../services/work.service'; import { NgForOf, SlicePipe } from '@angular/common'; @@ -12,7 +12,7 @@ import { WorkListDetailComponent } from '../work-list-detail/work-list-detail.co templateUrl: './works-list.component.html', styleUrl: './works-list.component.scss', }) -export class WorksListComponent { +export class WorksListComponent implements OnInit { works: Work[] = []; // TODO - REMOVE WHEN USER MANAGEMENT DONE @@ -21,7 +21,7 @@ export class WorksListComponent { constructor(protected workService: WorkService) {} ngOnInit() { - this.workService.getWorks().subscribe((works: Work[]) => { + this.workService.getWorks().then((works: Work[]) => { works.map((work: Work) => { if (work.user_id === this.FAKE_USER_ID) { this.works.push(work); diff --git a/src/app/services/user.service.ts b/src/app/services/user.service.ts index d2ca330..13c23e2 100644 --- a/src/app/services/user.service.ts +++ b/src/app/services/user.service.ts @@ -1,15 +1,12 @@ import { Injectable } from '@angular/core'; -import { User } from '../models/user.model'; -import { HttpClient, HttpResponse } from '@angular/common/http'; +import { HttpClient } from '@angular/common/http'; import { Observable } from 'rxjs'; -import { NgForm } from '@angular/forms'; +import { environment } from '../../environments/environment'; @Injectable({ providedIn: 'root', }) export class UserService { - API_URL = 'http://127.0.0.1:3000'; - constructor(private http: HttpClient) {} postUser( @@ -24,22 +21,27 @@ export class UserService { permissions: 0, }; - return this.http.post(`${this.API_URL}/users`, body); + return this.http.post(`${environment.apiUrl}/users`, body); } - loginUser(login: string, password: string): Observable { + loginUser(login: string, password: string): Promise { const body = { login: login, password: password, }; - return this.http.post(`${this.API_URL}/users/login`, body, { - withCredentials: true, - }); + return fetch(`${environment.apiUrl}/users/login`, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify(body), + credentials: 'include', + }).then((response) => response.json()); } logoutUser(): Observable { - return this.http.post(`${this.API_URL}/users/logout`, { + return this.http.post(`${environment.apiUrl}/users/logout`, { withCredentials: true, }); } diff --git a/src/app/services/work.service.ts b/src/app/services/work.service.ts index b46ff13..a5df748 100644 --- a/src/app/services/work.service.ts +++ b/src/app/services/work.service.ts @@ -1,51 +1,48 @@ import { Injectable } from '@angular/core'; import { Work } from '../models/work.model'; import { HttpClient } from '@angular/common/http'; -import { Observable } from 'rxjs'; +import { map, Observable } from 'rxjs'; import { NgForm } from '@angular/forms'; +import { environment } from '../../environments/environment'; @Injectable({ providedIn: 'root', }) export class WorkService { - API_URL = 'http://127.0.0.1:3000'; - - private works: Work[] = []; - constructor(private http: HttpClient) {} - getWorks(): Observable { - return this.http.get(`${this.API_URL}/works`); + getWorks(): Promise { + return fetch(`${environment.apiUrl}/works`, { + method: 'GET', + credentials: 'include', + }).then((response) => response.json()); } getWorkByLink(link: string): Observable { - return this.http.get(`${this.API_URL}/works/${link}`); + return this.http.get(`${environment.apiUrl}/works/${link}`); } saveWork(form: NgForm): void { const code = form.value.content; - this.http.post(`${this.API_URL}/works/save`, code).subscribe(); + this.http.post(`${environment.apiUrl}/works/save`, code, { withCredentials: true }); } - postWork(code: string, language: string, id_user: number): string { + postWork(code: string, language: string): Observable { const body = { - id_user, // tant que ça pas résolu -> je peux pas faire le share - link: crypto.randomUUID(), - language: language, + language, title: `Basic ${language}`, - code: code, + code, }; - this.http.post(`${this.API_URL}/works`, body).subscribe(); - return body.link; + return this.http.post(`${environment.apiUrl}/works`, body).pipe(map((work) => work.link)); } updateWork(id: string, code: string, language: string): void { - let body = { + const body = { newContent: code, language: language, }; - this.http.put(`${this.API_URL}/works/${id}/content`, body).subscribe(); + this.http.put(`${environment.apiUrl}/works/${id}/content`, body, { withCredentials: true }); } }