funky-framadate-front/src/app/pages/poll-display/poll-display.component.ts

74 lines
2.3 KiB
TypeScript
Raw Normal View History

2020-01-15 17:55:22 +01:00
import {Component, OnInit} from '@angular/core';
import {BaseComponent} from "../base-page/base.component";
import {ConfigService} from "../../services/config.service";
2020-01-20 14:36:26 +01:00
import {mockComments} from "../../config/mocks/mock-comments";
2020-01-23 14:23:07 +01:00
import {ActivatedRoute, Router} from "@angular/router";
2020-01-15 17:55:22 +01:00
@Component({
selector: 'framadate-poll-display',
templateUrl: './poll-display.component.html',
styleUrls: ['./poll-display.component.scss']
})
export class PollDisplayComponent extends BaseComponent implements OnInit {
2020-01-21 12:04:14 +01:00
comments = mockComments;
2020-01-16 10:20:15 +01:00
2020-01-20 15:50:09 +01:00
constructor(public config: ConfigService,
2020-01-22 16:49:15 +01:00
private router: Router,
2020-01-20 15:50:09 +01:00
public activeRoute: ActivatedRoute) {
2020-01-15 17:55:22 +01:00
super(config);
2020-01-23 14:23:07 +01:00
this.activeRoute.paramMap.subscribe(params => {
console.log('params', params)
this.config.pollId = params.get('poll');
this.config.pollSlug = params.get('pollSlug');
if (!this.config.loading) {
2020-01-22 16:49:15 +01:00
this.fetchPoll();
}
});
2020-01-15 17:55:22 +01:00
}
ngOnInit() {
2020-01-22 16:49:15 +01:00
}
// fetch poll with its ID or slug.
fetchPoll() {
2020-01-20 15:50:09 +01:00
const id = this.activeRoute.snapshot.params.poll;
const pollSlug = this.activeRoute.snapshot.params.pollSlug;
2020-01-22 16:49:15 +01:00
if (id) {
this.config.loading = true;
2020-01-23 14:23:07 +01:00
this.config.pollId = id;
2020-01-20 15:50:09 +01:00
// store it in the poll property here
this.config.getPollById(id).subscribe(
(res: any) => {
console.log('res', res);
2020-01-23 14:23:07 +01:00
this.config.currentPoll = res;
this.config.loading = false;
}, (e) => {
// handle need for a password
this.config.handleError(e)
}
);
} else if (pollSlug) {
2020-01-22 16:49:15 +01:00
this.config.loading = true;
this.config.getPollByURL(pollSlug).subscribe(
(res: any) => {
2020-01-22 16:49:15 +01:00
console.log('res', res)
this.config.pollId = res.id;
this.config.currentPoll = res;
this.config.loading = false;
2020-01-20 15:50:09 +01:00
}, (e) => {
// handle need for a password
this.config.handleError(e)
}
);
}
2020-01-15 17:55:22 +01:00
}
}