]> git.r.bdr.sh - rbdr/dasein/blame - app/components/posts.js
🔧 Update version in package.json
[rbdr/dasein] / app / components / posts.js
CommitLineData
a6ccda0f
RBR
1import Axios from 'axios';
2import Vue from 'vue';
3import AuthService from '../services/auth';
4
5import PostFormComponent from './post_form';
9b1f50fe 6import PostComponent from './post';
a6ccda0f
RBR
7import DatetimeFilter from '../filters/datetime';
8import UsertimeFilter from '../filters/usertime';
9
10const internals = {};
11
12internals.kPostsRoute = '/api/posts';
9b1f50fe 13internals.kPollFrequency = 5000;
a6ccda0f
RBR
14
15export default internals.PostsComponent = Vue.component('posts', {
9b1f50fe 16 template: '<div v-if="authService.authenticated" class="posts-container">' +
a6ccda0f
RBR
17 '<post-form v-on:post-submitted="addPost"></post-form>' +
18 '<h1>Posts.</h1>' +
19 '<p v-show="message" class="posts-error">{{message}}</p>' +
9b1f50fe 20 '<post v-for="post in posts" v-bind:post="post" :key="post.uuid"></post>' +
a6ccda0f
RBR
21 '</div>',
22
23 data() {
24
25 return {
26 message: '',
27 authService: new AuthService(),
28 posts: []
29 };
30 },
31
32 methods: {
33 fetchPosts() {
34
35 return Axios({
36 method: 'get',
37 headers: {
38 Authorization: `Bearer ${this.authService.token}`
39 },
40 url: internals.kPostsRoute
41 }).then((response) => {
42
43 this.posts = response.data;
9b1f50fe
RBR
44 if (!this._isBeingDestroyed) {
45 setTimeout(this.fetchPosts.bind(this), internals.kPollFrequency);
46 }
a6ccda0f
RBR
47 }).catch((err) => {
48
9b1f50fe 49 console.error(err.stack);
a6ccda0f
RBR
50 this.message = 'Error while loading the posts...';
51 });
52 },
53
54 addPost(post) {
55
56 this.posts.unshift(post);
57 }
58 },
59
60 components: {
61 postForm: PostFormComponent,
9b1f50fe 62 post: PostComponent,
a6ccda0f
RBR
63 datetime: DatetimeFilter,
64 usertime: UsertimeFilter
65 },
66
9b1f50fe 67 mounted() {
a6ccda0f
RBR
68
69 if (!this.authService.authenticated) {
70 return this.$router.push('/login');
71 }
72
9b1f50fe
RBR
73 this.fetchPosts();
74
a6ccda0f
RBR
75 }
76});
77