fix SeasonList axios preloading & object merging mutations (SeasonList component ready), change isPreloaded to directly access state variable

This commit is contained in:
Xevion
2020-09-12 16:17:16 -05:00
parent 209e2a9172
commit e6844df2d7
3 changed files with 67 additions and 19 deletions

View File

@@ -10,12 +10,13 @@
role="tab" role="tab"
v-b-toggle="'accordion-' + season.season_id" v-b-toggle="'accordion-' + season.season_id"
> >
<a class="no-link align-items-center justify-content-between d-flex"> <a class="no-link align-items-center justify-content-between d-flex" v-if="isPreloaded">
<h5 class="mb-0 pu-0 mu-0 season-title"> <h5 class="mb-0 pu-0 mu-0 season-title">
Season {{ season.season_id }} Season {{ season.season_id }}
</h5> </h5>
<b-icon class="" icon="chevron-down"></b-icon> <b-icon class="" icon="chevron-down"></b-icon>
</a> </a>
<Skeleton v-else></Skeleton>
</b-card-header> </b-card-header>
<b-collapse <b-collapse
:id="'accordion-' + season.season_id" :id="'accordion-' + season.season_id"
@@ -24,7 +25,7 @@
<b-card-body class="h-100 px-0"> <b-card-body class="h-100 px-0">
<b-list-group> <b-list-group>
<template v-for="(episode, index) in seasons[season.season_id - 1].episodes"> <template v-for="(episode, index) in seasons[season.season_id - 1].episodes">
<template v-if="episode !== null"> <template v-if="isPreloaded">
<b-list-group-item <b-list-group-item
class="no-link episode-item" class="no-link episode-item"
:id="`s-${season.season_id}-ep-${episode.episode_id}`" :id="`s-${season.season_id}-ep-${episode.episode_id}`"
@@ -42,9 +43,9 @@
placement="right" placement="right"
> >
<template v-slot:title> <template v-slot:title>
episode.title {{ episode.title }}
</template> </template>
episode.description {{ episode.description }}
</b-popover> </b-popover>
</template> </template>
<b-list-group-item v-else class="no-link episode-item" :key="index"> <b-list-group-item v-else class="no-link episode-item" :key="index">
@@ -60,6 +61,7 @@
<script> <script>
import Skeleton from './Skeleton.vue'; import Skeleton from './Skeleton.vue';
import {types} from "@/mutation_types";
export default { export default {
name: "SeasonList", name: "SeasonList",
@@ -69,10 +71,15 @@ export default {
computed: { computed: {
seasons() { seasons() {
return this.$store.state.quoteData; return this.$store.state.quoteData;
},
// if SeasonList episode data (titles/descriptions) is loaded and ready
isPreloaded() {
return this.$store.state.preloaded;
} }
}, },
methods: {}, methods: {},
created() { created() {
this.$store.dispatch(types.PRELOAD)
}, },
}; };
</script> </script>

View File

@@ -1,4 +1,8 @@
export const types = { export const types = {
FETCH_EPISODE: 'FETCH_EPISODE', FETCH_EPISODE: 'FETCH_EPISODE',
SET_EPISODE: 'SET_EPISODE' SET_EPISODE: 'SET_EPISODE',
MERGE_EPISODE: 'MERGE_EPISODE',
SET_PRELOADED: 'SET_PRELOADED',
PRELOAD: 'PRELOAD'
} }

View File

@@ -9,7 +9,9 @@ Vue.use(Vuex);
const episodeCount = [6, 22, 23, 14, 26, 24, 24, 24, 23]; const episodeCount = [6, 22, 23, 14, 26, 24, 24, 24, 23];
const baseData = Array.from({length: 9}, (x, season) => { const baseData = Array.from({length: 9}, (x, season) => {
// Array of null values representing each episode // Array of null values representing each episode
let episodeData = Array.from({length: episodeCount[season]}, () => null) const episodeData = Array.from({length: episodeCount[season]}, (x, episode) => {
return { episode_id: episode + 1, loaded: false }
})
return {season_id: season + 1, episodes: episodeData }; return {season_id: season + 1, episodes: episodeData };
}) })
@@ -18,11 +20,20 @@ export default new Vuex.Store({
seasonCount: 9, seasonCount: 9,
episodeCount: episodeCount, episodeCount: episodeCount,
quoteData: baseData, quoteData: baseData,
preloaded: false,
}, },
mutations: { mutations: {
[types.SET_EPISODE](state, payload) { [types.SET_EPISODE](state, payload) {
state.quoteData[payload.season - 1].episodes[payload.episode - 1] = payload.data state.quoteData[payload.season - 1].episodes[payload.episode - 1] = payload.data
} },
[types.MERGE_EPISODE](state, payload) {
const s = payload.season - 1;
const e = payload.episode - 1;
state.quoteData[s].episodes[e] = Object.assign(state.quoteData[s].episodes[e], payload.episodeData);
},
[types.SET_PRELOADED](state, status) {
state.preloaded = status;
},
}, },
actions: { actions: {
// Perform async API call to fetch specific Episode data // Perform async API call to fetch specific Episode data
@@ -38,27 +49,53 @@ export default new Vuex.Store({
// eslint-disable-next-line no-console // eslint-disable-next-line no-console
console.error(error); console.error(error);
}); });
},
[types.PRELOAD]({commit}) {
const path = `${process.env.VUE_APP_API_URL}/api/episodes/`;
console.log('preload axios')
axios.get(path)
.then((res) => {
for (const season of res.data) {
for (const episode of season.episodes) {
// Create payload and commit for each episode
commit(
types.MERGE_EPISODE,
{
season: season.season_id,
episode: episode.episode_id,
episodeData: episode
}
)
}
}
commit(types.SET_PRELOADED, true);
})
.catch((error) => {
// eslint-disable-next-line no-console
console.error(error);
})
} }
}, },
getters: { getters: {
// Check whether a episode has been fetched yet // Check whether a episode has been fetched yet
isFetched(season, episode) { isFetched: (state) => (season, episode) => {
return this.$store.state.quoteData[season - 1].episodes[episode] !== null; return state.quoteData[season - 1].episodes[episode] !== null;
}, },
// Get the number of episodes present for a given season // Get the number of episodes present for a given season
getEpisodeCount(season) { getEpisodeCount: (state) => (season) => {
return this.$store.state.episodeCount[season - 1]; return state.episodeCount[season - 1];
}, },
// return Episode data if present // return Episode data if present
getEpisode(season, episode) { getEpisode: (state, getters) => (season, episode) => {
if (this.getters.isFetched(season, episode)) if (getters.isFetched(season, episode))
return this.$store.state.quoteData[season] return state.quoteData[season]
else else
return null return null
}, },
// return true if a specific episode is valid // return true if a specific episode is valid
isValidEpisode(season, episode = 1) { isValidEpisode: (state, getters) => (season, episode = 1) => {
return season >= 1 && season <= 9 && episode >= 1 && episode <= this.$store.getters.getEpisodeCount(season) return season >= 1 && season <= 9 && episode >= 1 && episode <= getters.getEpisodeCount(season)
} },
} }
}); });