2020-02-03 10:24:25 +01:00
|
|
|
import { trimSlash } from "../common/trimSlash"
|
2019-09-23 07:08:06 +02:00
|
|
|
|
2020-02-03 10:24:25 +01: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
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
const recordtoSave = api.getState(statePath)
|
2019-09-23 07:08:06 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
if (!recordtoSave) {
|
|
|
|
api.error(`there is no record in state: ${statePath}`)
|
|
|
|
return
|
|
|
|
}
|
2019-09-23 07:08:06 +02:00
|
|
|
|
2020-02-03 10:24:25 +01: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
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
const savedRecord = await api.post({
|
|
|
|
url: `/api/record/${trimSlash(recordtoSave.key)}`,
|
|
|
|
body: recordtoSave,
|
|
|
|
})
|
2019-09-23 07:08:06 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
if (api.isSuccess(savedRecord)) api.setState(statePath, savedRecord)
|
|
|
|
}
|