admin-groups.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <template lang='pug'>
  2. v-card(flat)
  3. v-card(flat, color='grey lighten-5').pa-3.pt-4
  4. .headline.blue--text.text--darken-2 Groups
  5. .subheading.grey--text Manage groups and their permissions
  6. v-card
  7. v-card-title
  8. v-dialog(v-model='newGroupDialog', max-width='500')
  9. v-btn(color='primary', dark, slot='activator')
  10. v-icon(left) add
  11. | New Group
  12. v-card
  13. .dialog-header.is-short New Group
  14. v-card-text
  15. v-text-field(v-model='newGroupName', label='Group Name', autofocus, counter='255')
  16. v-card-actions
  17. v-spacer
  18. v-btn(flat, @click='newGroupDialog = false') Cancel
  19. v-btn(color='primary', @click='createGroup') Create
  20. v-btn(icon, @click='refresh')
  21. v-icon.grey--text refresh
  22. v-spacer
  23. v-text-field(append-icon='search', label='Search', single-line, hide-details, v-model='search')
  24. v-data-table(
  25. v-model='selected'
  26. :items='groups',
  27. :headers='headers',
  28. :search='search',
  29. :pagination.sync='pagination',
  30. :rows-per-page-items='[15]'
  31. hide-actions,
  32. disable-initial-sort
  33. )
  34. template(slot='items', slot-scope='props')
  35. tr(:active='props.selected')
  36. td.text-xs-right {{ props.item.id }}
  37. td {{ props.item.name }}
  38. td {{ props.item.userCount }}
  39. td: v-btn(icon): v-icon.grey--text.text--darken-1 more_horiz
  40. template(slot='no-data')
  41. v-alert.ma-3(icon='warning', :value='true', outline) No groups to display.
  42. .text-xs-center.py-2(v-if='groups.length > 15')
  43. v-pagination(v-model='pagination.page', :length='pages')
  44. </template>
  45. <script>
  46. import _ from 'lodash'
  47. import groupsQuery from 'gql/admin-groups-query-list.gql'
  48. import createGroupMutation from 'gql/admin-groups-mutation-create.gql'
  49. export default {
  50. data() {
  51. return {
  52. newGroupDialog: false,
  53. newGroupName: '',
  54. selected: [],
  55. pagination: {},
  56. groups: [],
  57. headers: [
  58. { text: 'ID', value: 'id', width: 50, align: 'right' },
  59. { text: 'Name', value: 'name' },
  60. { text: 'Users', value: 'userCount', width: 200 },
  61. { text: '', value: 'actions', sortable: false, width: 50 }
  62. ],
  63. search: ''
  64. }
  65. },
  66. computed: {
  67. pages () {
  68. if (this.pagination.rowsPerPage == null || this.pagination.totalItems == null) {
  69. return 0
  70. }
  71. return Math.ceil(this.pagination.totalItems / this.pagination.rowsPerPage)
  72. }
  73. },
  74. methods: {
  75. async refresh() {
  76. await this.$apollo.queries.groups.refetch()
  77. this.$store.commit('showNotification', {
  78. message: 'Groups have been refreshed.',
  79. style: 'success',
  80. icon: 'cached'
  81. })
  82. },
  83. async createGroup() {
  84. this.newGroupDialog = false
  85. try {
  86. await this.$apollo.mutate({
  87. mutation: createGroupMutation,
  88. variables: {
  89. name: this.newGroupName
  90. },
  91. update (store, resp) {
  92. const data = _.get(resp, 'data.groups.create', { responseResult: {} })
  93. if (data.responseResult.succeeded === true) {
  94. const apolloData = store.readQuery({ query: groupsQuery })
  95. apolloData.groups.list.push(data.group)
  96. store.writeQuery({ query: groupsQuery, data: apolloData })
  97. } else {
  98. throw new Error(data.responseResult.message)
  99. }
  100. },
  101. watchLoading (isLoading) {
  102. this.$store.commit(`loading${isLoading ? 'Start' : 'Stop'}`, 'admin-groups-create')
  103. }
  104. })
  105. this.$store.commit('showNotification', {
  106. style: 'success',
  107. message: `Group has been created successfully.`,
  108. icon: 'check'
  109. })
  110. } catch (err) {
  111. }
  112. }
  113. },
  114. apollo: {
  115. groups: {
  116. query: groupsQuery,
  117. update: (data) => data.groups.list,
  118. watchLoading (isLoading) {
  119. this.$store.commit(`loading${isLoading ? 'Start' : 'Stop'}`, 'admin-groups-refresh')
  120. }
  121. }
  122. }
  123. }
  124. </script>
  125. <style lang='scss'>
  126. </style>