|
|
|
@ -4,7 +4,10 @@ import { environment } from 'src/environments/environment';
|
|
|
|
|
|
|
|
|
|
import { Answer } from '../enums/answer.enum'; |
|
|
|
|
import { Poll } from '../models/poll.model'; |
|
|
|
|
import { HttpHeaders } from '@angular/common/http'; |
|
|
|
|
import { HttpClient, HttpHeaders } from '@angular/common/http'; |
|
|
|
|
import { Subscription } from 'rxjs'; |
|
|
|
|
import { ToastService } from './toast.service'; |
|
|
|
|
import { LoaderService } from './loader.service'; |
|
|
|
|
|
|
|
|
|
const apiVersion = environment.api.versionToUse; |
|
|
|
|
const currentApiRoutes = environment.api.version[apiVersion]; |
|
|
|
@ -16,6 +19,8 @@ const apiEndpoints = environment.api.endpoints;
|
|
|
|
|
providedIn: 'root', |
|
|
|
|
}) |
|
|
|
|
export class ApiService { |
|
|
|
|
private useDevLocalServer = true; |
|
|
|
|
private devLocalServerBaseHref = 'http://localhost:8000/'; |
|
|
|
|
private axiosInstance: AxiosInstance; |
|
|
|
|
|
|
|
|
|
private readonly pollsEndpoint = apiEndpoints.polls.name; |
|
|
|
@ -25,17 +30,42 @@ export class ApiService {
|
|
|
|
|
private readonly usersEndpoint = apiEndpoints.users.name; |
|
|
|
|
private readonly usersPollsEndpoint = apiEndpoints.users.polls.name; |
|
|
|
|
private readonly usersPollsSendEmailEndpoint = apiEndpoints.users.polls.sendEmail.name; |
|
|
|
|
private static loader: LoaderService; |
|
|
|
|
|
|
|
|
|
constructor() { |
|
|
|
|
constructor(private http: HttpClient, private loader: LoaderService, private toastService: ToastService) { |
|
|
|
|
this.axiosInstance = axios.create({ baseURL: apiBaseHref }); |
|
|
|
|
this.axiosInstance.defaults.timeout = 2500; |
|
|
|
|
this.axiosInstance.defaults.headers.post['Content-Type'] = 'application/json'; |
|
|
|
|
this.axiosInstance.defaults.headers.post['Accept'] = 'application/json'; |
|
|
|
|
this.axiosInstance.defaults.headers.post['Access-Control-Allow-Methods'] = 'GET, POST, PUT, DELETE, OPTIONS'; |
|
|
|
|
this.axiosInstance.defaults.headers.post['Access-Control-Allow-Origin'] = '*'; |
|
|
|
|
this.axiosInstance.defaults.headers.post['Charset'] = 'UTF-8'; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//////////////////////
|
|
|
|
|
// CREATE OR UPDATE //
|
|
|
|
|
//////////////////////
|
|
|
|
|
public async createPoll(poll: Poll): Promise<string> { |
|
|
|
|
public async createPoll(poll: Poll): Promise<Subscription> { |
|
|
|
|
// this.loader.setStatus(true);
|
|
|
|
|
console.log('config', poll); |
|
|
|
|
// const baseHref = this.useDevLocalServer ? 'http://localhost:8000' : apiBaseHref;
|
|
|
|
|
// return this.http
|
|
|
|
|
// .post(`${baseHref}${currentApiRoutes['api_new_poll']}`, poll, ApiService.makeHeaders())
|
|
|
|
|
// .subscribe(
|
|
|
|
|
// (res: Observable<any>) => {
|
|
|
|
|
// // redirect to the page to administrate the new poll
|
|
|
|
|
// this.toastService.display('Sondage Créé');
|
|
|
|
|
//
|
|
|
|
|
// console.log('res', res);
|
|
|
|
|
// // this.updateCurrentPollFromResponse(res);
|
|
|
|
|
//
|
|
|
|
|
// this.loader.setStatus(false);
|
|
|
|
|
// },
|
|
|
|
|
// (e) => {
|
|
|
|
|
// ApiService.handleError(e);
|
|
|
|
|
// }
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
console.log('currentApiRoutes', currentApiRoutes); |
|
|
|
|
return await this.axiosInstance.post(`${apiBaseHref}${currentApiRoutes['api_new_poll']}`, poll); |
|
|
|
@ -213,12 +243,13 @@ export class ApiService {
|
|
|
|
|
* prepare headers like the charset and json type for any call to the backend |
|
|
|
|
* @param bodyContent? |
|
|
|
|
*/ |
|
|
|
|
private makeHeaders(bodyContent?: any) { |
|
|
|
|
static makeHeaders(bodyContent?: any) { |
|
|
|
|
const headerDict = { |
|
|
|
|
Charset: 'UTF-8', |
|
|
|
|
'Content-Type': 'application/json', |
|
|
|
|
Accept: 'application/json', |
|
|
|
|
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS', |
|
|
|
|
'Access-Control-Allow-Headers': 'Accept,Accept-Language,Content-Language,Content-Type', |
|
|
|
|
'Access-Control-Allow-Origin': '*', |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
@ -231,6 +262,7 @@ export class ApiService {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private static handleError(error): void { |
|
|
|
|
// this.loader.setStatus(true);
|
|
|
|
|
if (error.response) { |
|
|
|
|
// The request was made and the server responded with a status code
|
|
|
|
|
// that falls out of the range of 2xx
|
|
|
|
|