SHARE
TWEET

Untitled

a guest Dec 10th, 2019 73 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import axios from 'axios';
  2.  
  3. export default {
  4.   namespaced: true,
  5.  
  6.   state() {
  7.     return {
  8.       all: [],
  9.     };
  10.   },
  11.  
  12.   actions: {
  13.     request({ commit }) {
  14.       return axios.get('/api/products').then((response) => {
  15.         commit('UPDATE_COLLECTION', { items: response.data });
  16.       });
  17.     },
  18.  
  19.     update({ commit }, { data }) {
  20.       return axios.put(`/api/products/${data.id}`, data).then((response) => {
  21.         commit('UPDATE_COLLECTION', { items: [response.data] });
  22.       });
  23.     },
  24.  
  25.     create({ commit }, { data }) {
  26.       return axios.post('/api/products', data).then((response) => {
  27.         commit('UPDATE_COLLECTION', { items: [response.data] });
  28.       });
  29.     },
  30.  
  31.     delete({ dispatch }, { id }) {
  32.       return axios.delete(`/api/products/${id}`).then(() => dispatch('refresh'));
  33.     },
  34.     refresh({ commit, dispatch }) {
  35.       commit('RESET');
  36.       dispatch('request');
  37.     },
  38.   },
  39.  
  40.   mutations: {
  41.     RESET(state) {
  42.       // eslint-disable-next-line
  43.       state.all = []; // : C'EST OK ICI!
  44.     },
  45.     UPDATE_COLLECTION(state, { items }) {
  46.       items.forEach((item) => {
  47.         const index = state.all.findIndex(x => x.id === item.id);
  48.         if (index < 0) {
  49.           state.all.push(item);
  50.         } else {
  51.           state.all.splice(index, 1, {
  52.             ...state.all[index],
  53.             ...item,
  54.           });
  55.         }
  56.       });
  57.     },
  58.   },
  59. };
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top