From a099cbd85ffa1a193a7897708b0de25f07e958c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Ml=C3=A1dek?= Date: Sun, 9 Jan 2022 21:24:49 +0100 Subject: [PATCH] [ui] rudimentary attribute editing also refactor to use actual correct types throughout --- tools/upend_js/index.ts | 4 +- tools/upend_js/types.ts | 10 +++- ui/src/components/AttributeView.svelte | 18 ++++-- ui/src/components/utils/Editable.svelte | 37 ++++++++++++ ui/src/components/utils/Input.svelte | 6 +- ui/src/components/utils/Selector.svelte | 78 +++++++++++++++++++------ ui/src/components/widgets/Table.svelte | 27 +++++---- ui/src/types/base.ts | 9 ++- ui/yarn.lock | 4 +- 9 files changed, 149 insertions(+), 44 deletions(-) create mode 100644 ui/src/components/utils/Editable.svelte diff --git a/tools/upend_js/index.ts b/tools/upend_js/index.ts index 1ee8058..f5c4e77 100644 --- a/tools/upend_js/index.ts +++ b/tools/upend_js/index.ts @@ -1,4 +1,4 @@ -import type { IEntry, ListingResult, VALUE_TYPE } from "./types"; +import type { IEntry, IValue, ListingResult, VALUE_TYPE } from "./types"; // export function listingAsOrdered(listing: ListingResult): OrderedListing { // const entries = Object.entries(listing) as [Address, IEntry][]; @@ -103,7 +103,7 @@ export class UpObject { export class UpEntry extends UpObject implements IEntry { entity: string; attribute: string; - value: { t: VALUE_TYPE; c: string | number }; + value: IValue; constructor(address: string, entry: IEntry, listing: UpListing) { super(address, listing); diff --git a/tools/upend_js/types.ts b/tools/upend_js/types.ts index f4e1c3a..c155023 100644 --- a/tools/upend_js/types.ts +++ b/tools/upend_js/types.ts @@ -4,7 +4,12 @@ export type VALUE_TYPE = "Value" | "Address" | "Invalid"; export interface IEntry { entity: Address; attribute: string; - value: { t: VALUE_TYPE; c: string | number }; + value: IValue; +} + +export interface IValue { + t: VALUE_TYPE; + c: string | number; } export interface ListingResult { @@ -28,8 +33,7 @@ export interface IJob { state: "InProgress" | "Done" | "Failed"; } - export interface VaultInfo { name: string | null; location: string; -} \ No newline at end of file +} diff --git a/ui/src/components/AttributeView.svelte b/ui/src/components/AttributeView.svelte index 18d0b01..ac78998 100644 --- a/ui/src/components/AttributeView.svelte +++ b/ui/src/components/AttributeView.svelte @@ -55,16 +55,26 @@ body: JSON.stringify({ entity: address, attribute: change.attribute, - value: { - t: "Value", - c: change.value, - }, + value: change.value, }), }); break; case "delete": await fetch(`/api/obj/${change.addr}`, { method: "DELETE" }); break; + case "update": + // TODO + await fetch(`/api/obj/${change.addr}`, { method: "DELETE" }); + await fetch(`/api/obj`, { + method: "PUT", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ + entity: address, + attribute: change.attribute, + value: change.value, + }), + }); + break; default: console.error("Unimplemented AttributeChange", change); return; diff --git a/ui/src/components/utils/Editable.svelte b/ui/src/components/utils/Editable.svelte new file mode 100644 index 0000000..c479c69 --- /dev/null +++ b/ui/src/components/utils/Editable.svelte @@ -0,0 +1,37 @@ + + +
+ {#if editable} +
+
+ +
+ dispatch("edit", newValue)} + /> +
+ {:else} + + {/if} +
+ + diff --git a/ui/src/components/utils/Input.svelte b/ui/src/components/utils/Input.svelte index 1bac8b6..45044b8 100644 --- a/ui/src/components/utils/Input.svelte +++ b/ui/src/components/utils/Input.svelte @@ -8,6 +8,10 @@ let focused = false; $: dispatch("focusChange", focused); + + function onInput() { + dispatch("input", value); + }
@@ -15,7 +19,7 @@ (focused = true)} on:blur={() => (focused = false)} /> diff --git a/ui/src/components/utils/Selector.svelte b/ui/src/components/utils/Selector.svelte index cd1883c..efd5d64 100644 --- a/ui/src/components/utils/Selector.svelte +++ b/ui/src/components/utils/Selector.svelte @@ -1,27 +1,68 @@
- (inputFocused = ev.detail)} /> + (inputFocused = ev.detail)} + />
    (hover = false)} > {#each options as option} -
  • { - value = option; - visible = false; - }} - > - {option} +
  • set(option)}> + {#if option.attribute} + {option.attribute} + {:else if option.value} + RESOLVING LOGIC + {/if}
  • {/each}
diff --git a/ui/src/components/widgets/Table.svelte b/ui/src/components/widgets/Table.svelte index afe6ec5..fbd2480 100644 --- a/ui/src/components/widgets/Table.svelte +++ b/ui/src/components/widgets/Table.svelte @@ -11,6 +11,8 @@ import IconButton from "../utils/IconButton.svelte"; import Input from "../utils/Input.svelte"; import Selector from "../utils/Selector.svelte"; + import type { IValue } from "upend/types"; + import Editable from "../utils/Editable.svelte"; const dispatch = createEventDispatcher(); const params = useParams(); @@ -28,7 +30,7 @@ // Editing let newEntryAttribute = ""; - let newEntryValue = ""; + let newEntryValue: IValue | undefined; async function addEntry() { dispatch("change", { @@ -37,14 +39,14 @@ value: newEntryValue, } as AttributeChange); newEntryAttribute = ""; - newEntryValue = ""; + newEntryValue = undefined; } async function removeEntry(addr: string) { if (confirm("Are you sure you want to remove the attribute?")) { dispatch("change", { type: "delete", addr } as AttributeChange); } } - async function updateEntry(addr: string, attribute: string, value: string) { + async function updateEntry(addr: string, attribute: string, value: IValue) { dispatch("change", { type: "update", addr, @@ -180,11 +182,11 @@ const handler = VALUE_FORMATTERS[attribute]; if (handler) { try { - return handler(value); + return handler(value); } catch (error) { console.warn(`Error while formatting "${value}": ${error}`); + } } - } return String(value); } @@ -264,10 +266,11 @@ {#if showValue} - updateEntry(entry.address, entry.attribute, val)} + value={entry.value} + on:edit={(ev) => + updateEntry(entry.address, entry.attribute, ev.detail)} > {#if entry.value.t === "Address"}
{/if} - + {/if} {/each} {#if editable} - + {#if showAttribute} - + {/if} {#if showValue} - + {/if} diff --git a/ui/src/types/base.ts b/ui/src/types/base.ts index 324f65e..8b97c57 100644 --- a/ui/src/types/base.ts +++ b/ui/src/types/base.ts @@ -1,3 +1,5 @@ +import type { IValue } from "upend/types"; + export type AttributeChange = | AttributeCreate | AttributeUpdate @@ -6,16 +8,17 @@ export type AttributeChange = export interface AttributeCreate { type: "create"; attribute: string; - value: any; + value: IValue; } export interface AttributeUpdate { type: "update"; addr: string; - value: any; + attribute: string; // TODO: remove + value: IValue; } export interface AttributeDelete { type: "delete"; addr: string; -} \ No newline at end of file +} diff --git a/ui/yarn.lock b/ui/yarn.lock index 65680ac..45c123e 100644 --- a/ui/yarn.lock +++ b/ui/yarn.lock @@ -3474,8 +3474,8 @@ __metadata: "upend@file:../tools/upend_js::locator=svelte-app%40workspace%3A.": version: 0.0.1 - resolution: "upend@file:../tools/upend_js#../tools/upend_js::hash=a954aa&locator=svelte-app%40workspace%3A." - checksum: 1b0605428365b2a672cb119e795292102f80171f03efec8290a5811b1514107455557a21157e7376aa7dcb7285484fd1db791d6d876893ec92f1794a0f61120f + resolution: "upend@file:../tools/upend_js#../tools/upend_js::hash=2fee87&locator=svelte-app%40workspace%3A." + checksum: 51fd21439f8a42d901e03c06e4797f89fd7c505c21ce53d9cb7340ae7495f05d774dd7f0ff758b5470ebb1fb6725e33e06b09d98ec91f17e9766ae1c05d2bd58 languageName: node linkType: hard