2020-11-19 19:39:22 +01:00
|
|
|
import API from "./api"
|
2020-11-12 13:24:45 +01:00
|
|
|
import { fetchTableDefinition } from "./tables"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetches data about a certain row in a table.
|
|
|
|
*/
|
|
|
|
export const fetchRow = async ({ tableId, rowId }) => {
|
2020-11-19 19:39:22 +01:00
|
|
|
const row = await API.get({
|
2020-11-12 13:24:45 +01:00
|
|
|
url: `/api/${tableId}/rows/${rowId}`,
|
|
|
|
})
|
2020-11-13 16:42:32 +01:00
|
|
|
return (await enrichRows([row], tableId))[0]
|
2020-11-12 13:24:45 +01:00
|
|
|
}
|
2020-11-11 13:25:50 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a row in a table.
|
|
|
|
*/
|
2020-11-17 13:08:24 +01:00
|
|
|
export const saveRow = async row => {
|
2020-11-19 19:39:22 +01:00
|
|
|
return await API.post({
|
2020-11-17 13:08:24 +01:00
|
|
|
url: `/api/${row.tableId}/rows`,
|
|
|
|
body: row,
|
2020-11-11 13:25:50 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates a row in a table.
|
|
|
|
*/
|
2020-11-17 13:08:24 +01:00
|
|
|
export const updateRow = async row => {
|
2020-11-19 19:39:22 +01:00
|
|
|
return await API.patch({
|
2020-11-17 13:08:24 +01:00
|
|
|
url: `/api/${row.tableId}/rows/${row._id}`,
|
2020-11-11 13:25:50 +01:00
|
|
|
body: row,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deletes a row from a table.
|
|
|
|
*/
|
|
|
|
export const deleteRow = async ({ tableId, rowId, revId }) => {
|
2020-11-19 19:39:22 +01:00
|
|
|
return await API.del({
|
2020-11-11 13:25:50 +01:00
|
|
|
url: `/api/${tableId}/rows/${rowId}/${revId}`,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-11-12 13:24:45 +01:00
|
|
|
/**
|
|
|
|
* Deletes many rows from a table.
|
|
|
|
*/
|
|
|
|
export const deleteRows = async ({ tableId, rows }) => {
|
2020-11-19 19:39:22 +01:00
|
|
|
return await API.post({
|
2020-11-12 13:24:45 +01:00
|
|
|
url: `/api/${tableId}/rows`,
|
|
|
|
body: {
|
|
|
|
rows,
|
|
|
|
type: "delete",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enriches rows which contain certain field types so that they can
|
|
|
|
* be properly displayed.
|
|
|
|
*/
|
|
|
|
export const enrichRows = async (rows, tableId) => {
|
|
|
|
if (rows && rows.length && tableId) {
|
|
|
|
// Fetch table schema so we can check column types
|
|
|
|
const tableDefinition = await fetchTableDefinition(tableId)
|
|
|
|
const schema = tableDefinition && tableDefinition.schema
|
|
|
|
if (schema) {
|
|
|
|
const keys = Object.keys(schema)
|
|
|
|
rows.forEach(row => {
|
|
|
|
for (let key of keys) {
|
|
|
|
const type = schema[key].type
|
|
|
|
if (type === "link") {
|
|
|
|
// Enrich row with the count of any relationship fields
|
|
|
|
row[`${key}_count`] = Array.isArray(row[key]) ? row[key].length : 0
|
|
|
|
} else if (type === "attachment") {
|
|
|
|
// Enrich row with the first image URL for any attachment fields
|
|
|
|
let url = null
|
|
|
|
if (Array.isArray(row[key]) && row[key][0] != null) {
|
|
|
|
url = row[key][0].url
|
|
|
|
}
|
|
|
|
row[`${key}_first`] = url
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rows
|
|
|
|
}
|