From be3b5cfedaa666dd7cb64d5f6160204d7e80692f Mon Sep 17 00:00:00 2001 From: Joe Fleming Date: Tue, 21 Mar 2017 19:45:12 -0700 Subject: [PATCH] move mock customer api to service --- src/services/customers.js | 70 +++++++++++++++++++++++++++++++++++++++ src/store/customers.js | 58 ++++---------------------------- 2 files changed, 76 insertions(+), 52 deletions(-) create mode 100644 src/services/customers.js diff --git a/src/services/customers.js b/src/services/customers.js new file mode 100644 index 0000000..6cff335 --- /dev/null +++ b/src/services/customers.js @@ -0,0 +1,70 @@ +// TODO: user server endpoints +const artificialDelay = 300; + +const customers = [{ + id: 'ai7rgail73ria3uhr', + firstname: 'Peter', + lastname: 'Amaya', + email: 'PeterPAmaya@teleworm.us', + city: 'Greensboro', + state: 'NC', +}, { + id: '7a4toi7g4ot8jaefg', + firstname: 'Derrick', + lastname: 'Duplantis', + email: 'DerrickCDuplantis@dayrep.com', + city: 'Freeport', + state: 'NY', +}, { + id: 'q93r7ga4otilsghae', + firstname: 'Andrea', + lastname: 'Hernandez', + email: 'AndreaRHernandez@armyspy.com', + city: 'Maumee', + state: 'OH', +}, { + id: 'ia73groi7ah4oitgn', + firstname: 'Daniel', + lastname: 'Hurt', + email: 'DanielGHurt@teleworm.us', + city: 'Cedar Rapids', + state: 'IA', +}, { + id: '98hi3kubtksgli9rg', + firstname: 'Milagros', + lastname: 'Anderson', + email: 'MilagrosCAnderson@jourrapide.com', + city: 'Kansas City', + state: 'KS', +}, { + id: 'pl74p39otujs5kuhz', + firstname: 'An', + lastname: 'K\'ung', + email: 'AnKung@dayrep.com', + city: 'Benton Harbor', + state: 'MI', +}]; + +export function update(id, customer) { + return new Promise((resolve, reject) => { + const index = customers.findIndex(c => c.id === id); + if (customer < 0) return reject(new Error('Invalid customer id')); + customers.splice(index, 1, customer); + return setTimeout(() => resolve(customer), artificialDelay); + }); +} + +export function fetchById(id) { + return new Promise((resolve, reject) => { + const customer = customers.find(c => c.id === id); + if (!customer) return reject(new Error('Invalid customer id')); + return setTimeout(() => resolve(customer), artificialDelay); + }); +} + +export function fetchAll() { + // TODO: fetch customers from server + return new Promise((resolve) => { + setTimeout(() => resolve(customers), artificialDelay); + }); +} diff --git a/src/store/customers.js b/src/store/customers.js index 2e2236f..42f3000 100644 --- a/src/store/customers.js +++ b/src/store/customers.js @@ -1,3 +1,5 @@ +import { fetchAll } from '../services/customers'; + export default { namespaced: true, state: { @@ -31,58 +33,10 @@ export default { fetchCustomers({ commit, state }, opts = {}) { commit('setPagination', opts); - // TODO: fetch customers from server - return new Promise((resolve) => { - const customers = [{ - id: 'ai7rgail73ria3uhr', - firstname: 'Peter', - lastname: 'Amaya', - email: 'PeterPAmaya@teleworm.us', - city: 'Greensboro', - state: 'NC', - }, { - id: '7a4toi7g4ot8jaefg', - firstname: 'Derrick', - lastname: 'Duplantis', - email: 'DerrickCDuplantis@dayrep.com', - city: 'Freeport', - state: 'NY', - }, { - id: 'q93r7ga4otilsghae', - firstname: 'Andrea', - lastname: 'Hernandez', - email: 'AndreaRHernandez@armyspy.com', - city: 'Maumee', - state: 'OH', - }, { - id: 'ia73groi7ah4oitgn', - firstname: 'Daniel', - lastname: 'Hurt', - email: 'DanielGHurt@teleworm.us', - city: 'Cedar Rapids', - state: 'IA', - }, { - id: '98hi3kubtksgli9rg', - firstname: 'Milagros', - lastname: 'Anderson', - email: 'MilagrosCAnderson@jourrapide.com', - city: 'Kansas City', - state: 'KS', - }, { - id: 'pl74p39otujs5kuhz', - firstname: 'An', - lastname: 'K\'ung', - email: 'AnKung@dayrep.com', - city: 'Benton Harbor', - state: 'MI', - }]; - - setTimeout(() => { - const { page, count } = state.pagination; - const results = customers.slice(page, count); - commit('setCustomers', results); - resolve(); - }, 300); + fetchAll().then((customers) => { + const { page, count } = state.pagination; + const results = customers.slice(page, count); + commit('setCustomers', results); }); }, },