admin-groups-edit-users.vue 4.29 KB
Newer Older
1
<template lang="pug">
2 3
  v-card
    v-card-title.pb-4(:class='$vuetify.theme.dark ? `grey darken-3-d3` : `grey lighten-5`')
4
      v-text-field(
5
        outlined
6
        flat
7
        prepend-inner-icon='mdi-magnify'
8 9 10 11 12 13
        v-model='search'
        label='Search Group Users...'
        hide-details
      )
      v-spacer
      v-btn(color='primary', depressed, @click='searchUserDialog = true', :disabled='group.id === 2')
14
        v-icon(left) mdi-clipboard-account
15 16 17 18 19
        | Assign User
    v-data-table(
      :items='group.users',
      :headers='headers',
      :search='search'
20 21 22 23 24
      :page.sync='pagination'
      :items-per-page='15'
      @page-count='pageCount = $event'
      must-sort,
      hide-default-footer
25
    )
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
      template(v-slot:item.actions='{ item }')
        v-menu(bottom, right, min-width='200')
          template(v-slot:activator='{ on }')
            v-btn(icon, v-on='on', small)
              v-icon.grey--text.text--darken-1 mdi-dots-horizontal
          v-list(dense, nav)
            v-list-item(:to='`/users/` + item.id')
              v-list-item-action: v-icon(color='primary') mdi-account-outline
              v-list-item-content
                v-list-item-title View User Profile
            template(v-if='item.id !== 2')
              v-list-item(@click='unassignUser(item.id)')
                v-list-item-action: v-icon(color='orange') mdi-account-remove-outline
                v-list-item-content
                  v-list-item-title Unassign
41
      template(slot='no-data')
42
        v-alert.ma-3(icon='mdi-alert', outlined) No users to display.
43 44
    .text-center.py-2(v-if='group.users.length > 15')
      v-pagination(v-model='pagination', :length='pageCount')
45 46 47 48 49 50 51 52 53 54 55 56 57

    user-search(v-model='searchUserDialog', @select='assignUser')
</template>

<script>
import UserSearch from '../common/user-search.vue'

import assignUserMutation from 'gql/admin/groups/groups-mutation-assign.gql'
import unassignUserMutation from 'gql/admin/groups/groups-mutation-unassign.gql'

export default {
  props: {
    value: {
58 59
      type: Object,
      default: () => ({})
60 61 62 63 64 65 66 67
    }
  },
  components: {
    UserSearch
  },
  data() {
    return {
      headers: [
68
        { text: 'ID', value: 'id', width: 50 },
69 70
        { text: 'Name', value: 'name' },
        { text: 'Email', value: 'email' },
71
        { text: 'Actions', value: 'actions', sortable: false, width: 50 }
72 73
      ],
      searchUserDialog: false,
74 75
      pagination: 1,
      pageCount: 0,
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
      search: ''
    }
  },
  computed: {
    group: {
      get() { return this.value },
      set(val) { this.$set('input', val) }
    },
    pages () {
      if (this.pagination.rowsPerPage == null || this.pagination.totalItems == null) {
        return 0
      }

      return Math.ceil(this.pagination.totalItems / this.pagination.rowsPerPage)
    }
  },
  methods: {
    async assignUser(id) {
      try {
        await this.$apollo.mutate({
          mutation: assignUserMutation,
          variables: {
            groupId: this.group.id,
            userId: id
          },
          watchLoading (isLoading) {
            this.$store.commit(`loading${isLoading ? 'Start' : 'Stop'}`, 'admin-groups-assign')
          }
        })
        this.$store.commit('showNotification', {
          style: 'success',
          message: `User has been assigned to ${this.group.name}.`,
          icon: 'assignment_ind'
        })
        this.$emit('refresh')
      } catch (err) {
        this.$store.commit('showNotification', {
          style: 'red',
          message: err.message,
          icon: 'warning'
        })
      }
    },
    async unassignUser(id) {
      try {
        await this.$apollo.mutate({
          mutation: unassignUserMutation,
          variables: {
            groupId: this.group.id,
            userId: id
          },
          watchLoading (isLoading) {
            this.$store.commit(`loading${isLoading ? 'Start' : 'Stop'}`, 'admin-groups-unassign')
          }
        })
        this.$store.commit('showNotification', {
          style: 'success',
          message: `User has been unassigned from ${this.group.name}.`,
          icon: 'assignment_ind'
        })
        this.$emit('refresh')
      } catch (err) {
        this.$store.commit('showNotification', {
          style: 'red',
          message: err.message,
          icon: 'warning'
        })
      }
    }
  }
}
</script>