Add deletion confirmation to snippets

This commit is contained in:
Andrew Kingston 2024-03-07 08:25:11 +00:00
parent d08628234f
commit ccb32af078
2 changed files with 19 additions and 6 deletions

View File

@ -12,6 +12,7 @@
import { decodeJSBinding, encodeJSBinding } from "@budibase/string-templates" import { decodeJSBinding, encodeJSBinding } from "@budibase/string-templates"
import { snippets } from "stores/builder" import { snippets } from "stores/builder"
import { getSequentialName } from "helpers/duplicate" import { getSequentialName } from "helpers/duplicate"
import ConfirmDialog from "components/common/ConfirmDialog.svelte"
export let snippet export let snippet
@ -25,6 +26,7 @@
let name = "" let name = ""
let code = "" let code = ""
let loading = false let loading = false
let deleteConfirmationDialog
$: defaultName = getSequentialName($snippets, "MySnippet", x => x.name) $: defaultName = getSequentialName($snippets, "MySnippet", x => x.name)
$: key = snippet?.name $: key = snippet?.name
@ -36,12 +38,10 @@
const saveSnippet = async () => { const saveSnippet = async () => {
loading = true loading = true
try { try {
await snippets.saveSnippet({ const newSnippet = { name, code: rawJS }
name, await snippets.saveSnippet(newSnippet)
code: rawJS,
})
drawer.hide() drawer.hide()
notifications.success(`Snippet ${name} saved`) notifications.success(`Snippet ${newSnippet.name} saved`)
} catch (error) { } catch (error) {
notifications.error("Error saving snippet") notifications.error("Error saving snippet")
} }
@ -109,7 +109,11 @@
</svelte:fragment> </svelte:fragment>
<svelte:fragment slot="buttons"> <svelte:fragment slot="buttons">
{#if snippet} {#if snippet}
<Button warning on:click={deleteSnippet} disabled={loading}> <Button
warning
on:click={deleteConfirmationDialog.show}
disabled={loading}
>
Delete Delete
</Button> </Button>
{/if} {/if}
@ -136,6 +140,13 @@
</svelte:fragment> </svelte:fragment>
</Drawer> </Drawer>
<ConfirmDialog
bind:this={deleteConfirmationDialog}
title="Delete snippet"
body={`Are you sure you want to delete ${snippet?.name}?`}
onOk={deleteSnippet}
/>
<style> <style>
.name { .name {
display: flex; display: flex;

View File

@ -10,6 +10,7 @@
navigationStore, navigationStore,
selectedScreen, selectedScreen,
hoverStore, hoverStore,
snippets,
} from "stores/builder" } from "stores/builder"
import ConfirmDialog from "components/common/ConfirmDialog.svelte" import ConfirmDialog from "components/common/ConfirmDialog.svelte"
import { import {
@ -68,6 +69,7 @@
hostname: window.location.hostname, hostname: window.location.hostname,
port: window.location.port, port: window.location.port,
}, },
snippets: $snippets,
} }
// Refresh the preview when required // Refresh the preview when required