diff --git a/src/app/core/models/vote.model.ts b/src/app/core/models/vote.model.ts index fa6645c0..81b5174c 100644 --- a/src/app/core/models/vote.model.ts +++ b/src/app/core/models/vote.model.ts @@ -2,9 +2,12 @@ export class Vote { public choice_id: number = 0; public value: string = ''; // valeur de réponse - constructor(choice_id?) { + constructor(choice_id?, value?) { if (choice_id) { this.choice_id = choice_id; } + if (value) { + this.value = value; + } } } diff --git a/src/app/core/services/api.service.ts b/src/app/core/services/api.service.ts index 0cf3ae3b..b6895eb3 100644 --- a/src/app/core/services/api.service.ts +++ b/src/app/core/services/api.service.ts @@ -8,6 +8,7 @@ import { HttpClient, HttpHeaders } from '@angular/common/http'; import { Subscription } from 'rxjs'; import { ToastService } from './toast.service'; import { LoaderService } from './loader.service'; +import { Stack } from '../models/stack.model'; const apiVersion = environment.api.versionToUse; const currentApiRoutes = environment.api.version[apiVersion]; @@ -30,8 +31,11 @@ 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 baseHref: string; constructor(private http: HttpClient, private loader: LoaderService, private toastService: ToastService) { + this.baseHref = apiBaseHref; + this.axiosInstance = axios.create({ baseURL: apiBaseHref }); this.axiosInstance.defaults.timeout = 2500; this.axiosInstance.defaults.headers.post['Content-Type'] = 'application/json'; @@ -91,9 +95,9 @@ export class ApiService { public async createPoll(poll: Poll): Promise { // this.loader.setStatus(true); console.log('createPoll config', poll); - const baseHref = this.useDevLocalServer ? 'http://localhost:8000' : apiBaseHref; + return this.axiosInstance.post( - `${baseHref}${currentApiRoutes['api_new_poll']}`, + `${this.baseHref}${currentApiRoutes['api_new_poll']}`, poll, ApiService.makeHeaders() ); @@ -108,6 +112,24 @@ export class ApiService { // } } + /** + * send a new vote stack + * @param poll + * @param vote_stack + */ + public async sendVoteStackOfPoll(poll: Poll, vote_stack: Stack) { + // api_new_vote_stack POST ANY ANY /api/v1/poll/{id}/answer + + console.log('vote_stack', vote_stack); + console.log('this.baseHref', this.baseHref); + + try { + return await this.axiosInstance.post(`${this.pollsEndpoint}/${poll.custom_url}/answer`, vote_stack); + } catch (error) { + ApiService.handleError(error); + } + } + ////////// // READ // diff --git a/src/app/core/services/storage.service.ts b/src/app/core/services/storage.service.ts index b993298d..b4c72bbc 100644 --- a/src/app/core/services/storage.service.ts +++ b/src/app/core/services/storage.service.ts @@ -6,6 +6,7 @@ import { Theme } from '../enums/theme.enum'; import { Stack } from '../models/stack.model'; import { Choice } from '../models/choice.model'; import { Vote } from '../models/vote.model'; +import { environment } from '../../../environments/environment'; @Injectable({ providedIn: 'root', @@ -27,7 +28,11 @@ export class StorageService { this.vote_stack.votes = []; for (let choice of choices_list) { - this.vote_stack.votes.push(new Vote(choice.id)); + if (environment.autofill) { + this.vote_stack.votes.push(new Vote(choice.id, 'yes')); + } else { + this.vote_stack.votes.push(new Vote(choice.id)); + } } } diff --git a/src/app/features/consultation/consultation.component.ts b/src/app/features/consultation/consultation.component.ts index 0a02873e..b04da9f7 100644 --- a/src/app/features/consultation/consultation.component.ts +++ b/src/app/features/consultation/consultation.component.ts @@ -8,6 +8,7 @@ import { DateService } from '../../core/services/date.service'; import { PollUtilities } from '../old-stuff/config/PollUtilities'; import { Comment } from '../../core/models/comment.model'; import { StorageService } from '../../core/services/storage.service'; +import { ApiService } from '../../core/services/api.service'; @Component({ selector: 'app-consultation', @@ -35,6 +36,7 @@ export class ConsultationComponent implements OnInit, OnDestroy { private utils: PollUtilities, private _Activatedroute: ActivatedRoute, public storageService: StorageService, + public api: ApiService, public pollService: PollService, public dateService: DateService, private modalService: ModalService @@ -72,12 +74,16 @@ export class ConsultationComponent implements OnInit, OnDestroy { } } - updateVote(myVoteStack: boolean) { + updateVote() { alert('TODO'); } addVoteStack() { - alert('TODO'); + this.api.sendVoteStackOfPoll(this.poll, this.storageService.vote_stack).then((resp) => { + console.log('resp', resp); + + this.api.getPollBySlug(this.poll.custom_url); + }); } /** diff --git a/src/environments/environment.prod.ts b/src/environments/environment.prod.ts index 7f6e47f0..77223ef5 100644 --- a/src/environments/environment.prod.ts +++ b/src/environments/environment.prod.ts @@ -12,6 +12,7 @@ const apiV1 = { export const environment = { production: true, + autofill: true, appTitle: 'FramaDate Funky', appVersion: '2.1.0', appLogo: 'assets/img/logo.png', diff --git a/src/environments/environment.ts b/src/environments/environment.ts index 91c038d9..d9fa6cb8 100644 --- a/src/environments/environment.ts +++ b/src/environments/environment.ts @@ -10,12 +10,14 @@ const apiV1 = { baseHref: 'http://localhost:8000/api/v1', api_new_poll: '/poll/', api_get_poll: '/poll/{id}', + api_new_vote_stack: '/poll/{id}/answer', 'api_test-mail-poll': '/api/v1/poll/mail/test-mail-poll/{emailChoice}', 'app.swagger': '/api/doc.json', }; export const environment = { production: false, + autofill: true, appTitle: 'FramaDate Funky', appVersion: '2.1.0', appLogo: 'assets/img/logo.png',