Groups.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <template>
  2. <responsive-width-wrapper>
  3. <h1 class="title has-text-grey-dark">
  4. {{ $t('groups.groups') }}
  5. </h1>
  6. <div class="is-size-7-mobile">
  7. {{ $t('groups.manage_groups_legend')}}
  8. </div>
  9. <div class="mt-3 mb-6">
  10. <router-link class="is-link mt-5" :to="{ name: 'createGroup' }">
  11. <font-awesome-icon :icon="['fas', 'plus-circle']" /> {{ $t('groups.create_group') }}
  12. </router-link>
  13. </div>
  14. <div v-if="groups.length > 0">
  15. <div v-for="group in groups" :key="group.id" class="group-item is-size-5 is-size-6-mobile">
  16. {{ group.name }}
  17. <!-- delete icon -->
  18. <button class="button tag is-pulled-right" :class="$root.showDarkMode ? 'is-dark' : 'is-white'" @click="deleteGroup(group.id)" :title="$t('commons.delete')">
  19. {{ $t('commons.delete') }}
  20. </button>
  21. <!-- edit link -->
  22. <router-link :to="{ name: 'editGroup', params: { id: group.id, name: group.name }}" class="has-text-grey px-1" :title="$t('commons.rename')">
  23. <font-awesome-icon :icon="['fas', 'pen-square']" />
  24. </router-link>
  25. <span class="is-family-primary is-size-6 is-size-7-mobile has-text-grey">{{ group.twofaccounts_count }} {{ $t('twofaccounts.accounts') }}</span>
  26. </div>
  27. <div class="mt-2 is-size-7 is-pulled-right" v-if="groups.length > 0">
  28. {{ $t('groups.deleting_group_does_not_delete_accounts')}}
  29. </div>
  30. </div>
  31. <div v-if="isFetching && groups.length === 0" class="has-text-centered">
  32. <span class="is-size-4">
  33. <font-awesome-icon :icon="['fas', 'spinner']" spin />
  34. </span>
  35. </div>
  36. <!-- footer -->
  37. <vue-footer :showButtons="true">
  38. <!-- close button -->
  39. <p class="control">
  40. <router-link :to="{ name: 'accounts', params: { toRefresh: true } }" class="button is-rounded" :class="{'is-dark' : $root.showDarkMode}">{{ $t('commons.close') }}</router-link>
  41. </p>
  42. </vue-footer>
  43. </responsive-width-wrapper>
  44. </template>
  45. <script>
  46. export default {
  47. data() {
  48. return {
  49. groups : [],
  50. TheAllGroup : null,
  51. isFetching: false,
  52. }
  53. },
  54. mounted() {
  55. // Load groups for localstorage at first to avoid latency
  56. const groups = this.$storage.get('groups', null) // use null as fallback if localstorage is empty
  57. // We don't want the pseudo group 'All' to be managed so we shift it
  58. if( groups ) {
  59. this.groups = groups
  60. this.TheAllGroup = this.groups.shift()
  61. }
  62. // we refresh the collection whatever
  63. this.fetchGroups()
  64. },
  65. methods: {
  66. /**
  67. * Get all groups from backend
  68. */
  69. async fetchGroups() {
  70. this.isFetching = true
  71. await this.axios.get('api/v1/groups').then(response => {
  72. const groups = []
  73. response.data.forEach((data) => {
  74. groups.push(data)
  75. })
  76. // Remove the 'All' pseudo group from the collection
  77. // and push it the TheAllGroup
  78. this.TheAllGroup = groups.shift()
  79. this.groups = groups
  80. })
  81. this.isFetching = false
  82. },
  83. /**
  84. * Delete a group (after confirmation)
  85. */
  86. deleteGroup(id) {
  87. if(confirm(this.$t('groups.confirm.delete'))) {
  88. this.axios.delete('/api/v1/groups/' + id)
  89. // Remove the deleted group from the collection
  90. this.groups = this.groups.filter(a => a.id !== id)
  91. this.$notify({ type: 'is-success', text: this.$t('groups.group_successfully_deleted') })
  92. // Reset persisted group filter to 'All' (groupId=0)
  93. // (backend will save to change automatically)
  94. if( parseInt(this.$root.appSettings.activeGroup) === id ) {
  95. this.$root.appSettings.activeGroup = 0
  96. }
  97. }
  98. }
  99. },
  100. beforeRouteLeave(to, from, next) {
  101. // reinject the 'All' pseudo group before refreshing the localstorage
  102. this.groups.unshift(this.TheAllGroup)
  103. this.$storage.set('groups', this.groups)
  104. next()
  105. }
  106. }
  107. </script>