blob: 4f68e1a38b84449905e1d06f52950c1da4785485 [file] [log] [blame]
Adrià Vilanova Martínezf7ee6582021-09-02 21:53:28 +02001<script>
2import {accessLevels} from '../consts.js';
3import * as ksObjectsPb from '../api_proto/kill_switch_objects_pb.js';
4import * as ksPb from '../api_proto/kill_switch_pb.js';
5
6export default {
7 props: {
8 modelValue: Boolean, // If the dialog is open
9 isUpdate: Boolean,
10 user: {
11 type: Object,
12 default: function() {
13 return new ksObjectsPb.KillSwitchAuthorizedUser()
14 },
15 },
16 },
17 emits: [
18 'update:modelValue',
19 'userAdded',
20 'userUpdated',
21 ],
22 data() {
23 return {
24 accessLevels,
25 userM: this.user.cloneMessage(), // User mutable
26 };
27 },
28 watch: {
29 user(newUser, oldUser) {
30 this.$data.userM = this.user.cloneMessage();
31 }
32 },
33 computed: {
34 accessLevel: {
35 get() {
36 return this.$data.userM.getAccessLevel().toString();
37 },
38 set(level) {
39 this.$data.userM.setAccessLevel(parseInt(level));
40 },
41 },
42 googleUid: {
43 get() {
44 return this.$data.userM.getGoogleUid();
45 },
46 set(uid) {
47 this.$data.userM.setGoogleUid(uid);
48 },
49 },
50 email: {
51 get() {
52 return this.$data.userM.getEmail();
53 },
54 set(email) {
55 this.$data.userM.setEmail(email);
56 },
57 },
58 },
59 methods: {
60 onSubmit() {
61 if (this.isUpdate)
62 this.doUpdate();
63 else
64 this.doAdd();
65 },
66 doUpdate() {
67 let request = new ksPb.UpdateAuthorizedUserRequest();
68 request.setUserId(this.userM.getId());
69 request.setUser(this.userM);
70
71 this.$store.state.client.updateAuthorizedUser(
72 request, {authorization: this.$store.state.jwtToken})
73 .then(res => {
74 this.$emit('userUpdated');
75 })
76 .catch(err => console.error(err));
77 },
78 doAdd() {
79 let request = new ksPb.AddAuthorizedUserRequest();
80 request.setUser(this.userM);
81
82 this.$store.state.client.addAuthorizedUser(
83 request, {authorization: this.$store.state.jwtToken})
84 .then(res => {
85 this.$emit('userAdded');
86 this.$data.userM = this.user.cloneMessage();
87 })
88 .catch(err => console.error(err));
89 },
90 onCancel() {
91 this.$data.userM = this.user.cloneMessage();
92 },
93 },
94};
95</script>
96
97<template>
98 <mcw-dialog
99 v-model="modelValue"
100 @update:modelValue="$emit('update:modelValue', $event)"
101 escape-key-action="close"
102 scrim-click-action="close"
103 :auto-stack-buttons="true">
104 <mcw-dialog-title>{{ isUpdate ? 'Update user ' + userM.getId() : 'Add new user' }}</mcw-dialog-title>
105 <mcw-dialog-content>
106 <div>
107 <p><mcw-select v-model="accessLevel" label="Access level" required>
108 <mcw-list-item v-for="(al, i) in accessLevels" :data-value="i" role="option">
109 {{ al }}
110 </mcw-list-item>
111 </mcw-select></p>
112 <p><mcw-textfield v-model="googleUid" label="Google UID" /></p>
113 <p><mcw-textfield v-model="email" type="email" label="E-mail address" /></p>
114 </div>
115 </mcw-dialog-content>
116 <mcw-dialog-footer>
117 <mcw-dialog-button @click="onCancel" action="dismiss">Cancel</mcw-dialog-button>
118 <mcw-dialog-button @click="onSubmit" action="accept">{{ isUpdate ? 'Update' : 'Add' }}</mcw-dialog-button>
119 </mcw-dialog-footer>
120 </mcw-dialog>
121</template>