import actionTypes from './action-types';
import mockCategories from '../mock/categories';

let categoriesLength = mockCategories.length;
let lastId = mockCategories[categoriesLength - 1].id;

export function createCategory(data) {
  return dispatch => {
    dispatch({type: actionTypes.CREATE_CATEGORY});

    const newId = ++lastId;

    // Simulate fake network latency
    window.setTimeout(() => {
      const newCategory = {
        ...data,
        id: newId
      };

      mockCategories.push(newCategory);

      dispatch({
        type: actionTypes.CREATE_CATEGORY_SUCCESS,
        category: newCategory
      });
    }, 1000);
  };
}

export function retrieveCategories() {
  return dispatch => {
    dispatch({type: actionTypes.RETRIEVE_CATEGORIES});

    window.setTimeout(() => {
      dispatch({
        type: actionTypes.RETRIEVE_CATEGORIES_SUCCESS,
        categories: [...mockCategories]
      });
    }, 1200);
  };
}

export function updateCategory(categoryId) {
  return dispatch => {
    dispatch({type: actionTypes.UPDATE_CATEGORY, categoryId});

    window.setTimeout(() => {
      // This needs to send the updated category
      dispatch({
        type: actionTypes.UPDATE_CATEGORY_SUCCESS,
        categoryId
      });
    }, 1000);
  };
}

export function deleteCategory(categoryId) {
  return dispatch => {
    dispatch({type: actionTypes.DELETE_CATEGORY, categoryId});

    window.setTimeout(() => {
      dispatch({
        type: actionTypes.DELETE_CATEGORY_SUCCESS,
        categoryId
      });
    }, 1000);
  };
}

@@ -473,49 +473,8 @@
 %7D;%0A%0A
-      mockCategories.push(newCategory);%0A%0A
     
@@ -478,32 +478,32 @@
      dispatch(%7B%0A
+
         type: ac
@@ -643,32 +643,33 @@
  %7B%0A  return 
+(
 dispatch
  =%3E %7B%0A    di
@@ -648,32 +648,43 @@
 return (dispatch
+, getState)
  =%3E %7B%0A    dispat
@@ -751,32 +751,268 @@
 Timeout(() =%3E %7B%0A
+      const existingCategories = getState().categories.categories;%0A      let categories;%0A      if (!existingCategories.length) %7B%0A        categories = %5B...mockCategories%5D;%0A      %7D else %7B%0A        categories = existingCategories;%0A      %7D%0A%0A
       dispatch(%7B
@@ -1059,32 +1059,32 @@
 GORIES_SUCCESS,%0A
+
         categori
@@ -1089,29 +1089,8 @@
 ries
-: %5B...mockCategories%5D
 %0A   

Make mock categories more rigorous