Enhance DashboardWidget functionality: Integrate Vuex state management for socket connections, enabling real-time updates for Falukant widgets. Refactor computed properties and methods to handle socket events and improve data fetching logic. Update localization for age representation and adjust styles for better UI presentation.
This commit is contained in:
@@ -56,6 +56,7 @@
|
|||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
|
import { mapState } from 'vuex';
|
||||||
import apiClient from '@/utils/axios.js';
|
import apiClient from '@/utils/axios.js';
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
@@ -75,10 +76,15 @@ export default {
|
|||||||
data: null,
|
data: null,
|
||||||
loading: false,
|
loading: false,
|
||||||
error: null,
|
error: null,
|
||||||
isDragging: false
|
isDragging: false,
|
||||||
|
_daemonMessageHandler: null
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
computed: {
|
computed: {
|
||||||
|
...mapState(['socket', 'daemonSocket']),
|
||||||
|
isFalukantWidget() {
|
||||||
|
return this.endpoint && String(this.endpoint).includes('falukant');
|
||||||
|
},
|
||||||
newsDataResults() {
|
newsDataResults() {
|
||||||
const d = this.data;
|
const d = this.data;
|
||||||
if (d && typeof d === 'object' && Array.isArray(d.results)) return d.results;
|
if (d && typeof d === 'object' && Array.isArray(d.results)) return d.results;
|
||||||
@@ -119,7 +125,7 @@ export default {
|
|||||||
const days = this.falukantData?.age;
|
const days = this.falukantData?.age;
|
||||||
if (days == null) return '—';
|
if (days == null) return '—';
|
||||||
const years = Math.floor(Number(days) / 365);
|
const years = Math.floor(Number(days) / 365);
|
||||||
return `${years} ${this.$t('admin.createNPC.years')}`;
|
return `${years} ${this.$t('falukant.overview.metadata.years')}`;
|
||||||
},
|
},
|
||||||
dataList() {
|
dataList() {
|
||||||
if (!Array.isArray(this.data) || this.data.length === 0) return [];
|
if (!Array.isArray(this.data) || this.data.length === 0) return [];
|
||||||
@@ -142,12 +148,54 @@ export default {
|
|||||||
watch: {
|
watch: {
|
||||||
endpoint: { handler: 'fetchData', immediate: false },
|
endpoint: { handler: 'fetchData', immediate: false },
|
||||||
requestCounter: { handler: 'fetchData', immediate: false },
|
requestCounter: { handler: 'fetchData', immediate: false },
|
||||||
pauseFetch: { handler(now) { if (!now) this.fetchData(); } }
|
pauseFetch: { handler(now) { if (!now) this.fetchData(); } },
|
||||||
|
socket(newVal, oldVal) {
|
||||||
|
if (!this.isFalukantWidget) return;
|
||||||
|
if (oldVal) this.teardownSocketListeners();
|
||||||
|
if (newVal) this.setupSocketListeners();
|
||||||
|
},
|
||||||
|
daemonSocket(newVal, oldVal) {
|
||||||
|
if (!this.isFalukantWidget) return;
|
||||||
|
if (oldVal) this.teardownSocketListeners();
|
||||||
|
if (newVal) this.setupSocketListeners();
|
||||||
|
}
|
||||||
},
|
},
|
||||||
mounted() {
|
mounted() {
|
||||||
if (!this.pauseFetch && this.endpoint) this.fetchData();
|
if (!this.pauseFetch && this.endpoint) this.fetchData();
|
||||||
|
if (this.isFalukantWidget) this.setupSocketListeners();
|
||||||
|
},
|
||||||
|
beforeUnmount() {
|
||||||
|
if (this.isFalukantWidget) this.teardownSocketListeners();
|
||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
|
setupSocketListeners() {
|
||||||
|
this.teardownSocketListeners();
|
||||||
|
const daemonEvents = ['falukantUpdateStatus', 'stock_change', 'familychanged'];
|
||||||
|
if (this.daemonSocket) {
|
||||||
|
this._daemonMessageHandler = (event) => {
|
||||||
|
if (event.data === 'ping') return;
|
||||||
|
try {
|
||||||
|
const data = JSON.parse(event.data);
|
||||||
|
if (daemonEvents.includes(data.event)) this.fetchData();
|
||||||
|
} catch (_) {}
|
||||||
|
};
|
||||||
|
this.daemonSocket.addEventListener('message', this._daemonMessageHandler);
|
||||||
|
}
|
||||||
|
if (this.socket) {
|
||||||
|
this.socket.on('falukantUpdateStatus', () => this.fetchData());
|
||||||
|
this.socket.on('falukantBranchUpdate', () => this.fetchData());
|
||||||
|
}
|
||||||
|
},
|
||||||
|
teardownSocketListeners() {
|
||||||
|
if (this.daemonSocket && this._daemonMessageHandler) {
|
||||||
|
this.daemonSocket.removeEventListener('message', this._daemonMessageHandler);
|
||||||
|
this._daemonMessageHandler = null;
|
||||||
|
}
|
||||||
|
if (this.socket) {
|
||||||
|
this.socket.off('falukantUpdateStatus');
|
||||||
|
this.socket.off('falukantBranchUpdate');
|
||||||
|
}
|
||||||
|
},
|
||||||
async fetchData() {
|
async fetchData() {
|
||||||
if (!this.endpoint || this.pauseFetch) return;
|
if (!this.endpoint || this.pauseFetch) return;
|
||||||
this.loading = true;
|
this.loading = true;
|
||||||
@@ -202,6 +250,7 @@ export default {
|
|||||||
<style scoped>
|
<style scoped>
|
||||||
.dashboard-widget {
|
.dashboard-widget {
|
||||||
min-height: 200px;
|
min-height: 200px;
|
||||||
|
max-height: 420px;
|
||||||
display: flex;
|
display: flex;
|
||||||
flex-direction: column;
|
flex-direction: column;
|
||||||
background: var(--dashboard-widget-bg, #fff);
|
background: var(--dashboard-widget-bg, #fff);
|
||||||
@@ -249,9 +298,9 @@ export default {
|
|||||||
|
|
||||||
.dashboard-widget__frame {
|
.dashboard-widget__frame {
|
||||||
flex: 1;
|
flex: 1;
|
||||||
min-height: 160px;
|
min-height: 0;
|
||||||
padding: 12px;
|
padding: 12px;
|
||||||
overflow: auto;
|
overflow-y: auto;
|
||||||
}
|
}
|
||||||
|
|
||||||
.dashboard-widget__state {
|
.dashboard-widget__state {
|
||||||
|
|||||||
@@ -119,6 +119,7 @@
|
|||||||
"name": "Name",
|
"name": "Name",
|
||||||
"money": "Vermögen",
|
"money": "Vermögen",
|
||||||
"age": "Alter",
|
"age": "Alter",
|
||||||
|
"years": "Jahre",
|
||||||
"mainbranch": "Heimatstadt",
|
"mainbranch": "Heimatstadt",
|
||||||
"nobleTitle": "Stand"
|
"nobleTitle": "Stand"
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -93,6 +93,18 @@
|
|||||||
"children": "Children",
|
"children": "Children",
|
||||||
"children_unbaptised": "Unbaptised children"
|
"children_unbaptised": "Unbaptised children"
|
||||||
},
|
},
|
||||||
|
"overview": {
|
||||||
|
"title": "Falukant - Overview",
|
||||||
|
"metadata": {
|
||||||
|
"title": "Personal",
|
||||||
|
"name": "Name",
|
||||||
|
"money": "Wealth",
|
||||||
|
"age": "Age",
|
||||||
|
"years": "Years",
|
||||||
|
"mainbranch": "Home city",
|
||||||
|
"nobleTitle": "Title"
|
||||||
|
}
|
||||||
|
},
|
||||||
"health": {
|
"health": {
|
||||||
"amazing": "Amazing",
|
"amazing": "Amazing",
|
||||||
"good": "Good",
|
"good": "Good",
|
||||||
|
|||||||
Reference in New Issue
Block a user