gamebrary/src/pages/GameBoard.vue

187 lines
4 KiB
Vue
Raw Normal View History

2018-10-19 05:15:28 +00:00
<template lang="html">
2019-11-21 21:47:58 +00:00
<div v-if="loaded" class="game-board" >
<game-board-placeholder v-if="loading" :id="gameDetailId" />
2019-11-08 20:34:06 +00:00
<modal
ref="game"
large
@close="closeGame"
2018-10-19 05:15:28 +00:00
>
2019-11-21 20:58:06 +00:00
<game
2019-11-08 20:34:06 +00:00
v-if="gameDetailId"
slot="content"
:id="gameDetailId"
:list-id="gameDetailListIndex"
/>
</modal>
<apply-tag />
<list
v-for="(list, listIndex) in gameLists[platform.code]"
v-if="list && !loading"
:name="list.name"
:game-list="list.games"
:list-index="listIndex"
:key="`${list.name}-${listIndex}`"
@end="dragEnd"
/>
2019-11-08 20:34:06 +00:00
<list-add />
2019-11-08 20:34:06 +00:00
</div>
2018-10-19 05:15:28 +00:00
</template>
<script>
2019-01-11 20:20:21 +00:00
import GameBoardPlaceholder from '@/components/GameBoard/GameBoardPlaceholder';
2019-04-05 22:46:56 +00:00
import ListAdd from '@/components/Lists/ListAdd';
import ApplyTag from '@/components/Tags/ApplyTag';
import Modal from '@/components/Modal';
import List from '@/components/Lists/List';
2019-11-21 20:58:06 +00:00
import Game from '@/pages/Game';
2019-10-09 16:30:07 +00:00
import { chunk } from 'lodash';
import { mapState } from 'vuex';
2018-10-19 05:15:28 +00:00
import draggable from 'vuedraggable';
2018-10-19 05:15:28 +00:00
export default {
2019-11-08 19:56:03 +00:00
components: {
draggable,
List,
GameBoardPlaceholder,
ListAdd,
ApplyTag,
2019-11-21 20:58:06 +00:00
Game,
2019-11-08 19:56:03 +00:00
Modal,
},
data() {
return {
dragging: false,
draggingId: null,
loading: false,
gameData: null,
gameDetailListIndex: null,
gameDetailId: null,
queryLimit: 500,
};
},
computed: {
...mapState(['user', 'gameLists', 'platform', 'games']),
2019-11-08 19:56:03 +00:00
list() {
return this.gameLists && this.platform && this.gameLists[this.platform.code]
? this.gameLists[this.platform.code]
: null;
2018-10-19 05:15:28 +00:00
},
2019-11-21 21:47:58 +00:00
loaded() {
return this.user && this.platform;
}
2019-11-08 19:56:03 +00:00
},
2018-10-19 05:15:28 +00:00
2019-11-08 19:56:03 +00:00
mounted() {
if (!this.platform) {
this.$router.push({ name: 'platforms' });
return;
}
this.load();
this.setPageTitle();
this.$bus.$on('OPEN_GAME', this.openGame);
},
beforeDestroy() {
this.$bus.$off('OPEN_GAME');
},
methods: {
closeGame() {
this.setPageTitle();
this.gameDetailId = null;
this.$store.commit('CLEAR_ACTIVE_GAME');
2018-10-19 05:15:28 +00:00
},
2019-11-08 19:56:03 +00:00
setPageTitle() {
2019-11-14 21:10:10 +00:00
document.title = this.platform ? `${this.platform.name} - Gamebrary` : 'Gamebrary';
2019-11-08 19:56:03 +00:00
},
2019-02-06 06:55:00 +00:00
2019-11-08 19:56:03 +00:00
openGame({ id, listId }) {
this.gameDetailId = id;
this.gameDetailListIndex = listId;
this.$refs.game.open();
2018-10-19 05:15:28 +00:00
},
2019-11-08 19:56:03 +00:00
dragEnd() {
this.dragging = false;
this.draggingId = null;
this.updateLists();
},
updateLists() {
2019-11-14 21:10:10 +00:00
this.$store
.dispatch('SAVE_LIST', this.gameLists)
2019-11-08 19:56:03 +00:00
.then(() => {
this.$bus.$emit('TOAST', { message: 'List updated' });
})
.catch(() => {
this.$bus.$emit('TOAST', { message: 'Authentication error', type: 'error' });
this.$router.push({ name: 'sessionExpired' });
});
},
load() {
2019-11-14 21:10:10 +00:00
const flattenedList = this.list ? this.list.map(({ games }) => games).flat() : [];
2019-11-08 19:56:03 +00:00
const dedupedList = Array.from(new Set(flattenedList));
return dedupedList.length > this.queryLimit
? this.loadGamesInChunks(dedupedList)
: this.loadGames(dedupedList);
},
loadGames(gameList) {
if (gameList && gameList.length > 0) {
this.loading = true;
2019-11-14 21:10:10 +00:00
this.$store
.dispatch('LOAD_GAMES', gameList.toString())
2019-11-08 19:56:03 +00:00
.then(() => {
this.loading = false;
})
.catch(() => {
this.$bus.$emit('TOAST', { message: 'Error loading games', type: 'error' });
});
}
},
loadGamesInChunks(gameList) {
const chunkedGameList = chunk(gameList, this.queryLimit);
chunkedGameList.forEach((gameListChunk) => {
this.loadGames(gameListChunk);
});
2018-10-19 05:15:28 +00:00
},
2019-11-08 19:56:03 +00:00
},
2018-10-19 05:15:28 +00:00
};
</script>
<style lang="scss" rel="stylesheet/scss" scoped>
2019-11-14 21:10:10 +00:00
@import '~styles/styles';
.game-board {
user-select: none;
display: flex;
align-items: flex-start;
height: calc(100vh - 48px);
padding: 0 $gp;
box-sizing: border-box;
overflow-x: auto;
overflow-x: overlay;
display: flex;
}
.list-placeholder {
opacity: 0.25;
}
2018-10-19 05:15:28 +00:00
</style>