feat: user store cleanup
This commit is contained in:
parent
d9fefe1d62
commit
44868f4673
|
|
@ -13,7 +13,7 @@ const hideVersion = (import.meta.env.VITE_APP_ENVIRONMENT || "local").startsWith
|
|||
);
|
||||
|
||||
async function changeLocale(language: AvailableLanguages) {
|
||||
userStore.setUserLanguages(language);
|
||||
await userStore.updateUserProfile({ language });
|
||||
}
|
||||
</script>
|
||||
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@ const props = defineProps<{
|
|||
const userStore = useUserStore();
|
||||
|
||||
async function changeLocale(language: AvailableLanguages) {
|
||||
await userStore.setUserLanguages(language);
|
||||
await userStore.updateUserProfile({ language });
|
||||
}
|
||||
</script>
|
||||
<template>
|
||||
|
|
|
|||
|
|
@ -57,7 +57,9 @@ async function avatarUpload(e: Event) {
|
|||
}
|
||||
|
||||
watch(selectedOrganisation, async (organisation) => {
|
||||
await user.setUserOrganisation(organisation.id);
|
||||
await user.updateUserProfile({
|
||||
organisation: organisation.id,
|
||||
});
|
||||
});
|
||||
|
||||
const nextRoute = computed(() => {
|
||||
|
|
|
|||
|
|
@ -15,6 +15,7 @@ const formData = ref();
|
|||
const saved = ref(false);
|
||||
|
||||
function startEditMode() {
|
||||
saved.value = false;
|
||||
formData.value = {
|
||||
first_name: user.first_name,
|
||||
last_name: user.last_name,
|
||||
|
|
|
|||
|
|
@ -1,5 +1,3 @@
|
|||
import log from "loglevel";
|
||||
|
||||
import { bustItGetCache, itGetCached, itPost } from "@/fetchHelpers";
|
||||
import { setI18nLanguage } from "@/i18nextWrapper";
|
||||
import type { Country } from "@/services/entities";
|
||||
|
|
@ -9,7 +7,6 @@ import { defineStore } from "pinia";
|
|||
|
||||
let logoutRedirectUrl = import.meta.env.VITE_LOGOUT_REDIRECT || "/";
|
||||
|
||||
// TODO: check if user logged in with SSO or login-local
|
||||
if (import.meta.env.VITE_OAUTH_API_BASE_URL) {
|
||||
logoutRedirectUrl = `${
|
||||
import.meta.env.VITE_OAUTH_API_BASE_URL
|
||||
|
|
@ -21,7 +18,7 @@ const AVAILABLE_LANGUAGES = ["de", "fr", "it"];
|
|||
export type AvailableLanguages = "de" | "fr" | "it";
|
||||
export type InvoiceAddress = "prv" | "org";
|
||||
|
||||
export interface UserProfile {
|
||||
export interface User {
|
||||
id: string;
|
||||
username: string;
|
||||
first_name: string;
|
||||
|
|
@ -30,6 +27,7 @@ export interface UserProfile {
|
|||
avatar_url: string;
|
||||
organisation: number | null;
|
||||
is_superuser: boolean;
|
||||
loggedIn: boolean;
|
||||
language: AvailableLanguages;
|
||||
course_session_experts: string[];
|
||||
invoice_address: InvoiceAddress | null;
|
||||
|
|
@ -46,10 +44,6 @@ export interface UserProfile {
|
|||
organisation_country: Country | null;
|
||||
}
|
||||
|
||||
export type UserState = {
|
||||
loggedIn: boolean;
|
||||
} & UserProfile;
|
||||
|
||||
let defaultLanguage: AvailableLanguages = "de";
|
||||
|
||||
const isAvailableLanguage = (language: string): language is AvailableLanguages => {
|
||||
|
|
@ -67,7 +61,7 @@ for (const language of languagesWithoutCountryCode) {
|
|||
}
|
||||
}
|
||||
|
||||
const initialUserState: UserState = {
|
||||
const initialUserState: User = {
|
||||
id: "",
|
||||
email: "",
|
||||
first_name: "",
|
||||
|
|
@ -110,7 +104,7 @@ async function setLocale(language: AvailableLanguages) {
|
|||
|
||||
export const useUserStore = defineStore({
|
||||
id: "user",
|
||||
state: () => initialUserState as UserState,
|
||||
state: () => initialUserState as User,
|
||||
getters: {
|
||||
getFullName(): string {
|
||||
return `${this.first_name} ${this.last_name}`.trim();
|
||||
|
|
@ -130,24 +124,20 @@ export const useUserStore = defineStore({
|
|||
},
|
||||
},
|
||||
actions: {
|
||||
handleLogin(username: string, password: string, next = "/") {
|
||||
async handleLogin(username: string, password: string, next = "/") {
|
||||
if (username && password) {
|
||||
itPost("/api/core/login/", {
|
||||
username,
|
||||
password,
|
||||
})
|
||||
.then((data) => {
|
||||
this.$state = data;
|
||||
this.loggedIn = true;
|
||||
log.debug("bust cache");
|
||||
bustItGetCache();
|
||||
log.debug(`redirect to ${next}`);
|
||||
window.location.href = next;
|
||||
})
|
||||
.catch(() => {
|
||||
this.loggedIn = false;
|
||||
alert("Login failed");
|
||||
try {
|
||||
this.$state = await itPost("/api/core/login/", {
|
||||
username,
|
||||
password,
|
||||
});
|
||||
this.loggedIn = true;
|
||||
bustItGetCache();
|
||||
window.location.href = next;
|
||||
} catch (e) {
|
||||
this.loggedIn = false;
|
||||
alert("Login failed");
|
||||
}
|
||||
}
|
||||
},
|
||||
handleLogout() {
|
||||
|
|
@ -170,20 +160,15 @@ export const useUserStore = defineStore({
|
|||
this.loggedIn = true;
|
||||
await setLocale(data.language);
|
||||
},
|
||||
async setUserLanguages(language: AvailableLanguages) {
|
||||
await setLocale(language);
|
||||
this.$state.language = language;
|
||||
await itPost("/api/core/me/", { language }, { method: "PUT" });
|
||||
},
|
||||
async setUserOrganisation(organisation: number) {
|
||||
this.$state.organisation = organisation;
|
||||
await itPost("/api/core/me/", { organisation }, { method: "PUT" });
|
||||
},
|
||||
async setUserAvatar(file: File) {
|
||||
const r = await directUpload("/api/core/avatar/", file);
|
||||
this.$state.avatar_url = r.url;
|
||||
},
|
||||
async updateUserProfile(profileData: UserProfile) {
|
||||
async updateUserProfile(profileData: Partial<User>) {
|
||||
if (profileData.language) {
|
||||
await setLocale(profileData.language);
|
||||
}
|
||||
|
||||
await itPost("/api/core/me/", profileData, { method: "PUT" });
|
||||
Object.assign(this.$state, profileData);
|
||||
},
|
||||
|
|
|
|||
Loading…
Reference in New Issue