forked from binxhealth/force-vue-awakens
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Kippy Nichols
authored and
Kippy Nichols
committed
Apr 17, 2019
1 parent
b6581b5
commit 5db7d50
Showing
5 changed files
with
124 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<template> | ||
<div> | ||
<ul | ||
class="list-group" | ||
style="width: 40vw; margin-left: 30vw"> | ||
<li | ||
v-for="(value, key, index) in planets[$route.params.id]" | ||
:key="index" | ||
class="list-group-item"> | ||
<p class="font-weight-bold"> | ||
{{ key }} | ||
</p> | ||
<div v-if="key !=='residents' && key !=='films' && key !=='url'"> | ||
{{ value }} | ||
</div> | ||
|
||
<div v-else> | ||
<div v-if="key !=='url'"> | ||
<div | ||
v-for="(link, linkIndex) in value" | ||
:key="linkIndex"> | ||
<a :href="link">{{ link }}</a> | ||
</div> | ||
</div> | ||
|
||
<div v-else> | ||
<a :href="value">{{ value }}</a> | ||
</div> | ||
</div> | ||
</li> | ||
</ul> | ||
</div> | ||
</template> | ||
|
||
<script> | ||
import { mapGetters } from 'vuex' | ||
import store from '@/store' | ||
export default { | ||
async beforeRouteEnter (to, from, next) { | ||
await store.dispatch('planets/listPlanets', to.params.id) | ||
next() | ||
}, | ||
async beforeRouteUpdate (to, from, next) { | ||
await store.dispatch('planets/listPlanets', to.params.id) | ||
next() | ||
}, | ||
computed: { | ||
...mapGetters('planets', ['planets']) | ||
} | ||
} | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
const initialState = () => ({ | ||
results: [] | ||
}) | ||
|
||
// Export the module so it can be included in the main store. | ||
export default { | ||
// This module is namespaced so that it is a little more self-contained: | ||
// https://vuex.vuejs.org/guide/modules.html#namespacing | ||
namespaced: true, | ||
// https://vuex.vuejs.org/guide/state.html | ||
state: initialState, | ||
// https://vuex.vuejs.org/guide/mutations.html | ||
mutations: { | ||
results: (state, results = []) => (state.results = results) | ||
}, | ||
// https://vuex.vuejs.org/guide/actions.html | ||
actions: { | ||
async listPlanets ({ commit }, page = 1) { | ||
const response = await fetch('https://swapi.co/api/planets') | ||
const { results } = await response.json() | ||
if (response.ok) { | ||
// If the response is OK commit the data to the store. | ||
commit('results', results) | ||
} else { | ||
// If the response is not OK, log the response to the console. | ||
console.error(response) | ||
} | ||
} | ||
}, | ||
getters: { | ||
planets: state => state.results | ||
} | ||
} |