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