diff --git a/src/elements/Form.ts b/src/elements/Form.ts new file mode 100644 index 0000000..e3b8f76 --- /dev/null +++ b/src/elements/Form.ts @@ -0,0 +1,18 @@ +import { makeElement } from "z-makeelement"; + +export function Form( + children: Element[], + submit: (form: HTMLFormElement) => unknown, +): HTMLFormElement { + const form = makeElement({ + tag: "form", + children: children, + }) as HTMLFormElement; + + form.addEventListener("submit", (e: Event) => { + e.preventDefault(); + submit(form); + }); + + return form; +} diff --git a/src/pages/Login.ts b/src/pages/Login.ts index 99c4aff..968d698 100644 --- a/src/pages/Login.ts +++ b/src/pages/Login.ts @@ -1,3 +1,4 @@ +import { Form } from "../elements/Form"; import { Margin } from "../elements/Margin"; import { MarginInline } from "../elements/MarginInline"; import { Page } from "../types/Page"; @@ -12,9 +13,8 @@ export class LoginPage extends Page { super(); } async render(): Promise { - const tokenLoginForm = makeElement({ - tag: "form", - children: [ + const tokenLoginForm = Form( + [ Margin( makeElement({ tag: "input", @@ -38,11 +38,28 @@ export class LoginPage extends Page { }), ), ], - }) as HTMLFormElement; + async (form: HTMLFormElement) => { + const formData = new FormData(form); + const token = formData.get("token"); + this.state.token = token as string; - const usernameLoginForm = makeElement({ - tag: "form", - children: [ + try { + await lookupSelf(); + await this.router.changePage("HOME"); + } catch (e: unknown) { + const error = e as Error; + document.getElementById("tokenInput").classList.add("uk-form-danger"); + if (error.message == "permission denied") { + setErrorText(i18next.t("token_login_error")); + } else { + setErrorText(error.message); + } + } + }, + ); + + const usernameLoginForm = Form( + [ Margin( makeElement({ tag: "input", @@ -80,7 +97,24 @@ export class LoginPage extends Page { }), ), ], - }) as HTMLFormElement; + async (form: HTMLFormElement) => { + const formData = new FormData(form); + + try { + const res = await usernameLogin( + formData.get("username") as string, + formData.get("password") as string, + ); + this.state.token = res; + await this.router.changePage("HOME"); + } catch (e: unknown) { + const error = e as Error; + document.getElementById("usernameInput").classList.add("uk-form-danger"); + document.getElementById("passwordInput").classList.add("uk-form-danger"); + setErrorText(error.message); + } + }, + ); await this.router.setPageContent( makeElement({ @@ -130,44 +164,6 @@ export class LoginPage extends Page { ], }), ); - - tokenLoginForm.addEventListener("submit", async (e) => { - e.preventDefault(); - const formData = new FormData(tokenLoginForm); - const token = formData.get("token"); - this.state.token = token as string; - - try { - await lookupSelf(); - await this.router.changePage("HOME"); - } catch (e: unknown) { - const error = e as Error; - document.getElementById("tokenInput").classList.add("uk-form-danger"); - if (error.message == "permission denied") { - setErrorText(i18next.t("token_login_error")); - } else { - setErrorText(error.message); - } - } - }); - usernameLoginForm.addEventListener("submit", async (e) => { - e.preventDefault(); - const formData = new FormData(usernameLoginForm); - - try { - const res = await usernameLogin( - formData.get("username") as string, - formData.get("password") as string, - ); - this.state.token = res; - await this.router.changePage("HOME"); - } catch (e: unknown) { - const error = e as Error; - document.getElementById("usernameInput").classList.add("uk-form-danger"); - document.getElementById("passwordInput").classList.add("uk-form-danger"); - setErrorText(error.message); - } - }); } get name(): string { diff --git a/src/pages/PwGen.ts b/src/pages/PwGen.ts index 27daa95..e2f3858 100644 --- a/src/pages/PwGen.ts +++ b/src/pages/PwGen.ts @@ -1,4 +1,5 @@ import { CopyableInputBox, CopyableInputBoxType } from "../elements/CopyableInputBox"; +import { Form } from "../elements/Form"; import { Margin } from "../elements/Margin"; import { Option } from "../elements/Option"; import { Page } from "../types/Page"; @@ -90,9 +91,8 @@ export class PwGenPage extends Page { ], }) as HTMLSelectElement; - this.passwordForm = makeElement({ - tag: "form", - children: [ + this.passwordForm = Form( + [ this.passwordLengthTitle, Margin(this.passwordLengthRange), Margin(this.passwordAlphabet), @@ -106,9 +106,8 @@ export class PwGenPage extends Page { }), ), ], - }) as HTMLFormElement; - - this.passwordForm.addEventListener("submit", (e) => this.formEvent(e)); + (_) => this.updatePassword(), + ); await this.router.setPageContent(this.passwordForm); } @@ -119,11 +118,6 @@ export class PwGenPage extends Page { }); } - formEvent(e: Event): void { - e.preventDefault(); - this.updatePassword(); - } - updatePassword(): void { this.passwordLengthTitle.innerText = this.getPasswordLengthText(); this.passwordBox.setText( diff --git a/src/pages/Secrets/KeyValue/KeyValueNew.ts b/src/pages/Secrets/KeyValue/KeyValueNew.ts index 71b5459..e7add7a 100644 --- a/src/pages/Secrets/KeyValue/KeyValueNew.ts +++ b/src/pages/Secrets/KeyValue/KeyValueNew.ts @@ -1,3 +1,4 @@ +import { Form } from "../../../elements/Form"; import { Page } from "../../../types/Page"; import { SecretTitleElement } from "../SecretTitleElement"; import { createOrUpdateSecret } from "../../../api/kv/createOrUpdateSecret"; @@ -14,52 +15,45 @@ export class KeyValueNewPage extends Page { await this.router.changePage("KEY_VALUE_VIEW"); } - addKVNewForm: HTMLFormElement; - async render(): Promise { - this.addKVNewForm = makeElement({ - tag: "form", - id: "addKVNewForm", - children: [ - makeElement({ - tag: "div", - class: "uk-margin", - children: makeElement({ - tag: "input", - class: ["uk-input", "uk-form-width-medium"], + await this.router.setPageContent( + Form( + [ + makeElement({ + tag: "div", + class: "uk-margin", + children: makeElement({ + tag: "input", + class: ["uk-input", "uk-form-width-medium"], + attributes: { + required: "true", + type: "text", + placeholder: i18next.t("kv_new_path"), + name: "path", + }, + }), + }), + makeElement({ + tag: "p", + id: "errorText", + class: "uk-text-danger", + }), + makeElement({ + tag: "button", + class: ["uk-button", "uk-button-primary"], + text: i18next.t("kv_new_create_btn"), attributes: { - required: "true", - type: "text", - placeholder: i18next.t("kv_new_path"), - name: "path", + type: "submit", }, }), - }), - makeElement({ - tag: "p", - id: "errorText", - class: "uk-text-danger", - }), - makeElement({ - tag: "button", - class: ["uk-button", "uk-button-primary"], - text: i18next.t("kv_new_create_btn"), - attributes: { - type: "submit", - }, - }), - ], - }) as HTMLFormElement; - await this.router.setPageContent(this.addKVNewForm); - - this.addKVNewForm.addEventListener("submit", async (e: Event) => { - e.preventDefault(); - await this.newKVSecretHandleForm(); - }); + ], + async (form: HTMLFormElement) => await this.newKVSecretHandleForm(form), + ), + ); } - async newKVSecretHandleForm(): Promise { - const formData = new FormData(this.addKVNewForm); + async newKVSecretHandleForm(form: HTMLFormElement): Promise { + const formData = new FormData(form); const path = formData.get("path") as string; let keyData = {}; diff --git a/src/pages/Secrets/NewEngines/NewKVEngine.ts b/src/pages/Secrets/NewEngines/NewKVEngine.ts index 586159d..652dae4 100644 --- a/src/pages/Secrets/NewEngines/NewKVEngine.ts +++ b/src/pages/Secrets/NewEngines/NewKVEngine.ts @@ -1,3 +1,4 @@ +import { Form } from "../../../elements/Form"; import { Margin } from "../../../elements/Margin"; import { Option } from "../../../elements/Option"; import { Page } from "../../../types/Page"; @@ -14,75 +15,72 @@ export class NewKVEnginePage extends Page { await this.router.changePage("SECRETS_HOME"); } async render(): Promise { - const newEngineForm = makeElement({ - tag: "form", - children: [ - Margin( + await this.router.setPageContent( + Form( + [ + Margin( + makeElement({ + tag: "input", + class: ["uk-input", "uk-form-width-medium"], + attributes: { + required: "true", + type: "text", + placeholder: i18next.t("new_kv_engine_name_input"), + name: "name", + }, + }), + ), + Margin( + makeElement({ + tag: "select", + class: ["uk-select", "uk-form-width-medium"], + attributes: { + name: "version", + }, + children: [ + Option(i18next.t("new_kv_engine_version_2"), "2"), + Option(i18next.t("new_kv_engine_version_1"), "1"), + ], + }), + ), makeElement({ - tag: "input", - class: ["uk-input", "uk-form-width-medium"], + tag: "p", + id: "errorText", + class: "uk-text-danger", + }), + makeElement({ + tag: "button", + class: ["uk-button", "uk-button-primary"], + text: i18next.t("new_kv_engine_create_btn"), attributes: { - required: "true", - type: "text", - placeholder: i18next.t("new_kv_engine_name_input"), - name: "name", + type: "submit", }, }), - ), - Margin( - makeElement({ - tag: "select", - class: ["uk-select", "uk-form-width-medium"], - attributes: { - name: "version", - }, - children: [ - Option(i18next.t("new_kv_engine_version_2"), "2"), - Option(i18next.t("new_kv_engine_version_1"), "1"), - ], - }), - ), - makeElement({ - tag: "p", - id: "errorText", - class: "uk-text-danger", - }), - makeElement({ - tag: "button", - class: ["uk-button", "uk-button-primary"], - text: i18next.t("new_kv_engine_create_btn"), - attributes: { - type: "submit", - }, - }), - ], - }) as HTMLFormElement; + ], + async (form: HTMLFormElement) => { + const formData = new FormData(form); - await this.router.setPageContent(newEngineForm); + const name = formData.get("name") as string; + const version = formData.get("version") as string; - newEngineForm.addEventListener("submit", async (e) => { - e.preventDefault(); - const formData = new FormData(newEngineForm); - - const name = formData.get("name") as string; - const version = formData.get("version") as string; - - try { - await newMount({ - name: name, - type: "kv", - options: { - version: version, - }, - }); - this.state.secretMountType = "kv-v" + version; - this.state.baseMount = name + "/"; - await this.router.changePage("KEY_VALUE_VIEW"); - } catch (e) { - const error = e as Error; - setErrorText(error.message); - } - }); + try { + await newMount({ + name: name, + type: "kv", + options: { + version: version, + }, + }); + this.state.secretMountType = "kv-v" + version; + this.state.baseMount = name + "/"; + await this.router.changePage("KEY_VALUE_VIEW"); + } catch (e) { + const error = e as Error; + setErrorText(error.message); + } + }, + ), + ); } get name(): string { return i18next.t("new_kv_engine_title"); diff --git a/src/pages/Secrets/NewEngines/NewTOTPEngine.ts b/src/pages/Secrets/NewEngines/NewTOTPEngine.ts index 5af2a84..49aa680 100644 --- a/src/pages/Secrets/NewEngines/NewTOTPEngine.ts +++ b/src/pages/Secrets/NewEngines/NewTOTPEngine.ts @@ -1,3 +1,4 @@ +import { Form } from "../../../elements/Form"; import { Margin } from "../../../elements/Margin"; import { Page } from "../../../types/Page"; import { makeElement } from "z-makeelement"; @@ -13,58 +14,55 @@ export class NewTOTPEnginePage extends Page { await this.router.changePage("SECRETS_HOME"); } async render(): Promise { - const newEngineForm = makeElement({ - tag: "form", - children: [ - Margin( + await this.router.setPageContent( + Form( + [ + Margin( + makeElement({ + tag: "input", + class: ["uk-input", "uk-form-width-medium"], + attributes: { + required: "true", + type: "text", + placeholder: i18next.t("new_totp_engine_name_input"), + name: "name", + }, + }), + ), makeElement({ - tag: "input", - class: ["uk-input", "uk-form-width-medium"], + tag: "p", + id: "errorText", + class: "uk-text-danger", + }), + makeElement({ + tag: "button", + class: ["uk-button", "uk-button-primary"], + text: i18next.t("new_totp_engine_create_btn"), attributes: { - required: "true", - type: "text", - placeholder: i18next.t("new_totp_engine_name_input"), - name: "name", + type: "submit", }, }), - ), - makeElement({ - tag: "p", - id: "errorText", - class: "uk-text-danger", - }), - makeElement({ - tag: "button", - class: ["uk-button", "uk-button-primary"], - text: i18next.t("new_totp_engine_create_btn"), - attributes: { - type: "submit", - }, - }), - ], - }) as HTMLFormElement; + ], + async (form: HTMLFormElement) => { + const formData = new FormData(form); - await this.router.setPageContent(newEngineForm); + const name = formData.get("name") as string; - newEngineForm.addEventListener("submit", async (e) => { - e.preventDefault(); - const formData = new FormData(newEngineForm); - - const name = formData.get("name") as string; - - try { - await newMount({ - name: name, - type: "totp", - }); - this.state.secretMountType = "totp"; - this.state.baseMount = name + "/"; - await this.router.changePage("TOTP"); - } catch (e) { - const error = e as Error; - setErrorText(error.message); - } - }); + try { + await newMount({ + name: name, + type: "totp", + }); + this.state.secretMountType = "totp"; + this.state.baseMount = name + "/"; + await this.router.changePage("TOTP"); + } catch (e) { + const error = e as Error; + setErrorText(error.message); + } + }, + ), + ); } get name(): string { return i18next.t("new_totp_engine_title"); diff --git a/src/pages/Secrets/NewEngines/NewTransitEngine.ts b/src/pages/Secrets/NewEngines/NewTransitEngine.ts index c038b2d..3f269e5 100644 --- a/src/pages/Secrets/NewEngines/NewTransitEngine.ts +++ b/src/pages/Secrets/NewEngines/NewTransitEngine.ts @@ -1,3 +1,4 @@ +import { Form } from "../../../elements/Form"; import { Margin } from "../../../elements/Margin"; import { Page } from "../../../types/Page"; import { makeElement } from "z-makeelement"; @@ -13,58 +14,55 @@ export class NewTransitEnginePage extends Page { await this.router.changePage("SECRETS_HOME"); } async render(): Promise { - const newEngineForm = makeElement({ - tag: "form", - children: [ - Margin( + await this.router.setPageContent( + Form( + [ + Margin( + makeElement({ + tag: "input", + class: ["uk-input", "uk-form-width-medium"], + attributes: { + required: "true", + type: "text", + placeholder: i18next.t("new_transit_engine_name_input"), + name: "name", + }, + }), + ), makeElement({ - tag: "input", - class: ["uk-input", "uk-form-width-medium"], + tag: "p", + id: "errorText", + class: "uk-text-danger", + }), + makeElement({ + tag: "button", + class: ["uk-button", "uk-button-primary"], + text: i18next.t("new_transit_engine_create_btn"), attributes: { - required: "true", - type: "text", - placeholder: i18next.t("new_transit_engine_name_input"), - name: "name", + type: "submit", }, }), - ), - makeElement({ - tag: "p", - id: "errorText", - class: "uk-text-danger", - }), - makeElement({ - tag: "button", - class: ["uk-button", "uk-button-primary"], - text: i18next.t("new_transit_engine_create_btn"), - attributes: { - type: "submit", - }, - }), - ], - }) as HTMLFormElement; + ], + async (form: HTMLFormElement) => { + const formData = new FormData(form); - await this.router.setPageContent(newEngineForm); + const name = formData.get("name") as string; - newEngineForm.addEventListener("submit", async (e) => { - e.preventDefault(); - const formData = new FormData(newEngineForm); - - const name = formData.get("name") as string; - - try { - await newMount({ - name: name, - type: "transit", - }); - this.state.secretMountType = "transit"; - this.state.baseMount = name + "/"; - await this.router.changePage("TRANSIT_VIEW"); - } catch (e) { - const error = e as Error; - setErrorText(error.message); - } - }); + try { + await newMount({ + name: name, + type: "transit", + }); + this.state.secretMountType = "transit"; + this.state.baseMount = name + "/"; + await this.router.changePage("TRANSIT_VIEW"); + } catch (e) { + const error = e as Error; + setErrorText(error.message); + } + }, + ), + ); } get name(): string { return i18next.t("new_transit_engine_title"); diff --git a/src/pages/Secrets/TOTP/NewTOTP.ts b/src/pages/Secrets/TOTP/NewTOTP.ts index 7050a5f..4864dde 100644 --- a/src/pages/Secrets/TOTP/NewTOTP.ts +++ b/src/pages/Secrets/TOTP/NewTOTP.ts @@ -1,3 +1,4 @@ +import { Form } from "../../../elements/Form"; import { Margin } from "../../../elements/Margin"; import { MarginInline } from "../../../elements/MarginInline"; import { Page } from "../../../types/Page"; @@ -24,86 +25,83 @@ export class NewTOTPPage extends Page { await this.router.changePage("TOTP"); } async render(): Promise { - const totpForm = makeElement({ - tag: "form", - children: [ - Margin( + await this.router.setPageContent( + Form( + [ + Margin( + makeElement({ + tag: "input", + class: ["uk-input", "uk-form-width-medium"], + attributes: { + required: "true", + type: "text", + placeholder: i18next.t("totp_new_name_text"), + name: "name", + }, + }), + ), makeElement({ - tag: "input", - class: ["uk-input", "uk-form-width-medium"], - attributes: { - required: "true", - type: "text", - placeholder: i18next.t("totp_new_name_text"), - name: "name", - }, + tag: "p", + text: i18next.t("totp_new_info"), }), - ), - makeElement({ - tag: "p", - text: i18next.t("totp_new_info"), - }), - Margin( + Margin( + makeElement({ + tag: "input", + class: ["uk-input", "uk-form-width-medium"], + attributes: { + type: "text", + placeholder: i18next.t("totp_new_uri_input"), + name: "uri", + }, + }), + ), + Margin( + makeElement({ + tag: "input", + class: ["uk-input", "uk-form-width-medium"], + attributes: { + type: "text", + placeholder: i18next.t("totp_new_key_input"), + name: "key", + }, + }), + ), makeElement({ - tag: "input", - class: ["uk-input", "uk-form-width-medium"], - attributes: { - type: "text", - placeholder: i18next.t("totp_new_uri_input"), - name: "uri", - }, + tag: "p", + id: "errorText", + class: "uk-text-danger", }), - ), - Margin( - makeElement({ - tag: "input", - class: ["uk-input", "uk-form-width-medium"], - attributes: { - type: "text", - placeholder: i18next.t("totp_new_key_input"), - name: "key", - }, - }), - ), - makeElement({ - tag: "p", - id: "errorText", - class: "uk-text-danger", - }), - MarginInline( - makeElement({ - tag: "button", - class: ["uk-button", "uk-button-primary"], - text: i18next.t("totp_new_add_btn"), - attributes: { - type: "submit", - }, - }), - ), - ], - }) as HTMLFormElement; - await this.router.setPageContent(totpForm); + MarginInline( + makeElement({ + tag: "button", + class: ["uk-button", "uk-button-primary"], + text: i18next.t("totp_new_add_btn"), + attributes: { + type: "submit", + }, + }), + ), + ], + async (form: HTMLFormElement) => { + const formData = new FormData(form); - totpForm.addEventListener("submit", async (e) => { - e.preventDefault(); + const parms = { + url: formData.get("uri") as string, + key: removeDashSpaces(formData.get("key") as string).toUpperCase(), + name: formData.get("name") as string, + generate: false, + }; - const formData = new FormData(totpForm); - - const parms = { - url: formData.get("uri") as string, - key: removeDashSpaces(formData.get("key") as string).toUpperCase(), - name: formData.get("name") as string, - generate: false, - }; - - try { - await addNewTOTP(this.state.baseMount, parms); - await this.router.changePage("TOTP"); - } catch (e: unknown) { - const error = e as Error; - setErrorText(`API Error: ${error.message}`); - } - }); + try { + await addNewTOTP(this.state.baseMount, parms); + await this.router.changePage("TOTP"); + } catch (e: unknown) { + const error = e as Error; + setErrorText(`API Error: ${error.message}`); + } + }, + ), + ); } async getPageTitle(): Promise { diff --git a/src/pages/Secrets/Transit/NewTransitKey.ts b/src/pages/Secrets/Transit/NewTransitKey.ts index 8d9d0c4..bb86006 100644 --- a/src/pages/Secrets/Transit/NewTransitKey.ts +++ b/src/pages/Secrets/Transit/NewTransitKey.ts @@ -1,3 +1,4 @@ +import { Form } from "../../../elements/Form"; import { Margin } from "../../../elements/Margin"; import { Option } from "../../../elements/Option"; import { Page } from "../../../types/Page"; @@ -12,79 +13,76 @@ export class NewTransitKeyPage extends Page { super(); } async render(): Promise { - const newTransitKeyForm = makeElement({ - tag: "form", - children: [ - Margin( + await this.router.setPageContent( + Form( + [ + Margin( + makeElement({ + tag: "input", + class: ["uk-input", "uk-form-width-medium"], + attributes: { + required: "true", + type: "text", + placeholder: i18next.t("transit_new_key_name_input"), + name: "name", + }, + }), + ), + Margin( + makeElement({ + tag: "select", + class: ["uk-select", "uk-form-width-medium"], + attributes: { + name: "type", + }, + children: [ + "aes128-gcm96", + "aes256-gcm96", + "chacha20-poly1305", + "ed25519", + "ecdsa-p256", + "ecdsa-p384", + "ecdsa-p521", + "rsa-2048", + "rsa-3072", + "rsa-4096", + ].map((type): HTMLElement => Option(type, type)), + }), + ), makeElement({ - tag: "input", - class: ["uk-input", "uk-form-width-medium"], + tag: "p", + id: "errorText", + class: "uk-text-danger", + }), + makeElement({ + tag: "button", + class: ["uk-button", "uk-button-primary"], + text: i18next.t("transit_new_key_create_btn"), attributes: { - required: "true", - type: "text", - placeholder: i18next.t("transit_new_key_name_input"), - name: "name", + type: "submit", }, }), - ), - Margin( - makeElement({ - tag: "select", - class: ["uk-select", "uk-form-width-medium"], - attributes: { - name: "type", - }, - children: [ - "aes128-gcm96", - "aes256-gcm96", - "chacha20-poly1305", - "ed25519", - "ecdsa-p256", - "ecdsa-p384", - "ecdsa-p521", - "rsa-2048", - "rsa-3072", - "rsa-4096", - ].map((type): HTMLElement => Option(type, type)), - }), - ), - makeElement({ - tag: "p", - id: "errorText", - class: "uk-text-danger", - }), - makeElement({ - tag: "button", - class: ["uk-button", "uk-button-primary"], - text: i18next.t("transit_new_key_create_btn"), - attributes: { - type: "submit", - }, - }), - ], - }) as HTMLFormElement; + ], + async (form: HTMLFormElement) => { + const formData = new FormData(form); - await this.router.setPageContent(newTransitKeyForm); + const name = formData.get("name") as string; + const type = formData.get("type") as string; - newTransitKeyForm.addEventListener("submit", async (e) => { - e.preventDefault(); - const formData = new FormData(newTransitKeyForm); - - const name = formData.get("name") as string; - const type = formData.get("type") as string; - - try { - await newTransitKey(this.state.baseMount, { - name: name, - type: type, - }); - this.state.secretItem = name; - await this.router.changePage("TRANSIT_VIEW_SECRET"); - } catch (e) { - const error = e as Error; - setErrorText(error.message); - } - }); + try { + await newTransitKey(this.state.baseMount, { + name: name, + type: type, + }); + this.state.secretItem = name; + await this.router.changePage("TRANSIT_VIEW_SECRET"); + } catch (e) { + const error = e as Error; + setErrorText(error.message); + } + }, + ), + ); } async getPageTitle(): Promise { diff --git a/src/pages/Secrets/Transit/TransitDecrypt.ts b/src/pages/Secrets/Transit/TransitDecrypt.ts index e13b8d9..fb638c0 100644 --- a/src/pages/Secrets/Transit/TransitDecrypt.ts +++ b/src/pages/Secrets/Transit/TransitDecrypt.ts @@ -1,5 +1,6 @@ import { CopyableModal } from "../../../elements/CopyableModal"; import { FileUploadInput } from "../../../elements/FileUploadInput"; +import { Form } from "../../../elements/Form"; import { Margin } from "../../../elements/Margin"; import { Page } from "../../../types/Page"; import { SecretTitleElement } from "../SecretTitleElement"; @@ -21,14 +22,8 @@ export class TransitDecryptPage extends Page { transitDecryptForm: HTMLFormElement; async render(): Promise { - await this.router.setPageContent( - makeElement({ - tag: "div", - }), - ); - this.transitDecryptForm = makeElement({ - tag: "form", - children: [ + this.transitDecryptForm = Form( + [ Margin( makeElement({ tag: "textarea", @@ -73,12 +68,9 @@ export class TransitDecryptPage extends Page { }, }), ], - }) as HTMLFormElement; + async (_) => await this.transitDecryptFormHandler(), + ); await this.router.setPageContent(this.transitDecryptForm); - this.transitDecryptForm.addEventListener("submit", async (e: Event) => { - e.preventDefault(); - await this.transitDecryptFormHandler(); - }); } async transitDecryptFormHandler(): Promise { diff --git a/src/pages/Secrets/Transit/TransitEncrypt.ts b/src/pages/Secrets/Transit/TransitEncrypt.ts index 4a160c4..276e32a 100644 --- a/src/pages/Secrets/Transit/TransitEncrypt.ts +++ b/src/pages/Secrets/Transit/TransitEncrypt.ts @@ -1,5 +1,6 @@ import { CopyableModal } from "../../../elements/CopyableModal"; import { FileUploadInput } from "../../../elements/FileUploadInput"; +import { Form } from "../../../elements/Form"; import { Margin } from "../../../elements/Margin"; import { Page } from "../../../types/Page"; import { SecretTitleElement } from "../SecretTitleElement"; @@ -19,16 +20,9 @@ export class TransitEncryptPage extends Page { } transitEncryptForm: HTMLFormElement; - async render(): Promise { - await this.router.setPageContent( - makeElement({ - tag: "div", - }), - ); - this.transitEncryptForm = makeElement({ - tag: "form", - children: [ + this.transitEncryptForm = Form( + [ Margin( makeElement({ tag: "textarea", @@ -73,13 +67,9 @@ export class TransitEncryptPage extends Page { }, }), ], - }) as HTMLFormElement; + async (_) => await this.transitEncryptFormHandler(), + ); await this.router.setPageContent(this.transitEncryptForm); - - this.transitEncryptForm.addEventListener("submit", async (e: Event) => { - e.preventDefault(); - await this.transitEncryptFormHandler(); - }); } async transitEncryptFormHandler(): Promise { diff --git a/src/pages/Secrets/Transit/TransitRewrap.ts b/src/pages/Secrets/Transit/TransitRewrap.ts index 94e4c87..adc093b 100644 --- a/src/pages/Secrets/Transit/TransitRewrap.ts +++ b/src/pages/Secrets/Transit/TransitRewrap.ts @@ -1,4 +1,5 @@ import { CopyableModal } from "../../../elements/CopyableModal"; +import { Form } from "../../../elements/Form"; import { Margin } from "../../../elements/Margin"; import { Option } from "../../../elements/Option"; import { Page } from "../../../types/Page"; @@ -47,9 +48,8 @@ export class TransitRewrapPage extends Page { }); await this.router.setPageContent(""); - this.transitRewrapForm = makeElement({ - tag: "form", - children: [ + this.transitRewrapForm = Form( + [ makeElement({ tag: "select", name: "version", @@ -80,13 +80,9 @@ export class TransitRewrapPage extends Page { }, }), ], - }) as HTMLFormElement; + async (_) => await this.transitRewrapFormHandler(), + ); await this.router.setPageContent(this.transitRewrapForm); - - this.transitRewrapForm.addEventListener("submit", async (e: Event) => { - e.preventDefault(); - await this.transitRewrapFormHandler(); - }); } async transitRewrapFormHandler(): Promise { diff --git a/src/pages/SetLanguage.ts b/src/pages/SetLanguage.ts index ada13c1..6b69cec 100644 --- a/src/pages/SetLanguage.ts +++ b/src/pages/SetLanguage.ts @@ -1,3 +1,4 @@ +import { Form } from "../elements/Form"; import { Margin } from "../elements/Margin"; import { Page } from "../types/Page"; import { makeElement } from "z-makeelement"; @@ -14,54 +15,52 @@ export class SetLanguagePage extends Page { super(); } async render(): Promise { - const setLanguageForm = makeElement({ - tag: "form", - id: "setLanguageForm", - children: [ - Margin( - makeElement({ - tag: "select", - class: ["uk-select", "uk-form-width-large"], - attributes: { - name: "language", - }, - children: languageIDs.map(function (languageID) { - return makeElement({ - tag: "option", - text: i18next.getFixedT(languageID, null)("language_name"), - attributes: { value: languageID }, - }); + await this.router.setPageContent( + Form( + [ + Margin( + makeElement({ + tag: "select", + class: ["uk-select", "uk-form-width-large"], + attributes: { + name: "language", + }, + children: languageIDs.map(function (languageID) { + return makeElement({ + tag: "option", + text: i18next.getFixedT(languageID, null)("language_name"), + attributes: { value: languageID }, + }); + }), }), + ), + makeElement({ + tag: "p", + id: "errorText", + class: "uk-text-danger", }), - ), - makeElement({ - tag: "p", - id: "errorText", - class: "uk-text-danger", - }), - makeElement({ - tag: "button", - class: ["uk-button", "uk-button-primary"], - text: i18next.t("set_language_btn"), - attributes: { - type: "submit", - }, - }), - ], - }) as HTMLFormElement; - await this.router.setPageContent(setLanguageForm); - setLanguageForm.addEventListener("submit", async (e) => { - e.preventDefault(); - const formData = new FormData(setLanguageForm); + makeElement({ + tag: "button", + class: ["uk-button", "uk-button-primary"], + text: i18next.t("set_language_btn"), + attributes: { + type: "submit", + }, + }), + ], + async (form: HTMLFormElement) => { + const formData = new FormData(form); - const language = formData.get("language") as string; - this.state.language = language; + const language = formData.get("language") as string; + this.state.language = language; - const t = await i18next.changeLanguage(language); - this.state.pageDirection = t("language_direction"); - reloadNavBar(this.router); - await this.router.changePage("HOME"); - }); + const t = await i18next.changeLanguage(language); + this.state.pageDirection = t("language_direction"); + reloadNavBar(this.router); + await this.router.changePage("HOME"); + }, + ), + ); } get name(): string { return i18next.t("set_language_title"); diff --git a/src/pages/SetVaultURL.ts b/src/pages/SetVaultURL.ts index d1b3d91..0ae1d56 100644 --- a/src/pages/SetVaultURL.ts +++ b/src/pages/SetVaultURL.ts @@ -1,3 +1,4 @@ +import { Form } from "../elements/Form"; import { Page } from "../types/Page"; import { makeElement } from "z-makeelement"; @@ -7,10 +8,8 @@ export class SetVaultURLPage extends Page { } async render(): Promise { await this.router.setPageContent( - makeElement({ - tag: "form", - id: "setVaultURLForm", - children: [ + Form( + [ makeElement({ tag: "div", class: "uk-margin", @@ -39,14 +38,13 @@ export class SetVaultURLPage extends Page { }, }), ], - }), + async (form: HTMLFormElement) => { + const formData = new FormData(form); + this.state.apiURL = formData.get("vaultURL") as string; + await this.router.changePage("HOME"); + }, + ), ); - document.getElementById("setVaultURLForm").addEventListener("submit", async (e) => { - e.preventDefault(); - const formData = new FormData(document.querySelector("#setVaultURLForm")); - this.state.apiURL = formData.get("vaultURL") as string; - await this.router.changePage("HOME"); - }); } get name(): string { return "Set Vault URL"; diff --git a/src/pages/Unseal.ts b/src/pages/Unseal.ts index 2944044..a94a842 100644 --- a/src/pages/Unseal.ts +++ b/src/pages/Unseal.ts @@ -1,3 +1,4 @@ +import { Form } from "../elements/Form"; import { MarginInline } from "../elements/MarginInline"; import { Page } from "../types/Page"; import { QRScanner, QRScannerType } from "../elements/QRScanner"; @@ -118,9 +119,8 @@ export class UnsealPage extends Page { } makeUnsealForm(): void { - this.unsealKeyForm = makeElement({ - tag: "form", - children: [ + this.unsealKeyForm = Form( + [ MarginInline( makeElement({ tag: "input", @@ -141,12 +141,11 @@ export class UnsealPage extends Page { }), ), ], - }) as HTMLFormElement; + async (_) => { + await this.handleKeySubmit(); + }, + ); this.unsealInputContent.appendChild(this.unsealKeyForm); - this.unsealKeyForm.addEventListener("submit", async (e: Event) => { - e.preventDefault(); - await this.handleKeySubmit(); - }); } async makeQRInput(): Promise {