-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38 from eseiker/feature/improve-ux
Dropdown UI & Error toast for Web UI
- Loading branch information
Showing
8 changed files
with
265 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { useEffect, useRef } from "preact/hooks"; | ||
|
||
export interface ToastProps { | ||
text: string; | ||
type?: "info" | "success" | "warning" | "error"; | ||
timeoutMs?: number; | ||
} | ||
|
||
export const Toast = ({ type, text, timeoutMs = 3000 }: ToastProps) => { | ||
const dialogRef = useRef<HTMLDialogElement>(null); | ||
const timeoutRef = useRef<number | null>(); | ||
const alertClass = type ? `alert-${type}` : ""; | ||
|
||
useEffect(() => { | ||
dialogRef.current?.show(); | ||
if (timeoutRef.current) clearTimeout(timeoutRef.current); | ||
timeoutRef.current = setTimeout(() => { | ||
dialogRef.current?.close(); | ||
}, timeoutMs); | ||
}); | ||
|
||
return ( | ||
<dialog ref={dialogRef} class="modal" style="width: 0; height: 0;"> | ||
<form method="dialog" class="toast"> | ||
<div class={`alert ${alertClass} inline`}> | ||
<span>{text}</span> | ||
<button class="btn btn-sm btn-ghost btn-circle">✕</button> | ||
</div> | ||
</form> | ||
</dialog> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,12 @@ | ||
import { useCallback, useRef } from "preact/hooks"; | ||
import { useCallback, useRef, useState } from "preact/hooks"; | ||
|
||
import { AbiTable } from "~/components/AbiTable.tsx"; | ||
import { | ||
CollapsibleTable, | ||
CollapsibleTableRow, | ||
} from "~/components/CollapsibleTable.tsx"; | ||
import { Modal } from "~/components/Modal.tsx"; | ||
import { Toast, type ToastProps } from "~/components/Toast.tsx"; | ||
|
||
import type { AbiEvent } from "https://esm.sh/[email protected]"; | ||
|
||
|
@@ -30,40 +31,57 @@ interface Props { | |
} | ||
|
||
export const ListAbi = ({ entries }: Props) => { | ||
const modalRef = useRef<HTMLDialogElement>(null); | ||
const [toast, setToast] = useState<ToastProps | null>(null); | ||
|
||
const handleSubmit = useCallback(async (e: Event) => { | ||
e.preventDefault(); | ||
|
||
const formData = new FormData(e.target as HTMLFormElement); | ||
const abiJson = formData.get("abiJson"); | ||
|
||
await fetch(`/api/abi`, { | ||
const res = await fetch(`/api/abi`, { | ||
method: "POST", | ||
body: abiJson, | ||
}); | ||
|
||
if (!res.ok) { | ||
setToast({ type: "error", text: "Failed to register ABI entries." }); | ||
return; | ||
} | ||
|
||
if ((await res.json()).length === 0) { | ||
setToast({ type: "info", text: "No new ABI entries to register." }); | ||
modalRef.current?.close(); | ||
return; | ||
} | ||
|
||
location.reload(); | ||
}, []); | ||
|
||
const handleDelete = useCallback( | ||
(hash: string) => async (e: Event) => { | ||
e.preventDefault(); | ||
|
||
await fetch(`/api/abi`, { | ||
const res = await fetch(`/api/abi`, { | ||
method: "DELETE", | ||
body: JSON.stringify({ hash }), | ||
}); | ||
|
||
if (!res.ok) { | ||
setToast({ type: "error", text: "Failed to delete an ABI entry." }); | ||
return; | ||
} | ||
|
||
location.reload(); | ||
}, | ||
[], | ||
); | ||
|
||
const modalRef = useRef<HTMLDialogElement>(null); | ||
|
||
return ( | ||
<> | ||
<div class="float-right pb-4"> | ||
<button class="btn" onClick={() => modalRef.current?.showModal()}> | ||
<button class="btn" onClick={() => modalRef.current?.show()}> | ||
+ | ||
</button> | ||
<Modal title="Register ABI" ref={modalRef}> | ||
|
@@ -80,6 +98,7 @@ export const ListAbi = ({ entries }: Props) => { | |
<input type="submit" class="btn" /> | ||
</form> | ||
</Modal> | ||
{toast && <Toast {...toast} />} | ||
</div> | ||
<CollapsibleTable headers={["Hash", "Signature"]}> | ||
{entries.map((entry) => ( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.