admin-groups.vue 5.6 KB

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