mirror of
https://github.com/Spythere/stacjownik.git
synced 2026-05-03 13:28:11 +00:00
chore: fetching & caching vehicles data information
This commit is contained in:
+24
-14
@@ -4,20 +4,17 @@ import { Status } from '../typings/common';
|
|||||||
import { StationJSONData } from './typings';
|
import { StationJSONData } from './typings';
|
||||||
import axios, { AxiosInstance } from 'axios';
|
import axios, { AxiosInstance } from 'axios';
|
||||||
|
|
||||||
export enum APIMode {
|
|
||||||
PRODUCTION = 0,
|
|
||||||
DEV = 1,
|
|
||||||
MOCK = 2
|
|
||||||
}
|
|
||||||
|
|
||||||
export const useApiStore = defineStore('apiStore', {
|
export const useApiStore = defineStore('apiStore', {
|
||||||
state: () => ({
|
state: () => ({
|
||||||
dataStatuses: {
|
dataStatuses: {
|
||||||
connection: Status.Data.Loading,
|
connection: Status.Data.Loading,
|
||||||
sceneries: Status.Data.Loading
|
sceneries: Status.Data.Loading,
|
||||||
|
vehicles: Status.Data.Loading
|
||||||
},
|
},
|
||||||
|
|
||||||
activeData: undefined as API.ActiveData.Response | undefined,
|
activeData: undefined as API.ActiveData.Response | undefined,
|
||||||
|
vehiclesData: undefined as API.Vehicles.Response | undefined,
|
||||||
|
|
||||||
donatorsData: [] as API.Donators.Response,
|
donatorsData: [] as API.Donators.Response,
|
||||||
sceneryData: [] as StationJSONData[],
|
sceneryData: [] as StationJSONData[],
|
||||||
|
|
||||||
@@ -54,6 +51,7 @@ export const useApiStore = defineStore('apiStore', {
|
|||||||
// Static data
|
// Static data
|
||||||
this.fetchDonatorsData();
|
this.fetchDonatorsData();
|
||||||
this.fetchStationsGeneralInfo();
|
this.fetchStationsGeneralInfo();
|
||||||
|
this.fetchVehiclesInfo();
|
||||||
},
|
},
|
||||||
|
|
||||||
async fetchActiveData() {
|
async fetchActiveData() {
|
||||||
@@ -82,17 +80,29 @@ export const useApiStore = defineStore('apiStore', {
|
|||||||
},
|
},
|
||||||
|
|
||||||
async fetchStationsGeneralInfo() {
|
async fetchStationsGeneralInfo() {
|
||||||
const sceneryData: StationJSONData[] = (
|
try {
|
||||||
await this.client!.get<StationJSONData[]>('api/getSceneries')
|
const sceneryData: StationJSONData[] = (
|
||||||
).data;
|
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;
|
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;
|
async fetchVehiclesInfo() {
|
||||||
this.sceneryData = sceneryData;
|
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
@@ -1,4 +1,4 @@
|
|||||||
import { Status } from './common';
|
import { Status, VehiclesData } from './common';
|
||||||
|
|
||||||
export enum APIDataStatus {
|
export enum APIDataStatus {
|
||||||
OK = 'OK',
|
OK = 'OK',
|
||||||
@@ -19,6 +19,7 @@ export namespace API {
|
|||||||
apiStatuses?: APIStatuses;
|
apiStatuses?: APIStatuses;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export namespace DispatcherHistory {
|
export namespace DispatcherHistory {
|
||||||
export type Response = Data[];
|
export type Response = Data[];
|
||||||
|
|
||||||
@@ -316,6 +317,10 @@ export namespace API {
|
|||||||
export namespace Donators {
|
export namespace Donators {
|
||||||
export type Response = string[];
|
export type Response = string[];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export namespace Vehicles {
|
||||||
|
export type Response = VehiclesData;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export namespace GithubAPI {
|
export namespace GithubAPI {
|
||||||
|
|||||||
@@ -189,3 +189,27 @@ export interface CheckpointTrain {
|
|||||||
checkpointStop: TrainStop;
|
checkpointStop: TrainStop;
|
||||||
train: Train;
|
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;
|
||||||
|
}
|
||||||
|
|||||||
@@ -33,6 +33,13 @@ export default defineConfig({
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
urlPattern: /^https:\/\/stacjownik.spythere.eu\/vehicles/i,
|
||||||
|
handler: 'StaleWhileRevalidate',
|
||||||
|
options: {
|
||||||
|
cacheName: 'spythere-vehicles-cache'
|
||||||
|
}
|
||||||
|
},
|
||||||
{
|
{
|
||||||
urlPattern: /^https:\/\/static.spythere.eu\/.*/i,
|
urlPattern: /^https:\/\/static.spythere.eu\/.*/i,
|
||||||
handler: 'CacheFirst',
|
handler: 'CacheFirst',
|
||||||
|
|||||||
Reference in New Issue
Block a user