diff --git a/src/components/Table/EditableTable.jsx b/src/components/Table/EditableTable.jsx index 5715e48..a81d8f7 100644 --- a/src/components/Table/EditableTable.jsx +++ b/src/components/Table/EditableTable.jsx @@ -6,6 +6,13 @@ import { invokeWebApiWrapperAsync } from '../factory' const newRowKeyValue = 'newRow' +const actions = [ + [['insert'], (data) => [data]], + [['insertRange'], (data) => [[data].flat(1)]], + [['update', 'put'], (data) => data.id && [data.id, data]], + [['delete'], (data) => data.id && [data.id]], +] + export const makeActionHandler = (action, { idWell, service, setLoader, errorMsg, onComplete }, recordParser) => service && action && ( (record) => invokeWebApiWrapperAsync( async () => { @@ -13,21 +20,11 @@ export const makeActionHandler = (action, { idWell, service, setLoader, errorMsg if (typeof recordParser === 'function') record = recordParser(record) - if (action === 'insert') { - record.key = Date.now() - await service.insert(...addIdWell(record)) - } else if (action === 'insertRange') { - if (!Array.isArray(record)) - record = [record] - await service.insertRange(...addIdWell(record)) - } else if (record.id) { - if (action === 'update') - await service.put(...addIdWell(record.id, record)) - else if (action === 'delete') - await service.delete(...addIdWell(record.id)) - } - if (onComplete) - await onComplete() + const actionId = actions.findIndex((elm) => elm[0].includes(action)) + const params = actions[actionId]?.[1](record) + + if (params) await service[action](...addIdWell(...params)) + await onComplete?.() }, setLoader, errorMsg @@ -208,7 +205,7 @@ export const EditableTable = ({ const mergedColumns = [...columns.map(handleColumn), operationColumn] return ( -