gamebrary/src/components/GameCards/GameCard.js

183 lines
4.5 KiB
JavaScript
Raw Normal View History

import moment from 'moment';
2019-11-21 19:32:41 +00:00
import { mapState, mapGetters } from 'vuex';
export default {
2019-11-08 19:56:03 +00:00
props: {
gameId: Number,
listId: Number,
},
data() {
return {
showEditOptions: false,
};
},
computed: {
...mapState(['settings', 'games', 'gameLists', 'platform', 'user', 'tags', 'activeList', 'notes', 'progresses']),
2019-11-21 19:32:41 +00:00
...mapGetters(['hasTags']),
2019-11-08 19:56:03 +00:00
showGameRatings() {
2019-12-03 21:21:50 +00:00
return this.game.rating && this.list && !this.list.hideGameRatings;
},
showReleaseDates() {
return this.releaseDate && this.list && !this.list.hideReleaseDates;
},
showGameInfo() {
return this.list && !this.list.hideGameInfo;
},
showGameInfoOnCover() {
return this.list && !this.list.hideGameInfoOnCover;
},
gameProgress() {
2019-12-18 07:24:33 +00:00
return this.game
&& this.platform
&& this.progresses[this.platform.code]
&& this.progresses[this.platform.code][this.game.id];
2019-12-15 04:32:40 +00:00
},
releaseDate() {
const releaseDate = this.game
&& this.game.release_dates
&& this.game.release_dates.find(
({ platform }) => this.platform.id === platform,
);
let daysUntilRelease = releaseDate.date
? Math.ceil(moment.unix(releaseDate.date).diff(moment(), 'days', true))
: this.$t('releaseDates.ToBeAnnounced');
daysUntilRelease = daysUntilRelease < 0
? ''
: daysUntilRelease;
daysUntilRelease = daysUntilRelease >= 0 && daysUntilRelease === 0
? this.$t('releaseDates.Today')
: daysUntilRelease;
return daysUntilRelease;
},
releaseDateText() {
if (this.releaseDate >= 1) {
return this.releaseDate === 1
? this.$t('releaseDates.ReleasesTomorrow')
: this.$t('releaseDates.ReleasesInXDays', { days: this.releaseDate });
} else if (this.releaseDate === this.$t('releaseDates.Today')) {
return new Date().getHours() < 15
? this.$t('releaseDates.ReleasesToday')
: this.$t('releaseDates.ReleasedToday');
}
return this.releaseDate;
},
2019-11-08 19:56:03 +00:00
gameCardClass() {
const badge = this.showGameInfoOnCover && this.gameProgress === '100'
? 'badge'
: '';
2019-11-08 19:56:03 +00:00
return [
'game-card',
this.list.view,
badge,
2019-11-08 19:56:03 +00:00
];
},
2019-11-08 19:56:03 +00:00
activePlatform() {
return this.gameLists[this.platform.code];
},
2019-11-08 19:56:03 +00:00
note() {
return this.notes && this.notes[this.gameId] && this.notes[this.gameId].text;
},
progress() {
return this.progresses && this.progresses[this.gameId] && this.progresses[this.gameId].number;
},
2019-11-08 19:56:03 +00:00
list() {
return this.activePlatform[this.listId];
},
game() {
return this.games[this.gameId];
},
coverUrl() {
const game = this.games[this.gameId];
2020-08-11 04:16:43 +00:00
return game && game.cover && game.cover.image_id
2019-11-08 19:56:03 +00:00
? `https://images.igdb.com/igdb/image/upload/t_cover_small_2x/${game.cover.image_id}.jpg`
: '/static/no-image.jpg';
},
addToLabel() {
return this.list.name.length >= 25
? 'list'
: this.list.name;
},
},
methods: {
openDetails() {
this.$bus.$emit('OPEN_GAME', {
id: this.game.id,
listId: this.listId,
});
},
openTags() {
this.$bus.$emit('OPEN_TAGS', this.game.id);
},
addGame() {
const data = {
listId: this.listId,
gameId: this.gameId,
};
this.$emit('added');
this.$store.commit('ADD_GAME', data);
this.$ga.event({
eventCategory: 'game',
eventAction: 'add',
eventLabel: 'addGame',
eventValue: data,
});
this.$store.dispatch('SAVE_LIST', this.gameLists)
.then(() => {
this.$bus.$emit('TOAST', {
message: `Added ${this.game.name} to list ${this.list.name}`,
imageUrl: this.coverUrl,
});
})
.catch(() => {
this.$bus.$emit('TOAST', { message: 'Authentication error', type: 'error' });
this.$router.push({ name: 'sessionExpired' });
});
},
2019-11-08 19:56:03 +00:00
removeTag(tagName) {
this.$store.commit('REMOVE_GAME_TAG', { tagName, gameId: this.gameId });
2019-12-03 18:26:19 +00:00
this.saveTags();
},
async saveTags() {
await this.$store.dispatch('SAVE_TAGS', this.tags)
.catch(() => {
this.$bus.$emit('TOAST', { message: 'There was an error saving your tag', type: 'error' });
this.$router.push({ name: 'sessionExpired' });
});
this.$bus.$emit('TOAST', { message: 'Tags updated' });
},
2019-11-08 19:56:03 +00:00
},
};