Refactor user setting

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel 2021-03-04 15:26:30 +01:00
parent dc9fe0d22b
commit fba9ff4a62
No known key found for this signature in database
GPG Key ID: A061B9DDE0CA0773
1 changed files with 29 additions and 35 deletions

View File

@ -94,7 +94,7 @@
</div> </div>
</template> </template>
<script lang="ts"> <script lang="ts">
import { Component, Vue, Watch } from "vue-property-decorator"; import { Component, Vue } from "vue-property-decorator";
import ngeohash from "ngeohash"; import ngeohash from "ngeohash";
import { saveLocaleData } from "@/utils/auth"; import { saveLocaleData } from "@/utils/auth";
import { TIMEZONES } from "../../graphql/config"; import { TIMEZONES } from "../../graphql/config";
@ -125,27 +125,41 @@ export default class Preferences extends Vue {
loggedUser!: IUser; loggedUser!: IUser;
selectedTimezone: string | undefined = undefined;
locale: string | null = null;
RouteName = RouteName; RouteName = RouteName;
langs: Record<string, string> = langs; langs: Record<string, string> = langs;
AddressSearchType = AddressSearchType; AddressSearchType = AddressSearchType;
@Watch("loggedUser") get selectedTimezone(): string {
setSavedTimezone(loggedUser: IUser): void { if (this.loggedUser?.settings?.timezone) {
if (loggedUser?.settings?.timezone) { return this.loggedUser.settings.timezone;
this.selectedTimezone = loggedUser.settings.timezone;
} else {
this.selectedTimezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
} }
if (loggedUser?.locale) { return Intl.DateTimeFormat().resolvedOptions().timeZone;
this.locale = loggedUser.locale; }
} else {
this.locale = this.$i18n.locale; set selectedTimezone(selectedTimezone: string) {
if (selectedTimezone !== this.loggedUser?.settings?.timezone) {
this.updateUserSettings({ timezone: selectedTimezone });
}
}
get locale(): string {
if (this.loggedUser?.locale) {
return this.loggedUser?.locale;
}
return this.$i18n.locale;
}
set locale(locale: string) {
if (locale) {
this.$apollo.mutate({
mutation: UPDATE_USER_LOCALE,
variables: {
locale,
},
});
saveLocaleData(locale);
} }
} }
@ -186,26 +200,6 @@ export default class Preferences extends Vue {
); );
} }
@Watch("selectedTimezone")
async updateTimezone(): Promise<void> {
if (this.selectedTimezone !== this.loggedUser.settings.timezone) {
this.updateUserSettings({ timezone: this.selectedTimezone });
}
}
@Watch("locale")
async updateLocale(): Promise<void> {
if (this.locale) {
await this.$apollo.mutate({
mutation: UPDATE_USER_LOCALE,
variables: {
locale: this.locale,
},
});
saveLocaleData(this.locale);
}
}
get address(): IAddress | null { get address(): IAddress | null {
if ( if (
this.loggedUser?.settings?.location?.name && this.loggedUser?.settings?.location?.name &&