Yoshie Muranaka | 463a570 | 2019-12-04 09:09:36 -0800 | [diff] [blame] | 1 | <template> |
Yoshie Muranaka | 4b0fc1d | 2020-01-06 07:36:16 -0800 | [diff] [blame] | 2 | <b-modal id="modal-user" ref="modal" @ok="onOk" @hidden="resetForm"> |
Yoshie Muranaka | 463a570 | 2019-12-04 09:09:36 -0800 | [diff] [blame] | 3 | <template v-slot:modal-title> |
| 4 | <template v-if="newUser"> |
| 5 | Add user |
| 6 | </template> |
| 7 | <template v-else> |
| 8 | Edit user |
| 9 | </template> |
| 10 | </template> |
Yoshie Muranaka | 4b0fc1d | 2020-01-06 07:36:16 -0800 | [diff] [blame] | 11 | <b-form novalidate @submit="handleSubmit"> |
| 12 | <b-container> |
| 13 | <b-row> |
| 14 | <b-col> |
| 15 | <b-form-group label="Account status"> |
| 16 | <b-form-radio |
| 17 | v-model="form.status" |
| 18 | name="user-status" |
| 19 | :value="true" |
| 20 | @input="$v.form.status.$touch()" |
| 21 | > |
| 22 | Enabled |
| 23 | </b-form-radio> |
| 24 | <b-form-radio |
| 25 | v-model="form.status" |
| 26 | name="user-status" |
| 27 | :value="false" |
| 28 | @input="$v.form.status.$touch()" |
| 29 | > |
| 30 | Disabled |
| 31 | </b-form-radio> |
| 32 | </b-form-group> |
| 33 | <b-form-group label="Username" label-for="username"> |
| 34 | <b-form-text id="username-help-block"> |
| 35 | Cannot start with a number |
| 36 | <br /> |
| 37 | No special characters except underscore |
| 38 | </b-form-text> |
| 39 | <b-form-input |
Derick Montague | 09e45cd | 2020-01-23 15:45:57 -0600 | [diff] [blame] | 40 | id="username" |
Yoshie Muranaka | 4b0fc1d | 2020-01-06 07:36:16 -0800 | [diff] [blame] | 41 | v-model="form.username" |
| 42 | type="text" |
Yoshie Muranaka | 4b0fc1d | 2020-01-06 07:36:16 -0800 | [diff] [blame] | 43 | aria-describedby="username-help-block" |
| 44 | :state="getValidationState($v.form.username)" |
| 45 | :disabled="!newUser && originalUsername === 'root'" |
| 46 | /> |
| 47 | <b-form-invalid-feedback role="alert"> |
| 48 | <template v-if="!$v.form.username.required"> |
| 49 | Field required |
| 50 | </template> |
| 51 | <template v-else-if="!$v.form.username.maxLength"> |
| 52 | Length must be between 1 – 16 characters |
| 53 | </template> |
| 54 | <template v-else-if="!$v.form.username.pattern"> |
| 55 | Invalid format |
| 56 | </template> |
| 57 | </b-form-invalid-feedback> |
| 58 | </b-form-group> |
| 59 | <b-form-group label="Privilege"> |
| 60 | <b-form-select |
| 61 | v-model="form.privilege" |
| 62 | :options="privilegeTypes" |
| 63 | :state="getValidationState($v.form.privilege)" |
| 64 | @input="$v.form.privilege.$touch()" |
| 65 | > |
| 66 | </b-form-select> |
| 67 | <b-form-invalid-feedback role="alert"> |
| 68 | <template v-if="!$v.form.privilege.required"> |
| 69 | Field required |
| 70 | </template> |
| 71 | </b-form-invalid-feedback> |
| 72 | </b-form-group> |
| 73 | </b-col> |
| 74 | <b-col> |
| 75 | <b-form-group label="User password" label-for="password"> |
| 76 | <b-form-text id="password-help-block" text-variant="black"> |
| 77 | <!-- TODO: Should be dynamic values --> |
| 78 | Password must between 8 – 20 characters |
| 79 | </b-form-text> |
Yoshie Muranaka | 4ee8d29 | 2020-02-20 07:29:58 -0800 | [diff] [blame] | 80 | <input-password-toggle> |
| 81 | <b-form-input |
| 82 | id="password" |
| 83 | v-model="form.password" |
| 84 | type="password" |
| 85 | aria-describedby="password-help-block" |
| 86 | :state="getValidationState($v.form.password)" |
| 87 | @input="$v.form.password.$touch()" |
| 88 | /> |
| 89 | <b-form-invalid-feedback role="alert"> |
| 90 | <template v-if="!$v.form.password.required"> |
| 91 | Field required |
| 92 | </template> |
| 93 | <template |
| 94 | v-if=" |
| 95 | !$v.form.password.minLength || !$v.form.password.maxLength |
| 96 | " |
| 97 | > |
| 98 | Length must be between 8 – 20 characters |
| 99 | </template> |
| 100 | </b-form-invalid-feedback> |
| 101 | </input-password-toggle> |
Yoshie Muranaka | 4b0fc1d | 2020-01-06 07:36:16 -0800 | [diff] [blame] | 102 | </b-form-group> |
| 103 | <b-form-group |
| 104 | label="Confirm user password" |
| 105 | label-for="password-confirmation" |
| 106 | > |
Yoshie Muranaka | 4ee8d29 | 2020-02-20 07:29:58 -0800 | [diff] [blame] | 107 | <input-password-toggle> |
| 108 | <b-form-input |
| 109 | id="password-confirmation" |
| 110 | v-model="form.passwordConfirmation" |
| 111 | type="password" |
| 112 | :state="getValidationState($v.form.passwordConfirmation)" |
| 113 | @input="$v.form.passwordConfirmation.$touch()" |
| 114 | /> |
| 115 | <b-form-invalid-feedback role="alert"> |
| 116 | <template v-if="!$v.form.passwordConfirmation.required"> |
| 117 | Field required |
| 118 | </template> |
| 119 | <template |
| 120 | v-else-if="!$v.form.passwordConfirmation.sameAsPassword" |
| 121 | > |
| 122 | Passwords do not match |
| 123 | </template> |
| 124 | </b-form-invalid-feedback> |
| 125 | </input-password-toggle> |
Yoshie Muranaka | 4b0fc1d | 2020-01-06 07:36:16 -0800 | [diff] [blame] | 126 | </b-form-group> |
| 127 | </b-col> |
| 128 | </b-row> |
| 129 | </b-container> |
Yoshie Muranaka | 463a570 | 2019-12-04 09:09:36 -0800 | [diff] [blame] | 130 | </b-form> |
| 131 | <template v-slot:modal-ok> |
| 132 | <template v-if="newUser"> |
| 133 | Add user |
| 134 | </template> |
| 135 | <template v-else> |
| 136 | Save |
| 137 | </template> |
| 138 | </template> |
| 139 | </b-modal> |
| 140 | </template> |
| 141 | |
| 142 | <script> |
Yoshie Muranaka | 4b0fc1d | 2020-01-06 07:36:16 -0800 | [diff] [blame] | 143 | import { |
| 144 | required, |
| 145 | maxLength, |
| 146 | minLength, |
| 147 | sameAs, |
| 148 | helpers, |
| 149 | requiredIf |
| 150 | } from 'vuelidate/lib/validators'; |
| 151 | import VuelidateMixin from '../../../components/Mixins/VuelidateMixin.js'; |
Yoshie Muranaka | 4ee8d29 | 2020-02-20 07:29:58 -0800 | [diff] [blame] | 152 | import InputPasswordToggle from '../../../components/Global/InputPasswordToggle'; |
Yoshie Muranaka | 4b0fc1d | 2020-01-06 07:36:16 -0800 | [diff] [blame] | 153 | |
Yoshie Muranaka | 463a570 | 2019-12-04 09:09:36 -0800 | [diff] [blame] | 154 | export default { |
Yoshie Muranaka | 4ee8d29 | 2020-02-20 07:29:58 -0800 | [diff] [blame] | 155 | components: { InputPasswordToggle }, |
Yoshie Muranaka | 4b0fc1d | 2020-01-06 07:36:16 -0800 | [diff] [blame] | 156 | mixins: [VuelidateMixin], |
Derick Montague | 09e45cd | 2020-01-23 15:45:57 -0600 | [diff] [blame] | 157 | props: { |
| 158 | user: { |
| 159 | type: Object, |
| 160 | default: null |
| 161 | } |
| 162 | }, |
Yoshie Muranaka | 463a570 | 2019-12-04 09:09:36 -0800 | [diff] [blame] | 163 | data() { |
| 164 | return { |
Yoshie Muranaka | 4b0fc1d | 2020-01-06 07:36:16 -0800 | [diff] [blame] | 165 | privilegeTypes: ['Administrator', 'Operator', 'ReadOnly', 'NoAccess'], |
| 166 | originalUsername: '', |
| 167 | form: { |
| 168 | status: true, |
| 169 | username: '', |
| 170 | privilege: '', |
| 171 | password: '', |
| 172 | passwordConfirmation: '' |
| 173 | } |
Yoshie Muranaka | 463a570 | 2019-12-04 09:09:36 -0800 | [diff] [blame] | 174 | }; |
| 175 | }, |
| 176 | computed: { |
| 177 | newUser() { |
| 178 | return this.user ? false : true; |
Yoshie Muranaka | 4b0fc1d | 2020-01-06 07:36:16 -0800 | [diff] [blame] | 179 | } |
| 180 | }, |
| 181 | watch: { |
| 182 | user: function(value) { |
| 183 | if (value === null) return; |
| 184 | this.originalUsername = value.username; |
| 185 | this.form.username = value.username; |
| 186 | this.form.status = value.Enabled; |
| 187 | this.form.privilege = value.privilege; |
| 188 | } |
| 189 | }, |
| 190 | validations: { |
| 191 | form: { |
| 192 | status: { |
| 193 | required |
| 194 | }, |
| 195 | username: { |
| 196 | required, |
| 197 | maxLength: maxLength(16), |
| 198 | pattern: helpers.regex('pattern', /^([a-zA-Z_][a-zA-Z0-9_]*)/) |
| 199 | }, |
| 200 | privilege: { |
| 201 | required |
| 202 | }, |
| 203 | password: { |
| 204 | required: requiredIf(function() { |
| 205 | return this.requirePassword(); |
| 206 | }), |
| 207 | minLength: minLength(8), //TODO: Update to dynamic backend values |
| 208 | maxLength: maxLength(20) //TODO: UPdate to dynamic backend values |
| 209 | }, |
| 210 | passwordConfirmation: { |
| 211 | required: requiredIf(function() { |
| 212 | return this.requirePassword(); |
| 213 | }), |
| 214 | sameAsPassword: sameAs('password') |
| 215 | } |
| 216 | } |
| 217 | }, |
| 218 | methods: { |
| 219 | handleSubmit() { |
| 220 | let userData = {}; |
| 221 | |
| 222 | if (this.newUser) { |
| 223 | this.$v.$touch(); |
| 224 | if (this.$v.$invalid) return; |
| 225 | userData.username = this.form.username; |
| 226 | userData.status = this.form.status; |
| 227 | userData.privilege = this.form.privilege; |
| 228 | userData.password = this.form.password; |
| 229 | } else { |
| 230 | if (this.$v.$invalid) return; |
| 231 | userData.originalUsername = this.originalUsername; |
| 232 | if (this.$v.form.status.$dirty) { |
| 233 | userData.status = this.form.status; |
| 234 | } |
| 235 | if (this.$v.form.username.$dirty) { |
| 236 | userData.username = this.form.username; |
| 237 | } |
| 238 | if (this.$v.form.privilege.$dirty) { |
| 239 | userData.privilege = this.form.privilege; |
| 240 | } |
| 241 | if (this.$v.form.password.$dirty) { |
| 242 | userData.password = this.form.password; |
| 243 | } |
| 244 | if (Object.entries(userData).length === 1) { |
| 245 | this.closeModal(); |
| 246 | return; |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | this.$emit('ok', { isNewUser: this.newUser, userData }); |
| 251 | this.closeModal(); |
Yoshie Muranaka | 463a570 | 2019-12-04 09:09:36 -0800 | [diff] [blame] | 252 | }, |
Yoshie Muranaka | 4b0fc1d | 2020-01-06 07:36:16 -0800 | [diff] [blame] | 253 | closeModal() { |
| 254 | this.$nextTick(() => { |
| 255 | this.$refs.modal.hide(); |
| 256 | }); |
| 257 | }, |
| 258 | resetForm() { |
| 259 | this.form.originalUsername = ''; |
| 260 | this.form.status = true; |
| 261 | this.form.username = ''; |
| 262 | this.form.privilege = ''; |
| 263 | this.form.password = ''; |
| 264 | this.form.passwordConfirmation = ''; |
| 265 | this.$v.$reset(); |
| 266 | }, |
| 267 | requirePassword() { |
| 268 | if (this.newUser) return true; |
| 269 | if (this.$v.form.password.$dirty) return true; |
| 270 | if (this.$v.form.passwordConfirmation.$dirty) return true; |
| 271 | return false; |
| 272 | }, |
| 273 | onOk(bvModalEvt) { |
| 274 | // prevent modal close |
| 275 | bvModalEvt.preventDefault(); |
| 276 | this.handleSubmit(); |
Yoshie Muranaka | 463a570 | 2019-12-04 09:09:36 -0800 | [diff] [blame] | 277 | } |
| 278 | } |
| 279 | }; |
| 280 | </script> |