use httpclient

This commit is contained in:
tykayn 2021-04-28 15:35:08 +02:00 committed by Baptiste Lemoine
parent 30d4adec37
commit e61542b652
2 changed files with 22 additions and 16 deletions

View File

@ -4,8 +4,8 @@ import { environment } from 'src/environments/environment';
import { Answer } from '../enums/answer.enum'; import { Answer } from '../enums/answer.enum';
import { Poll } from '../models/poll.model'; import { Poll } from '../models/poll.model';
import { HttpClient, HttpHeaders } from '@angular/common/http'; import { HttpClient } from '@angular/common/http';
import { Subscription } from 'rxjs'; import { Observable, Subscription } from 'rxjs';
import { ToastService } from './toast.service'; import { ToastService } from './toast.service';
import { LoaderService } from './loader.service'; import { LoaderService } from './loader.service';
import { Stack } from '../models/stack.model'; import { Stack } from '../models/stack.model';
@ -21,6 +21,7 @@ const apiEndpoints = environment.api.endpoints;
}) })
export class ApiService { export class ApiService {
private static loader: LoaderService; private static loader: LoaderService;
private http: HttpClient;
private useDevLocalServer = true; private useDevLocalServer = true;
private devLocalServerBaseHref = 'http://localhost:8000/'; private devLocalServerBaseHref = 'http://localhost:8000/';
private axiosInstance: AxiosInstance; private axiosInstance: AxiosInstance;
@ -59,19 +60,20 @@ export class ApiService {
static makeHeaders(bodyContent?: any) { static makeHeaders(bodyContent?: any) {
const headerDict = { const headerDict = {
Charset: 'UTF-8', Charset: 'UTF-8',
// 'Content-Type': 'application/json',
// Accept: 'application/json',
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/json', 'Content-Type': 'application/json',
Accept: 'application/json', mode: 'no-cors',
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS', 'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS',
'Access-Control-Allow-Headers': 'Accept,Accept-Language,Content-Language,Content-Type', 'Access-Control-Allow-Headers': 'Accept,Accept-Language,Content-Language,Content-Type',
'Access-Control-Allow-Origin': '*', // 'Access-Control-Allow-Origin': '*',
}; };
const requestOptions = { return {
headers: new HttpHeaders(headerDict), headers: headerDict,
body: bodyContent, body: bodyContent,
}; };
return requestOptions;
} }
private static handleError(error): void { private static handleError(error): void {
@ -110,19 +112,23 @@ export class ApiService {
* @param poll * @param poll
* @param vote_stack * @param vote_stack
*/ */
public sendVoteStackOfPoll(poll: Poll, vote_stack: Stack) { public sendVoteStackOfPoll(poll: Poll, vote_stack: Stack): Observable<any> {
// api_new_vote_stack POST ANY ANY /api/v1/poll/{id}/answer // api_new_vote_stack POST ANY ANY /api/v1/poll/{id}/answer
console.log('vote_stack', vote_stack); console.log('vote_stack', vote_stack);
console.log('this.baseHref', this.baseHref); console.log('this.baseHref', this.baseHref);
let headers = ApiService.makeHeaders(vote_stack); let headers = ApiService.makeHeaders();
console.log('headers', headers); console.log('headers', headers);
let url = `${this.baseHref}/${poll.custom_url}/answer`;
try { let axiosconf = {
return this.axiosInstance.post(`${this.pollsEndpoint}/${poll.custom_url}/answer`, vote_stack, headers); url,
} catch (error) { method: 'POST',
ApiService.handleError(error); body: vote_stack,
} headers,
};
return this.http.post(url, vote_stack, headers);
} }
////////// //////////

View File

@ -79,7 +79,7 @@ export class ConsultationComponent implements OnInit, OnDestroy {
} }
addVoteStack() { addVoteStack() {
this.api.sendVoteStackOfPoll(this.poll, this.storageService.vote_stack).then((resp) => { this.api.sendVoteStackOfPoll(this.poll, this.storageService.vote_stack).subscribe((resp) => {
console.log('resp', resp); console.log('resp', resp);
this.api.getPollBySlug(this.poll.custom_url); this.api.getPollBySlug(this.poll.custom_url);