admin-groups.vue 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. .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', @keyup.enter='createGroup')
  16. v-card-chin
  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(solo, append-icon='search', label='Search', single-line, hide-details, v-model='search')
  24. v-data-table(
  25. :items='groups',
  26. :headers='headers',
  27. :search='search',
  28. :pagination.sync='pagination',
  29. :rows-per-page-items='[15]'
  30. hide-actions
  31. )
  32. template(slot='items', slot-scope='props')
  33. tr.is-clickable(:active='props.selected', @click='$router.push("/groups/" + props.item.id)')
  34. td.text-xs-right {{ props.item.id }}
  35. td {{ props.item.name }}
  36. td {{ props.item.userCount }}
  37. td {{ props.item.createdAt | moment('calendar') }}
  38. td {{ props.item.updatedAt | moment('calendar') }}
  39. template(slot='no-data')
  40. v-alert.ma-3(icon='warning', :value='true', outline) No groups to display.
  41. .text-xs-center.py-2(v-if='groups.length > 15')
  42. v-pagination(v-model='pagination.page', :length='pages')
  43. </template>
  44. <script>
  45. import _ from 'lodash'
  46. import groupsQuery from 'gql/admin/groups/groups-query-list.gql'
  47. import createGroupMutation from 'gql/admin/groups/groups-mutation-create.gql'
  48. import deleteGroupMutation from 'gql/admin/groups/groups-mutation-delete.gql'
  49. export default {
  50. data() {
  51. return {
  52. newGroupDialog: false,
  53. newGroupName: '',
  54. selectedGroup: {},
  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: 'Created', value: 'createdAt', width: 250 },
  62. { text: 'Last Updated', value: 'updatedAt', width: 250 }
  63. ],
  64. search: ''
  65. }
  66. },
  67. computed: {
  68. pages () {
  69. if (this.pagination.rowsPerPage == null || this.pagination.totalItems == null) {
  70. return 0
  71. }
  72. return Math.ceil(this.pagination.totalItems / this.pagination.rowsPerPage)
  73. }
  74. },
  75. methods: {
  76. async refresh() {
  77. await this.$apollo.queries.groups.refetch()
  78. this.$store.commit('showNotification', {
  79. message: 'Groups have been refreshed.',
  80. style: 'success',
  81. icon: 'cached'
  82. })
  83. },
  84. async createGroup() {
  85. this.newGroupDialog = false
  86. try {
  87. await this.$apollo.mutate({
  88. mutation: createGroupMutation,
  89. variables: {
  90. name: this.newGroupName
  91. },
  92. update (store, resp) {
  93. const data = _.get(resp, 'data.groups.create', { responseResult: {} })
  94. if (data.responseResult.succeeded === true) {
  95. const apolloData = store.readQuery({ query: groupsQuery })
  96. data.group.userCount = 0
  97. apolloData.groups.list.push(data.group)
  98. store.writeQuery({ query: groupsQuery, data: apolloData })
  99. } else {
  100. throw new Error(data.responseResult.message)
  101. }
  102. },
  103. watchLoading (isLoading) {
  104. this.$store.commit(`loading${isLoading ? 'Start' : 'Stop'}`, 'admin-groups-create')
  105. }
  106. })
  107. this.newGroupName = ''
  108. this.$store.commit('showNotification', {
  109. style: 'success',
  110. message: `Group has been created successfully.`,
  111. icon: 'check'
  112. })
  113. } catch (err) {
  114. this.$store.commit('showNotification', {
  115. style: 'red',
  116. message: err.message,
  117. icon: 'warning'
  118. })
  119. }
  120. },
  121. async deleteGroupConfirm(group) {
  122. this.deleteGroupDialog = true
  123. this.selectedGroup = group
  124. },
  125. async deleteGroup() {
  126. this.deleteGroupDialog = false
  127. try {
  128. await this.$apollo.mutate({
  129. mutation: deleteGroupMutation,
  130. variables: {
  131. id: this.selectedGroup.id
  132. },
  133. watchLoading (isLoading) {
  134. this.$store.commit(`loading${isLoading ? 'Start' : 'Stop'}`, 'admin-groups-delete')
  135. }
  136. })
  137. await this.$apollo.queries.groups.refetch()
  138. this.$store.commit('showNotification', {
  139. style: 'success',
  140. message: `Group ${this.selectedGroup.name} has been deleted.`,
  141. icon: 'delete'
  142. })
  143. } catch (err) {
  144. this.$store.commit('showNotification', {
  145. style: 'red',
  146. message: err.message,
  147. icon: 'warning'
  148. })
  149. }
  150. }
  151. },
  152. apollo: {
  153. groups: {
  154. query: groupsQuery,
  155. fetchPolicy: 'network-only',
  156. update: (data) => data.groups.list,
  157. watchLoading (isLoading) {
  158. this.$store.commit(`loading${isLoading ? 'Start' : 'Stop'}`, 'admin-groups-refresh')
  159. }
  160. }
  161. }
  162. }
  163. </script>
  164. <style lang='scss'>
  165. </style>