axios: baseURL

This commit is contained in:
2023-02-10 14:42:11 +01:00
parent 446698b832
commit 580e109dda
7 changed files with 86 additions and 98 deletions
+68 -1
View File
@@ -1,5 +1,8 @@
import { defineStore } from 'pinia';
import { AuthState, IStore } from './types/types';
import { AuthState, ILoginResponse, IStore, IUser, SceneryRowItem } from './types/types';
import axios from 'axios';
const baseURL = import.meta.env[`VITE_API_URL${import.meta.env.DEV ? '_DEV' : ''}`];
export const useStore = defineStore('store', {
state: () =>
@@ -29,4 +32,68 @@ export const useStore = defineStore('store', {
changesResponse: [],
} as IStore),
actions: {
fetchSceneriesData() {
this.dataState = 'LOADING';
const data = axios.get<SceneryRowItem[]>(`api/getSceneries?time=${Date.now()}`, {
baseURL,
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${this.token}`,
},
});
data
.then((res) => {
this.dataState = 'LOADED';
this.backupList = JSON.parse(JSON.stringify(res.data));
this.stationList = res.data;
this.unsavedChanges = false;
this.changeList = [];
})
.catch(() => {
this.dataState = 'ERROR';
this.token = '';
this.isAuthorized = false;
});
},
updateSceneriesData(mappedChangeList: any[]) {
const response = axios.post(
'/manager/updateSceneryList',
{
changeList: mappedChangeList,
token: this.token,
notify: this.notifyDiscord,
},
{
baseURL,
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${this.token}`,
},
}
);
return response;
},
login(name: string, pwd: string) {
return axios.post<ILoginResponse>(
'auth/login',
{ username: name, password: pwd },
{
baseURL,
headers: {
'Content-Type': 'application/json',
},
}
);
},
fetchTokenData() {
return axios.post<{ user: IUser }>('auth/token', { token: this.token }, { baseURL });
},
},
});