1
0
Fork 0

Add tsx syntax to TransitRewrap.tsx.

This commit is contained in:
Kitteh 2021-05-23 15:34:16 +01:00
parent f53eeb62cc
commit 5b52fd34d6

View file

@ -1,14 +1,14 @@
import { CopyableModal } from "../../../elements/CopyableModal"; import { CopyableModal } from "../../../elements/ReactCopyableModal";
import { Form } from "../../../elements/Form"; import { Form } from "../../../elements/ReactForm";
import { Margin } from "../../../elements/Margin"; import { Margin } from "../../../elements/ReactMargin";
import { Option } from "../../../elements/Option";
import { Page } from "../../../types/Page"; import { Page } from "../../../types/Page";
import { SecretTitleElement } from "../SecretTitleElement"; import { SecretTitleElement } from "../SecretTitleElement";
import { getTransitKey } from "../../../api/transit/getTransitKey"; import { getTransitKey } from "../../../api/transit/getTransitKey";
import { makeElement } from "z-makeelement";
import { objectToMap } from "../../../utils"; import { objectToMap } from "../../../utils";
import { render } from "preact";
import { setErrorText } from "../../../pageUtils"; import { setErrorText } from "../../../pageUtils";
import { transitRewrap } from "../../../api/transit/transitRewrap"; import { transitRewrap } from "../../../api/transit/transitRewrap";
import UIkit from "uikit";
import i18next from "i18next"; import i18next from "i18next";
type versionOption = { version: string; label: string }; type versionOption = { version: string; label: string };
@ -22,8 +22,6 @@ export class TransitRewrapPage extends Page {
await this.router.changePage("TRANSIT_VIEW_SECRET"); await this.router.changePage("TRANSIT_VIEW_SECRET");
} }
transitRewrapForm: HTMLFormElement;
async render(): Promise<void> { async render(): Promise<void> {
const transitKey = await getTransitKey(this.state.baseMount, this.state.secretItem); const transitKey = await getTransitKey(this.state.baseMount, this.state.secretItem);
@ -47,54 +45,49 @@ export class TransitRewrapPage extends Page {
}; };
}); });
await this.router.setPageContent(""); render(
this.transitRewrapForm = Form( <Form onSubmit={async (data) => await this.onSubmit(data)}>
[ <Margin>
makeElement({ <select class="uk-select uk-width-1-2" name="version">
tag: "select", {options.map((option) => (
name: "version", <option label={option.label} value={option.version}>
class: ["uk-select", "uk-width-1-2"], {option.label}
children: options.map((option): HTMLElement => Option(option.label, option.version)), </option>
}), ))}
Margin( </select>
makeElement({ </Margin>
tag: "textarea", <Margin>
class: ["uk-textarea", "uk-width-1-2"], <textarea
attributes: { class="uk-textarea uk-width-1-2"
placeholder: i18next.t("transit_rewrap_input_placeholder"), name="ciphertext"
name: "ciphertext", placeholder={i18next.t("transit_rewrap_input_placeholder")}
}, />
}), </Margin>
), <p class="uk-text-danger" id="errorText" />
makeElement({ <button class="uk-button uk-button-primary" type="submit">
tag: "p", {i18next.t("transit_rewrap_rewrap_btn")}
id: "errorText", </button>
class: "uk-text-danger", <div id="modalAttachmentPoint" />
}), </Form>,
makeElement({ this.router.pageContentElement,
tag: "button",
class: ["uk-button", "uk-button-primary"],
text: i18next.t("transit_rewrap_rewrap_btn"),
attributes: {
type: "submit",
},
}),
],
async (_) => await this.transitRewrapFormHandler(),
); );
await this.router.setPageContent(this.transitRewrapForm);
} }
async transitRewrapFormHandler(): Promise<void> { async onSubmit(data: FormData): Promise<void> {
const formData = new FormData(this.transitRewrapForm);
try { try {
const res = await transitRewrap(this.state.baseMount, this.state.secretItem, { const res = await transitRewrap(this.state.baseMount, this.state.secretItem, {
ciphertext: formData.get("ciphertext") as string, ciphertext: data.get("ciphertext") as string,
key_version: parseInt(formData.get("version") as string, 10), key_version: parseInt(data.get("version") as string, 10),
}); });
const modal = CopyableModal(i18next.t("transit_rewrap_result_modal_title"), res.ciphertext); render(
this.router.pageContentElement.appendChild(modal); <CopyableModal
modal.show(); id="transitResultModal"
name={i18next.t("transit_rewrap_result_modal_title")}
contentString={res.ciphertext}
/>,
document.querySelector("#modalAttachmentPoint"),
);
UIkit.modal(document.querySelector("#transitResultModal")).show();
} catch (e: unknown) { } catch (e: unknown) {
const error = e as Error; const error = e as Error;
setErrorText(`API Error: ${error.message}`); setErrorText(`API Error: ${error.message}`);