Refactor App component, apply configured page title

This commit is contained in:
James Lyne 2020-12-12 19:04:51 +00:00
parent b5454b1e97
commit f45dcd4ae8
2 changed files with 34 additions and 49 deletions

View File

@ -4,75 +4,60 @@
</template> </template>
<script lang="ts"> <script lang="ts">
import {defineComponent, computed, ref} from 'vue'; import {defineComponent, computed, ref, onMounted, onBeforeUnmount, watch} from 'vue';
import Map from './components/Map.vue'; import Map from './components/Map.vue';
import Sidebar from './components/Sidebar.vue'; import Sidebar from './components/Sidebar.vue';
import {useStore} from "./store"; import {useStore} from "./store";
import {ActionTypes} from "@/store/action-types"; import {ActionTypes} from "@/store/action-types";
export default defineComponent({ export default defineComponent({
name: 'WorldList', name: 'App',
components: { components: {
Map, Map,
Sidebar, Sidebar,
}, },
setup() { setup() {
let store = useStore(), const store = useStore(),
updateInterval = computed(() => store.state.configuration.updateInterval), updateInterval = computed(() => store.state.configuration.updateInterval),
title = computed(() => store.state.configuration.title),
updatesEnabled = ref(false), updatesEnabled = ref(false),
updateTimeout = ref(0); updateTimeout = ref(0),
return { loadConfiguration = () => {
store, store.dispatch(ActionTypes.LOAD_CONFIGURATION, undefined).then(() => {
updateInterval, startUpdates();
updatesEnabled, window.hideSplash();
updateTimeout });
} },
},
mounted() { startUpdates = () => {
this.loadConfiguration(); updatesEnabled.value = true;
}, update();
},
beforeUnmount() { update = () => {
this.stopUpdates(); store.dispatch(ActionTypes.GET_UPDATE, undefined).then(() => {
}, if(updatesEnabled.value) {
updateTimeout.value = setTimeout(() => update(), updateInterval.value);
}
});
},
methods: { stopUpdates = () => {
loadConfiguration() { updatesEnabled.value = false;
useStore().dispatch(ActionTypes.LOAD_CONFIGURATION, undefined).then(() => {
this.startUpdates();
window.hideSplash();
});
},
startUpdates() { if (updateTimeout.value) {
this.updatesEnabled = true; clearTimeout(updateTimeout.value);
this.update();
},
update() {
useStore().dispatch(ActionTypes.GET_UPDATE, undefined).then(() => {
if(this.updatesEnabled) {
this.updateTimeout = setTimeout(() => this.update(), this.updateInterval);
} }
});
},
stopUpdates() { updateTimeout.value = 0;
this.updatesEnabled = false; };
if (this.updateTimeout) { watch(title, (title) => document.title = title);
clearTimeout(this.updateTimeout);
}
this.updateTimeout = 0; onMounted(() => loadConfiguration());
} onBeforeUnmount(() => stopUpdates());
} },
}); });
</script> </script>
<style lang="scss">
</style>

View File

@ -9,7 +9,7 @@ import {
DynmapConfigurationResponse, DynmapLineUpdate, DynmapConfigurationResponse, DynmapLineUpdate,
DynmapMarkerSet, DynmapMarkerSet,
DynmapMarkerUpdate, DynmapMarkerUpdate,
DynmapPlayer, DynmapTileUpdate, DynmapPlayer, DynmapServerConfig, DynmapTileUpdate,
DynmapUpdateResponse DynmapUpdateResponse
} from "@/dynmap"; } from "@/dynmap";
@ -57,7 +57,7 @@ export interface Actions {
} }
export const actions: ActionTree<State, State> & Actions = { export const actions: ActionTree<State, State> & Actions = {
[ActionTypes.LOAD_CONFIGURATION]({commit}) { [ActionTypes.LOAD_CONFIGURATION]({commit}): Promise<DynmapConfigurationResponse> {
return API.getConfiguration().then(config => { return API.getConfiguration().then(config => {
commit(MutationTypes.SET_CONFIGURATION, config.config); commit(MutationTypes.SET_CONFIGURATION, config.config);
commit(MutationTypes.SET_MESSAGES, config.messages); commit(MutationTypes.SET_MESSAGES, config.messages);