add customer loading, and refresh button
reload customer data if it's been more than 30 seconds
This commit is contained in:
@@ -6,7 +6,17 @@
|
|||||||
|
|
||||||
<div class="container" v-if="!loading">
|
<div class="container" v-if="!loading">
|
||||||
<header class="is-clearfix">
|
<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')">
|
<a class="button" :class="{ 'is-primary': mode === 'cards' }" @click="setMode('cards')">
|
||||||
<span class="icon is-small">
|
<span class="icon is-small">
|
||||||
<i class="fa fa-th-large"></i>
|
<i class="fa fa-th-large"></i>
|
||||||
@@ -20,6 +30,7 @@
|
|||||||
<span>Table</span>
|
<span>Table</span>
|
||||||
</a>
|
</a>
|
||||||
</div>
|
</div>
|
||||||
|
</div>
|
||||||
</header>
|
</header>
|
||||||
|
|
||||||
<div class="columns is-multiline" v-if="mode === 'cards'">
|
<div class="columns is-multiline" v-if="mode === 'cards'">
|
||||||
@@ -62,14 +73,23 @@
|
|||||||
mode: 'cards',
|
mode: 'cards',
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
computed: Object.assign({}, mapState('customers', ['customers', 'loading'])),
|
computed: Object.assign({}, mapState('customers', [
|
||||||
|
'customers',
|
||||||
|
'loading',
|
||||||
|
'updatedAt',
|
||||||
|
])),
|
||||||
methods: Object.assign({
|
methods: Object.assign({
|
||||||
setMode(mode) {
|
setMode(mode) {
|
||||||
this.mode = (mode === 'table') ? 'table' : 'cards';
|
this.mode = (mode === 'table') ? 'table' : 'cards';
|
||||||
},
|
},
|
||||||
}, mapActions('customers', ['fetchCustomers'])),
|
}, mapActions('customers', [
|
||||||
|
'fetchCustomers',
|
||||||
|
])),
|
||||||
created() {
|
created() {
|
||||||
this.fetchCustomers();
|
const diffTime = (new Date().getTime()) - this.updatedAt;
|
||||||
|
const seconds = 30;
|
||||||
|
|
||||||
|
if (diffTime >= seconds * 1000) this.fetchCustomers();
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
@@ -3,26 +3,31 @@ export default {
|
|||||||
state: {
|
state: {
|
||||||
loading: false,
|
loading: false,
|
||||||
customers: [],
|
customers: [],
|
||||||
|
updatedAt: 0,
|
||||||
|
pagination: {
|
||||||
|
page: 0,
|
||||||
|
count: 10,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
mutations: {
|
mutations: {
|
||||||
setCustomers(state, customers) {
|
setCustomers(state, customers) {
|
||||||
state.customers = customers;
|
state.customers = customers;
|
||||||
state.loaded = true;
|
state.updatedAt = new Date().getTime();
|
||||||
},
|
},
|
||||||
toggleLoading(state) {
|
setPagination(state, opts = {}) {
|
||||||
state.loading = !state.loading;
|
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: {
|
actions: {
|
||||||
fetchCustomers({ commit }, opts = {}) {
|
fetchCustomers({ commit, state }, opts = {}) {
|
||||||
commit('toggleLoading');
|
commit('toggleLoading', true);
|
||||||
|
commit('setPagination', opts);
|
||||||
|
|
||||||
// TODO: fetch customers from server
|
// TODO: fetch customers from server
|
||||||
const options = Object.assign({
|
|
||||||
page: 0,
|
|
||||||
count: 10,
|
|
||||||
}, opts);
|
|
||||||
|
|
||||||
return new Promise((resolve) => {
|
return new Promise((resolve) => {
|
||||||
const customers = [{
|
const customers = [{
|
||||||
id: 'ai7rgail73ria3uhr',
|
id: 'ai7rgail73ria3uhr',
|
||||||
@@ -69,9 +74,10 @@ export default {
|
|||||||
}];
|
}];
|
||||||
|
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
const results = customers.slice(options.page, options.count);
|
const { page, count } = state.pagination;
|
||||||
|
const results = customers.slice(page, count);
|
||||||
commit('setCustomers', results);
|
commit('setCustomers', results);
|
||||||
commit('toggleLoading');
|
commit('toggleLoading', false);
|
||||||
resolve();
|
resolve();
|
||||||
}, 300);
|
}, 300);
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user