admin-groups.vue 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <template lang='pug'>
  2. v-card(flat)
  3. v-card(flat, tile, :color='$vuetify.dark ? "grey darken-4" : "grey lighten-5"').pa-3.pt-4
  4. .admin-header-icon: v-icon(size='80', color='grey lighten-2') people
  5. .headline.blue--text.text--darken-2 Groups
  6. .subheading.grey--text Manage groups and their permissions
  7. v-card
  8. v-card-title
  9. v-dialog(v-model='newGroupDialog', max-width='500')
  10. v-btn(color='primary', dark, slot='activator')
  11. v-icon(left) add
  12. | New Group
  13. v-card
  14. .dialog-header.is-short New Group
  15. v-card-text
  16. v-text-field(v-model='newGroupName', label='Group Name', autofocus, counter='255', @keyup.enter='createGroup')
  17. v-card-chin
  18. v-spacer
  19. v-btn(flat, @click='newGroupDialog = false') Cancel
  20. v-btn(color='primary', @click='createGroup') Create
  21. v-btn(icon, @click='refresh')
  22. v-icon.grey--text refresh
  23. v-spacer
  24. v-text-field(solo, append-icon='search', label='Search', single-line, hide-details, v-model='search')
  25. v-data-table(
  26. :items='groups'
  27. :headers='headers'
  28. :search='search'
  29. :pagination.sync='pagination'
  30. :rows-per-page-items='[15]'
  31. hide-actions
  32. )
  33. template(slot='items', slot-scope='props')
  34. tr.is-clickable(:active='props.selected', @click='$router.push("/groups/" + props.item.id)')
  35. td.text-xs-right {{ props.item.id }}
  36. td {{ props.item.name }}
  37. td {{ props.item.userCount }}
  38. td {{ props.item.createdAt | moment('calendar') }}
  39. td {{ props.item.updatedAt | moment('calendar') }}
  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/groups-query-list.gql'
  48. import createGroupMutation from 'gql/admin/groups/groups-mutation-create.gql'
  49. import deleteGroupMutation from 'gql/admin/groups/groups-mutation-delete.gql'
  50. export default {
  51. data() {
  52. return {
  53. newGroupDialog: false,
  54. newGroupName: '',
  55. selectedGroup: {},
  56. pagination: {},
  57. groups: [],
  58. headers: [
  59. { text: 'ID', value: 'id', width: 50, align: 'right' },
  60. { text: 'Name', value: 'name' },
  61. { text: 'Users', value: 'userCount', width: 200 },
  62. { text: 'Created', value: 'createdAt', width: 250 },
  63. { text: 'Last Updated', value: 'updatedAt', width: 250 }
  64. ],
  65. search: ''
  66. }
  67. },
  68. computed: {
  69. pages () {
  70. if (this.pagination.rowsPerPage == null || this.pagination.totalItems == null) {
  71. return 0
  72. }
  73. return Math.ceil(this.pagination.totalItems / this.pagination.rowsPerPage)
  74. }
  75. },
  76. methods: {
  77. async refresh() {
  78. await this.$apollo.queries.groups.refetch()
  79. this.$store.commit('showNotification', {
  80. message: 'Groups have been refreshed.',
  81. style: 'success',
  82. icon: 'cached'
  83. })
  84. },
  85. async createGroup() {
  86. this.newGroupDialog = false
  87. try {
  88. await this.$apollo.mutate({
  89. mutation: createGroupMutation,
  90. variables: {
  91. name: this.newGroupName
  92. },
  93. update (store, resp) {
  94. const data = _.get(resp, 'data.groups.create', { responseResult: {} })
  95. if (data.responseResult.succeeded === true) {
  96. const apolloData = store.readQuery({ query: groupsQuery })
  97. data.group.userCount = 0
  98. apolloData.groups.list.push(data.group)
  99. store.writeQuery({ query: groupsQuery, data: apolloData })
  100. } else {
  101. throw new Error(data.responseResult.message)
  102. }
  103. },
  104. watchLoading (isLoading) {
  105. this.$store.commit(`loading${isLoading ? 'Start' : 'Stop'}`, 'admin-groups-create')
  106. }
  107. })
  108. this.newGroupName = ''
  109. this.$store.commit('showNotification', {
  110. style: 'success',
  111. message: `Group has been created successfully.`,
  112. icon: 'check'
  113. })
  114. } catch (err) {
  115. this.$store.commit('showNotification', {
  116. style: 'red',
  117. message: err.message,
  118. icon: 'warning'
  119. })
  120. }
  121. },
  122. async deleteGroupConfirm(group) {
  123. this.deleteGroupDialog = true
  124. this.selectedGroup = group
  125. },
  126. async deleteGroup() {
  127. this.deleteGroupDialog = false
  128. try {
  129. await this.$apollo.mutate({
  130. mutation: deleteGroupMutation,
  131. variables: {
  132. id: this.selectedGroup.id
  133. },
  134. watchLoading (isLoading) {
  135. this.$store.commit(`loading${isLoading ? 'Start' : 'Stop'}`, 'admin-groups-delete')
  136. }
  137. })
  138. await this.$apollo.queries.groups.refetch()
  139. this.$store.commit('showNotification', {
  140. style: 'success',
  141. message: `Group ${this.selectedGroup.name} has been deleted.`,
  142. icon: 'delete'
  143. })
  144. } catch (err) {
  145. this.$store.commit('showNotification', {
  146. style: 'red',
  147. message: err.message,
  148. icon: 'warning'
  149. })
  150. }
  151. }
  152. },
  153. apollo: {
  154. groups: {
  155. query: groupsQuery,
  156. fetchPolicy: 'network-only',
  157. update: (data) => data.groups.list,
  158. watchLoading (isLoading) {
  159. this.$store.commit(`loading${isLoading ? 'Start' : 'Stop'}`, 'admin-groups-refresh')
  160. }
  161. }
  162. }
  163. }
  164. </script>
  165. <style lang='scss'>
  166. </style>