2020-03-13 03:23:29 +13:00
|
|
|
import api from "../../builderStore/api";
|
2020-03-17 07:54:48 +13:00
|
|
|
import { getNewRecord } from "../../common/core"
|
2020-03-13 03:23:29 +13:00
|
|
|
|
2020-03-17 07:54:48 +13:00
|
|
|
export async function deleteRecord(record, { appname, instanceId }) {
|
2020-03-21 07:47:01 +13:00
|
|
|
const DELETE_RECORDS_URL = `/_builder/instance/${appname}/${instanceId}/api/record${record.key}`
|
|
|
|
const response = await api.delete(DELETE_RECORDS_URL);
|
2020-03-17 07:54:48 +13:00
|
|
|
return response;
|
2020-03-13 03:23:29 +13:00
|
|
|
}
|
|
|
|
|
2020-03-17 07:54:48 +13:00
|
|
|
export async function saveRecord(record, { appname, instanceId }) {
|
2020-03-21 07:47:01 +13:00
|
|
|
let recordBase = { ...record }
|
|
|
|
|
|
|
|
// brand new record
|
|
|
|
if (record.collectionName) {
|
|
|
|
const collectionKey = `/${record.collectionName}`
|
|
|
|
recordBase = getNewRecord(recordBase, collectionKey)
|
|
|
|
// overwrite the new record template values
|
|
|
|
for (let key in recordBase) {
|
|
|
|
if (record[key]) recordBase[key] = record[key]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const SAVE_RECORDS_URL = `/_builder/instance/${appname}/${instanceId}/api/record/`
|
|
|
|
const response = await api.post(SAVE_RECORDS_URL, recordBase)
|
|
|
|
return await response.json()
|
2020-03-13 03:23:29 +13:00
|
|
|
}
|
|
|
|
|
2020-03-17 07:54:48 +13:00
|
|
|
export async function fetchDataForView(viewName, { appname, instanceId }) {
|
|
|
|
const FETCH_RECORDS_URL = `/_builder/instance/${appname}/${instanceId}/api/listRecords/${viewName}`;
|
2020-03-13 03:23:29 +13:00
|
|
|
|
2020-03-17 07:54:48 +13:00
|
|
|
// TODO: Error handling
|
|
|
|
const response = await api.get(FETCH_RECORDS_URL);
|
|
|
|
return await response.json();
|
2020-03-13 03:23:29 +13:00
|
|
|
|
|
|
|
}
|