Split out logic for getting rows from datasources
This commit is contained in:
parent
d443bf3616
commit
3eeb945934
|
@ -98,6 +98,10 @@ export const createActions = context => {
|
||||||
return await getAPI()?.actions.deleteRows(rows)
|
return await getAPI()?.actions.deleteRows(rows)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const getRow = async id => {
|
||||||
|
return await getAPI()?.actions.getRow(id)
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
datasource: {
|
datasource: {
|
||||||
...datasource,
|
...datasource,
|
||||||
|
@ -107,6 +111,7 @@ export const createActions = context => {
|
||||||
addRow,
|
addRow,
|
||||||
updateRow,
|
updateRow,
|
||||||
deleteRows,
|
deleteRows,
|
||||||
|
getRow,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,8 +3,6 @@ import { fetchData } from "../../../fetch/fetchData"
|
||||||
import { NewRowID, RowPageSize } from "../lib/constants"
|
import { NewRowID, RowPageSize } from "../lib/constants"
|
||||||
import { tick } from "svelte"
|
import { tick } from "svelte"
|
||||||
|
|
||||||
const SuppressErrors = true
|
|
||||||
|
|
||||||
export const createStores = () => {
|
export const createStores = () => {
|
||||||
const rows = writable([])
|
const rows = writable([])
|
||||||
const loading = writable(false)
|
const loading = writable(false)
|
||||||
|
@ -280,21 +278,6 @@ export const createActions = context => {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fetches a row by ID using the search endpoint
|
|
||||||
const fetchRow = async id => {
|
|
||||||
const res = await API.searchTable({
|
|
||||||
tableId: get(datasource).tableId,
|
|
||||||
limit: 1,
|
|
||||||
query: {
|
|
||||||
equal: {
|
|
||||||
_id: id,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
paginate: false,
|
|
||||||
})
|
|
||||||
return res?.rows?.[0]
|
|
||||||
}
|
|
||||||
|
|
||||||
// Replaces a row in state with the newly defined row, handling updates,
|
// Replaces a row in state with the newly defined row, handling updates,
|
||||||
// addition and deletion
|
// addition and deletion
|
||||||
const replaceRow = (id, row) => {
|
const replaceRow = (id, row) => {
|
||||||
|
@ -323,7 +306,7 @@ export const createActions = context => {
|
||||||
|
|
||||||
// Refreshes a specific row
|
// Refreshes a specific row
|
||||||
const refreshRow = async id => {
|
const refreshRow = async id => {
|
||||||
const row = await fetchRow(id)
|
const row = await datasource.actions.getRow(id)
|
||||||
replaceRow(id, row)
|
replaceRow(id, row)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -336,7 +319,6 @@ export const createActions = context => {
|
||||||
const updateRow = async (rowId, changes) => {
|
const updateRow = async (rowId, changes) => {
|
||||||
const $rows = get(rows)
|
const $rows = get(rows)
|
||||||
const $rowLookupMap = get(rowLookupMap)
|
const $rowLookupMap = get(rowLookupMap)
|
||||||
const $datasource = get(datasource)
|
|
||||||
const index = $rowLookupMap[rowId]
|
const index = $rowLookupMap[rowId]
|
||||||
const row = $rows[index]
|
const row = $rows[index]
|
||||||
if (index == null || !Object.keys(changes || {}).length) {
|
if (index == null || !Object.keys(changes || {}).length) {
|
||||||
|
|
|
@ -25,6 +25,20 @@ export const createActions = context => {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const getRow = async id => {
|
||||||
|
const res = await API.searchTable({
|
||||||
|
tableId: get(datasource).tableId,
|
||||||
|
limit: 1,
|
||||||
|
query: {
|
||||||
|
equal: {
|
||||||
|
_id: id,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
paginate: false,
|
||||||
|
})
|
||||||
|
return res?.rows?.[0]
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
table: {
|
table: {
|
||||||
actions: {
|
actions: {
|
||||||
|
@ -33,6 +47,7 @@ export const createActions = context => {
|
||||||
addRow: saveRow,
|
addRow: saveRow,
|
||||||
updateRow: saveRow,
|
updateRow: saveRow,
|
||||||
deleteRows,
|
deleteRows,
|
||||||
|
getRow,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
|
@ -45,6 +45,21 @@ export const createActions = context => {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: update in future. We can't depend on having table read access.
|
||||||
|
const getRow = async id => {
|
||||||
|
const res = await API.searchTable({
|
||||||
|
tableId: get(datasource).tableId,
|
||||||
|
limit: 1,
|
||||||
|
query: {
|
||||||
|
equal: {
|
||||||
|
_id: id,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
paginate: false,
|
||||||
|
})
|
||||||
|
return res?.rows?.[0]
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
viewV2: {
|
viewV2: {
|
||||||
actions: {
|
actions: {
|
||||||
|
@ -53,6 +68,7 @@ export const createActions = context => {
|
||||||
addRow,
|
addRow,
|
||||||
updateRow,
|
updateRow,
|
||||||
deleteRows,
|
deleteRows,
|
||||||
|
getRow,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue