aboutsummaryrefslogtreecommitdiff
path: root/src/components/user_list_menu/user_list_menu.js
blob: 21996031bee5dec063e3f81aa717afbf96f2131c (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import { library } from '@fortawesome/fontawesome-svg-core'
import { faChevronRight } from '@fortawesome/free-solid-svg-icons'
import { mapState } from 'vuex'

import DialogModal from '../dialog_modal/dialog_modal.vue'
import Popover from '../popover/popover.vue'

library.add(faChevronRight)

const UserListMenu = {
  props: [
    'user'
  ],
  data () {
    return {}
  },
  components: {
    DialogModal,
    Popover
  },
  created () {
    this.$store.dispatch('fetchUserInLists', this.user.id)
  },
  computed: {
    ...mapState({
      allLists: state => state.lists.allLists
    }),
    inListsSet () {
      return new Set(this.user.inLists.map(x => x.id))
    },
    lists () {
      if (!this.user.inLists) return []
      return this.allLists.map(list => ({
        ...list,
        inList: this.inListsSet.has(list.id)
      }))
    }
  },
  methods: {
    toggleList (listId) {
      if (this.inListsSet.has(listId)) {
        this.$store.dispatch('removeListAccount', { accountId: this.user.id, listId }).then((response) => {
          if (!response.ok) { return }
          this.$store.dispatch('fetchUserInLists', this.user.id)
        })
      } else {
        this.$store.dispatch('addListAccount', { accountId: this.user.id, listId }).then((response) => {
          if (!response.ok) { return }
          this.$store.dispatch('fetchUserInLists', this.user.id)
        })
      }
    },
    toggleRight (right) {
      const store = this.$store
      if (this.user.rights[right]) {
        store.state.api.backendInteractor.deleteRight({ user: this.user, right }).then(response => {
          if (!response.ok) { return }
          store.commit('updateRight', { user: this.user, right, value: false })
        })
      } else {
        store.state.api.backendInteractor.addRight({ user: this.user, right }).then(response => {
          if (!response.ok) { return }
          store.commit('updateRight', { user: this.user, right, value: true })
        })
      }
    },
    toggleActivationStatus () {
      this.$store.dispatch('toggleActivationStatus', { user: this.user })
    },
    deleteUserDialog (show) {
      this.showDeleteUserDialog = show
    },
    deleteUser () {
      const store = this.$store
      const user = this.user
      const { id, name } = user
      store.state.api.backendInteractor.deleteUser({ user })
        .then(e => {
          this.$store.dispatch('markStatusesAsDeleted', status => user.id === status.user.id)
          const isProfile = this.$route.name === 'external-user-profile' || this.$route.name === 'user-profile'
          const isTargetUser = this.$route.params.name === name || this.$route.params.id === id
          if (isProfile && isTargetUser) {
            window.history.back()
          }
        })
    },
    setToggled (value) {
      this.toggled = value
    }
  }
}

export default UserListMenu