2020-03-12 14:29:21 +01:00
|
|
|
<template>
|
2020-02-18 08:57:00 +01:00
|
|
|
<div>
|
2020-06-25 11:36:35 +02:00
|
|
|
<nav class="breadcrumb" aria-label="breadcrumbs">
|
|
|
|
<ul>
|
|
|
|
<li>
|
|
|
|
<router-link :to="{ name: RouteName.ACCOUNT_SETTINGS }">{{ $t("Account") }}</router-link>
|
|
|
|
</li>
|
|
|
|
<li class="is-active">
|
|
|
|
<router-link :to="{ name: RouteName.PREFERENCES }">{{ $t("Preferences") }}</router-link>
|
|
|
|
</li>
|
|
|
|
</ul>
|
|
|
|
</nav>
|
|
|
|
<div>
|
|
|
|
<b-field :label="$t('Language')">
|
|
|
|
<b-select
|
|
|
|
:loading="!config || !loggedUser"
|
|
|
|
v-model="$i18n.locale"
|
|
|
|
:placeholder="$t('Select a language')"
|
|
|
|
>
|
2020-10-13 20:39:59 +02:00
|
|
|
<option v-for="(language, lang) in langs" :value="lang" :key="lang">
|
2020-06-25 11:36:35 +02:00
|
|
|
{{ language }}
|
2020-02-18 08:57:00 +01:00
|
|
|
</option>
|
2020-06-25 11:36:35 +02:00
|
|
|
</b-select>
|
|
|
|
</b-field>
|
|
|
|
<b-field :label="$t('Timezone')">
|
|
|
|
<b-select
|
|
|
|
:placeholder="$t('Select a timezone')"
|
|
|
|
:loading="!config || !loggedUser"
|
|
|
|
v-model="selectedTimezone"
|
|
|
|
>
|
|
|
|
<optgroup :label="group" v-for="(groupTimezones, group) in timezones" :key="group">
|
|
|
|
<option
|
|
|
|
v-for="timezone in groupTimezones"
|
|
|
|
:value="`${group}/${timezone}`"
|
|
|
|
:key="timezone"
|
|
|
|
>
|
|
|
|
{{ sanitize(timezone) }}
|
|
|
|
</option>
|
|
|
|
</optgroup>
|
|
|
|
</b-select>
|
|
|
|
</b-field>
|
2020-10-21 09:04:29 +02:00
|
|
|
<em v-if="Intl.DateTimeFormat().resolvedOptions().timeZone">{{
|
2020-06-25 11:36:35 +02:00
|
|
|
$t("Timezone detected as {timezone}.", {
|
|
|
|
timezone: Intl.DateTimeFormat().resolvedOptions().timeZone,
|
|
|
|
})
|
|
|
|
}}</em>
|
2020-10-21 09:04:29 +02:00
|
|
|
<b-message v-else type="is-danger">{{ $t("Unable to detect timezone.") }}</b-message>
|
2020-06-25 11:36:35 +02:00
|
|
|
</div>
|
2020-02-18 08:57:00 +01:00
|
|
|
</div>
|
2020-03-12 14:29:21 +01:00
|
|
|
</template>
|
|
|
|
<script lang="ts">
|
2020-02-18 08:57:00 +01:00
|
|
|
import { Component, Vue, Watch } from "vue-property-decorator";
|
|
|
|
import { TIMEZONES } from "../../graphql/config";
|
2020-06-16 18:00:27 +02:00
|
|
|
import { USER_SETTINGS, SET_USER_SETTINGS, UPDATE_USER_LOCALE } from "../../graphql/user";
|
2020-02-18 08:57:00 +01:00
|
|
|
import { IConfig } from "../../types/config.model";
|
2020-06-27 19:12:45 +02:00
|
|
|
import { IUser } from "../../types/current-user.model";
|
2020-06-16 18:00:27 +02:00
|
|
|
import langs from "../../i18n/langs.json";
|
2020-06-25 11:36:35 +02:00
|
|
|
import RouteName from "../../router/name";
|
2020-03-12 14:29:21 +01:00
|
|
|
|
2020-02-18 08:57:00 +01:00
|
|
|
@Component({
|
|
|
|
apollo: {
|
|
|
|
config: TIMEZONES,
|
|
|
|
loggedUser: USER_SETTINGS,
|
|
|
|
},
|
|
|
|
})
|
2020-03-12 14:29:21 +01:00
|
|
|
export default class Preferences extends Vue {
|
2020-02-18 08:57:00 +01:00
|
|
|
config!: IConfig;
|
2020-03-12 14:29:21 +01:00
|
|
|
|
2020-06-27 19:12:45 +02:00
|
|
|
loggedUser!: IUser;
|
2020-02-18 08:57:00 +01:00
|
|
|
|
|
|
|
selectedTimezone: string | null = null;
|
|
|
|
|
2020-06-16 18:00:27 +02:00
|
|
|
locale: string | null = null;
|
|
|
|
|
2020-06-25 11:36:35 +02:00
|
|
|
RouteName = RouteName;
|
|
|
|
|
2020-10-13 20:39:59 +02:00
|
|
|
langs: Record<string, string> = langs;
|
|
|
|
|
2020-02-18 08:57:00 +01:00
|
|
|
@Watch("loggedUser")
|
2020-10-13 20:39:59 +02:00
|
|
|
setSavedTimezone(loggedUser: IUser): void {
|
2020-02-18 08:57:00 +01:00
|
|
|
if (loggedUser && loggedUser.settings.timezone) {
|
|
|
|
this.selectedTimezone = loggedUser.settings.timezone;
|
|
|
|
} else {
|
|
|
|
this.selectedTimezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
|
|
|
|
}
|
2020-06-16 18:00:27 +02:00
|
|
|
if (loggedUser && loggedUser.locale) {
|
|
|
|
this.locale = loggedUser.locale;
|
|
|
|
} else {
|
|
|
|
this.locale = this.$i18n.locale;
|
|
|
|
}
|
2020-02-18 08:57:00 +01:00
|
|
|
}
|
|
|
|
|
2020-10-13 20:39:59 +02:00
|
|
|
// eslint-disable-next-line class-methods-use-this
|
2020-02-18 08:57:00 +01:00
|
|
|
sanitize(timezone: string): string {
|
|
|
|
return timezone.split("_").join(" ").replace("St ", "St. ").split("/").join(" - ");
|
|
|
|
}
|
|
|
|
|
2020-10-13 20:39:59 +02:00
|
|
|
get timezones(): Record<string, string[]> {
|
2020-02-18 08:57:00 +01:00
|
|
|
if (!this.config || !this.config.timezones) return {};
|
|
|
|
return this.config.timezones.reduce((acc: { [key: string]: Array<string> }, val: string) => {
|
|
|
|
const components = val.split("/");
|
|
|
|
const [prefix, suffix] = [components.shift() as string, components.join("/")];
|
|
|
|
const pushOrCreate = (
|
2020-10-13 20:39:59 +02:00
|
|
|
acc2: { [key: string]: Array<string> },
|
|
|
|
prefix2: string,
|
|
|
|
suffix2: string
|
2020-02-18 08:57:00 +01:00
|
|
|
) => {
|
2020-10-13 20:39:59 +02:00
|
|
|
// eslint-disable-next-line no-param-reassign
|
|
|
|
(acc2[prefix2] = acc2[prefix2] || []).push(suffix2);
|
|
|
|
return acc2;
|
2020-02-18 08:57:00 +01:00
|
|
|
};
|
|
|
|
if (suffix) {
|
|
|
|
return pushOrCreate(acc, prefix, suffix);
|
|
|
|
}
|
|
|
|
return pushOrCreate(acc, this.$t("Other") as string, prefix);
|
|
|
|
}, {});
|
|
|
|
}
|
|
|
|
|
|
|
|
@Watch("selectedTimezone")
|
2020-10-13 20:39:59 +02:00
|
|
|
async updateTimezone(): Promise<void> {
|
2020-06-16 18:00:27 +02:00
|
|
|
if (this.selectedTimezone !== this.loggedUser.settings.timezone) {
|
|
|
|
await this.$apollo.mutate<{ setUserSetting: string }>({
|
|
|
|
mutation: SET_USER_SETTINGS,
|
|
|
|
variables: {
|
|
|
|
timezone: this.selectedTimezone,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Watch("$i18n.locale")
|
2020-10-13 20:39:59 +02:00
|
|
|
async updateLocale(): Promise<void> {
|
2020-06-16 18:00:27 +02:00
|
|
|
await this.$apollo.mutate({
|
|
|
|
mutation: UPDATE_USER_LOCALE,
|
2020-02-18 08:57:00 +01:00
|
|
|
variables: {
|
2020-06-16 18:00:27 +02:00
|
|
|
locale: this.$i18n.locale,
|
2020-02-18 08:57:00 +01:00
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
2020-03-12 14:29:21 +01:00
|
|
|
}
|
2020-02-18 08:57:00 +01:00
|
|
|
</script>
|