added axios, datarest, vuex
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
import { HTTP } from './http-axios'
|
||||
// import { store } from 'modules/data/store'
|
||||
|
||||
export default {
|
||||
getPosts (cb) {
|
||||
HTTP.get('posts')
|
||||
.then(response => {
|
||||
console.log('response', response)
|
||||
cb(response.data)
|
||||
})
|
||||
},
|
||||
getPost (id, cb) {
|
||||
HTTP.get(`posts/` + id)
|
||||
.then(response => {
|
||||
console.log('response', response)
|
||||
cb(response.data)
|
||||
})
|
||||
.catch(e => {
|
||||
this.errors.push(e)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
import axios from 'axios'
|
||||
|
||||
export const HTTP = axios.create({
|
||||
baseURL: `http://jsonplaceholder.typicode.com/`,
|
||||
headers: {
|
||||
Authorization: 'Bearer {token}'
|
||||
}
|
||||
})
|
||||
@@ -0,0 +1,46 @@
|
||||
<template>
|
||||
<section v-if="opened != null">
|
||||
<h2>{{ opened.title }}</h2>
|
||||
<p>{{ opened.body }}</p>
|
||||
<span
|
||||
@click="closePost()"
|
||||
>
|
||||
close</span>
|
||||
</section>
|
||||
<ul v-else>
|
||||
<li
|
||||
v-for="post in posts"
|
||||
:key="post.id"
|
||||
>
|
||||
<h2>
|
||||
<a
|
||||
@click="openPost(post)"
|
||||
>
|
||||
{{ post.title }}</a></h2>
|
||||
</li>
|
||||
</ul>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
|
||||
// import store from './store'
|
||||
// import datarest from 'components/data/datarest'
|
||||
|
||||
import { mapState, mapActions } from 'vuex'
|
||||
// import { mapState } from 'vuex'
|
||||
|
||||
export default {
|
||||
computed: mapState({
|
||||
opened: state => state.posts.opened,
|
||||
posts: state => state.posts.all
|
||||
}),
|
||||
methods: mapActions('posts', [
|
||||
'openPost',
|
||||
'closePost'
|
||||
]),
|
||||
created () {
|
||||
this.$store.dispatch('posts/getAllPosts')
|
||||
}
|
||||
}
|
||||
|
||||
</script>
|
||||
@@ -1,5 +0,0 @@
|
||||
<template>
|
||||
<section>
|
||||
<p>Post hoc impie perpetratum quod in aliis quoque iam timebatur, tamquam licentia crudelitati indulta per suspicionum nebulas aestimati quidam noxii damnabantur. quorum pars necati, alii puniti bonorum multatione actique laribus suis extorres nullo sibi relicto praeter querelas et lacrimas, stipe conlaticia victitabant, et civili iustoque imperio ad voluntatem converso cruentam, claudebantur opulentae domus et clarae.</p>
|
||||
</section>
|
||||
</template>
|
||||
+5
-1
@@ -1,10 +1,14 @@
|
||||
import Vue from 'vue'
|
||||
import Vuex from 'vuex'
|
||||
import router from './router'
|
||||
import store from './store'
|
||||
import App from './App'
|
||||
|
||||
import 'assets/css/app.styl'
|
||||
|
||||
Vue.use(Vuex)
|
||||
|
||||
new Vue({
|
||||
router,
|
||||
store,
|
||||
render: h => h(App)
|
||||
}).$mount('#app')
|
||||
|
||||
+3
-3
@@ -1,15 +1,15 @@
|
||||
<template>
|
||||
<div class="full-width">
|
||||
<PresentationMessage />
|
||||
<Posts />
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import PresentationMessage from 'components/Home/PresentationMessage'
|
||||
import Posts from 'components/Home/Posts'
|
||||
|
||||
export default {
|
||||
components: {
|
||||
PresentationMessage
|
||||
Posts
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
import Vue from 'vue'
|
||||
import Vuex from 'vuex'
|
||||
import posts from './modules/posts'
|
||||
|
||||
Vue.use(Vuex)
|
||||
|
||||
// https://github.com/vuejs/vuex/tree/dev/examples/shopping-cart
|
||||
|
||||
// const debug = process.env.NODE_ENV !== 'production'
|
||||
|
||||
export default new Vuex.Store({
|
||||
modules: {
|
||||
posts
|
||||
}
|
||||
// strict: debug,
|
||||
// plugins: debug ? [createLogger()] : []
|
||||
})
|
||||
@@ -0,0 +1,48 @@
|
||||
import datarest from '../../api/datarest'
|
||||
|
||||
// initial state
|
||||
const state = {
|
||||
all: [],
|
||||
opened: null
|
||||
}
|
||||
|
||||
// getters
|
||||
const getters = {}
|
||||
|
||||
// actions
|
||||
const actions = {
|
||||
getAllPosts ({ commit }) {
|
||||
datarest.getPosts(posts => {
|
||||
commit('setPosts', posts)
|
||||
})
|
||||
},
|
||||
openPost ({ commit }, post) {
|
||||
datarest.getPost(post.id, post => {
|
||||
commit('setOpened', post)
|
||||
})
|
||||
},
|
||||
closePost ({ commit }) {
|
||||
commit('clearOpened')
|
||||
}
|
||||
}
|
||||
|
||||
// mutations
|
||||
const mutations = {
|
||||
setPosts (state, posts) {
|
||||
state.all = posts
|
||||
},
|
||||
setOpened (state, post) {
|
||||
state.opened = post
|
||||
},
|
||||
clearOpened (state) {
|
||||
state.opened = null
|
||||
}
|
||||
}
|
||||
|
||||
export default {
|
||||
namespaced: true,
|
||||
state,
|
||||
getters,
|
||||
actions,
|
||||
mutations
|
||||
}
|
||||
Reference in New Issue
Block a user