I have three getters in my store:
getActiveTournaments(state) {
return state.tournaments.data
? state.tournaments.data
.filter(tournament => tournament.state_value === 'in_progress')
// sort tournaments by date of creation
.sort((tournamentA, tournamentB) => tournamentA.created_at - tournamentB.created_at)
: [];
},
getFutureTournaments(state) {
return state.tournaments.data
? state.tournaments.data
.filter(tournament => tournament.state_value === 'promo')
// sort tournaments by date of creation
.sort((tournamentA, tournamentB) => tournamentA.created_at - tournamentB.created_at)
: [];
},
getPastTournaments(state) {
return state.tournaments.data
? state.tournaments.data
.filter(tournament => tournament.state_value === 'finished')
// sort tournaments by date of creation
.sort((tournamentA, tournamentB) => tournamentA.created_at - tournamentB.created_at)
: [];
},
In my component I have a slider which accept an array of three items, each first one from an arrays returned by every getters. So I created a computed property in my component in order to get these items and then pass them to the slider:
<template>
...
<hovering-image-slider v-if="getLatestTournaments.length > 0"
:slideData="getLatestTournaments" />
...
</template>
<script>
...
getLatestTournaments() {
return this.getFutureTournaments[0]
&& this.getActiveTournaments[0]
&& this.getPastTournaments[0]
? [
this.getFutureTournaments[0],
this.getActiveTournaments[0],
this.getPastTournaments[0]]
: [];
},
</script>
It works fine but it seems like an ugly solution. Slider component will get an error of undefined data if I will not check the presence of [0]
element in the computed property. I thought that by doing some checking at the level of my getters, it will not require checking in the component but apparently it does. Can you please advice how I can improve this existing code?
console.log(state.tournaments.data)
in your getters, what do they say? \$\endgroup\$