2021-03-16 14:54:34 +01:00
|
|
|
import { notificationStore, dataSourceStore } from "../store"
|
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-03 09:31:09 +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-01-25 13:39:19 +01:00
|
|
|
? notificationStore.danger("An error has occurred")
|
2021-01-25 12:55:29 +01:00
|
|
|
: notificationStore.success("Row saved")
|
2021-02-05 17:16:41 +01:00
|
|
|
|
|
|
|
// Refresh related datasources
|
2021-03-16 14:54:34 +01:00
|
|
|
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-03 09:31:09 +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({
|
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,
|
|
|
|
})
|
2021-01-25 12:55:29 +01:00
|
|
|
res.error
|
2021-01-25 13:39:19 +01:00
|
|
|
? notificationStore.danger("An error has occurred")
|
2021-01-25 12:55:29 +01:00
|
|
|
: notificationStore.success("Row updated")
|
2021-02-05 17:16:41 +01:00
|
|
|
|
|
|
|
// Refresh related datasources
|
2021-03-16 14:54:34 +01:00
|
|
|
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({
|
2020-11-11 13:25:50 +01:00
|
|
|
url: `/api/${tableId}/rows/${rowId}/${revId}`,
|
|
|
|
})
|
2021-01-25 12:55:29 +01:00
|
|
|
res.error
|
2021-01-25 13:39:19 +01:00
|
|
|
? notificationStore.danger("An error has occurred")
|
2021-01-25 12:55:29 +01:00
|
|
|
: notificationStore.success("Row deleted")
|
2021-02-05 17:16:41 +01:00
|
|
|
|
|
|
|
// Refresh related datasources
|
2021-03-16 14:54:34 +01:00
|
|
|
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-01-25 12:44:44 +01:00
|
|
|
const res = await API.post({
|
2020-11-12 13:24:45 +01:00
|
|
|
url: `/api/${tableId}/rows`,
|
|
|
|
body: {
|
|
|
|
rows,
|
|
|
|
type: "delete",
|
|
|
|
},
|
|
|
|
})
|
2021-01-25 12:55:29 +01:00
|
|
|
res.error
|
2021-01-25 13:39:19 +01:00
|
|
|
? notificationStore.danger("An error has occurred")
|
2021-01-25 12:57:04 +01:00
|
|
|
: notificationStore.success(`${rows.length} row(s) deleted`)
|
2021-02-05 17:16:41 +01:00
|
|
|
|
|
|
|
// Refresh related datasources
|
2021-03-16 14:54:34 +01:00
|
|
|
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 []
|
|
|
|
}
|
|
|
|
if (rows.length && tableId) {
|
2020-11-12 13:24:45 +01:00
|
|
|
// 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)
|
2021-05-03 09:31:09 +02:00
|
|
|
rows.forEach((row) => {
|
2020-11-12 13:24:45 +01:00
|
|
|
for (let key of keys) {
|
|
|
|
const type = schema[key].type
|
|
|
|
if (type === "link") {
|
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-03 09:31:09 +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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rows
|
|
|
|
}
|