gamebrary/src/pages/GameBoard.vue

257 lines
6.8 KiB
Vue
Raw Normal View History

2018-10-18 22:15:28 -07:00
<template lang="html">
<div
2019-10-01 15:26:31 -07:00
class="game-board"
ref="gameBoard"
2019-01-23 23:16:08 -07:00
v-if="user && platform"
2019-01-11 22:33:01 -07:00
:class="{ dark: darkModeEnabled }"
2018-10-18 22:15:28 -07:00
>
2019-02-15 14:57:11 -07:00
<game-board-placeholder :id="gameDetailId" v-if="loading" />
2018-11-26 23:45:37 -07:00
2019-02-07 23:13:48 -07:00
<modal
ref="game"
large
@close="closeGame"
>
2019-04-18 23:20:29 -07:00
<game-detail
v-if="gameDetailId"
2019-05-08 15:40:15 -07:00
slot="content"
2019-04-18 23:20:29 -07:00
:id="gameDetailId"
:list-id="gameDetailListIndex"
/>
2019-02-05 23:55:00 -07:00
</modal>
2019-02-07 23:15:16 -07:00
<modal
ref="tag"
:title="$t('tags.applyTag')"
:message="$t('tags.useTags')"
2019-02-21 23:27:39 -07:00
padded
2019-07-11 13:27:20 -07:00
confirm
2019-02-07 23:15:16 -07:00
>
<div slot="content">
2019-05-08 15:40:15 -07:00
<div
class="tags"
v-for="(tag, name) in tags"
:key="name"
>
2019-04-25 23:18:53 -07:00
<tag
:label="name"
:hex="tag.hex"
:readonly="!tag.games.includes(gameTagsId)"
@action="tryAdd(tag.games, name)"
@close="removeTag(name)"
/>
2019-02-07 23:15:16 -07:00
</div>
</div>
</modal>
2018-12-19 09:30:22 -07:00
<template>
2018-11-17 15:21:50 -07:00
<list
:name="list.name"
2019-10-05 22:26:13 -07:00
:game-list="list.games"
2018-11-17 15:21:50 -07:00
:listIndex="listIndex"
:key="`${list.name}-${listIndex}`"
2019-01-11 13:20:21 -07:00
v-if="list && !loading"
2018-11-17 15:21:50 -07:00
v-for="(list, listIndex) in gameLists[platform.code]"
@end="dragEnd"
/>
2019-04-05 15:46:56 -07:00
<list-add
@scroll="scroll"
@update="updateLists"
/>
2018-11-17 15:21:50 -07:00
</template>
2018-10-18 22:15:28 -07:00
</div>
</template>
<script>
2019-01-11 13:20:21 -07:00
import GameBoardPlaceholder from '@/components/GameBoard/GameBoardPlaceholder';
import Tag from '@/components/Tag';
2019-04-05 15:46:56 -07:00
import ListAdd from '@/components/Lists/ListAdd';
import Panel from '@/components/Panel';
2019-04-17 21:52:21 -07:00
import GameDetail from '@/pages/GameDetail';
import Modal from '@/components/Modal';
import { chunk } from 'lodash';
import List from '@/components/Lists/List';
2018-10-18 22:15:28 -07:00
import draggable from 'vuedraggable';
2018-12-21 15:52:11 -06:00
import { mapState, mapGetters } from 'vuex';
import firebase from 'firebase/app';
import 'firebase/firestore';
2018-10-18 22:15:28 -07:00
const db = firebase.firestore();
2018-10-18 22:15:28 -07:00
export default {
components: {
draggable,
List,
2019-01-11 13:20:21 -07:00
GameBoardPlaceholder,
2019-04-05 15:46:56 -07:00
ListAdd,
2019-04-25 23:18:53 -07:00
Tag,
2018-12-19 09:30:22 -07:00
Panel,
2019-02-05 23:55:00 -07:00
GameDetail,
Modal,
2018-10-18 22:15:28 -07:00
},
data() {
return {
dragging: false,
draggingId: null,
2018-11-17 15:21:50 -07:00
loading: false,
2018-10-18 22:15:28 -07:00
gameData: null,
2019-04-18 23:20:29 -07:00
gameDetailListIndex: null,
2019-02-05 23:55:00 -07:00
gameDetailId: null,
2019-02-07 23:15:16 -07:00
gameTagsId: null,
queryLimit: 500,
2018-10-18 22:15:28 -07:00
};
},
computed: {
2019-10-01 15:26:31 -07:00
...mapState(['user', 'gameLists', 'platform', 'tags', 'games']),
2018-12-21 15:52:11 -06:00
...mapGetters(['darkModeEnabled']),
2018-10-18 22:15:28 -07:00
list() {
2019-01-23 23:16:08 -07:00
return this.gameLists && this.platform && this.gameLists[this.platform.code]
2018-12-19 09:30:22 -07:00
? this.gameLists[this.platform.code]
: null;
2018-10-18 22:15:28 -07:00
},
},
mounted() {
2019-09-03 21:09:31 -07:00
if (this.platform) {
this.load();
2019-02-07 23:13:48 -07:00
this.setPageTitle();
2019-01-23 23:16:08 -07:00
} else {
2019-02-07 23:13:48 -07:00
this.$router.push({ name: 'platforms' });
2019-01-23 23:16:08 -07:00
}
2019-02-05 23:55:00 -07:00
this.$bus.$on('OPEN_GAME', this.openGame);
2019-02-07 23:15:16 -07:00
this.$bus.$on('OPEN_TAGS', this.openTags);
2019-02-05 23:55:00 -07:00
},
beforeDestroy() {
this.$bus.$off('OPEN_GAME');
2019-02-07 23:15:16 -07:00
this.$bus.$off('OPEN_TAGS');
2018-10-18 22:15:28 -07:00
},
methods: {
2019-04-25 23:18:53 -07:00
tryAdd(games, tagName) {
if (!games.includes(this.gameTagsId)) {
this.addTag(tagName);
}
},
2019-02-07 23:15:16 -07:00
addTag(tagName) {
this.$store.commit('ADD_GAME_TAG', { tagName, gameId: this.gameTagsId });
this.$bus.$emit('SAVE_TAGS', this.tags);
},
2019-02-07 23:13:48 -07:00
closeGame() {
this.setPageTitle();
this.gameDetailId = null;
},
setPageTitle() {
document.title = this.platform
? `${this.platform.name} - Gamebrary`
: 'Gamebrary';
},
2019-04-25 23:55:05 -07:00
removeTag(tagName) {
this.$store.commit('REMOVE_GAME_TAG', { tagName, gameId: this.gameTagsId });
this.$bus.$emit('SAVE_TAGS', this.tags);
},
2019-04-18 23:20:29 -07:00
openGame({ id, listId }) {
2019-02-05 23:55:00 -07:00
this.gameDetailId = id;
2019-04-18 23:20:29 -07:00
this.gameDetailListIndex = listId;
2019-02-05 23:55:00 -07:00
this.$refs.game.open();
},
2019-02-07 23:15:16 -07:00
openTags(id) {
this.gameTagsId = id;
this.$refs.tag.open(id);
},
2018-10-18 22:15:28 -07:00
scroll() {
this.$nextTick(() => {
2019-10-01 15:26:31 -07:00
const gameBoard = this.$refs.gameBoard;
gameBoard.scrollLeft = gameBoard.scrollWidth;
2018-10-18 22:15:28 -07:00
});
},
dragEnd() {
this.dragging = false;
this.draggingId = null;
this.$bus.$emit('TOAST', { message: 'Collection updated' });
2018-10-18 22:15:28 -07:00
this.updateLists();
},
updateLists(force) {
2019-09-23 13:03:30 -07:00
// TOOD: move to actions
db.collection('lists').doc(this.user.uid).set(this.gameLists, { merge: !force })
.catch(() => {
this.$bus.$emit('TOAST', { message: 'Authentication error', type: 'error' });
2019-08-09 12:12:11 -07:00
this.$router.push({ name: 'sessionExpired' });
2018-10-18 22:15:28 -07:00
});
},
load() {
2019-09-03 21:09:31 -07:00
const flattenedList = this.list
? this.list.map(({ games }) => games).flat()
: [];
const dedupedList = Array.from(new Set(flattenedList));
return dedupedList.length > this.queryLimit
? this.loadGamesInChunks(dedupedList)
: this.loadGames(dedupedList);
},
loadGames(gameList) {
2019-09-03 21:09:31 -07:00
if (gameList && gameList.length > 0) {
this.loading = true;
this.$store.dispatch('LOAD_GAMES', gameList.toString())
.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-18 22:15:28 -07:00
},
},
};
</script>
<style lang="scss" rel="stylesheet/scss" scoped>
2019-09-10 13:02:16 -07:00
@import "~styles/styles";
2018-10-18 22:15:28 -07:00
.draggable * {
color: $color-white;
}
2019-10-01 15:26:31 -07:00
.game-board {
2018-11-17 15:21:50 -07:00
user-select: none;
2018-10-29 16:53:49 -07:00
display: flex;
align-items: flex-start;
height: calc(100vh - 48px);
padding: 0 $gp;
2018-10-29 16:53:49 -07:00
box-sizing: border-box;
overflow-x: auto;
overflow-x: overlay;
display: flex;
2018-10-18 22:15:28 -07:00
}
2018-10-29 16:53:49 -07:00
.list-placeholder {
opacity: 0.25;
}
2018-10-18 22:15:28 -07:00
</style>