chore: fetching & caching vehicles data information

This commit is contained in:
2024-06-03 01:31:31 +02:00
parent 785a42b849
commit 8190dfa2cb
4 changed files with 61 additions and 15 deletions
+24 -14
View File
@@ -4,20 +4,17 @@ import { Status } from '../typings/common';
import { StationJSONData } from './typings';
import axios, { AxiosInstance } from 'axios';
export enum APIMode {
PRODUCTION = 0,
DEV = 1,
MOCK = 2
}
export const useApiStore = defineStore('apiStore', {
state: () => ({
dataStatuses: {
connection: Status.Data.Loading,
sceneries: Status.Data.Loading
sceneries: Status.Data.Loading,
vehicles: Status.Data.Loading
},
activeData: undefined as API.ActiveData.Response | undefined,
vehiclesData: undefined as API.Vehicles.Response | undefined,
donatorsData: [] as API.Donators.Response,
sceneryData: [] as StationJSONData[],
@@ -54,6 +51,7 @@ export const useApiStore = defineStore('apiStore', {
// Static data
this.fetchDonatorsData();
this.fetchStationsGeneralInfo();
this.fetchVehiclesInfo();
},
async fetchActiveData() {
@@ -82,17 +80,29 @@ export const useApiStore = defineStore('apiStore', {
},
async fetchStationsGeneralInfo() {
const sceneryData: StationJSONData[] = (
await this.client!.get<StationJSONData[]>('api/getSceneries')
).data;
try {
const sceneryData: StationJSONData[] = (
await this.client!.get<StationJSONData[]>('api/getSceneries')
).data;
if (!sceneryData) {
this.dataStatuses.sceneries = Status.Data.Loaded;
this.sceneryData = sceneryData;
} catch (error) {
this.dataStatuses.sceneries = Status.Data.Error;
return;
console.error('Ups! Wystąpił błąd podczas pobierania informacji o sceneriach:', error);
}
},
this.dataStatuses.sceneries = Status.Data.Loaded;
this.sceneryData = sceneryData;
async fetchVehiclesInfo() {
try {
const response = await this.client!.get<API.Vehicles.Response>('vehicles');
this.vehiclesData = response.data;
this.dataStatuses.vehicles = response.data ? Status.Data.Loaded : Status.Data.Warning;
} catch (error) {
this.dataStatuses.vehicles = Status.Data.Error;
console.error('Ups! Wystąpił błąd podczas pobierania informacji o pojazdach:', error);
}
}
}
});
+6 -1
View File
@@ -1,4 +1,4 @@
import { Status } from './common';
import { Status, VehiclesData } from './common';
export enum APIDataStatus {
OK = 'OK',
@@ -19,6 +19,7 @@ export namespace API {
apiStatuses?: APIStatuses;
}
}
export namespace DispatcherHistory {
export type Response = Data[];
@@ -316,6 +317,10 @@ export namespace API {
export namespace Donators {
export type Response = string[];
}
export namespace Vehicles {
export type Response = VehiclesData;
}
}
export namespace GithubAPI {
+24
View File
@@ -189,3 +189,27 @@ export interface CheckpointTrain {
checkpointStop: TrainStop;
train: Train;
}
// Vehicles Data
export interface VehiclesData {
simulatorVersion: string;
vehicleList: any[][];
vehicleProps: VehicleProps[];
}
export interface VehicleProps {
type: string;
speed: number;
length: number;
weight: number;
cargoTypes?: VehicleCargo[];
coldStart?: boolean;
doubleManned?: boolean;
}
export interface VehicleCargo {
id: string;
weight: number;
}