2021-09-01 12:41:48 +02:00
|
|
|
import { notificationStore, dataSourceStore } from "stores"
|
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 }) => {
|
2021-02-05 17:16:41 +01:00
|
|
|
if (!tableId || !rowId) {
|
|
|
|
return
|
|
|
|
}
|
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.
|
|
|
|
*/
|
2021-05-04 12:32:22 +02:00
|
|
|
export const saveRow = async row => {
|
2021-02-05 17:16:41 +01:00
|
|
|
if (!row?.tableId) {
|
|
|
|
return
|
|
|
|
}
|
2021-01-25 12:44:44 +01:00
|
|
|
const res = 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
|
|
|
})
|
2021-01-25 12:55:29 +01:00
|
|
|
res.error
|
2021-07-30 15:01:01 +02:00
|
|
|
? notificationStore.actions.error("An error has occurred")
|
|
|
|
: notificationStore.actions.success("Row saved")
|
2021-02-05 17:16:41 +01:00
|
|
|
|
|
|
|
// Refresh related datasources
|
2021-10-26 20:12:55 +02:00
|
|
|
await dataSourceStore.actions.invalidateDataSource(row.tableId)
|
2021-02-05 17:16:41 +01:00
|
|
|
|
2021-01-25 12:44:44 +01:00
|
|
|
return res
|
2020-11-11 13:25:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates a row in a table.
|
|
|
|
*/
|
2021-05-04 12:32:22 +02:00
|
|
|
export const updateRow = async row => {
|
2021-02-05 17:16:41 +01:00
|
|
|
if (!row?.tableId || !row?._id) {
|
|
|
|
return
|
|
|
|
}
|
2021-01-25 12:44:44 +01:00
|
|
|
const res = await API.patch({
|
2021-06-14 20:05:39 +02:00
|
|
|
url: `/api/${row.tableId}/rows`,
|
2020-11-11 13:25:50 +01:00
|
|
|
body: row,
|
|
|
|
})
|
2021-01-25 12:55:29 +01:00
|
|
|
res.error
|
2021-07-30 15:01:01 +02:00
|
|
|
? notificationStore.actions.error("An error has occurred")
|
|
|
|
: notificationStore.actions.success("Row updated")
|
2021-02-05 17:16:41 +01:00
|
|
|
|
|
|
|
// Refresh related datasources
|
2021-10-26 20:12:55 +02:00
|
|
|
await dataSourceStore.actions.invalidateDataSource(row.tableId)
|
2021-02-05 17:16:41 +01:00
|
|
|
|
2021-01-25 12:44:44 +01:00
|
|
|
return res
|
2020-11-11 13:25:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deletes a row from a table.
|
|
|
|
*/
|
|
|
|
export const deleteRow = async ({ tableId, rowId, revId }) => {
|
2021-02-05 17:16:41 +01:00
|
|
|
if (!tableId || !rowId || !revId) {
|
|
|
|
return
|
|
|
|
}
|
2021-01-25 12:44:44 +01:00
|
|
|
const res = await API.del({
|
2021-06-14 14:52:06 +02:00
|
|
|
url: `/api/${tableId}/rows`,
|
|
|
|
body: {
|
|
|
|
_id: rowId,
|
|
|
|
_rev: revId,
|
2021-06-14 20:07:13 +02:00
|
|
|
},
|
2020-11-11 13:25:50 +01:00
|
|
|
})
|
2021-01-25 12:55:29 +01:00
|
|
|
res.error
|
2021-07-30 15:01:01 +02:00
|
|
|
? notificationStore.actions.error("An error has occurred")
|
|
|
|
: notificationStore.actions.success("Row deleted")
|
2021-02-05 17:16:41 +01:00
|
|
|
|
|
|
|
// Refresh related datasources
|
2021-10-26 20:12:55 +02:00
|
|
|
await dataSourceStore.actions.invalidateDataSource(tableId)
|
2021-02-05 17:16:41 +01:00
|
|
|
|
2021-01-25 12:44:44 +01:00
|
|
|
return res
|
2020-11-11 13:25:50 +01:00
|
|
|
}
|
|
|
|
|
2020-11-12 13:24:45 +01:00
|
|
|
/**
|
|
|
|
* Deletes many rows from a table.
|
|
|
|
*/
|
|
|
|
export const deleteRows = async ({ tableId, rows }) => {
|
2021-02-05 17:16:41 +01:00
|
|
|
if (!tableId || !rows) {
|
|
|
|
return
|
|
|
|
}
|
2021-06-14 14:52:06 +02:00
|
|
|
const res = await API.del({
|
2020-11-12 13:24:45 +01:00
|
|
|
url: `/api/${tableId}/rows`,
|
|
|
|
body: {
|
|
|
|
rows,
|
|
|
|
},
|
|
|
|
})
|
2021-01-25 12:55:29 +01:00
|
|
|
res.error
|
2021-07-30 15:01:01 +02:00
|
|
|
? notificationStore.actions.error("An error has occurred")
|
|
|
|
: notificationStore.actions.success(`${rows.length} row(s) deleted`)
|
2021-02-05 17:16:41 +01:00
|
|
|
|
|
|
|
// Refresh related datasources
|
2021-10-26 20:12:55 +02:00
|
|
|
await dataSourceStore.actions.invalidateDataSource(tableId)
|
2021-02-05 17:16:41 +01:00
|
|
|
|
2021-01-25 12:44:44 +01:00
|
|
|
return res
|
2020-11-12 13:24:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enriches rows which contain certain field types so that they can
|
|
|
|
* be properly displayed.
|
|
|
|
*/
|
|
|
|
export const enrichRows = async (rows, tableId) => {
|
2021-02-02 11:35:00 +01:00
|
|
|
if (!Array.isArray(rows)) {
|
|
|
|
return []
|
|
|
|
}
|
2021-09-03 16:04:34 +02:00
|
|
|
if (rows.length) {
|
|
|
|
// map of tables, incase a row being loaded is not from the same table
|
|
|
|
const tables = {}
|
|
|
|
for (let row of rows) {
|
|
|
|
// fallback to passed in tableId if row doesn't have it specified
|
|
|
|
let rowTableId = row.tableId || tableId
|
|
|
|
let table = tables[rowTableId]
|
|
|
|
if (!table) {
|
|
|
|
// Fetch table schema so we can check column types
|
|
|
|
table = await fetchTableDefinition(rowTableId)
|
|
|
|
tables[rowTableId] = table
|
|
|
|
}
|
|
|
|
const schema = table?.schema
|
|
|
|
if (schema) {
|
|
|
|
const keys = Object.keys(schema)
|
2020-11-12 13:24:45 +01:00
|
|
|
for (let key of keys) {
|
|
|
|
const type = schema[key].type
|
2021-09-03 15:49:56 +02:00
|
|
|
if (type === "link" && Array.isArray(row[key])) {
|
2021-02-19 13:00:06 +01:00
|
|
|
// Enrich row a string join of relationship fields
|
2021-02-25 12:06:46 +01:00
|
|
|
row[`${key}_text`] =
|
|
|
|
row[key]
|
2021-05-04 12:32:22 +02:00
|
|
|
?.map(option => option?.primaryDisplay)
|
|
|
|
.filter(option => !!option)
|
2021-02-25 12:06:46 +01:00
|
|
|
.join(", ") || ""
|
2020-11-12 13:24:45 +01:00
|
|
|
} 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
|
|
|
|
}
|
|
|
|
}
|
2021-09-03 16:04:34 +02:00
|
|
|
}
|
2020-11-12 13:24:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return rows
|
|
|
|
}
|