Adrià Vilanova Martínez | f7ee658 | 2021-09-02 21:53:28 +0200 | [diff] [blame] | 1 | <script> |
Adrià Vilanova Martínez | 035b543 | 2021-09-03 14:16:59 +0200 | [diff] [blame] | 2 | import {mapGetters} from 'vuex'; |
| 3 | |
Adrià Vilanova Martínez | f7ee658 | 2021-09-02 21:53:28 +0200 | [diff] [blame] | 4 | import NotAuthorized from './NotAuthorized.vue'; |
| 5 | import Page from './utils/Page.vue'; |
| 6 | import AuthorizedUserDialog from '../components/AuthorizedUserDialog.vue'; |
| 7 | |
| 8 | //import * as grpcWeb from "grpc-web"; |
| 9 | import * as ksObjectsPb from '../api_proto/kill_switch_objects_pb.js'; |
| 10 | import * as ksPb from '../api_proto/kill_switch_pb.js'; |
| 11 | import {accessLevels} from '../consts.js'; |
| 12 | |
| 13 | export default { |
| 14 | data() { |
| 15 | let emptyUser = new ksObjectsPb.KillSwitchAuthorizedUser(); |
| 16 | |
| 17 | return { |
| 18 | users: [], |
Adrià Vilanova Martínez | 035b543 | 2021-09-03 14:16:59 +0200 | [diff] [blame] | 19 | currentUpdateUser: emptyUser.cloneMessage(), // Current user being updated |
| 20 | currentDeleteUser: emptyUser.cloneMessage(), // Current user being confirmed deletion |
Adrià Vilanova Martínez | f7ee658 | 2021-09-02 21:53:28 +0200 | [diff] [blame] | 21 | addDialogOpen: false, |
| 22 | updateDialogOpen: false, |
| 23 | deleteDialogOpen: false, |
| 24 | accessLevels, |
| 25 | }; |
| 26 | }, |
| 27 | components: { |
| 28 | NotAuthorized, |
| 29 | Page, |
| 30 | AuthorizedUserDialog, |
| 31 | }, |
| 32 | mounted() { |
| 33 | this.loadData(); |
| 34 | }, |
| 35 | methods: { |
| 36 | loadData() { |
| 37 | if (!this.isSignedIn) return; |
| 38 | |
| 39 | let request = new ksPb.ListAuthorizedUsersRequest(); |
| 40 | |
| 41 | this.$store.state.client.listAuthorizedUsers( |
| 42 | request, {authorization: this.$store.state.jwtToken}) |
| 43 | .then(res => { |
| 44 | this.$data.users = res.getUsersList(); |
| 45 | }) |
| 46 | .catch(err => console.error(err)); |
| 47 | }, |
| 48 | showAddForm(user) { |
| 49 | this.$data.addDialogOpen = true; |
| 50 | }, |
| 51 | showUpdateForm(user) { |
| 52 | this.currentUpdateUser = user; |
| 53 | this.$data.updateDialogOpen = true; |
| 54 | }, |
| 55 | showDeleteDialog(user) { |
| 56 | this.$data.currentDeleteUser = user; |
| 57 | this.$data.deleteDialogOpen = true; |
| 58 | }, |
| 59 | deleteUser(user) { |
| 60 | let request = new ksPb.DeleteAuthorizedUserRequest(); |
| 61 | request.setUserId(user.getId()); |
| 62 | |
| 63 | this.$store.state.client.deleteAuthorizedUser( |
| 64 | request, {authorization: this.$store.state.jwtToken}) |
| 65 | .then(res => { |
| 66 | this.loadData(); |
| 67 | }) |
| 68 | .catch(err => console.error(err)); |
| 69 | }, |
| 70 | }, |
| 71 | computed: { |
Adrià Vilanova Martínez | 035b543 | 2021-09-03 14:16:59 +0200 | [diff] [blame] | 72 | ...mapGetters([ |
| 73 | 'isSignedIn', |
| 74 | ]), |
Adrià Vilanova Martínez | f7ee658 | 2021-09-02 21:53:28 +0200 | [diff] [blame] | 75 | }, |
| 76 | }; |
| 77 | </script> |
| 78 | |
| 79 | <template> |
| 80 | <template v-if="isSignedIn"> |
| 81 | <div class="container"> |
| 82 | <mcw-data-table> |
| 83 | <table class="mdc-data-table__table" aria-label="Authorized users"> |
| 84 | <thead> |
| 85 | <tr class="mdc-data-table__header-row"> |
| 86 | <th |
| 87 | class="mdc-data-table__header-cell" |
| 88 | role="columnheader" |
| 89 | scope="col"> |
| 90 | ID |
| 91 | </th> |
| 92 | <th |
| 93 | class="mdc-data-table__header-cell" |
| 94 | role="columnheader" |
| 95 | scope="col"> |
| 96 | Google UID |
| 97 | </th> |
| 98 | <th |
| 99 | class="mdc-data-table__header-cell" |
| 100 | role="columnheader" |
| 101 | scope="col"> |
| 102 | E-mail address |
| 103 | </th> |
| 104 | <th |
| 105 | class="mdc-data-table__header-cell" |
| 106 | role="columnheader" |
| 107 | scope="col"> |
| 108 | Scope |
| 109 | </th> |
| 110 | <th |
| 111 | class="mdc-data-table__header-cell" |
| 112 | role="columnheader" |
| 113 | scope="col"> |
| 114 | </th> |
| 115 | </tr> |
| 116 | </thead> |
| 117 | <tbody class="mdc-data-table__content"> |
| 118 | <tr class="mdc-data-table__row" v-for="user in users"> |
| 119 | <td class="mdc-data-table__cell mdc-data-table__cell--numeric">{{ user.getId() }}</td> |
| 120 | <td class="mdc-data-table__cell">{{ user.getGoogleUid() || "-" }}</td> |
| 121 | <td class="mdc-data-table__cell">{{ user.getEmail() || "-" }}</td> |
| 122 | <td class="mdc-data-table__cell">{{ accessLevels[user.getAccessLevel()] }}</td> |
| 123 | <td class="mdc-data-table__cell"> |
| 124 | <mcw-icon-button @click="(showUpdateForm(user))"><mcw-material-icon icon="edit" /></mcw-icon-button> |
| 125 | <mcw-icon-button @click="(showDeleteDialog(user))"><mcw-material-icon icon="delete" /></mcw-icon-button> |
| 126 | </td> |
| 127 | </tr> |
| 128 | </tbody> |
| 129 | </table> |
| 130 | </mcw-data-table> |
| 131 | </div> |
| 132 | <mcw-fab @click="showAddForm()" class="add-user" icon="add" /> |
| 133 | <mcw-dialog |
| 134 | v-model="deleteDialogOpen" |
| 135 | escape-key-action="close" |
| 136 | scrim-click-action="close" |
| 137 | aria-labelledby="delete-title" |
| 138 | aria-describedby="delete-content" |
| 139 | :auto-stack-buttons="true"> |
| 140 | <mcw-dialog-title id="delete-title">Delete authorized user?</mcw-dialog-title> |
| 141 | <mcw-dialog-content id="delete-content"> |
| 142 | <div>User {{ currentDeleteUser.getId() }} will no longer have access to the TW Power Tools dashboard.</div> |
| 143 | </mcw-dialog-content> |
| 144 | <mcw-dialog-footer> |
| 145 | <mcw-dialog-button action="dismiss">Cancel</mcw-dialog-button> |
| 146 | <mcw-dialog-button @click="deleteUser(currentDeleteUser)" action="accept">Delete</mcw-dialog-button> |
| 147 | </mcw-dialog-footer> |
| 148 | </mcw-dialog> |
| 149 | <!-- Add user dialog --> |
| 150 | <authorized-user-dialog @user-added="loadData()" v-model="addDialogOpen" /> |
| 151 | <!-- Update user dialog --> |
| 152 | <authorized-user-dialog @user-updated="loadData()" v-model="updateDialogOpen" is-update :user="currentUpdateUser" /> |
| 153 | </template> |
| 154 | <template v-else> |
| 155 | <not-authorized> |
| 156 | </not-authorized> |
| 157 | </template> |
| 158 | </template> |
| 159 | |
| 160 | <style scoped> |
| 161 | .container { |
| 162 | margin-top: 16px; |
| 163 | display: flex; |
| 164 | justify-content: space-evenly; |
| 165 | } |
| 166 | |
| 167 | .add-user { |
| 168 | position: fixed; |
| 169 | right: 16px; |
| 170 | bottom: 16px; |
| 171 | } |
| 172 | </style> |