budibase/packages/client/src/api/saveRecord.js

30 lines
685 B
JavaScript
Raw Normal View History

import { trimSlash } from "../common/trimSlash"
2019-09-23 07:08:06 +02:00
export const saveRecord = api => async ({ statePath }) => {
if (!statePath) {
api.error("Load Record: state path not set")
return
}
2019-09-23 07:08:06 +02:00
const recordtoSave = api.getState(statePath)
2019-09-23 07:08:06 +02:00
if (!recordtoSave) {
api.error(`there is no record in state: ${statePath}`)
return
}
2019-09-23 07:08:06 +02:00
if (!recordtoSave.key) {
api.error(
`item in state does not appear to be a record - it has no key (${statePath})`
)
return
}
2019-09-23 07:08:06 +02:00
const savedRecord = await api.post({
url: `/api/record/${trimSlash(recordtoSave.key)}`,
body: recordtoSave,
})
2019-09-23 07:08:06 +02:00
if (api.isSuccess(savedRecord)) api.setState(statePath, savedRecord)
}