blob: 2abee6332a432cb73b82860aa16853a5bba92a8f [file] [log] [blame]
Sukanya Pandeyb1f559f2020-04-28 20:18:28 +05301<template>
2 <b-container fluid="xl">
3 <page-title />
4
5 <b-row>
6 <b-col md="8" lg="8" xl="6">
Sukanya Pandeyfc16f3c2020-06-23 22:54:27 +05307 <page-section
8 :section-title="$t('pageProfileSettings.profileInfoTitle')"
9 >
Sukanya Pandeyb1f559f2020-04-28 20:18:28 +053010 <dl>
Sukanya Pandeyfc16f3c2020-06-23 22:54:27 +053011 <dt>{{ $t('pageProfileSettings.username') }}</dt>
Sukanya Pandeyb1f559f2020-04-28 20:18:28 +053012 <dd>
13 {{ username }}
14 </dd>
15 </dl>
16 </page-section>
17 </b-col>
18 </b-row>
19
20 <b-form @submit.prevent="submitForm">
21 <b-row>
22 <b-col sm="8" md="6" xl="3">
Sukanya Pandeyfc16f3c2020-06-23 22:54:27 +053023 <page-section
24 :section-title="$t('pageProfileSettings.changePassword')"
25 >
Sukanya Pandeyb1f559f2020-04-28 20:18:28 +053026 <b-form-group
27 id="input-group-1"
Sukanya Pandeyfc16f3c2020-06-23 22:54:27 +053028 :label="$t('pageProfileSettings.newPassword')"
Sukanya Pandeyb1f559f2020-04-28 20:18:28 +053029 label-for="input-1"
30 >
31 <b-form-text id="password-help-block">
32 {{
33 $t('pageLocalUserManagement.modal.passwordMustBeBetween', {
34 min: passwordRequirements.minLength,
35 max: passwordRequirements.maxLength
36 })
37 }}
38 </b-form-text>
39 <input-password-toggle>
40 <b-form-input
41 id="password"
42 v-model="form.newPassword"
43 type="password"
44 aria-describedby="password-help-block"
45 :state="getValidationState($v.form.newPassword)"
46 @input="$v.form.newPassword.$touch()"
47 />
48 <b-form-invalid-feedback role="alert">
Sukanya Pandeyb1f559f2020-04-28 20:18:28 +053049 <template
50 v-if="
51 !$v.form.newPassword.minLength ||
52 !$v.form.newPassword.maxLength
53 "
54 >
55 {{
Sukanya Pandeyfc16f3c2020-06-23 22:54:27 +053056 $t('pageProfileSettings.newPassLabelTextInfo', {
Sukanya Pandeyb1f559f2020-04-28 20:18:28 +053057 min: passwordRequirements.minLength,
58 max: passwordRequirements.maxLength
59 })
60 }}
61 </template>
62 </b-form-invalid-feedback>
63 </input-password-toggle>
64 </b-form-group>
65 <b-form-group
66 id="input-group-2"
Sukanya Pandeyfc16f3c2020-06-23 22:54:27 +053067 :label="$t('pageProfileSettings.confirmPassword')"
Sukanya Pandeyb1f559f2020-04-28 20:18:28 +053068 label-for="input-2"
69 >
70 <input-password-toggle>
71 <b-form-input
72 id="password-confirmation"
73 v-model="form.confirmPassword"
74 type="password"
75 :state="getValidationState($v.form.confirmPassword)"
76 @input="$v.form.confirmPassword.$touch()"
77 />
78 <b-form-invalid-feedback role="alert">
Sukanya Pandeyfc16f3c2020-06-23 22:54:27 +053079 <template v-if="!$v.form.confirmPassword.sameAsPassword">
80 {{ $t('pageProfileSettings.passwordsDoNotMatch') }}
Sukanya Pandeyb1f559f2020-04-28 20:18:28 +053081 </template>
82 </b-form-invalid-feedback>
83 </input-password-toggle>
84 </b-form-group>
85 </page-section>
86 </b-col>
87 </b-row>
Sukanya Pandeyfc16f3c2020-06-23 22:54:27 +053088 <page-section :section-title="$t('pageProfileSettings.timezoneDisplay')">
89 <p>{{ $t('pageProfileSettings.timezoneDisplayDesc') }}</p>
90 <b-row>
91 <b-col md="9" lg="8" xl="9">
92 <b-form-group :label="$t('pageProfileSettings.timezone')">
93 <b-form-radio
94 v-model="form.isUtcDisplay"
95 :value="true"
96 @change="$v.form.isUtcDisplay.$touch()"
97 >
98 {{ $t('pageProfileSettings.defaultUTC') }}
99 </b-form-radio>
100 <b-form-radio
101 v-model="form.isUtcDisplay"
102 :value="false"
103 @change="$v.form.isUtcDisplay.$touch()"
104 >
105 {{
106 $t('pageProfileSettings.browserOffset', {
107 timezone
108 })
109 }}
110 </b-form-radio>
111 </b-form-group>
112 </b-col>
113 </b-row>
114 </page-section>
Sukanya Pandeyb1f559f2020-04-28 20:18:28 +0530115 <b-button variant="primary" type="submit">
Sukanya Pandeyfc16f3c2020-06-23 22:54:27 +0530116 {{ $t('global.action.saveSettings') }}
Sukanya Pandeyb1f559f2020-04-28 20:18:28 +0530117 </b-button>
118 </b-form>
119 </b-container>
120</template>
121
122<script>
Sukanya Pandeyfc16f3c2020-06-23 22:54:27 +0530123import i18n from '@/i18n';
Sukanya Pandeyb1f559f2020-04-28 20:18:28 +0530124import BVToastMixin from '@/components/Mixins/BVToastMixin';
Sukanya Pandeyb1f559f2020-04-28 20:18:28 +0530125import InputPasswordToggle from '@/components/Global/InputPasswordToggle';
Sukanya Pandeyfc16f3c2020-06-23 22:54:27 +0530126import { format } from 'date-fns-tz';
Sukanya Pandeyb1f559f2020-04-28 20:18:28 +0530127import {
128 maxLength,
129 minLength,
130 required,
131 sameAs
132} from 'vuelidate/lib/validators';
Sukanya Pandeyb93608d2020-07-10 18:51:58 +0530133import LoadingBarMixin from '@/components/Mixins/LoadingBarMixin';
134import PageTitle from '@/components/Global/PageTitle';
135import PageSection from '@/components/Global/PageSection';
136import VuelidateMixin from '@/components/Mixins/VuelidateMixin.js';
Sukanya Pandeyb1f559f2020-04-28 20:18:28 +0530137
138export default {
139 name: 'ProfileSettings',
Sukanya Pandeyb93608d2020-07-10 18:51:58 +0530140 components: { InputPasswordToggle, PageSection, PageTitle },
141 mixins: [BVToastMixin, LoadingBarMixin, VuelidateMixin],
Sukanya Pandeyb1f559f2020-04-28 20:18:28 +0530142 data() {
143 return {
Sukanya Pandeyb1f559f2020-04-28 20:18:28 +0530144 form: {
145 newPassword: '',
Sukanya Pandeyfc16f3c2020-06-23 22:54:27 +0530146 confirmPassword: '',
147 isUtcDisplay: this.$store.getters['global/isUtcDisplay']
Sukanya Pandeyb1f559f2020-04-28 20:18:28 +0530148 }
149 };
150 },
Sukanya Pandeyb93608d2020-07-10 18:51:58 +0530151 computed: {
152 username() {
153 return this.$store.getters['global/username'];
154 },
155 passwordRequirements() {
156 return this.$store.getters['localUsers/accountPasswordRequirements'];
Sukanya Pandeyfc16f3c2020-06-23 22:54:27 +0530157 },
158 timezone() {
159 const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
160 const shortTz = this.$options.filters.shortTimeZone(new Date());
161 const pattern = `'${shortTz}' O`;
162 return format(new Date(), pattern, { timezone }).replace('GMT', 'UTC');
Sukanya Pandeyb93608d2020-07-10 18:51:58 +0530163 }
164 },
165 created() {
166 this.startLoader();
167 this.$store
168 .dispatch('localUsers/getAccountSettings')
169 .finally(() => this.endLoader());
170 },
Sukanya Pandeyb1f559f2020-04-28 20:18:28 +0530171 validations() {
172 return {
173 form: {
Sukanya Pandeyfc16f3c2020-06-23 22:54:27 +0530174 isUtcDisplay: { required },
Sukanya Pandeyb1f559f2020-04-28 20:18:28 +0530175 newPassword: {
Sukanya Pandeyb1f559f2020-04-28 20:18:28 +0530176 minLength: minLength(this.passwordRequirements.minLength),
177 maxLength: maxLength(this.passwordRequirements.maxLength)
178 },
179 confirmPassword: {
Sukanya Pandeyb1f559f2020-04-28 20:18:28 +0530180 sameAsPassword: sameAs('newPassword')
181 }
182 }
183 };
184 },
Sukanya Pandeyb1f559f2020-04-28 20:18:28 +0530185 methods: {
Sukanya Pandeyfc16f3c2020-06-23 22:54:27 +0530186 saveNewPasswordInputData() {
187 this.$v.form.confirmPassword.$touch();
188 this.$v.form.newPassword.$touch();
Sukanya Pandeyb1f559f2020-04-28 20:18:28 +0530189 if (this.$v.$invalid) return;
190 let userData = {
191 originalUsername: this.username,
192 password: this.form.newPassword
193 };
194
195 this.$store
196 .dispatch('localUsers/updateUser', userData)
Sukanya Pandey9422e1a2020-07-13 19:13:07 +0530197 .then(message => {
198 (this.form.newPassword = ''), (this.form.confirmPassword = '');
199 this.$v.$reset();
200 this.successToast(message);
201 })
Sukanya Pandeyb1f559f2020-04-28 20:18:28 +0530202 .catch(({ message }) => this.errorToast(message));
Sukanya Pandeyfc16f3c2020-06-23 22:54:27 +0530203 },
204 saveTimeZonePrefrenceData() {
205 localStorage.setItem('storedUtcDisplay', this.form.isUtcDisplay);
206 this.$store.commit('global/setUtcTime', this.form.isUtcDisplay);
207 this.successToast(
208 i18n.t('pageProfileSettings.toast.successSaveSettings')
209 );
210 },
211 submitForm() {
212 if (this.form.confirmPassword || this.form.newPassword) {
213 this.saveNewPasswordInputData();
214 }
215 if (this.$v.form.isUtcDisplay.$anyDirty) {
216 this.saveTimeZonePrefrenceData();
217 }
Sukanya Pandeyb1f559f2020-04-28 20:18:28 +0530218 }
219 }
220};
221</script>