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