10 Commits

Author SHA1 Message Date
47d7b7ba02 Added toast and copy to clipboard functionality 2023-08-27 20:26:25 -04:00
1a00388229 Updated tests 2023-08-27 12:08:21 -04:00
7dfd190969 Merge pull request #3 from enxoco/features/timer
Features/timer
2023-08-27 12:04:13 -04:00
147717f130 Updated timer logic 2023-08-27 12:03:44 -04:00
9a89dc5365 Revert "Refactored to show correct time. Still needs to be made dynamic"
This reverts commit 21b6f0609e.
2023-08-27 11:16:13 -04:00
21b6f0609e Refactored to show correct time. Still needs to be made dynamic 2023-08-27 10:51:06 -04:00
8e349ca307 Merge pull request #2 from enxoco/feature/totp-generator
Added totp-generator dependency
2023-08-25 21:58:18 -04:00
5f92144e0b Merge branch 'master' into feature/totp-generator 2023-08-25 21:58:11 -04:00
4e5bced49a Added totp-generator dependency 2023-08-25 21:56:39 -04:00
0dcee9190e Merge pull request #1 from enxoco/features/vitest
Added vitest and broke up components
2023-08-25 21:44:39 -04:00
8 changed files with 111 additions and 22 deletions

View File

@ -1,8 +1,8 @@
import "./style.css"; import "./style.css";
import { tokenList } from "./tokenList"; import { tokenList } from "./tokenList";
document.querySelector<HTMLDivElement>("#app")!.innerHTML = ` import rootDiv from "./utils/root";
rootDiv!.innerHTML = `
${tokenList()} ${tokenList()}
`; `;
// setupTokenList(document.querySelector<HTMLDivElement>("#tokens")!);

View File

@ -41,6 +41,10 @@ h1 {
margin: 0 auto; margin: 0 auto;
padding: 2rem; padding: 2rem;
text-align: center; text-align: center;
display: flex;
align-items: center;
justify-content: center;
flex-direction: column-reverse;
} }
.card { .card {
@ -81,14 +85,16 @@ button:focus-visible {
.fieldset-wrapper, form { .fieldset-wrapper, form {
display: flex; display: flex;
flex: 1; flex: 1;
} }
form { form {
flex-direction: column; flex-direction: column;
} }
.fieldset-wrapper { .fieldset-wrapper {
border: 1px solid; border: 1px solid gainsboro;
padding: 5px; padding: 5px;
border-bottom: 1px solid; border-radius: 4px;
box-shadow: 1px 0px 6px 2px gainsboro;
} }
fieldset { fieldset {
@ -96,8 +102,30 @@ fieldset {
flex: 1; flex: 1;
font-weight: bold; font-weight: bold;
border: none; border: none;
min-width: 200px;
} }
label { label {
font-weight: 300; font-weight: 300;
}
.timer {
width: 24px;
height: 24px;
border-radius: 24px;
padding: 8px;
display: flex;
justify-content: center;
align-items: center;
border: 1px solid;
}
.toast {
background-color: lightseagreen;
padding: 10px;
position: relative;
top: 10px;
right: 10px;
color: white;
font-weight: bold;
} }

14
src/toast.ts Normal file
View File

@ -0,0 +1,14 @@
export function toast(element: HTMLDivElement, message: string) {
// Target our predefined DIV that will hold toast messages.
const toastDiv = element.getElementsByClassName('toast')
// If we currently have a toast displayed, let's remove it from the DOM.
if (toastDiv && toastDiv.length != 0) {
for (const el of toastDiv){
el.remove()
}
}
// Finally add our toast message.
element.innerHTML += `<div class='toast'>${message}</div>`
}

View File

@ -1,6 +1,6 @@
// @vitest-environment jsdom // @vitest-environment jsdom
import { assert, expect, test } from 'vitest' import { expect, test } from 'vitest'
import {displayToken} from './token' import {displayToken} from './token'
test('a 6 digit token is displayed', () => { test('a 6 digit token is displayed', () => {

View File

@ -1,3 +1,8 @@
export function tokenList(){ import { displayTokenListItem } from "./TokenListItem";
return `<form id="tokens"></form>` import { tokens } from "./tokens";
}
export function tokenList() {
return `<form id="tokens">${tokens.map((token) =>
displayTokenListItem(token.account, token.secret)
)}</form>`;
}

View File

@ -1,12 +1,11 @@
// @vitest-environment jsdom // @vitest-environment jsdom
import { expect, test } from 'vitest' import { expect, test } from "vitest";
import { tokenListitem } from './tokenListItem' import { displayTokenListItem } from "./tokenListItem";
test("displays correct account name", () => {
test('displays correct account name', () => { const div: HTMLDivElement = document.createElement("div");
const div: HTMLDivElement = document.createElement('div') displayTokenListItem("Github", "ABCDEFGHIJKLMNOP");
tokenListitem('Github', 'ABCDEFGHIJKLMNOP', div); expect(div.innerHTML).toContain("Github");
expect(div.innerHTML).toContain('Github') });
})

View File

@ -1,14 +1,51 @@
import { toast } from "./toast";
import { displayToken } from "./token";
import rootDiv from "./utils/root"
let secondsSinceEpoch: number;
let secondsSinceStart: number = 0;
let secondsRemaining: number = 30;
const period: 30 | 60 = 30;
let token: string;
export function tokenListitem(account: string, secret: string, element: HTMLDivElement){ export function displayTokenListItem(
account: string,
secret: string,
) {
function countdown() {
secondsSinceEpoch = Math.ceil(Date.now() / 1000) - 1;
secondsSinceStart = 0 + (secondsSinceEpoch % period);
secondsRemaining = period - (secondsSinceEpoch % period);
const timerDiv = document.getElementById(`timer-${account}`);
const tokenDiv = document.getElementById(`secret-${account}`);
element.innerHTML += `<div class="fieldset-wrapper"> if (timerDiv && tokenDiv && rootDiv) {
timerDiv.innerHTML = secondsRemaining.toString();
timerDiv.style.background = `conic-gradient(transparent ${
(100 / 30) * secondsSinceStart
}%, ${secondsRemaining < 10 ? "salmon" : "lightgreen"} 0)`;
token = displayToken(secret);
tokenDiv.innerHTML = token;
tokenDiv.addEventListener("click", () => {
navigator.clipboard.writeText(tokenDiv.innerHTML);
toast(rootDiv!, `${tokenDiv.innerHTML} Copied successfully`)
});
}
}
setInterval(() => {
countdown();
}, 1000);
return (`<div class="fieldset-wrapper">
<fieldset> <fieldset>
<label>Account</label> <label>Account</label>
<p data-test-id="account">${account}</p> <p data-test-id="account">${account}</p>
</fieldset> </fieldset>
<fieldset> <fieldset>
<label>Code</label> <label>Code</label>
<p data-test-id="secret">${token}</p> <p id="secret-${account}"></p>
</fieldset> </fieldset>
</div>`; <fieldset>
} <label></label>
<p><div class="timer" id="timer-${account}">
</div></p>
</div>`);
}

6
src/utils/root.ts Normal file
View File

@ -0,0 +1,6 @@
const rootDiv = document.querySelector<HTMLDivElement>("#app")
if (!rootDiv) {
const rootDiv = document.createElement('div')
rootDiv.id = 'app'
}
export default rootDiv