1
0
Fork 0

Add typing to CopyableInputBox.ts.

This commit is contained in:
Kitteh 2021-05-08 00:28:31 +01:00
parent 1b1a7a1d2d
commit 73cc78125f
5 changed files with 32 additions and 9 deletions

View file

@ -4,8 +4,12 @@ import { makeElement } from "../htmlUtils";
import ClipboardJS from "clipboard"; import ClipboardJS from "clipboard";
import i18next from "i18next"; import i18next from "i18next";
export function CopyableInputBox(text, copyable = true) { interface CopyableInputBoxType extends HTMLElement {
let inputBoxDiv = makeElement({ tag: "div" }); setText(text: string): void;
}
export function CopyableInputBox(text: string, copyable = true): CopyableInputBoxType {
const inputBoxDiv = (makeElement({ tag: "div" }) as CopyableInputBoxType);
let inputBoxCopyButton = null; let inputBoxCopyButton = null;
if (copyable) { if (copyable) {
inputBoxCopyButton = makeElement({ inputBoxCopyButton = makeElement({
@ -17,26 +21,26 @@ export function CopyableInputBox(text, copyable = true) {
"aria-label": i18next.t("copy_input_box_copy_icon_text") "aria-label": i18next.t("copy_input_box_copy_icon_text")
}, },
thenRun: (e) => { thenRun: (e) => {
let clipboard = new ClipboardJS(e); const clipboard = new ClipboardJS(e);
addClipboardNotifications(clipboard, 600); addClipboardNotifications(clipboard, 600);
} }
}); });
} }
let inputBoxInput = makeElement({ const inputBoxInput = makeElement({
tag: "input", tag: "input",
class: ["uk-input"], class: ["uk-input"],
attributes: { "readonly": true, "type": "text" }, attributes: { "readonly": true, "type": "text" },
}); });
let inputBoxInner = MarginInline([ const inputBoxInner = MarginInline([
inputBoxCopyButton, inputBoxCopyButton,
inputBoxInput inputBoxInput
]); ]);
inputBoxDiv.appendChild(inputBoxInner); inputBoxDiv.appendChild(inputBoxInner);
inputBoxDiv.setText = function (text) { inputBoxDiv.setText = function (text) {
inputBoxInput.value = `${text}`; (inputBoxInput as HTMLInputElement).value = `${text}`;
if (copyable) { if (copyable) {
inputBoxCopyButton.dataset.clipboardText = `${text}`; inputBoxCopyButton.dataset.clipboardText = `${text}`;
} }

19
src/elements/renewSelf.ts Normal file
View file

@ -0,0 +1,19 @@
import { appendAPIURL, getHeaders } from "../api/apiUtils";
export async function renewSelf(): Promise<void> {
const request = new Request(appendAPIURL("/v1/auth/token/renew-self"), {
method: 'POST',
headers: {
...getHeaders(),
'Content-Type': 'application/json'
},
body: JSON.stringify({})
});
return fetch(request).then(response => {
return response.json();
}).then(data => {
if ("errors" in data) {
throw new Error(data.errors[0]);
}
});
}

View file

@ -1,4 +1,4 @@
import { CopyableInputBox } from "../../elements/CopyableInputBox.js"; import { CopyableInputBox } from "../../elements/CopyableInputBox";
import { Page } from "../../types/Page"; import { Page } from "../../types/Page";
import { changePage, setPageContent, setTitleElement } from "../../pageUtils"; import { changePage, setPageContent, setTitleElement } from "../../pageUtils";
import { getCapabilities } from "../../api/getCapabilities"; import { getCapabilities } from "../../api/getCapabilities";

View file

@ -1,4 +1,4 @@
import { CopyableInputBox } from "../elements/CopyableInputBox.js"; import { CopyableInputBox } from "../elements/CopyableInputBox";
import { Margin } from "../elements/Margin.js"; import { Margin } from "../elements/Margin.js";
import { Page } from "../types/Page"; import { Page } from "../types/Page";
import { makeElement } from "../htmlUtils"; import { makeElement } from "../htmlUtils";

View file

@ -1,4 +1,4 @@
import { CopyableInputBox } from "../../elements/CopyableInputBox.js"; import { CopyableInputBox } from "../../elements/CopyableInputBox";
import { DoesNotExistError } from "../../types/internalErrors"; import { DoesNotExistError } from "../../types/internalErrors";
import { Page } from "../../types/Page"; import { Page } from "../../types/Page";
import { changePage, setErrorText, setPageContent, setTitleElement } from "../../pageUtils"; import { changePage, setErrorText, setPageContent, setTitleElement } from "../../pageUtils";