Disable modal confirm button while async callbacks are being processed to avoid multiple executions

This commit is contained in:
Andrew Kingston 2020-10-02 19:09:19 +01:00
parent 2df104568d
commit 4d8e4061b3
1 changed files with 24 additions and 5 deletions

View File

@ -10,31 +10,50 @@
export let onConfirm export let onConfirm
const modalContext = getContext(ContextKey) const modalContext = getContext(ContextKey)
let loading = false
function hide() { function hide() {
modalContext.hide() modalContext.hide()
} }
async function confirm() { async function confirm() {
loading = true
if (!onConfirm || (await onConfirm()) !== false) { if (!onConfirm || (await onConfirm()) !== false) {
hide() hide()
} }
loading = false
} }
</script> </script>
{#if showCancelButton || showConfirmButton}
<footer> <footer>
<div class="content">
<slot />
</div>
<div class="buttons">
{#if showCancelButton} {#if showCancelButton}
<Button secondary on:click={hide}>{cancelText}</Button> <Button secondary on:click={hide}>{cancelText}</Button>
{/if} {/if}
{#if showConfirmButton} {#if showConfirmButton}
<Button primary {...$$restProps} on:click={confirm}>{confirmText}</Button> <Button
primary
{...$$restProps}
disabled={$$props.disabled || loading}
on:click={confirm}>
{confirmText}
</Button>
{/if} {/if}
</div>
</footer> </footer>
{/if}
<style> <style>
footer { footer {
display: flex;
flex-direction: row;
justify-content: space-between;
align-items: center;
gap: var(--spacing-m);
}
.buttons {
display: flex; display: flex;
flex-direction: row; flex-direction: row;
justify-content: flex-end; justify-content: flex-end;