fix paging and add deletion

This commit is contained in:
Peter Clement 2022-10-20 11:46:04 +01:00
parent 9d35e4f533
commit f5c9e22d42
10 changed files with 117 additions and 38 deletions

View File

@ -56,6 +56,7 @@
{schema} {schema}
value={cellValue} value={cellValue}
on:clickrelationship on:clickrelationship
on:buttonclick
> >
<slot /> <slot />
</svelte:component> </svelte:component>

View File

@ -387,6 +387,7 @@
schema={schema[field]} schema={schema[field]}
value={deepGet(row, field)} value={deepGet(row, field)}
on:clickrelationship on:clickrelationship
on:buttonclick
> >
<slot /> <slot />
</CellRenderer> </CellRenderer>

View File

@ -1,20 +1,64 @@
<script> <script>
import { ActionButton, ActionMenu, MenuItem, Icon } from "@budibase/bbui" import { ActionButton, ActionMenu, MenuItem, Icon } from "@budibase/bbui"
import ConfirmDialog from "components/common/ConfirmDialog.svelte"
import { createEventDispatcher } from "svelte"
export let value export let row
let deleteDialog
let restoreDialog
let editDialog
const dispatch = createEventDispatcher()
const onClickRestore = () => {
dispatch("buttonclick", {
type: "backupRestore",
backupId: row._id,
})
}
const onClickDelete = () => {
dispatch("buttonclick", {
type: "backupDelete",
backupId: row._id,
})
}
</script> </script>
<div class="cell"> <div class="cell">
<ActionButton>Restore</ActionButton> <ActionButton on:click={restoreDialog.show}>Restore</ActionButton>
<ActionMenu> <ActionMenu>
<div slot="control"> <div slot="control">
<Icon size="M" hoverable name="MoreSmallList" /> <Icon size="M" hoverable name="MoreSmallList" />
</div> </div>
<MenuItem icon="Delete">Delete</MenuItem> <MenuItem on:click={deleteDialog.show} icon="Delete">Delete</MenuItem>
<MenuItem on:click={editDialog.show} icon="Edit">Edit</MenuItem>
</ActionMenu> </ActionMenu>
</div> </div>
<ConfirmDialog
bind:this={deleteDialog}
okText="Delete Backup"
onOk={onClickDelete}
title="Confirm Deletion"
>
Are you sure you wish to delete the backup
<i>{row.name}</i>
This action cannot be undone.
</ConfirmDialog>
<ConfirmDialog
bind:this={restoreDialog}
okText="Restore Backup"
onOk={onClickRestore}
title="Confirm Restore"
warning={false}
>
Are you sure you wish to restore this backup
<i>{row.name}</i>. This action cannot be undone.
</ConfirmDialog>
<style> <style>
.cell { .cell {
display: flex; display: flex;

View File

@ -6,7 +6,7 @@
<div class="cell"> <div class="cell">
<Icon name="JourneyVoyager" /> <Icon name="JourneyVoyager" />
<div>{value?.length}</div> <div>{value || 0}</div>
</div> </div>
<style> <style>

View File

@ -3,14 +3,15 @@
ActionButton, ActionButton,
DatePicker, DatePicker,
Layout, Layout,
Modal,
notifications, notifications,
Pagination, Pagination,
Select, Select,
Table, Table,
Modal,
} from "@budibase/bbui" } from "@budibase/bbui"
import { backups } from "stores/portal" import { backups } from "stores/portal"
import { createPaginationStore } from "helpers/pagination" import { createPaginationStore } from "helpers/pagination"
import DatasourceRenderer from "./DatasourceRenderer.svelte" import DatasourceRenderer from "./DatasourceRenderer.svelte"
import ScreensRenderer from "./ScreensRenderer.svelte" import ScreensRenderer from "./ScreensRenderer.svelte"
import AutomationsRenderer from "./AutomationsRenderer.svelte" import AutomationsRenderer from "./AutomationsRenderer.svelte"
@ -27,9 +28,8 @@
let trigger = null let trigger = null
let pageInfo = createPaginationStore() let pageInfo = createPaginationStore()
$: page = $pageInfo.page $: page = $pageInfo.page
$: console.log(page)
$: fetchBackups(app.instance._id, trigger, page) $: fetchBackups(trigger, page)
const triggers = { const triggers = {
PUBLISH: "Publish", PUBLISH: "Publish",
@ -83,10 +83,9 @@
return { return {
...backup, ...backup,
days: getDaysBetween(backup.createdAt), days: getDaysBetween(backup.createdAt),
//...Object.assign(...backup?.contents), ...backup?.contents,
} }
}) })
return flattened return flattened
} }
@ -94,15 +93,18 @@
const now = new Date() const now = new Date()
const backupDate = new Date(date) const backupDate = new Date(date)
backupDate.setDate(backupDate.getDate() - 1) backupDate.setDate(backupDate.getDate() - 1)
console.log(backupDate)
const oneDay = 24 * 60 * 60 * 1000 const oneDay = 24 * 60 * 60 * 1000
return now > backupDate return now > backupDate
? Math.round(Math.abs((now - backupDate) / oneDay)) ? Math.round(Math.abs((now - backupDate) / oneDay))
: 0 : 0
} }
async function fetchBackups(appId, trigger, page) { async function fetchBackups(trigger, page) {
const response = await backups.searchBackups(appId, trigger, page) const response = await backups.searchBackups({
appId: app.instance._id,
trigger,
page,
})
pageInfo.fetched(response.hasNextPage, response.nextPage) pageInfo.fetched(response.hasNextPage, response.nextPage)
// flatten so we have an easier structure to use for the table schema // flatten so we have an easier structure to use for the table schema
@ -120,6 +122,26 @@
notifications.error("Unable to create backup") notifications.error("Unable to create backup")
} }
} }
function selectBackup({ detail }) {
console.log(detail)
}
async function deleteBackup(backupId) {
await backups.deleteBackup({ appId: app.instance._id, backupId })
await fetchBackups(app.instance._id, trigger, page)
}
async function restoreBackup(backupId) {
console.log(backupId)
//backups.restoreBackup(app.instance._id, backupId)
}
async function handleButtonClick({ detail }) {
if (detail.type === "backupDelete") {
deleteBackup(detail.backupId)
} else if (detail.type === "backupRestore") {
restoreBackup(detail.backupId)
}
}
</script> </script>
<div class="root"> <div class="root">
@ -145,6 +167,7 @@
{#if backupData} {#if backupData}
<div> <div>
<Table <Table
on:click={selectBackup}
{schema} {schema}
allowSelectRows={false} allowSelectRows={false}
allowEditColumns={false} allowEditColumns={false}
@ -153,6 +176,7 @@
{customRenderers} {customRenderers}
placeholderText="No backups found" placeholderText="No backups found"
border={false} border={false}
on:buttonclick={handleButtonClick}
/> />
<div class="pagination"> <div class="pagination">
<Pagination <Pagination

View File

@ -6,7 +6,7 @@
<div class="cell"> <div class="cell">
<Icon name="Data" /> <Icon name="Data" />
<div>{value?.length}</div> <div>{value || 0}</div>
</div> </div>
<style> <style>

View File

@ -1,6 +1,5 @@
<script> <script>
export let value export let value
$: console.log(value)
</script> </script>
<div class="cell"> <div class="cell">

View File

@ -6,7 +6,7 @@
<div class="cell"> <div class="cell">
<Icon name="WebPage" /> <Icon name="WebPage" />
<div>{value?.length}</div> <div>{value || 0}</div>
</div> </div>
<style> <style>

View File

@ -2,38 +2,38 @@ import { writable } from "svelte/store"
import { API } from "api" import { API } from "api"
export function createBackupsStore() { export function createBackupsStore() {
const { subscribe, set } = writable([]) const store = writable({})
async function load() { function selectBackup(backupId) {
set([ store.update(state => {
{ state.selectedBackup = backupId
trigger: "PUBLISH", return state
name: "A Backup", })
date: "1665407451",
userId: "Peter Clement",
contents: [
{ datasources: ["datasource1", "datasource2"] },
{ screens: ["screen1", "screen2"] },
{ automations: ["automation1", "automation2"] },
],
},
])
} }
async function searchBackups(appId, trigger, page) { async function searchBackups({ appId, trigger, page } = {}) {
return API.searchBackups({ appId, trigger, page }) return API.searchBackups({ appId, trigger, page })
} }
async function restoreBackup({ appId, backupId }) {
return API.restoreBackup({ appId, backupId })
}
async function deleteBackup({ appId, backupId }) {
return API.deleteBackup({ appId, backupId })
}
async function createManualBackup(appId, name) { async function createManualBackup(appId, name) {
let resp = API.createManualBackup(appId, name) return API.createManualBackup(appId, name)
return resp
} }
return { return {
subscribe,
load,
createManualBackup, createManualBackup,
searchBackups, searchBackups,
selectBackup,
deleteBackup,
restoreBackup,
subscribe: store.subscribe,
} }
} }

View File

@ -2,13 +2,11 @@ export const buildBackupsEndpoints = API => ({
/** /**
* Gets a list of users in the current tenant. * Gets a list of users in the current tenant.
*/ */
searchBackups: async ({ appId, trigger, page }) => { searchBackups: async ({ appId, page }) => {
console.log(page)
return await API.post({ return await API.post({
url: `/api/apps/${appId}/backups/search`, url: `/api/apps/${appId}/backups/search`,
body: { body: {
page, page,
trigger,
}, },
}) })
}, },
@ -19,4 +17,16 @@ export const buildBackupsEndpoints = API => ({
body: { name }, body: { name },
}) })
}, },
deleteBackup: async ({ appId, backupId }) => {
return await API.delete({
url: `/api/apps/${appId}/backups/${backupId}`,
})
},
updateBackup: async ({ appId, backupId }) => {
return await API.patch({
url: `/api/apps/${appId}/backups/${backupId}}`,
})
},
}) })