Add Alert component

This commit is contained in:
Dallas Hoffman 2023-09-25 00:35:22 -04:00
parent a09e630739
commit a56e08e59a
2 changed files with 81 additions and 6 deletions

View File

@ -0,0 +1,52 @@
<script lang="ts">
import FaCircleExclamation from '$icon/fa-circle-exclamation.svelte';
import FaCircleInfo from '$icon/fa-circle-info.svelte';
import FaTriangleExclamation from '$icon/fa-triangle-exclamation.svelte';
export let type: 'info' | 'warning' | 'error' | null = null;
</script>
<div role="alert" class="app-alert app-alert--{type}">
{#if type !== null}
<div class="app-alert__icon">
{#if type === 'info'}
<FaCircleInfo />
{:else if type === 'warning'}
<FaTriangleExclamation />
{:else if type === 'error'}
<FaCircleExclamation />
{/if}
</div>
{/if}
<p><slot /></p>
</div>
<style lang="scss">
.app-alert {
--type-color: currentColor;
display: flex;
align-items: center;
gap: 1rem;
padding: 0.5rem 1rem;
background-color: var(--card-sectionning-background-color);
border: 1px solid var(--type-color);
border-radius: var(--border-radius);
&__icon {
flex: 0 0 1rem;
color: var(--type-color);
}
&--info {
--type-color: var(--primary);
}
&--warning {
--type-color: hsl(44 93% 37%);
}
&--error {
--type-color: hsl(0 70% 53%);
}
}
</style>

View File

@ -1,21 +1,27 @@
<script lang="ts"> <script lang="ts">
import { importDb } from '$lib/db/import-db'; import { importDb } from '$lib/db/import-db';
import Alert from '../alert.svelte';
import Button from '../button.svelte'; import Button from '../button.svelte';
import Modal from '../modal.svelte'; import Modal from '../modal.svelte';
export let open: boolean; export let open: boolean;
let submitting: boolean = false; let submitting: boolean = false;
let noFileError: boolean = false;
let importFailedError: boolean = false;
async function submit(event: SubmitEvent) { async function submit(event: SubmitEvent) {
submitting = true; submitting = true;
noFileError = false;
importFailedError = false;
const form = event.target as HTMLFormElement; const form = event.target as HTMLFormElement;
const formData = new FormData(form); const formData = new FormData(form);
const [dbFile] = formData.getAll('dbFile') as File[]; const [dbFile] = formData.getAll('dbFile') as File[];
if (!dbFile || dbFile.name === '') { if (!dbFile || dbFile.name === '') {
// TODO: message that no file was selected
submitting = false; submitting = false;
noFileError = true;
return; return;
} }
@ -25,7 +31,7 @@
form.reset(); form.reset();
open = false; open = false;
} else { } else {
// TODO: message that import failed importFailedError = true;
} }
submitting = false; submitting = false;
@ -39,13 +45,30 @@
<strong>Warning:</strong> <strong>Warning:</strong>
This will overwrite all of your current notes. This will overwrite all of your current notes.
</p> </p>
<input type="file" name="dbFile" accept=".db" /> <div class="import-controls">
<Button type="submit" busy={submitting}>Import</Button> <input type="file" name="dbFile" accept=".db" />
{#if noFileError === true}
<Alert type="error">Please choose a backup file.</Alert>
{/if}
{#if importFailedError === true}
<Alert type="error">
Import failed. Please make sure that you selected a notes backup file.
</Alert>
{/if}
<Button type="submit" busy={submitting}>Import</Button>
</div>
</form> </form>
</Modal> </Modal>
<style lang="scss"> <style lang="scss">
input { .import-controls {
margin: 1rem 0 1.5rem; display: flex;
flex-direction: column;
gap: 1.5rem;
margin-top: 1rem;
input {
margin: 0;
}
} }
</style> </style>