Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import axios from 'axios';
- export default {
- namespaced: true,
- state() {
- return {
- all: [],
- };
- },
- actions: {
- request({ commit }) {
- return axios.get('/api/products').then((response) => {
- commit('UPDATE_COLLECTION', { items: response.data });
- });
- },
- update({ commit }, { data }) {
- return axios.put(`/api/products/${data.id}`, data).then((response) => {
- commit('UPDATE_COLLECTION', { items: [response.data] });
- });
- },
- create({ commit }, { data }) {
- return axios.post('/api/products', data).then((response) => {
- commit('UPDATE_COLLECTION', { items: [response.data] });
- });
- },
- delete({ dispatch }, { id }) {
- return axios.delete(`/api/products/${id}`).then(() => dispatch('refresh'));
- },
- refresh({ commit, dispatch }) {
- commit('RESET');
- dispatch('request');
- },
- },
- mutations: {
- RESET(state) {
- // eslint-disable-next-line
- state.all = []; // : C'EST OK ICI!
- },
- UPDATE_COLLECTION(state, { items }) {
- items.forEach((item) => {
- const index = state.all.findIndex(x => x.id === item.id);
- if (index < 0) {
- state.all.push(item);
- } else {
- state.all.splice(index, 1, {
- ...state.all[index],
- ...item,
- });
- }
- });
- },
- },
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement