admin-groups.vue 5.6 KB

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