move loading state into component

change behavior and add getter to trigger the reloading of data
This commit is contained in:
2017-02-20 19:03:02 -07:00
parent a1186f4bfd
commit 87e02e52ce
2 changed files with 30 additions and 30 deletions

View File

@@ -1,14 +1,10 @@
<template> <template>
<section class="section"> <section class="section">
<div class="container" v-if="loading"> <div class="container">
Loading...
</div>
<div class="container" v-if="!loading">
<header class="is-clearfix"> <header class="is-clearfix">
<div class="control is-grouped is-pulled-right"> <div class="control is-grouped is-pulled-right">
<div class="control"> <div class="control">
<a class="button" @click="fetchCustomers()"> <a class="button" :class="{ 'is-loading': loading }" @click="fetchCustomers()">
<span class="icon is-small"> <span class="icon is-small">
<i class="fa fa-refresh"></i> <i class="fa fa-refresh"></i>
</span> </span>
@@ -48,7 +44,7 @@
</template> </template>
<script> <script>
import { mapActions, mapState } from 'vuex'; import { mapState, mapGetters } from 'vuex';
import CustomerCard from '../components/CustomerCard.vue'; import CustomerCard from '../components/CustomerCard.vue';
import CustomerTable from '../components/CustomerTable.vue'; import CustomerTable from '../components/CustomerTable.vue';
import CustomerRow from '../components/CustomerRow.vue'; import CustomerRow from '../components/CustomerRow.vue';
@@ -61,35 +57,35 @@
CustomerRow, CustomerRow,
}, },
data() { data() {
const mockCustomer = (num) => ({
firstname: 'John',
lastname: `Doe${num}`,
city: 'cityton',
state: 'AZ',
email: 'john@email.com',
});
return { return {
mode: 'cards', mode: 'cards',
loading: false,
} }
}, },
computed: Object.assign({}, mapState('customers', [ computed: Object.assign({}, mapState('customers', [
'customers', 'customers',
'loading',
'updatedAt', 'updatedAt',
]), mapGetters('customers', [
'shouldUpdate'
])), ])),
methods: Object.assign({ methods: {
setMode(mode) { setMode(mode) {
this.mode = (mode === 'table') ? 'table' : 'cards'; this.mode = (mode === 'table') ? 'table' : 'cards';
}, },
}, mapActions('customers', [ fetchCustomers() {
'fetchCustomers', this.loading = true;
])),
created() {
const diffTime = (new Date().getTime()) - this.updatedAt;
const seconds = 30;
if (diffTime >= seconds * 1000) this.fetchCustomers(); this.$store.dispatch('customers/fetchCustomers')
.then(() => this.loading = false);
}
},
created() {
if (this.shouldUpdate(30)) {
this.loading = true;
this.$store.dispatch('customers/fetchCustomers')
.then(() => this.loading = false);
}
}, },
} }
</script> </script>

View File

@@ -1,7 +1,6 @@
export default { export default {
namespaced: true, namespaced: true,
state: { state: {
loading: false,
customers: [], customers: [],
updatedAt: 0, updatedAt: 0,
pagination: { pagination: {
@@ -9,6 +8,13 @@ export default {
count: 10, count: 10,
}, },
}, },
getters: {
shouldUpdate: state => (seconds = 30) => {
const diffTime = (new Date().getTime()) - state.updatedAt;
console.log('shouldUpdate', diffTime >= seconds * 1000)
return diffTime >= seconds * 1000;
},
},
mutations: { mutations: {
setCustomers(state, customers) { setCustomers(state, customers) {
state.customers = customers; state.customers = customers;
@@ -18,13 +24,12 @@ export default {
if (opts.page) state.pagination.page = opts.page; if (opts.page) state.pagination.page = opts.page;
if (opts.count) state.pagination.count = opts.count; if (opts.count) state.pagination.count = opts.count;
}, },
toggleLoading(state, loading) {
state.loading = loading || !state.loading;
},
}, },
actions: { actions: {
setPagination({ commit }, opts = {}) {
commit('setPagination', opts);
},
fetchCustomers({ commit, state }, opts = {}) { fetchCustomers({ commit, state }, opts = {}) {
commit('toggleLoading', true);
commit('setPagination', opts); commit('setPagination', opts);
// TODO: fetch customers from server // TODO: fetch customers from server
@@ -77,7 +82,6 @@ export default {
const { page, count } = state.pagination; const { page, count } = state.pagination;
const results = customers.slice(page, count); const results = customers.slice(page, count);
commit('setCustomers', results); commit('setCustomers', results);
commit('toggleLoading', false);
resolve(); resolve();
}, 300); }, 300);
}); });