Refactoring: Exported axios instance creation, now watching route param changes in EmployeesDetails.
This commit is contained in:
5
src/axios/index.ts
Normal file
5
src/axios/index.ts
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
import axios from 'axios'
|
||||||
|
|
||||||
|
export default axios.create({
|
||||||
|
baseURL: 'http://localhost:3333/api/v1/'
|
||||||
|
})
|
||||||
@@ -2,7 +2,8 @@ import { defineStore, storeToRefs } from 'pinia'
|
|||||||
import { useNotifications } from './notifications'
|
import { useNotifications } from './notifications'
|
||||||
import { getUnixTime } from 'date-fns'
|
import { getUnixTime } from 'date-fns'
|
||||||
import { AxiosError } from 'axios'
|
import { AxiosError } from 'axios'
|
||||||
import axios from 'axios'
|
import axios from '@/axios'
|
||||||
|
import Axios from 'axios'
|
||||||
|
|
||||||
type AuthSuccResult = {
|
type AuthSuccResult = {
|
||||||
user: string,
|
user: string,
|
||||||
@@ -38,7 +39,7 @@ export const useUser = defineStore({
|
|||||||
const notifications = useNotifications()
|
const notifications = useNotifications()
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await axios.post<AuthSuccResult>('http://localhost:3333/api/v1/login', {
|
const response = await axios.post<AuthSuccResult>('login', {
|
||||||
username: username,
|
username: username,
|
||||||
password: password
|
password: password
|
||||||
})
|
})
|
||||||
@@ -54,7 +55,7 @@ export const useUser = defineStore({
|
|||||||
|
|
||||||
return true
|
return true
|
||||||
} catch(err : unknown) {
|
} catch(err : unknown) {
|
||||||
if (axios.isAxiosError(err) && err.response && err.response.data){
|
if (Axios.isAxiosError(err) && err.response && err.response.data){
|
||||||
|
|
||||||
const data = err.response.data
|
const data = err.response.data
|
||||||
|
|
||||||
@@ -80,14 +81,11 @@ export const useUser = defineStore({
|
|||||||
const notifications = useNotifications()
|
const notifications = useNotifications()
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const ai = axios.create({
|
await axios.post('logout', '', {
|
||||||
baseURL: 'http://localhost:3333/api/v1/',
|
|
||||||
headers: {
|
headers: {
|
||||||
'Authorization': 'Bearer '+this.token
|
'Authorization': 'Bearer '+useUser().token
|
||||||
}
|
}
|
||||||
|
|
||||||
})
|
})
|
||||||
await ai.post('/logout')
|
|
||||||
|
|
||||||
|
|
||||||
notifications.add('success','Logged out successfully')
|
notifications.add('success','Logged out successfully')
|
||||||
|
|||||||
@@ -19,14 +19,15 @@ type ResultData = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
import VProfileControls from '@/components/VProfileControls.vue';
|
import VProfileControls from '@/components/VProfileControls.vue';
|
||||||
import { reactive, onMounted } from 'vue'
|
import { reactive, onMounted, computed, watch } from 'vue'
|
||||||
import { useUser } from '@/stores/user';
|
import { useUser } from '@/stores/user';
|
||||||
import axios from 'axios'
|
import axios from '@/axios'
|
||||||
import { useNotifications } from '@/stores/notifications';
|
import { useNotifications } from '@/stores/notifications';
|
||||||
import { useRoute } from 'vue-router';
|
import { useRoute, useRouter } from 'vue-router';
|
||||||
|
|
||||||
const userStore = useUser()
|
const userStore = useUser()
|
||||||
const useNotification = useNotifications()
|
const useNotification = useNotifications()
|
||||||
|
const route = useRoute()
|
||||||
|
|
||||||
const employee = reactive({
|
const employee = reactive({
|
||||||
id: NaN,
|
id: NaN,
|
||||||
@@ -47,24 +48,27 @@ const user = reactive({
|
|||||||
passwordRepeat: ''
|
passwordRepeat: ''
|
||||||
})
|
})
|
||||||
|
|
||||||
|
function onUpdate() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
function onEnter() {
|
function onEnter() {
|
||||||
console.log("hi")
|
console.log("hi")
|
||||||
}
|
}
|
||||||
|
|
||||||
onMounted(async () => {
|
watch(() => [route.params, route.name], ([newParam, newName], [oldParam, oldName]) => {
|
||||||
const ai = axios.create({
|
if(newName === oldName && newParam !== oldParam) {
|
||||||
baseURL: 'http://localhost:3333/api/v1/',
|
getEmployee()
|
||||||
headers: {
|
}
|
||||||
'Authorization': 'Bearer '+userStore.token
|
})
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
|
async function getEmployee() {
|
||||||
try {
|
try {
|
||||||
const id = useRoute().params.id
|
const data : ResultData = await <ResultData>(await axios.get('employees/'+route.params.id, {
|
||||||
|
headers: {
|
||||||
const data : ResultData = await <ResultData>(await ai.get('employees/'+id)).data
|
'Authorization': 'Bearer '+useUser().token
|
||||||
|
}
|
||||||
|
})).data
|
||||||
|
|
||||||
Object.assign(employee, data.employee)
|
Object.assign(employee, data.employee)
|
||||||
Object.assign(user, data.user)
|
Object.assign(user, data.user)
|
||||||
@@ -72,7 +76,10 @@ onMounted(async () => {
|
|||||||
catch(err){
|
catch(err){
|
||||||
if(err instanceof Error) useNotification.add('danger', err.message, -1)
|
if(err instanceof Error) useNotification.add('danger', err.message, -1)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
onMounted(async () => {
|
||||||
|
getEmployee()
|
||||||
})
|
})
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
Reference in New Issue
Block a user