1
0
Fork 0

Add tsx syntax to Me.

This commit is contained in:
Kitteh 2021-05-24 12:48:21 +01:00
parent 1ad2209571
commit 8a71f8fd0e
3 changed files with 103 additions and 105 deletions

View file

@ -5,9 +5,5 @@ export async function sealVault(): Promise<void> {
method: "PUT", method: "PUT",
headers: getHeaders(), headers: getHeaders(),
}); });
const resp = await fetch(request); await fetch(request);
const data = (await resp.json()) as { errors?: string[] };
if ("errors" in data) {
throw new Error(data.errors[0]);
}
} }

View file

@ -1,100 +0,0 @@
import { Page } from "../types/Page";
import { addClipboardNotifications, prePageChecks, setErrorText } from "../pageUtils";
import { getCapabilitiesPath } from "../api/sys/getCapabilities";
import { makeElement } from "z-makeelement";
import { renewSelf } from "../api/sys/renewSelf";
import { sealVault } from "../api/sys/sealVault";
import ClipboardJS from "clipboard";
import i18next from "i18next";
export class MePage extends Page {
constructor() {
super();
}
async render(): Promise<void> {
if (!(await prePageChecks(this.router))) return;
await this.router.setPageContent(
makeElement({
tag: "ul",
class: "uk-nav",
children: [
makeElement({
tag: "li",
children: makeElement({
tag: "a",
text: i18next.t("log_out_btn"),
onclick: async () => {
this.state.token = "";
await this.router.changePage("HOME");
},
}),
}),
makeElement({
tag: "li",
children: makeElement({
tag: "a",
text: i18next.t("copy_token_btn"),
attributes: {
"data-clipboard-text": this.state.token,
},
thenRun: (e) => {
const clipboard = new ClipboardJS(e);
addClipboardNotifications(clipboard);
},
}),
}),
makeElement({
tag: "li",
children: makeElement({
tag: "a",
text: i18next.t("renew_lease_btn"),
onclick: async () => {
try {
await renewSelf();
await this.router.changePage("HOME");
} catch (e: unknown) {
const error = e as Error;
setErrorText(error.message);
}
},
}),
}),
makeElement({
tag: "li",
children: makeElement({
tag: "a",
condition: await (async () => {
try {
const caps = await getCapabilitiesPath("sys/seal");
return caps.includes("sudo") && caps.includes("update");
} catch (e) {
return !true;
}
})(),
text: i18next.t("seal_vault_btn"),
onclick: async () => {
await sealVault();
await this.router.changePage("UNSEAL_VAULT");
},
}),
}),
makeElement({
tag: "li",
children: makeElement({
tag: "a",
text: i18next.t("change_language_btn"),
onclick: async () => {
await this.router.changePage("SET_LANGUAGE");
},
}),
}),
],
}),
);
}
get name(): string {
return i18next.t("me_page_title");
}
}

102
src/pages/Me.tsx Normal file
View file

@ -0,0 +1,102 @@
import { Component, JSX, createRef, render } from "preact";
import { Page } from "../types/Page";
import { addClipboardNotifications, prePageChecks, setErrorText } from "../pageUtils";
import { getCapabilitiesPath } from "../api/sys/getCapabilities";
import { renewSelf } from "../api/sys/renewSelf";
import { sealVault } from "../api/sys/sealVault";
import ClipboardJS from "clipboard";
import i18next from "i18next";
export class CopyLink extends Component<{ text: string; data: string }, unknown> {
linkRef = createRef();
componentDidMount(): void {
const clipboard = new ClipboardJS(this.linkRef.current);
addClipboardNotifications(clipboard, 600);
}
render(): JSX.Element {
return (
<a ref={this.linkRef} data-clipboard-text={this.props.data}>
{this.props.text}
</a>
);
}
}
export class MePage extends Page {
constructor() {
super();
}
async render(): Promise<void> {
if (!(await prePageChecks(this.router))) return;
let canSealVault = false;
try {
const caps = await getCapabilitiesPath("sys/seal");
canSealVault = caps.includes("sudo") && caps.includes("update");
} catch (e) {
canSealVault = false;
}
render(
<ul class="uk-nav">
<li>
<a
onClick={async () => {
this.state.token = "";
await this.router.changePage("HOME");
}}
>
{i18next.t("log_out_btn")}
</a>
</li>
<li>
<CopyLink text={i18next.t("copy_token_btn")} data={this.state.token} />
</li>
<li>
<a
onClick={async () => {
try {
await renewSelf();
await this.router.changePage("HOME");
} catch (e: unknown) {
const error = e as Error;
setErrorText(error.message);
}
}}
>
{i18next.t("renew_lease_btn")}
</a>
</li>
{canSealVault && (
<li>
<a
onClick={async () => {
await sealVault();
await this.router.changePage("UNSEAL");
}}
>
{i18next.t("seal_vault_btn")}
</a>
</li>
)}
<li>
<a
onClick={async () => {
await this.router.changePage("SET_LANGUAGE");
}}
>
{i18next.t("change_language_btn")}
</a>
</li>
</ul>,
this.router.pageContentElement,
);
}
get name(): string {
return i18next.t("me_page_title");
}
}