Move banner system to store, migrate login/register API handling to own file, display Account details on accountPage

This commit is contained in:
2024-09-19 16:20:12 +02:00
parent 5b3a753233
commit ed264ff026
19 changed files with 250 additions and 141 deletions

View File

@@ -0,0 +1,63 @@
import { defineStore } from "pinia";
import { ref } from "vue";
import { BannerStateEnum } from "../enums/bannerStateEnum";
import { Composer } from 'vue-i18n';
export const useFeedbackStore = defineStore("feedbackStore", {
state: () => ({
showBanner: ref(false),
title: ref(""),
color: ref(""),
icon: ref(""),
$i18n: {}
}),
getters: {
i18n(): Composer {
return this.$i18n.global as Composer
}
},
actions: {
changeBanner(bannerState: BannerStateEnum) {
switch (bannerState) {
case BannerStateEnum.ERROR: {
this.title = this.i18n.t('bannerMessages.error'); break;
}
case BannerStateEnum.DATABASERESETSUCCESSFUL: {
this.title = this.i18n.t('bannerMessages.databaseResetSuccessful'); break;
}
case BannerStateEnum.LOGINSUCCESSFUL: {
this.title = this.i18n.t('bannerMessages.loginSuccessful'); break;
}
case BannerStateEnum.WRONGLOGIN: {
this.title = this.i18n.t('bannerMessages.wrongLogin'); break;
}
case BannerStateEnum.REGISTERSUCCESSFUL: {
this.title = this.i18n.t("bannerMessages.registerSuccessful"); break;
}
case BannerStateEnum.USERNAMEINUSE: {
this.title = this.i18n.t("bannerMessages.usernameInUse"); break;
}
}
switch (bannerState) {
case BannerStateEnum.ERROR:
case BannerStateEnum.WRONGLOGIN:
case BannerStateEnum.USERNAMEINUSE:
this.color = "red"
this.icon = "mdi-alert-circle"
break;
case BannerStateEnum.DATABASERESETSUCCESSFUL:
case BannerStateEnum.LOGINSUCCESSFUL:
case BannerStateEnum.REGISTERSUCCESSFUL:
this.color = "green"
this.icon = "mdi-check-circle"
break
}
this.showBanner = true
}
}
})

View File

@@ -2,11 +2,61 @@ import { defineStore } from "pinia";
import { useLocalStorage } from "@vueuse/core";
import { ThemeEnum } from "../enums/themeEnums";
import { LanguageEnum } from "../enums/languageEnum";
import { AccountModel } from "../models/accountModel";
import { login, register } from "../api/accountApi";
import { useFeedbackStore } from "./feedbackStore";
import { BannerStateEnum } from "../enums/bannerStateEnum";
export const useUserStore = defineStore('user', {
export const useUserStore = defineStore('userStore', {
state: () => ({
theme: useLocalStorage<ThemeEnum>("hackmycart/userStore/theme", ThemeEnum.DARKRED),
language: useLocalStorage<LanguageEnum>("hackmycart/userStore/language", LanguageEnum.GERMAN),
userAccountId: useLocalStorage<number>("hackmycart/userStore/userAccountId", -1)
})
userAccount: useLocalStorage<AccountModel>("hackmycart/userStore/userAccount", new AccountModel()),
loggedIn: useLocalStorage<Boolean>("hackmycart/userStore/loggedIn", false)
}),
actions: {
async login(username: string, password: string) {
const feedbackStore = useFeedbackStore()
await login(username, password)
.then(result => {
if (result.data.loginSuccessful) {
this.userAccount = result.data.account
this.loggedIn = true
feedbackStore.changeBanner(BannerStateEnum.LOGINSUCCESSFUL)
}
})
.catch(error => {
this.userAccount = new AccountModel()
this.loggedIn = false
feedbackStore.changeBanner(BannerStateEnum.WRONGLOGIN)
})
},
async registerAccount(account: AccountModel) {
const feedbackStore = useFeedbackStore()
await register(account)
.then(res => {
console.log(res)
if (res.status == 200) {
feedbackStore.changeBanner(BannerStateEnum.REGISTERSUCCESSFUL)
}
})
.catch((error) => {
console.log(error)
if (error.status == 400) {
feedbackStore.changeBanner(BannerStateEnum.USERNAMEINUSE)
}
})
},
logout() {
this.userAccount = new AccountModel()
this.loggedIn = false
}
}
})