aboutsummaryrefslogtreecommitdiff
path: root/src/components/announcements_page/announcements_page.js
blob: 8d1204d4c9e164dca307bfa341dc8589017b34cc (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
import { mapState } from 'vuex'
import Announcement from '../announcement/announcement.vue'
import AnnouncementEditor from '../announcement_editor/announcement_editor.vue'

const AnnouncementsPage = {
  components: {
    Announcement,
    AnnouncementEditor
  },
  data () {
    return {
      newAnnouncement: {
        content: '',
        startsAt: undefined,
        endsAt: undefined,
        allDay: false
      },
      posting: false,
      error: undefined
    }
  },
  mounted () {
    this.$store.dispatch('fetchAnnouncements')
  },
  computed: {
    ...mapState({
      currentUser: state => state.users.currentUser
    }),
    announcements () {
      return this.$store.state.announcements.announcements
    },
    canPostAnnouncement () {
      return this.currentUser && this.currentUser.privileges.includes('announcements_manage_announcements')
    }
  },
  methods: {
    postAnnouncement () {
      this.posting = true
      this.$store.dispatch('postAnnouncement', this.newAnnouncement)
        .then(() => {
          this.newAnnouncement.content = ''
          this.startsAt = undefined
          this.endsAt = undefined
        })
        .catch(error => {
          this.error = error.error
        })
        .finally(() => {
          this.posting = false
        })
    },
    clearError () {
      this.error = undefined
    }
  }
}

export default AnnouncementsPage