diff --git a/src/app/components/stream/status/poll/poll.component.ts b/src/app/components/stream/status/poll/poll.component.ts index c62c7e5d..1016ece8 100644 --- a/src/app/components/stream/status/poll/poll.component.ts +++ b/src/app/components/stream/status/poll/poll.component.ts @@ -2,6 +2,8 @@ import { Component, OnInit, Input } from '@angular/core'; import { Poll, PollOption } from '../../../../services/models/mastodon.interfaces'; import { AccountInfo } from '../../../../states/accounts.state'; +import { MastodonService } from '../../../../services/mastodon.service'; +import { NotificationService } from '../../../../services/notification.service'; @Component({ selector: 'app-poll', @@ -18,12 +20,14 @@ export class PollComponent implements OnInit { @Input() poll: Poll; @Input() provider: AccountInfo; - constructor() { } + constructor( + private notificationService: NotificationService, + private mastodonService: MastodonService) { } ngOnInit() { this.pollName = this.poll.id; - // this.poll.multiple = true; + //this.poll.multiple = true; if(this.poll.multiple){ this.choiceType = 'checkbox'; @@ -42,6 +46,13 @@ export class PollComponent implements OnInit { vote(): boolean { console.log(this.pollSelection); + this.mastodonService.voteOnPoll(this.provider, this.poll.id, this.pollSelection) + .then((poll: Poll) => { + this.poll = poll; + }) + .catch(err => { + this.notificationService.notifyHttpError(err); + }); return false; } diff --git a/src/app/services/mastodon.service.ts b/src/app/services/mastodon.service.ts index dce27b31..5dc797a5 100644 --- a/src/app/services/mastodon.service.ts +++ b/src/app/services/mastodon.service.ts @@ -2,12 +2,12 @@ import { Injectable } from '@angular/core'; import { HttpHeaders, HttpClient, HttpResponse } from '@angular/common/http'; import { ApiRoutes } from './models/api.settings'; -import { Account, Status, Results, Context, Relationship, Instance, Attachment, Notification, List } from "./models/mastodon.interfaces"; +import { Account, Status, Results, Context, Relationship, Instance, Attachment, Notification, List, Poll } from "./models/mastodon.interfaces"; import { AccountInfo } from '../states/accounts.state'; import { StreamTypeEnum, StreamElement } from '../states/streams.state'; @Injectable() -export class MastodonService { +export class MastodonService { private apiRoutes = new ApiRoutes(); constructor(private readonly httpClient: HttpClient) { } @@ -306,6 +306,14 @@ export class MastodonService { const headers = new HttpHeaders({ 'Authorization': `Bearer ${account.token.access_token}` }); return this.httpClient.delete(route, { headers: headers }).toPromise(); } + + voteOnPoll(account: AccountInfo, pollId: string, pollSelection: number[]): Promise { + let route = `https://${account.instance}${this.apiRoutes.voteOnPoll}`.replace('{0}', pollId); + route += `?${this.formatArray(pollSelection.map(x => x.toString()), 'choices')}`; + + const headers = new HttpHeaders({ 'Authorization': `Bearer ${account.token.access_token}` }); + return this.httpClient.post(route, null, { headers: headers }).toPromise(); + } } export enum VisibilityEnum { diff --git a/src/app/services/models/api.settings.ts b/src/app/services/models/api.settings.ts index 4cb3702c..09668996 100644 --- a/src/app/services/models/api.settings.ts +++ b/src/app/services/models/api.settings.ts @@ -59,4 +59,5 @@ export class ApiRoutes { deleteList = '/api/v1/lists/{0}'; addAccountToList = '/api/v1/lists/{0}/accounts'; removeAccountFromList = '/api/v1/lists/{0}/accounts'; + voteOnPoll = '/api/v1/polls/{0}/votes'; }