xref: /openbmc/webui-vue/src/views/ProfileSettings/ProfileSettings.vue (revision 4334d2604a206d77cbc797276c23c6ea3d667c5d)
1b1f559f0SSukanya Pandey<template>
2b1f559f0SSukanya Pandey  <b-container fluid="xl">
3b1f559f0SSukanya Pandey    <page-title />
4b1f559f0SSukanya Pandey
5b1f559f0SSukanya Pandey    <b-row>
6b1f559f0SSukanya Pandey      <b-col md="8" lg="8" xl="6">
7fc16f3c2SSukanya Pandey        <page-section
8fc16f3c2SSukanya Pandey          :section-title="$t('pageProfileSettings.profileInfoTitle')"
9fc16f3c2SSukanya Pandey        >
10b1f559f0SSukanya Pandey          <dl>
11fc16f3c2SSukanya Pandey            <dt>{{ $t('pageProfileSettings.username') }}</dt>
12b1f559f0SSukanya Pandey            <dd>
13b1f559f0SSukanya Pandey              {{ username }}
14b1f559f0SSukanya Pandey            </dd>
15b1f559f0SSukanya Pandey          </dl>
16b1f559f0SSukanya Pandey        </page-section>
17b1f559f0SSukanya Pandey      </b-col>
18b1f559f0SSukanya Pandey    </b-row>
19b1f559f0SSukanya Pandey
20b1f559f0SSukanya Pandey    <b-form @submit.prevent="submitForm">
21b1f559f0SSukanya Pandey      <b-row>
22b1f559f0SSukanya Pandey        <b-col sm="8" md="6" xl="3">
23fc16f3c2SSukanya Pandey          <page-section
24fc16f3c2SSukanya Pandey            :section-title="$t('pageProfileSettings.changePassword')"
25fc16f3c2SSukanya Pandey          >
26b1f559f0SSukanya Pandey            <b-form-group
27b1f559f0SSukanya Pandey              id="input-group-1"
28fc16f3c2SSukanya Pandey              :label="$t('pageProfileSettings.newPassword')"
29b1f559f0SSukanya Pandey              label-for="input-1"
30b1f559f0SSukanya Pandey            >
31b1f559f0SSukanya Pandey              <b-form-text id="password-help-block">
32b1f559f0SSukanya Pandey                {{
33b1f559f0SSukanya Pandey                  $t('pageLocalUserManagement.modal.passwordMustBeBetween', {
34b1f559f0SSukanya Pandey                    min: passwordRequirements.minLength,
35b1f559f0SSukanya Pandey                    max: passwordRequirements.maxLength
36b1f559f0SSukanya Pandey                  })
37b1f559f0SSukanya Pandey                }}
38b1f559f0SSukanya Pandey              </b-form-text>
39b1f559f0SSukanya Pandey              <input-password-toggle>
40b1f559f0SSukanya Pandey                <b-form-input
41b1f559f0SSukanya Pandey                  id="password"
42b1f559f0SSukanya Pandey                  v-model="form.newPassword"
43b1f559f0SSukanya Pandey                  type="password"
44b1f559f0SSukanya Pandey                  aria-describedby="password-help-block"
45b1f559f0SSukanya Pandey                  :state="getValidationState($v.form.newPassword)"
46*4334d260SSukanya Pandey                  data-test-id="profileSettings-input-newPassword"
47b1f559f0SSukanya Pandey                  @input="$v.form.newPassword.$touch()"
48b1f559f0SSukanya Pandey                />
49b1f559f0SSukanya Pandey                <b-form-invalid-feedback role="alert">
50b1f559f0SSukanya Pandey                  <template
51b1f559f0SSukanya Pandey                    v-if="
52b1f559f0SSukanya Pandey                      !$v.form.newPassword.minLength ||
53b1f559f0SSukanya Pandey                        !$v.form.newPassword.maxLength
54b1f559f0SSukanya Pandey                    "
55b1f559f0SSukanya Pandey                  >
56b1f559f0SSukanya Pandey                    {{
57fc16f3c2SSukanya Pandey                      $t('pageProfileSettings.newPassLabelTextInfo', {
58b1f559f0SSukanya Pandey                        min: passwordRequirements.minLength,
59b1f559f0SSukanya Pandey                        max: passwordRequirements.maxLength
60b1f559f0SSukanya Pandey                      })
61b1f559f0SSukanya Pandey                    }}
62b1f559f0SSukanya Pandey                  </template>
63b1f559f0SSukanya Pandey                </b-form-invalid-feedback>
64b1f559f0SSukanya Pandey              </input-password-toggle>
65b1f559f0SSukanya Pandey            </b-form-group>
66b1f559f0SSukanya Pandey            <b-form-group
67b1f559f0SSukanya Pandey              id="input-group-2"
68fc16f3c2SSukanya Pandey              :label="$t('pageProfileSettings.confirmPassword')"
69b1f559f0SSukanya Pandey              label-for="input-2"
70b1f559f0SSukanya Pandey            >
71b1f559f0SSukanya Pandey              <input-password-toggle>
72b1f559f0SSukanya Pandey                <b-form-input
73b1f559f0SSukanya Pandey                  id="password-confirmation"
74b1f559f0SSukanya Pandey                  v-model="form.confirmPassword"
75b1f559f0SSukanya Pandey                  type="password"
76b1f559f0SSukanya Pandey                  :state="getValidationState($v.form.confirmPassword)"
77*4334d260SSukanya Pandey                  data-test-id="profileSettings-input-confirmPassword"
78b1f559f0SSukanya Pandey                  @input="$v.form.confirmPassword.$touch()"
79b1f559f0SSukanya Pandey                />
80b1f559f0SSukanya Pandey                <b-form-invalid-feedback role="alert">
81fc16f3c2SSukanya Pandey                  <template v-if="!$v.form.confirmPassword.sameAsPassword">
82fc16f3c2SSukanya Pandey                    {{ $t('pageProfileSettings.passwordsDoNotMatch') }}
83b1f559f0SSukanya Pandey                  </template>
84b1f559f0SSukanya Pandey                </b-form-invalid-feedback>
85b1f559f0SSukanya Pandey              </input-password-toggle>
86b1f559f0SSukanya Pandey            </b-form-group>
87b1f559f0SSukanya Pandey          </page-section>
88b1f559f0SSukanya Pandey        </b-col>
89b1f559f0SSukanya Pandey      </b-row>
90fc16f3c2SSukanya Pandey      <page-section :section-title="$t('pageProfileSettings.timezoneDisplay')">
91fc16f3c2SSukanya Pandey        <p>{{ $t('pageProfileSettings.timezoneDisplayDesc') }}</p>
92fc16f3c2SSukanya Pandey        <b-row>
93fc16f3c2SSukanya Pandey          <b-col md="9" lg="8" xl="9">
94fc16f3c2SSukanya Pandey            <b-form-group :label="$t('pageProfileSettings.timezone')">
95fc16f3c2SSukanya Pandey              <b-form-radio
96fc16f3c2SSukanya Pandey                v-model="form.isUtcDisplay"
97fc16f3c2SSukanya Pandey                :value="true"
98*4334d260SSukanya Pandey                data-test-id="profileSettings-radio-defaultUTC"
99fc16f3c2SSukanya Pandey                @change="$v.form.isUtcDisplay.$touch()"
100fc16f3c2SSukanya Pandey              >
101fc16f3c2SSukanya Pandey                {{ $t('pageProfileSettings.defaultUTC') }}
102fc16f3c2SSukanya Pandey              </b-form-radio>
103fc16f3c2SSukanya Pandey              <b-form-radio
104fc16f3c2SSukanya Pandey                v-model="form.isUtcDisplay"
105fc16f3c2SSukanya Pandey                :value="false"
106*4334d260SSukanya Pandey                data-test-id="profileSettings-radio-browserOffset"
107fc16f3c2SSukanya Pandey                @change="$v.form.isUtcDisplay.$touch()"
108fc16f3c2SSukanya Pandey              >
109fc16f3c2SSukanya Pandey                {{
110fc16f3c2SSukanya Pandey                  $t('pageProfileSettings.browserOffset', {
111fc16f3c2SSukanya Pandey                    timezone
112fc16f3c2SSukanya Pandey                  })
113fc16f3c2SSukanya Pandey                }}
114fc16f3c2SSukanya Pandey              </b-form-radio>
115fc16f3c2SSukanya Pandey            </b-form-group>
116fc16f3c2SSukanya Pandey          </b-col>
117fc16f3c2SSukanya Pandey        </b-row>
118fc16f3c2SSukanya Pandey      </page-section>
119*4334d260SSukanya Pandey      <b-button
120*4334d260SSukanya Pandey        variant="primary"
121*4334d260SSukanya Pandey        type="submit"
122*4334d260SSukanya Pandey        data-test-id="profileSettings-button-saveSettings"
123*4334d260SSukanya Pandey      >
124fc16f3c2SSukanya Pandey        {{ $t('global.action.saveSettings') }}
125b1f559f0SSukanya Pandey      </b-button>
126b1f559f0SSukanya Pandey    </b-form>
127b1f559f0SSukanya Pandey  </b-container>
128b1f559f0SSukanya Pandey</template>
129b1f559f0SSukanya Pandey
130b1f559f0SSukanya Pandey<script>
131fc16f3c2SSukanya Pandeyimport i18n from '@/i18n';
132b1f559f0SSukanya Pandeyimport BVToastMixin from '@/components/Mixins/BVToastMixin';
133b1f559f0SSukanya Pandeyimport InputPasswordToggle from '@/components/Global/InputPasswordToggle';
134fc16f3c2SSukanya Pandeyimport { format } from 'date-fns-tz';
135b1f559f0SSukanya Pandeyimport {
136b1f559f0SSukanya Pandey  maxLength,
137b1f559f0SSukanya Pandey  minLength,
138b1f559f0SSukanya Pandey  required,
139b1f559f0SSukanya Pandey  sameAs
140b1f559f0SSukanya Pandey} from 'vuelidate/lib/validators';
141b93608dbSSukanya Pandeyimport LoadingBarMixin from '@/components/Mixins/LoadingBarMixin';
142b93608dbSSukanya Pandeyimport PageTitle from '@/components/Global/PageTitle';
143b93608dbSSukanya Pandeyimport PageSection from '@/components/Global/PageSection';
144b93608dbSSukanya Pandeyimport VuelidateMixin from '@/components/Mixins/VuelidateMixin.js';
145b1f559f0SSukanya Pandey
146b1f559f0SSukanya Pandeyexport default {
147b1f559f0SSukanya Pandey  name: 'ProfileSettings',
148b93608dbSSukanya Pandey  components: { InputPasswordToggle, PageSection, PageTitle },
149b93608dbSSukanya Pandey  mixins: [BVToastMixin, LoadingBarMixin, VuelidateMixin],
150b1f559f0SSukanya Pandey  data() {
151b1f559f0SSukanya Pandey    return {
152b1f559f0SSukanya Pandey      form: {
153b1f559f0SSukanya Pandey        newPassword: '',
154fc16f3c2SSukanya Pandey        confirmPassword: '',
155fc16f3c2SSukanya Pandey        isUtcDisplay: this.$store.getters['global/isUtcDisplay']
156b1f559f0SSukanya Pandey      }
157b1f559f0SSukanya Pandey    };
158b1f559f0SSukanya Pandey  },
159b93608dbSSukanya Pandey  computed: {
160b93608dbSSukanya Pandey    username() {
161b93608dbSSukanya Pandey      return this.$store.getters['global/username'];
162b93608dbSSukanya Pandey    },
163b93608dbSSukanya Pandey    passwordRequirements() {
164b93608dbSSukanya Pandey      return this.$store.getters['localUsers/accountPasswordRequirements'];
165fc16f3c2SSukanya Pandey    },
166fc16f3c2SSukanya Pandey    timezone() {
167fc16f3c2SSukanya Pandey      const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
168fc16f3c2SSukanya Pandey      const shortTz = this.$options.filters.shortTimeZone(new Date());
169fc16f3c2SSukanya Pandey      const pattern = `'${shortTz}' O`;
170fc16f3c2SSukanya Pandey      return format(new Date(), pattern, { timezone }).replace('GMT', 'UTC');
171b93608dbSSukanya Pandey    }
172b93608dbSSukanya Pandey  },
173b93608dbSSukanya Pandey  created() {
174b93608dbSSukanya Pandey    this.startLoader();
175b93608dbSSukanya Pandey    this.$store
176b93608dbSSukanya Pandey      .dispatch('localUsers/getAccountSettings')
177b93608dbSSukanya Pandey      .finally(() => this.endLoader());
178b93608dbSSukanya Pandey  },
179b1f559f0SSukanya Pandey  validations() {
180b1f559f0SSukanya Pandey    return {
181b1f559f0SSukanya Pandey      form: {
182fc16f3c2SSukanya Pandey        isUtcDisplay: { required },
183b1f559f0SSukanya Pandey        newPassword: {
184b1f559f0SSukanya Pandey          minLength: minLength(this.passwordRequirements.minLength),
185b1f559f0SSukanya Pandey          maxLength: maxLength(this.passwordRequirements.maxLength)
186b1f559f0SSukanya Pandey        },
187b1f559f0SSukanya Pandey        confirmPassword: {
188b1f559f0SSukanya Pandey          sameAsPassword: sameAs('newPassword')
189b1f559f0SSukanya Pandey        }
190b1f559f0SSukanya Pandey      }
191b1f559f0SSukanya Pandey    };
192b1f559f0SSukanya Pandey  },
193b1f559f0SSukanya Pandey  methods: {
194fc16f3c2SSukanya Pandey    saveNewPasswordInputData() {
195fc16f3c2SSukanya Pandey      this.$v.form.confirmPassword.$touch();
196fc16f3c2SSukanya Pandey      this.$v.form.newPassword.$touch();
197b1f559f0SSukanya Pandey      if (this.$v.$invalid) return;
198b1f559f0SSukanya Pandey      let userData = {
199b1f559f0SSukanya Pandey        originalUsername: this.username,
200b1f559f0SSukanya Pandey        password: this.form.newPassword
201b1f559f0SSukanya Pandey      };
202b1f559f0SSukanya Pandey
203b1f559f0SSukanya Pandey      this.$store
204b1f559f0SSukanya Pandey        .dispatch('localUsers/updateUser', userData)
2059422e1a6SSukanya Pandey        .then(message => {
2069422e1a6SSukanya Pandey          (this.form.newPassword = ''), (this.form.confirmPassword = '');
2079422e1a6SSukanya Pandey          this.$v.$reset();
2089422e1a6SSukanya Pandey          this.successToast(message);
2099422e1a6SSukanya Pandey        })
210b1f559f0SSukanya Pandey        .catch(({ message }) => this.errorToast(message));
211fc16f3c2SSukanya Pandey    },
212fc16f3c2SSukanya Pandey    saveTimeZonePrefrenceData() {
213fc16f3c2SSukanya Pandey      localStorage.setItem('storedUtcDisplay', this.form.isUtcDisplay);
214fc16f3c2SSukanya Pandey      this.$store.commit('global/setUtcTime', this.form.isUtcDisplay);
215fc16f3c2SSukanya Pandey      this.successToast(
216fc16f3c2SSukanya Pandey        i18n.t('pageProfileSettings.toast.successSaveSettings')
217fc16f3c2SSukanya Pandey      );
218fc16f3c2SSukanya Pandey    },
219fc16f3c2SSukanya Pandey    submitForm() {
220fc16f3c2SSukanya Pandey      if (this.form.confirmPassword || this.form.newPassword) {
221fc16f3c2SSukanya Pandey        this.saveNewPasswordInputData();
222fc16f3c2SSukanya Pandey      }
223fc16f3c2SSukanya Pandey      if (this.$v.form.isUtcDisplay.$anyDirty) {
224fc16f3c2SSukanya Pandey        this.saveTimeZonePrefrenceData();
225fc16f3c2SSukanya Pandey      }
226b1f559f0SSukanya Pandey    }
227b1f559f0SSukanya Pandey  }
228b1f559f0SSukanya Pandey};
229b1f559f0SSukanya Pandey</script>
230