add customer loading, and refresh button

reload customer data if it's been more than 30 seconds
This commit is contained in:
2017-02-20 16:59:46 -07:00
parent 3540920d2d
commit e76a233d00
2 changed files with 54 additions and 28 deletions

View File

@@ -6,7 +6,17 @@
<div class="container" v-if="!loading">
<header class="is-clearfix">
<div class="control has-addons is-pulled-right">
<div class="control is-grouped is-pulled-right">
<div class="control">
<a class="button" @click="fetchCustomers()">
<span class="icon is-small">
<i class="fa fa-refresh"></i>
</span>
<span>Reload</span>
</a>
</div>
<div class="control has-addons">
<a class="button" :class="{ 'is-primary': mode === 'cards' }" @click="setMode('cards')">
<span class="icon is-small">
<i class="fa fa-th-large"></i>
@@ -20,6 +30,7 @@
<span>Table</span>
</a>
</div>
</div>
</header>
<div class="columns is-multiline" v-if="mode === 'cards'">
@@ -62,14 +73,23 @@
mode: 'cards',
}
},
computed: Object.assign({}, mapState('customers', ['customers', 'loading'])),
computed: Object.assign({}, mapState('customers', [
'customers',
'loading',
'updatedAt',
])),
methods: Object.assign({
setMode(mode) {
this.mode = (mode === 'table') ? 'table' : 'cards';
},
}, mapActions('customers', ['fetchCustomers'])),
}, mapActions('customers', [
'fetchCustomers',
])),
created() {
this.fetchCustomers();
const diffTime = (new Date().getTime()) - this.updatedAt;
const seconds = 30;
if (diffTime >= seconds * 1000) this.fetchCustomers();
},
}
</script>

View File

@@ -3,26 +3,31 @@ export default {
state: {
loading: false,
customers: [],
updatedAt: 0,
pagination: {
page: 0,
count: 10,
},
},
mutations: {
setCustomers(state, customers) {
state.customers = customers;
state.loaded = true;
state.updatedAt = new Date().getTime();
},
toggleLoading(state) {
state.loading = !state.loading;
setPagination(state, opts = {}) {
if (opts.page) state.pagination.page = opts.page;
if (opts.count) state.pagination.count = opts.count;
},
toggleLoading(state, loading) {
state.loading = loading || !state.loading;
},
},
actions: {
fetchCustomers({ commit }, opts = {}) {
commit('toggleLoading');
fetchCustomers({ commit, state }, opts = {}) {
commit('toggleLoading', true);
commit('setPagination', opts);
// TODO: fetch customers from server
const options = Object.assign({
page: 0,
count: 10,
}, opts);
return new Promise((resolve) => {
const customers = [{
id: 'ai7rgail73ria3uhr',
@@ -69,9 +74,10 @@ export default {
}];
setTimeout(() => {
const results = customers.slice(options.page, options.count);
const { page, count } = state.pagination;
const results = customers.slice(page, count);
commit('setCustomers', results);
commit('toggleLoading');
commit('toggleLoading', false);
resolve();
}, 300);
});