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 $: schema = $tables.selected?.schema
$: type = $tables.selected?.type $: type = $tables.selected?.type
$: isInternal = type !== "external" $: isInternal = type !== "external"
$: fetchTable($tables.selected?._id) $: id = $tables.selected?._id
$: fetchTable(id)
const fetchTable = tableId => { const fetchTable = tableId => {
data.update({ data.update({

View File

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