Sync with API changes
continuous-integration/drone/pr Build is passing Details
continuous-integration/drone/push Build is passing Details

pull/15/head
Clément FRÉVILLE 10 months ago
parent 6f4e1476e2
commit bb5b4c0c2a

@ -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);
});
}
}
}

@ -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é.';

@ -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);

@ -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<Response>(`${this.API_URL}/users`, body);
return this.http.post<Response>(`${environment.apiUrl}/users`, body);
}
loginUser(login: string, password: string): Observable<Response> {
loginUser(login: string, password: string): Promise<Response> {
const body = {
login: login,
password: password,
};
return this.http.post<Response>(`${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<Response> {
return this.http.post<Response>(`${this.API_URL}/users/logout`, {
return this.http.post<Response>(`${environment.apiUrl}/users/logout`, {
withCredentials: true,
});
}

@ -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<Work[]> {
return this.http.get<Work[]>(`${this.API_URL}/works`);
getWorks(): Promise<Work[]> {
return fetch(`${environment.apiUrl}/works`, {
method: 'GET',
credentials: 'include',
}).then((response) => response.json());
}
getWorkByLink(link: string): Observable<Work | null> {
return this.http.get<Work>(`${this.API_URL}/works/${link}`);
return this.http.get<Work>(`${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<string> {
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<Work>(`${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<any>(`${this.API_URL}/works/${id}/content`, body).subscribe();
this.http.put(`${environment.apiUrl}/works/${id}/content`, body, { withCredentials: true });
}
}

Loading…
Cancel
Save