/**
 * mSupply Mobile
 * Sustainable Solutions (NZ) Ltd. 2019
 */

/**
 * Actions for use with a data table reducer
 */

export const editTotalQuantity = (value, rowKey, columnKey) => ({
  type: 'editTotalQuantity',
  value,
  rowKey,
  columnKey,
});

export const focusCell = (rowKey, columnKey) => ({
  type: 'focusCell',
  rowKey,
  columnKey,
});

export const focusNext = (rowKey, columnKey) => ({
  type: 'focusNextCell',
  rowKey,
  columnKey,
});

export const selectRow = rowKey => ({
  type: 'selectRow',
  rowKey,
});

export const deselectRow = rowKey => ({
  type: 'deselectRow',
  rowKey,
});

export const deselectAll = () => ({
  type: 'deselectAll',
});

export const sortData = (sortBy, isAscending) => ({
  type: 'sortData',
  sortBy,
  isAscending,
});

export const filterData = searchTerm => ({
  type: 'filterData',
  searchTerm,
});

export const openBasicModal = modalKey => ({
  type: 'openBasicModal',
  modalKey,
});

export const closeBasicModal = () => ({
  type: 'closeBasicModal',
});

export const addMasterListItems = objectType => ({
  type: 'addMasterListItems',
  objectType,
});

export const addItem = (item, addedItemType) => ({
  type: 'addItem',
  item,
  addedItemType,
});

export const editTheirRef = (value, pageObjectType) => ({
  type: 'editTheirRef',
  pageObjectType,
  value,
});

export const editComment = (value, pageObjectType) => ({
  type: 'editComment',
  pageObjectType,
  value,
});

export const deleteItemsById = (pageObjectType, object) => ({
  type: 'deleteItemsById',
  pageObjectType,
  object,
});

export const refreshData = () => ({
  type: 'refreshData',
});

@@ -1057,27 +1057,224 @@
 ectType =%3E (
-%7B%0A 
+dispatch, state) =%3E %7B%0A  const %7B pageObject, database %7D = state;%0A%0A  database.write(() =%3E %7B%0A    pageObject.addItemsFromMasterList(database);%0A    database.save(objectType, pageObject);%0A  %7D);%0A  dispatch(%7B
  type: 'addM
@@ -1289,18 +1289,16 @@
 tItems',
-%0A 
  objectT
@@ -1296,28 +1296,30 @@
 , objectType
-,%0A%7D)
+ %7D);%0A%7D
 ;%0A%0Aexport co
@@ -1469,36 +1469,186 @@
 =%3E (
-%7B%0A  type: 'editTheirRef',%0A  
+dispatch, state) =%3E %7B%0A  const %7B database, pageObject %7D = state;%0A%0A  const %7B theirRef %7D = pageObject;%0A%0A  if (theirRef !== value) %7B%0A    database.write(() =%3E %7B%0A      database.update(
 page
@@ -1662,105 +1662,347 @@
 ype,
+ %7B
 %0A  
-value,%0A%7D);%0A%0Aexport const editComment = (value, pageObjectType) =%3E (%7B%0A  type: 'editComment',%0A  
+      ...pageObject,%0A        theirRef: value,%0A      %7D);%0A    %7D);%0A  %7D%0A%0A  dispatch(closeBasicModal());%0A%7D;%0A%0Aexport const editComment = (value, pageObjectType) =%3E (dispatch, state) =%3E %7B%0A  const %7B database, pageObject %7D = state;%0A  const %7B comment %7D = pageObject;%0A%0A  if (comment !== value) %7B%0A    database.write(() =%3E %7B%0A      database.update(
 page
@@ -2016,20 +2016,113 @@
 ype,
+ %7B
 %0A  
-value,%0A%7D)
+      ...pageObject,%0A        comment: value,%0A      %7D);%0A    %7D);%0A  %7D%0A%0A  dispatch(closeBasicModal());%0A%7D
 ;%0A%0Ae
@@ -2151,17 +2151,16 @@
 sById = 
-(
 pageObje
@@ -2169,81 +2169,363 @@
 Type
-, object) =%3E (%7B%0A  type: 'd
+ =%3E (dispatch, state) =%3E %7B%0A  const %7B dataState, database, pageObject %7D = state;%0A%0A  const itemsIds = Array.from(dataState.keys()).filter(rowKey =%3E dataState.get(rowKey).isS
 ele
+c
 te
-ItemsById',%0A  pageObjectType,%0A  object,%0A%7D)
+d);%0A%0A  database.write(() =%3E %7B%0A    pageObject.removeItemsById(database, itemsIds);%0A    database.save(pageObjectType, pageObject);%0A  %7D);%0A%0A  dispatch(%7B type: 'deleteItemsById' %7D);%0A%7D
 ;%0A%0Ae

Add thunks to editComment, editTheirRef and deleteItemsById actions