added lots of animations, added debounce to search query, fixed faulty refetching when logging out from profile page

This commit is contained in:
Sockenklaus
2021-11-14 11:20:01 +01:00
parent a92342d445
commit 0d00d73eb4
11 changed files with 174 additions and 118 deletions

View File

@@ -23,7 +23,7 @@ export const useNotifications = defineStore('notifications', {
text:text,
}
)
if (timeout !== -1) setTimeout(() => this.remove(id), timeout)
if (timeout > -1) setTimeout(() => this.remove(id), timeout)
}
}
})

View File

@@ -1,20 +1,18 @@
import { defineStore, storeToRefs } from 'pinia'
import { defineStore } from 'pinia'
import { useNotifications } from './notifications'
import { getUnixTime } from 'date-fns'
import { AxiosError } from 'axios'
import axios from '@/axios'
import { apiUrl } from '@/axios'
import Axios from 'axios'
import { ref, reactive } from 'vue'
import { useRequest } from 'vue-request'
import { json } from 'stream/consumers'
import axios from '@/axios'
import Axios from 'axios'
import { useRouter } from 'vue-router'
import type { Router } from 'vue-router'
type AuthSuccResult = {
user: string,
role: string,
user: string,
role: string,
token: string
}
export const useUser = defineStore({
id: 'storeUser',
@@ -23,8 +21,9 @@ export const useUser = defineStore({
user: '',
role: '',
isLoggedIn: false,
rememberMe: false,
token: ''
token: '',
errorMessage: '',
loading: ref(false)
}
},
@@ -36,18 +35,27 @@ export const useUser = defineStore({
},
actions: {
async login(username: string, password: string):
Promise<boolean |
'E_INVALID_AUTH_PASSWORD: Password mis-match' |
'E_INVALID_AUTH_UID: User not found'>
{
async login(username: string, password: string, router: Router) {
this.loading = true
const notifications = useNotifications()
function sleep(ms: number): Promise<void> {
return new Promise(resolve => setTimeout(resolve, ms));
}
/* async function axiosQuery<T>(username: string, password: string){
return await axios.post<T>('login',{
username,
password
})
}
const { loading } = useRequest(axiosQuery(username, password))
this.loading */
try {
const response = await axios.post<AuthSuccResult>('login', {
username: username,
password: password
username,
password
})
this.isLoggedIn = true
@@ -57,53 +65,43 @@ export const useUser = defineStore({
notifications.add('success', 'Login successful.')
return true
} catch(err : unknown) {
await router.push({name: 'Home'})
this.loading = false
} catch(err : unknown) {
this.loading = false
if (Axios.isAxiosError(err) && err.response && err.response.data){
const data = err.response.data
if(
data === 'E_INVALID_AUTH_PASSWORD: Password mis-match' ||
data === 'E_INVALID_AUTH_UID: User not found'
) return data
this.errorMessage = err.response.data as string
}
else if(err instanceof Error){
notifications.add('danger', err.message, -1)
return false
}
notifications.add('danger', err as string, -1)
return false
}
}
},
async logout(): Promise<boolean> {
async logout(router: Router) {
const notifications = useNotifications()
try {
await axios.post('logout', '', {
const result = await axios.post('logout', '', {
headers: {
'Authorization': 'Bearer '+useUser().token
}
})
await router.push({name: 'Login'})
notifications.add('success','Logged out successfully')
this.$reset()
return true
}
catch(error) {
if(error instanceof Error) {
notifications.add('danger', error.message, -1)
}
return false
}
}
},