Skip to content

Instantly share code, notes, and snippets.

@oxavibes
Created March 20, 2020 00:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oxavibes/09b603e456d2babe63b061ea86fc8b11 to your computer and use it in GitHub Desktop.
Save oxavibes/09b603e456d2babe63b061ea86fc8b11 to your computer and use it in GitHub Desktop.
Bank Module
import Vue from 'vue'
import Nprogress from 'nprogress'
import api from '../../../api/apibackend'
import {
SET_BANKS,
FETCH_BANKS_START,
FETCH_BANKS_END
} from './mutations.type'
import { GET_BANKS, DELETE_BANK, UPDATE_BANK, CREATE_BANK } from './actions.type';
const state = {
banks: [],
loadingBanks: false
}
const getters = {
banks: state => state.banks,
loadingBanks: state => state.loadingBanks
}
const mutations = {
[SET_BANKS] (state, banks) {
state.banks = banks
},
[FETCH_BANKS_START] (state) {
state.loadingBanks = true
Nprogress.start()
},
[FETCH_BANKS_END] (state) {
state.loadingBanks = false
Nprogress.done()
}
}
const actions = {
async [GET_BANKS] ({ commit }) {
try {
commit(FETCH_BANKS_START)
const { data } = await api.get('banks')
if (data) {
commit(SET_BANKS, data)
}
commit(FETCH_BANKS_END)
return data
} catch (err) {
console.error(err)
Vue.notify({
group: 'loggedIn',
type: 'error',
text: err.message
})
commit(FETCH_BANKS_END)
}
},
async [CREATE_BANK] ({ commit, dispatch }, bank) {
try {
commit(FETCH_BANKS_START)
const { data } = await api.post('banks', bank)
commit(FETCH_BANKS_END)
await dispatch(GET_BANKS)
Vue.notify({
group: 'loggedIn',
type: 'success',
text: '✔️ Banco creado exitosamente'
})
return data
} catch (err) {
commit(FETCH_BANKS_END)
console.error(err)
Vue.notify({
group: 'loggedIn',
type: 'error',
text: '❌ Ha ocurrido un error al crear el banco'
})
}
},
async [DELETE_BANK] ({ commit, dispatch }, bankId) {
try {
commit(FETCH_BANKS_START)
const { data } = await api.delete(`banks/${bankId}`)
commit(FETCH_BANKS_END)
dispatch(GET_BANKS)
Vue.notify({
group: 'loggedIn',
type: 'success',
text: '✔️ Banco eliminado exitosamente'
})
return data
} catch (err) {
commit(FETCH_BANKS_END)
console.error(err)
Vue.notify({
group: 'loggedIn',
type: 'error',
text: '❌ Ha ocurrido un error al eliminar el banco'
})
}
},
async [UPDATE_BANK] ({ commit, dispatch }, bank) {
try {
commit(FETCH_BANKS_START)
const { data } = await api.patch(`banks/${bank.id}`, bank)
commit(FETCH_BANKS_END)
dispatch(GET_BANKS)
Vue.notify({
group: 'loggedIn',
type: 'success',
text: '✔️ Banco actualizado exitosamente'
})
return data
} catch (err) {
commit(FETCH_BANKS_END)
console.error(err)
Vue.notify({
group: 'loggedIn',
type: 'error',
text: '❌ Ha ocurrido un error al actualizar el banco'
})
}
}
}
export default {
state,
getters,
mutations,
actions
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment