Add a refresh action to common table searching logic

This commit is contained in:
Andrew Kingston 2021-09-24 16:13:25 +01:00
parent 13163102f8
commit db6032ca95
2 changed files with 17 additions and 3 deletions

View File

@ -21,7 +21,8 @@
$: schema = $tables.selected?.schema
$: type = $tables.selected?.type
$: isInternal = type !== "external"
$: fetchTable($tables.selected?._id)
$: id = $tables.selected?._id
$: fetchTable(id)
const fetchTable = tableId => {
data.update({

View File

@ -22,6 +22,7 @@ export const fetchTableData = opts => {
// Local non-observable state
let query
let sortType
let lastBookmark
// Local observable state
const store = writable({
@ -43,6 +44,7 @@ export const fetchTableData = opts => {
})
const fetchPage = async bookmark => {
lastBookmark = bookmark
const { tableId, limit, sortColumn, sortOrder, paginate } = options
store.update($store => ({ ...$store, loading: true }))
const res = await API.post(`/api/${options.tableId}/search`, {
@ -126,7 +128,7 @@ export const fetchTableData = opts => {
// Fetches the next page of data
const nextPage = async () => {
const state = get(derivedStore)
if (!options.paginate || !state.hasNextPage) {
if (state.loading || !options.paginate || !state.hasNextPage) {
return
}
@ -151,7 +153,7 @@ export const fetchTableData = opts => {
// Fetches the previous page of data
const prevPage = async () => {
const state = get(derivedStore)
if (!options.paginate || !state.hasPrevPage) {
if (state.loading || !options.paginate || !state.hasPrevPage) {
return
}
@ -179,6 +181,16 @@ export const fetchTableData = opts => {
await fetchData()
}
// Loads the same page again
const refresh = async () => {
if (get(store).loading) {
return
}
console.log("refresh")
const page = await fetchPage(lastBookmark)
store.update($store => ({ ...$store, rows: page.rows }))
}
// Initially fetch data but don't bother waiting for the result
fetchData()
@ -188,5 +200,6 @@ export const fetchTableData = opts => {
nextPage,
prevPage,
update,
refresh,
}
}