refactoring: split engine.js into components

This commit is contained in:
Settel 2021-08-13 00:07:51 +02:00
parent d63d7437a8
commit d7220714f0
4 changed files with 34 additions and 3 deletions

View File

@ -18,7 +18,7 @@ export default {
], ],
components: true, components: true,
modules: ['@nuxtjs/axios'], modules: ['@nuxtjs/axios'],
plugins: [{ src: '~/plugins/engine.js', mode: 'client' }], plugins: [{ src: '~/plugins/engine', mode: 'client' }],
axios: { proxy: true }, axios: { proxy: true },
publicRuntimeConfig: { publicRuntimeConfig: {
serverBaseUrl: 'http://localhost:3000/', serverBaseUrl: 'http://localhost:3000/',

View File

@ -0,0 +1,21 @@
<template>
<div>
<nav>
<h1 v-if="isGamemaster">Gamemaster</h1>
</nav>
</div>
</template>
<script>
export default {
computed: {
user() {
return this.$store.state.engine.user || {}
},
isGamemaster() {
const user = this.$store.state.engine.user
return user && user.role === 'gamemaster'
},
},
}
</script>

View File

@ -1,5 +1,10 @@
<template> <template>
<div> <div>
<div>
<GameControls />
</div>
<hr />
<div v-if="userJson">{{ userJson.role }}</div>
<pre class="json">{{ userJson }}</pre> <pre class="json">{{ userJson }}</pre>
<pre class="json">{{ engineJson }}</pre> <pre class="json">{{ engineJson }}</pre>
</div> </div>

View File

@ -1,14 +1,14 @@
import buildUrl from 'build-url' import buildUrl from 'build-url'
export default (context, inject) => { export default (context, inject) => {
const { store, $axios, $config, redirect } = context
const engine = { const engine = {
context,
lastFetched: [0, 0, 0, 0, 0], lastFetched: [0, 0, 0, 0, 0],
isActive: false, isActive: false,
shouldStop: false, shouldStop: false,
async start() { async start() {
const { store, redirect } = this.context
if (!store.state.engine.user) { if (!store.state.engine.user) {
if (!await this.fetchUserInfo()) { if (!await this.fetchUserInfo()) {
redirect('/') redirect('/')
@ -29,6 +29,8 @@ export default (context, inject) => {
}, },
async fetchUpdate() { async fetchUpdate() {
const { store, $axios, $config } = this.context
if (this.shouldStop || !this.isActive) { if (this.shouldStop || !this.isActive) {
this.isActive = false this.isActive = false
this.shouldStop = false this.shouldStop = false
@ -69,6 +71,8 @@ export default (context, inject) => {
}, },
async fetchUserInfo() { async fetchUserInfo() {
const { store, $axios } = this.context
try { try {
const response = await $axios.get('/api/userinfo') const response = await $axios.get('/api/userinfo')
store.commit('engine/setUser', response.data) store.commit('engine/setUser', response.data)
@ -80,5 +84,6 @@ export default (context, inject) => {
return true return true
}, },
} }
inject('engine', engine) inject('engine', engine)
} }