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