From 8190dfa2cb69c85219f65bc6c0c2143516abd51d Mon Sep 17 00:00:00 2001 From: Spythere Date: Mon, 3 Jun 2024 01:31:31 +0200 Subject: [PATCH] chore: fetching & caching vehicles data information --- src/store/apiStore.ts | 38 ++++++++++++++++++++++++-------------- src/typings/api.ts | 7 ++++++- src/typings/common.ts | 24 ++++++++++++++++++++++++ vite.config.ts | 7 +++++++ 4 files changed, 61 insertions(+), 15 deletions(-) diff --git a/src/store/apiStore.ts b/src/store/apiStore.ts index 990dd53..0b558ad 100644 --- a/src/store/apiStore.ts +++ b/src/store/apiStore.ts @@ -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('api/getSceneries') - ).data; + try { + const sceneryData: StationJSONData[] = ( + await this.client!.get('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('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); + } } } }); diff --git a/src/typings/api.ts b/src/typings/api.ts index f37635d..2f97f43 100644 --- a/src/typings/api.ts +++ b/src/typings/api.ts @@ -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 { diff --git a/src/typings/common.ts b/src/typings/common.ts index 0e5393f..e53b9da 100644 --- a/src/typings/common.ts +++ b/src/typings/common.ts @@ -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; +} diff --git a/vite.config.ts b/vite.config.ts index b629cac..e32fe95 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -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, handler: 'CacheFirst',