Fix błędów z reaktywnością danych

This commit is contained in:
2022-06-12 22:49:55 +02:00
parent ed4c5a649e
commit 6167ab5826
8 changed files with 67 additions and 90 deletions
+4 -3
View File
@@ -69,7 +69,7 @@
padding: 0.5em 0.4em; padding: 0.5em 0.4em;
max-width: 1100px; max-width: 1100px;
margin: 0 auto; margin: 0 auto;
border-radius: 0 0 1em 1em; border-radius: 0 0 1em 1em;
} }
@@ -180,7 +180,7 @@
@include smallScreen() { @include smallScreen() {
flex-direction: column; flex-direction: column;
a { a {
margin: 0.25em 0; margin: 0.25em 0;
} }
@@ -201,8 +201,9 @@
width: 1.35em; width: 1.35em;
} }
& > .region { .region {
color: paleturquoise; color: paleturquoise;
font-weight: bold;
} }
} }
+12 -15
View File
@@ -33,13 +33,13 @@
<span class="header_info"> <span class="header_info">
<Clock /> <Clock />
<div class="info_counter"> <div class="info_counter">
<span class="region" @click="openFilterCard"> <span class="region" @click="changeRegion">
{{ currentRegion.value }} {{ store.region.value }}
</span> </span>
<img src="@/assets/icon-dispatcher.svg" alt="icon dispatcher" /> <img src="@/assets/icon-dispatcher.svg" alt="icon dispatcher" />
<span class="text--primary">{{ dispatcherCount }}</span> <span class="text--primary">{{ onlineDispatchers.length }}</span>
<span class="text--grayed">|</span> <span class="text--grayed">|</span>
<span class="text--primary">{{ trainCount }}</span> <span class="text--primary">{{ store.trainList.length }}</span>
<img src="@/assets/icon-train.svg" alt="icon train" /> <img src="@/assets/icon-train.svg" alt="icon train" />
</div> </div>
</span> </span>
@@ -99,8 +99,6 @@ export default defineComponent({
const store = useStore(); const store = useStore();
store.connectToAPI(); store.connectToAPI();
const currentRegion = store.region;
// const sceneryDataStatus = computed(() => data.value.sceneryDataStatus); // const sceneryDataStatus = computed(() => data.value.sceneryDataStatus);
const isFilterCardVisible = ref(false); const isFilterCardVisible = ref(false);
@@ -108,18 +106,13 @@ export default defineComponent({
provide('isFilterCardVisible', isFilterCardVisible); provide('isFilterCardVisible', isFilterCardVisible);
return { return {
currentRegion, store,
isFilterCardVisible, isFilterCardVisible,
onlineDispatchers: computed(() =>
store.stationList.filter((station) => station.onlineInfo && station.onlineInfo.region == store.region.id)
),
dispatcherDataStatus: store.dataStatuses.dispatchers, dispatcherDataStatus: store.dataStatuses.dispatchers,
trainCount: store.trainCount,
dispatcherCount: store.stationList.filter((station) => station.onlineInfo).length,
openFilterCard() {
isFilterCardVisible.value = true;
},
}; };
}, },
@@ -163,6 +156,10 @@ export default defineComponent({
StorageManager.setBooleanValue('version_notes_read', true); StorageManager.setBooleanValue('version_notes_read', true);
}, },
changeRegion() {
this.store.changeRegion({ id: 'cae', value: 'PL2' });
},
changeLang(lang: string) { changeLang(lang: string) {
this.$i18n.locale = lang; this.$i18n.locale = lang;
this.currentLang = lang; this.currentLang = lang;
@@ -13,7 +13,7 @@
<div class="card_content"> <div class="card_content">
<div class="card_title flex">{{ $t('filters.title') }}</div> <div class="card_title flex">{{ $t('filters.title') }}</div>
<section class="card_regions"> <!-- <section class="card_regions">
<div class="regions_content"> <div class="regions_content">
<span v-for="region in inputs.regions" :key="region.id" :class="`region-${region.id}`"> <span v-for="region in inputs.regions" :key="region.id" :class="`region-${region.id}`">
<label> <label>
@@ -31,7 +31,7 @@
</label> </label>
</span> </span>
</div> </div>
</section> </section> -->
<section class="card_options"> <section class="card_options">
<filter-option <filter-option
v-for="(option, i) in inputs.options" v-for="(option, i) in inputs.options"
@@ -190,11 +190,6 @@ export default defineComponent({
}, 400); }, 400);
}, },
handleChangeRegion() {
this.store.region = this.currentRegion;
this.closeCard();
},
changeNumericFilterValue(name: string, value: number, saveToStorage = false) { changeNumericFilterValue(name: string, value: number, saveToStorage = false) {
this.$emit('changeFilterValue', { this.$emit('changeFilterValue', {
name, name,
@@ -314,7 +309,7 @@ export default defineComponent({
&_content { &_content {
display: grid; display: grid;
grid-template-rows: 70px 35px 1fr 100px 50px auto; grid-template-rows: 70px 1fr 100px 50px auto;
min-height: 0; min-height: 0;
max-height: 100vh; max-height: 100vh;
} }
+1 -34
View File
@@ -33,6 +33,7 @@ export default interface Station {
onlineInfo?: { onlineInfo?: {
hash: string; hash: string;
name: string; name: string;
region: string;
maxUsers: number; maxUsers: number;
currentUsers: number; currentUsers: number;
@@ -57,38 +58,4 @@ export default interface Station {
scheduledTrains?: ScheduledTrain[]; scheduledTrains?: ScheduledTrain[];
} }
// stationName: string;
// stationHash: string;
// stationURL: string;
// maxUsers: number;
// currentUsers: number;
// stationLines: string;
// stationProject: string;
// reqLevel: number;
// supportersOnly: boolean;
// SBL: string;
// TWB: string;
// signalType: string;
// controlType: string;
// default: boolean;
// nonPublic: boolean;
// unavailable: boolean;
// routes: {
// oneWay: { catenary: number; noCatenary: number };
// twoWay: { catenary: number; noCatenary: number };
// };
// checkpoints: {
// checkpointName: string;
// scheduledTrains: ScheduledTrain[];
// }[];
} }
+35 -17
View File
@@ -22,6 +22,8 @@ import { APIData, StationJSONData, StoreState } from './storeTypes';
export const useStore = defineStore('store', { export const useStore = defineStore('store', {
state: () => state: () =>
({ ({
apiData: {} as unknown,
stationList: [], stationList: [],
trainList: [], trainList: [],
@@ -47,7 +49,9 @@ export const useStore = defineStore('store', {
} as StoreState), } as StoreState),
actions: { actions: {
setTrainsOnlineData(trains?: TrainAPIData[]) { setTrainsOnlineData() {
const { trains } = this.apiData;
if (!trains) return []; if (!trains) return [];
this.trainList = trains this.trainList = trains
@@ -93,15 +97,17 @@ export const useStore = defineStore('store', {
}) as Train[]; }) as Train[];
}, },
getDispatcherStatus(dispatchers: any[][] | undefined, stationAPIData: StationAPIData) { getDispatcherStatus(onlineStationData: StationAPIData) {
const { dispatchers } = this.apiData;
const prevDispatcherStatus = this.lastDispatcherStatuses.find( const prevDispatcherStatus = this.lastDispatcherStatuses.find(
(dispatcher) => dispatcher.hash === stationAPIData.stationHash (dispatcher) => dispatcher.hash === onlineStationData.stationHash
); );
const stationStatus = !dispatchers const stationStatus = !dispatchers
? undefined ? undefined
: dispatchers.find( : dispatchers.find(
(status: string[]) => status[0] == stationAPIData.stationHash && status[1] == this.region.id (status: string[]) => status[0] == onlineStationData.stationHash && status[1] == this.region.id
) || -1; ) || -1;
const statusTimestamp = const statusTimestamp =
@@ -110,7 +116,7 @@ export const useStore = defineStore('store', {
prevDispatcherStatus && !dispatchers ? prevDispatcherStatus.statusID : getStatusID(stationStatus); prevDispatcherStatus && !dispatchers ? prevDispatcherStatus.statusID : getStatusID(stationStatus);
return { return {
hash: stationAPIData.stationHash, hash: onlineStationData.stationHash,
statusID, statusID,
statusTimestamp, statusTimestamp,
}; };
@@ -188,17 +194,17 @@ export const useStore = defineStore('store', {
.map((train) => ({ driverName: train.driverName, driverId: train.driverId, trainNo: train.trainNo })); .map((train) => ({ driverName: train.driverName, driverId: train.driverId, trainNo: train.trainNo }));
}, },
setStationsOnlineInfo(data: APIData) { setStationsOnlineInfo() {
const onlineStationNames: string[] = []; const onlineStationNames: string[] = [];
const prevDispatcherStatuses: StoreState['lastDispatcherStatuses'] = []; const prevDispatcherStatuses: StoreState['lastDispatcherStatuses'] = [];
data.stations?.forEach((stationAPIData) => { this.apiData.stations?.forEach((stationAPIData) => {
if (stationAPIData.region !== this.region.id || !stationAPIData.isOnline) return; if (stationAPIData.region !== this.region.id || !stationAPIData.isOnline) return;
const station = this.stationList.find((s) => s.name === stationAPIData.stationName); const station = this.stationList.find((s) => s.name === stationAPIData.stationName);
onlineStationNames.push(stationAPIData.stationName); onlineStationNames.push(stationAPIData.stationName);
const dispatcherStatus = this.getDispatcherStatus(data.dispatchers, stationAPIData); const dispatcherStatus = this.getDispatcherStatus(stationAPIData);
prevDispatcherStatuses.push(dispatcherStatus); prevDispatcherStatuses.push(dispatcherStatus);
const stationTrains = this.getStationTrains(stationAPIData); const stationTrains = this.getStationTrains(stationAPIData);
@@ -207,6 +213,7 @@ export const useStore = defineStore('store', {
const onlineInfo = { const onlineInfo = {
name: stationAPIData.stationName, name: stationAPIData.stationName,
hash: stationAPIData.stationHash, hash: stationAPIData.stationHash,
region: stationAPIData.region,
maxUsers: stationAPIData.maxUsers, maxUsers: stationAPIData.maxUsers,
currentUsers: stationAPIData.currentUsers, currentUsers: stationAPIData.currentUsers,
spawns: parseSpawns(stationAPIData.spawnString), spawns: parseSpawns(stationAPIData.spawnString),
@@ -239,7 +246,7 @@ export const useStore = defineStore('store', {
}); });
}); });
if (data.dispatchers != null) this.lastDispatcherStatuses = prevDispatcherStatuses; if (this.apiData.dispatchers != null) this.lastDispatcherStatuses = prevDispatcherStatuses;
}, },
async fetchStationsGeneralInfo() { async fetchStationsGeneralInfo() {
@@ -324,7 +331,9 @@ export const useStore = defineStore('store', {
); );
socket.on('UPDATE', (data: APIData) => { socket.on('UPDATE', (data: APIData) => {
this.fetchOnlineData(data); this.apiData = data;
this.setOnlineData();
}); });
socket.emit('connection'); socket.emit('connection');
@@ -337,8 +346,14 @@ export const useStore = defineStore('store', {
this.connectToWebsocket(); this.connectToWebsocket();
}, },
async fetchOnlineData(data: APIData) { async changeRegion(region: StoreState['region']) {
if (!data.stations) { this.region = region;
await this.setOnlineData();
},
async setOnlineData() {
if (!this.apiData.stations) {
this.dataStatuses.sceneries = DataStatus.Error; this.dataStatuses.sceneries = DataStatus.Error;
this.dataStatuses.trains = DataStatus.Error; this.dataStatuses.trains = DataStatus.Error;
this.dataStatuses.dispatchers = DataStatus.Error; this.dataStatuses.dispatchers = DataStatus.Error;
@@ -347,11 +362,14 @@ export const useStore = defineStore('store', {
} }
this.dataStatuses.sceneries = DataStatus.Loaded; this.dataStatuses.sceneries = DataStatus.Loaded;
this.dataStatuses.trains = !data.trains ? DataStatus.Warning : DataStatus.Loaded; this.dataStatuses.trains = !this.apiData.trains ? DataStatus.Warning : DataStatus.Loaded;
this.dataStatuses.dispatchers = !data.dispatchers ? DataStatus.Warning : DataStatus.Loaded; this.dataStatuses.dispatchers = !this.apiData.dispatchers ? DataStatus.Warning : DataStatus.Loaded;
this.setStationsOnlineInfo(data); this.setTrainsOnlineData();
this.setTrainsOnlineData(data.trains); this.setStationsOnlineInfo();
console.log("Loading");
}, },
}, },
}); });
+1
View File
@@ -10,6 +10,7 @@ export type Availability = 'default' | 'unavailable' | 'nonPublic' | 'abandoned'
export interface StoreState { export interface StoreState {
stationList: Station[]; stationList: Station[];
trainList: Train[]; trainList: Train[];
apiData: APIData;
lastDispatcherStatuses: { hash: string; statusTimestamp: number; statusID: string }[]; lastDispatcherStatuses: { hash: string; statusTimestamp: number; statusID: string }[];
+4 -7
View File
@@ -71,19 +71,16 @@ export default defineComponent({
const filterManager = reactive(new StationFilterManager()); const filterManager = reactive(new StationFilterManager());
const focusedStationName = ''; const focusedStationName = '';
const computedStations: ComputedRef<Station[]> = computed(() => { const computedStations: ComputedRef<Station[]> = computed(() =>
return filterManager.getFilteredStationList(store.stationList); filterManager
}); .getFilteredStationList(store.stationList)
.filter((station) => !station.onlineInfo || station.onlineInfo.region == store.region.id)
const focusedStationInfo = computed(() =>
computedStations.value.find((station) => station.name === focusedStationName)
); );
return { return {
computedStations, computedStations,
filterManager, filterManager,
focusedStationName, focusedStationName,
focusedStationInfo,
}; };
}, },
mounted() { mounted() {
+7 -6
View File
@@ -2,8 +2,6 @@
<section class="trains-view"> <section class="trains-view">
<div class="wrapper"> <div class="wrapper">
<div class="options-bar"> <div class="options-bar">
<!-- <TrainStats :trains="trainList" :trainStatsOpen="trainStatsOpen" /> -->
<train-options /> <train-options />
</div> </div>
@@ -51,8 +49,6 @@ export default defineComponent({
setup() { setup() {
const store = useStore(); const store = useStore();
const trainList = store.trainList;
const sorterActive = ref({ id: 'distance', dir: -1 }); const sorterActive = ref({ id: 'distance', dir: -1 });
const filterList = reactive([...trainFilters]) as TrainFilter[]; const filterList = reactive([...trainFilters]) as TrainFilter[];
const isTrainOptionsCardVisible = ref(false); const isTrainOptionsCardVisible = ref(false);
@@ -67,11 +63,16 @@ export default defineComponent({
provide('isTrainOptionsCardVisible', isTrainOptionsCardVisible); provide('isTrainOptionsCardVisible', isTrainOptionsCardVisible);
const computedTrains: ComputedRef<Train[]> = computed(() => { const computedTrains: ComputedRef<Train[]> = computed(() => {
return filteredTrainList(trainList, searchedTrain.value, searchedDriver.value, sorterActive.value, filterList); return filteredTrainList(
store.trainList,
searchedTrain.value,
searchedDriver.value,
sorterActive.value,
filterList
);
}); });
return { return {
trainList,
computedTrains, computedTrains,
searchedTrain, searchedTrain,
searchedDriver, searchedDriver,