Re-write character preload/fetching to fetch all at once, async too

This commit is contained in:
Xevion
2022-05-21 12:23:43 -05:00
parent b8dad42bc8
commit 56835a27f5
2 changed files with 24 additions and 43 deletions

View File

@@ -4,9 +4,8 @@ export const types = {
MERGE_EPISODE: 'MERGE_EPISODE', MERGE_EPISODE: 'MERGE_EPISODE',
MERGE_EPISODES: 'MERGE_EPISODES', MERGE_EPISODES: 'MERGE_EPISODES',
SET_PRELOADED: 'SET_PRELOADED', SET_PRELOADED: 'SET_PRELOADED',
PRELOAD_CHARACTER: 'PRELOAD_CHARACTER', FETCH_CHARACTERS: 'PRELOAD_CHARACTERS',
PRELOAD: 'PRELOAD', PRELOAD: 'PRELOAD',
SET_CHARACTER: 'SET_CHARACTER', SET_CHARACTER: 'SET_CHARACTER',
MERGE_CHARACTER: 'MERGE_CHARACTER', MERGE_CHARACTERS: 'MERGE_CHARACTERS',
FETCH_CHARACTER: 'FETCH_CHARACTER'
} }

View File

@@ -20,7 +20,8 @@ export default new Vuex.Store({
seasonCount: 9, seasonCount: 9,
episodeCount: episodeCount, episodeCount: episodeCount,
quoteData: baseData, quoteData: baseData,
preloaded: false, preloaded: {episodes: false, characters: false},
characters_loaded: false,
characters: {} characters: {}
}, },
mutations: { mutations: {
@@ -57,20 +58,17 @@ export default new Vuex.Store({
if (payload.episodeData.scenes !== undefined) if (payload.episodeData.scenes !== undefined)
state.quoteData[s].episodes[e].loaded = true; state.quoteData[s].episodes[e].loaded = true;
}, },
[types.SET_PRELOADED](state, status) { [types.SET_PRELOADED](state, payload) {
state.preloaded = status; state.preloaded[payload.type] = payload.status;
}, },
[types.SET_CHARACTER](state, payload) { [types.SET_CHARACTER](state, payload) {
state.characters[payload.id] = payload.characterData state.characters[payload.id] = payload.characterData
}, },
[types.MERGE_CHARACTER](state, payload) { [types.MERGE_CHARACTERS](state, payload) {
const id = payload.id; // Iterate and store.
// If character has not been defined in character list yet, simply set the characterData for (const [charId, charData] of Object.entries(payload.characters)) {
if (state.characters[id] === undefined) state.characters[charId] = charData;
state.characters[id] = payload.characterData }
// Otherwise use intended merge & overwrite effect.
else
state.characters[id] = Object.assign(state.characters[id], payload.characterData)
}, },
}, },
actions: { actions: {
@@ -107,41 +105,25 @@ export default new Vuex.Store({
axios.get(path) axios.get(path)
.then((res) => { .then((res) => {
commit(types.MERGE_EPISODES, res.data) commit(types.MERGE_EPISODES, res.data)
commit(types.SET_PRELOADED, true); commit(types.SET_PRELOADED, {type: 'episodes', status: true});
}) })
.catch((error) => { .catch((error) => {
// eslint-disable-next-line no-console // eslint-disable-next-line no-console
console.error(error); console.error(error);
}) })
}, },
[types.PRELOAD_CHARACTER]({commit}) { async [types.FETCH_CHARACTERS]({commit}) {
return new Promise((resolve, reject) => {
const path = `/json/characters.json`; const path = `/json/characters.json`;
axios.get(path) let res = null;
.then((res) => { try {
for (const [character_id, character_data] of Object.entries(res.data)) res = await axios.get(path)
commit(types.MERGE_CHARACTER, {id: character_id, characterData: character_data}) } catch (error) {
resolve();
})
.catch((error) => {
console.error(error); console.error(error);
reject() throw error
}) }
})
}, commit(types.MERGE_CHARACTERS, {characters: res.data})
[types.FETCH_CHARACTER]({commit}, character_id) { commit(types.SET_PRELOADED, {type: 'characters', status: 'true'})
return new Promise((resolve, reject) => {
const path = `/json/character/${character_id}.json`;
axios.get(path)
.then((res) => {
commit(types.MERGE_CHARACTER, {id: character_id, characterData: res.data})
resolve();
})
.catch((error) => {
reject();
console.error(error);
})
})
} }
}, },
getters: { getters: {