aboutsummaryrefslogtreecommitdiff
path: root/src/components/edit_status_modal/edit_status_modal.js
blob: 75adfea755768962901cc72062920fc812f1f156 (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
74
75
import PostStatusForm from '../post_status_form/post_status_form.vue'
import Modal from '../modal/modal.vue'
import statusPosterService from '../../services/status_poster/status_poster.service.js'
import get from 'lodash/get'

const EditStatusModal = {
  components: {
    PostStatusForm,
    Modal
  },
  data () {
    return {
      resettingForm: false
    }
  },
  computed: {
    isLoggedIn () {
      return !!this.$store.state.users.currentUser
    },
    modalActivated () {
      return this.$store.state.editStatus.modalActivated
    },
    isFormVisible () {
      return this.isLoggedIn && !this.resettingForm && this.modalActivated
    },
    params () {
      return this.$store.state.editStatus.params || {}
    }
  },
  watch: {
    params (newVal, oldVal) {
      if (get(newVal, 'statusId') !== get(oldVal, 'statusId')) {
        this.resettingForm = true
        this.$nextTick(() => {
          this.resettingForm = false
        })
      }
    },
    isFormVisible (val) {
      if (val) {
        this.$nextTick(() => this.$el && this.$el.querySelector('textarea').focus())
      }
    }
  },
  methods: {
    doEditStatus ({ status, spoilerText, sensitive, media, contentType, poll }) {
      const params = {
        store: this.$store,
        statusId: this.$store.state.editStatus.params.statusId,
        status,
        spoilerText,
        sensitive,
        poll,
        media,
        contentType
      }

      return statusPosterService.editStatus(params)
        .then((data) => {
          return data
        })
        .catch((err) => {
          console.error('Error editing status', err)
          return {
            error: err.message
          }
        })
    },
    closeModal () {
      this.$store.dispatch('closeEditStatusModal')
    }
  }
}

export default EditStatusModal