admin-groups.vue 5.79 KB
Newer Older
1
<template lang='pug'>
2 3 4 5
  v-container(fluid, grid-list-lg)
    v-layout(row wrap)
      v-flex(xs12)
        .admin-header
6
          img.animated.fadeInUp(src='/_assets/svg/icon-people.svg', alt='Groups', style='width: 80px;')
7
          .admin-header-title
Nick's avatar
Nick committed
8
            .headline.blue--text.text--darken-2.animated.fadeInLeft Groups
9
            .subtitle-1.grey--text.animated.fadeInLeft.wait-p4s Manage groups and their permissions
10
          v-spacer
11 12 13
          v-btn.animated.fadeInDown.wait-p3s(icon, outlined, color='grey', href='https://docs.requarks.io/groups', target='_blank')
            v-icon mdi-help-circle
          v-btn.animated.fadeInDown.wait-p2s.mx-3(color='grey', outlined, @click='refresh', icon)
14
            v-icon mdi-refresh
15
          v-dialog(v-model='newGroupDialog', max-width='500')
16 17 18 19 20
            template(v-slot:activator='{ on }')
              v-btn.animated.fadeInDown(color='primary', depressed, v-on='on', large)
                v-icon(left) mdi-plus
                span New Group
            v-card
21
              .dialog-header.is-short New Group
22
              v-card-text.pt-5
23
                v-text-field.md2(
24 25
                  outlined
                  prepend-icon='mdi-account-group'
26 27 28 29
                  v-model='newGroupName'
                  label='Group Name'
                  counter='255'
                  @keyup.enter='createGroup'
30
                  @keyup.esc='newGroupDialog = false'
31
                  ref='groupNameIpt'
32
                  )
33 34
              v-card-chin
                v-spacer
35
                v-btn(text, @click='newGroupDialog = false') Cancel
36
                v-btn(color='primary', @click='createGroup') Create
Nick's avatar
Nick committed
37
        v-card.mt-3.animated.fadeInUp
38 39 40 41
          v-data-table(
            :items='groups'
            :headers='headers'
            :search='search'
42 43 44 45 46 47
            :page.sync='pagination'
            :items-per-page='15'
            :loading='loading'
            @page-count='pageCount = $event'
            must-sort,
            hide-default-footer
48
          )
49
            template(slot='item', slot-scope='props')
50
              tr.is-clickable(:active='props.selected', @click='$router.push("/groups/" + props.item.id)')
51
                td {{ props.item.id }}
52
                td: strong {{ props.item.name }}
53 54 55
                td {{ props.item.userCount }}
                td {{ props.item.createdAt | moment('calendar') }}
                td {{ props.item.updatedAt | moment('calendar') }}
56 57
                td
                  v-tooltip(left, v-if='props.item.isSystem')
58 59
                    template(v-slot:activator='{ on }')
                      v-icon(v-on='on') mdi-lock-outline
60
                    span System Group
61
            template(slot='no-data')
62
              v-alert.ma-3(icon='mdi-alert', :value='true', outline) No groups to display.
63 64
          .text-xs-center.py-2(v-if='pageCount > 1')
            v-pagination(v-model='pagination', :length='pageCount')
65 66 67
</template>

<script>
68 69
import _ from 'lodash'

70 71
import groupsQuery from 'gql/admin/groups/groups-query-list.gql'
import createGroupMutation from 'gql/admin/groups/groups-mutation-create.gql'
72

73 74 75
export default {
  data() {
    return {
76 77
      newGroupDialog: false,
      newGroupName: '',
NGPixel's avatar
NGPixel committed
78
      selectedGroup: {},
79 80
      pagination: 1,
      pageCount: 0,
81
      groups: [],
82
      headers: [
83
        { text: 'ID', value: 'id', width: 80, sortable: true },
84 85
        { text: 'Name', value: 'name' },
        { text: 'Users', value: 'userCount', width: 200 },
NGPixel's avatar
NGPixel committed
86
        { text: 'Created', value: 'createdAt', width: 250 },
87 88
        { text: 'Last Updated', value: 'updatedAt', width: 250 },
        { text: '', value: 'isSystem', width: 20, sortable: false }
89
      ],
90 91
      search: '',
      loading: false
92 93
    }
  },
94 95 96 97 98 99 100 101 102
  watch: {
    newGroupDialog(newValue, oldValue) {
      if (newValue) {
        this.$nextTick(() => {
          this.$refs.groupNameIpt.focus()
        })
      }
    }
  },
103
  methods: {
104 105 106 107 108 109 110 111
    async refresh() {
      await this.$apollo.queries.groups.refetch()
      this.$store.commit('showNotification', {
        message: 'Groups have been refreshed.',
        style: 'success',
        icon: 'cached'
      })
    },
112
    async createGroup() {
113 114 115 116 117 118 119 120
      if (_.trim(this.newGroupName).length < 1) {
        this.$store.commit('showNotification', {
          style: 'red',
          message: 'Enter a group name.',
          icon: 'warning'
        })
        return
      }
121 122 123 124 125 126 127 128 129 130 131
      this.newGroupDialog = false
      try {
        await this.$apollo.mutate({
          mutation: createGroupMutation,
          variables: {
            name: this.newGroupName
          },
          update (store, resp) {
            const data = _.get(resp, 'data.groups.create', { responseResult: {} })
            if (data.responseResult.succeeded === true) {
              const apolloData = store.readQuery({ query: groupsQuery })
NGPixel's avatar
NGPixel committed
132
              data.group.userCount = 0
133 134 135 136 137 138 139 140 141 142
              apolloData.groups.list.push(data.group)
              store.writeQuery({ query: groupsQuery, data: apolloData })
            } else {
              throw new Error(data.responseResult.message)
            }
          },
          watchLoading (isLoading) {
            this.$store.commit(`loading${isLoading ? 'Start' : 'Stop'}`, 'admin-groups-create')
          }
        })
NGPixel's avatar
NGPixel committed
143
        this.newGroupName = ''
144 145 146 147 148 149
        this.$store.commit('showNotification', {
          style: 'success',
          message: `Group has been created successfully.`,
          icon: 'check'
        })
      } catch (err) {
150
        this.$store.commit('pushGraphError', err)
NGPixel's avatar
NGPixel committed
151
      }
152 153 154 155 156
    }
  },
  apollo: {
    groups: {
      query: groupsQuery,
157
      fetchPolicy: 'network-only',
158 159
      update: (data) => data.groups.list,
      watchLoading (isLoading) {
160
        this.loading = isLoading
161 162
        this.$store.commit(`loading${isLoading ? 'Start' : 'Stop'}`, 'admin-groups-refresh')
      }
163 164 165 166 167 168 169 170
    }
  }
}
</script>

<style lang='scss'>

</style>