aboutsummaryrefslogtreecommitdiff
path: root/app/components/comment_form.js
blob: bf60d060c5fc3a57c93ac4872637ac7780fa6776 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import Axios from 'axios';
import Vue from 'vue';
import AuthService from '../services/auth';

const internals = {};

internals.kPostsRoute = '/api/posts';
internals.kCommentsRoute = '/comments';

export default internals.CommentFormComponent = Vue.component('comment-form', {
  template: '<div class="comment-form-container">' +
      '<p v-show="!active" class="comment-form-error">' +
      '<button class="comment-activate" v-on:click="activate">Add comment.</button>' +
      '</p>' +
      '<textarea v-show="active" :disabled="submitting" v-model="content" class="comment-content-input" placeholder="tell us something" maxlength=255></textarea>' +
      '<p v-show="message" class="comment-form-error">{{message}}</p>' +
      '<button v-show="active" :disabled="submitting" class="comment-submit" v-on:click="submit">Go.</button>' +
      '</div>',

  props: ['postUuid'],

  data() {

    return {
      content: '',
      message: '',
      active: false,
      submitting: false,
      authService: new AuthService()
    };
  },

  methods: {

    // Activates the form.

    activate() {

      this.active = true;
    },

    // Creates a comment

    submit() {

      this.submitting = true;
      const route = `${internals.kPostsRoute}/${this.postUuid}${internals.kCommentsRoute}`;

      return Axios({
        method: 'post',
        headers: {
          Authorization: `Bearer ${this.authService.token}`
        },
        data: {
          content: this.content
        },
        url: route
      }).then((response) => {

        this.$emit('comment-submitted', response.data);
        this.content = '';
        this.message = '';
        this.submitting = false;
        this.active = false;
      }).catch((err) => {

        console.error(err.stack);
        this.submitting = false;
        this.message = 'Error while creating the post...';
      });
    }
  }
});