added service #93

This commit is contained in:
Nicolas Constant 2019-06-04 19:33:36 -04:00
parent 55684d5ada
commit 979d21bdfe
No known key found for this signature in database
GPG Key ID: 1E9F677FB01A5688
3 changed files with 24 additions and 4 deletions

View File

@ -2,6 +2,8 @@ import { Component, OnInit, Input } from '@angular/core';
import { Poll, PollOption } from '../../../../services/models/mastodon.interfaces'; import { Poll, PollOption } from '../../../../services/models/mastodon.interfaces';
import { AccountInfo } from '../../../../states/accounts.state'; import { AccountInfo } from '../../../../states/accounts.state';
import { MastodonService } from '../../../../services/mastodon.service';
import { NotificationService } from '../../../../services/notification.service';
@Component({ @Component({
selector: 'app-poll', selector: 'app-poll',
@ -18,12 +20,14 @@ export class PollComponent implements OnInit {
@Input() poll: Poll; @Input() poll: Poll;
@Input() provider: AccountInfo; @Input() provider: AccountInfo;
constructor() { } constructor(
private notificationService: NotificationService,
private mastodonService: MastodonService) { }
ngOnInit() { ngOnInit() {
this.pollName = this.poll.id; this.pollName = this.poll.id;
// this.poll.multiple = true; //this.poll.multiple = true;
if(this.poll.multiple){ if(this.poll.multiple){
this.choiceType = 'checkbox'; this.choiceType = 'checkbox';
@ -42,6 +46,13 @@ export class PollComponent implements OnInit {
vote(): boolean { vote(): boolean {
console.log(this.pollSelection); 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; return false;
} }

View File

@ -2,12 +2,12 @@ import { Injectable } from '@angular/core';
import { HttpHeaders, HttpClient, HttpResponse } from '@angular/common/http'; import { HttpHeaders, HttpClient, HttpResponse } from '@angular/common/http';
import { ApiRoutes } from './models/api.settings'; 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 { AccountInfo } from '../states/accounts.state';
import { StreamTypeEnum, StreamElement } from '../states/streams.state'; import { StreamTypeEnum, StreamElement } from '../states/streams.state';
@Injectable() @Injectable()
export class MastodonService { export class MastodonService {
private apiRoutes = new ApiRoutes(); private apiRoutes = new ApiRoutes();
constructor(private readonly httpClient: HttpClient) { } constructor(private readonly httpClient: HttpClient) { }
@ -306,6 +306,14 @@ export class MastodonService {
const headers = new HttpHeaders({ 'Authorization': `Bearer ${account.token.access_token}` }); const headers = new HttpHeaders({ 'Authorization': `Bearer ${account.token.access_token}` });
return this.httpClient.delete(route, { headers: headers }).toPromise(); return this.httpClient.delete(route, { headers: headers }).toPromise();
} }
voteOnPoll(account: AccountInfo, pollId: string, pollSelection: number[]): Promise<Poll> {
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<Poll>(route, null, { headers: headers }).toPromise();
}
} }
export enum VisibilityEnum { export enum VisibilityEnum {

View File

@ -59,4 +59,5 @@ export class ApiRoutes {
deleteList = '/api/v1/lists/{0}'; deleteList = '/api/v1/lists/{0}';
addAccountToList = '/api/v1/lists/{0}/accounts'; addAccountToList = '/api/v1/lists/{0}/accounts';
removeAccountFromList = '/api/v1/lists/{0}/accounts'; removeAccountFromList = '/api/v1/lists/{0}/accounts';
voteOnPoll = '/api/v1/polls/{0}/votes';
} }