From 53720ff7401d3875f11092f4b055a0fdede92ac2 Mon Sep 17 00:00:00 2001 From: realaravinth Date: Fri, 8 Oct 2021 15:24:29 +0530 Subject: [PATCH] frontend linting --- .eslintrc.js | 21 + .github/workflows/linux.yml | 3 + package.json | 4 + src/api/v1/mcaptcha/levels.rs | 14 +- templates/api/v1/routes.ts | 18 +- templates/auth/captcha/index.ts | 4 +- templates/auth/login/ts/index.ts | 34 +- .../auth/register/ts/emailExists.test.ts | 16 +- templates/auth/register/ts/emailExists.ts | 12 +- templates/auth/register/ts/index.ts | 38 +- templates/auth/register/ts/userExists.test.ts | 18 +- templates/auth/register/ts/userExists.ts | 12 +- templates/auth/sudo/getForm.test.ts | 10 +- templates/auth/sudo/index.ts | 4 +- .../additional-data/getAdditionalData.test.ts | 8 +- templates/components/additional-data/index.ts | 6 +- templates/components/clipboard/index.ts | 16 +- templates/components/error/error.test.ts | 20 +- templates/components/error/index.ts | 18 +- templates/components/error/setUpTests.ts | 6 +- templates/components/showPassword/index.ts | 38 +- .../showPassword/showpassword.test.ts | 56 +- templates/index.ts | 62 +- templates/mobile.ts | 20 +- templates/panel/navbar/index.ts | 2 +- templates/panel/notifications/ts/index.ts | 14 +- templates/panel/settings/account/delete.ts | 18 +- templates/panel/settings/index.ts | 54 +- templates/panel/settings/secret/update.ts | 18 +- .../sitekey/add/ts/addLevelButton.test.ts | 10 +- .../panel/sitekey/add/ts/addLevelButton.ts | 56 +- templates/panel/sitekey/add/ts/const.ts | 28 +- templates/panel/sitekey/add/ts/form/index.ts | 27 +- .../add/ts/form/validateDescription.test.ts | 18 +- .../add/ts/form/validateDescription.ts | 8 +- .../add/ts/form/validateDuration.test.ts | 26 +- .../sitekey/add/ts/form/validateDuration.ts | 10 +- templates/panel/sitekey/add/ts/index.ts | 6 +- .../add/ts/levels/getLevelFields.test.ts | 14 +- .../sitekey/add/ts/levels/getLevelFields.ts | 14 +- .../add/ts/levels/getNumLevels.test.ts | 6 +- .../sitekey/add/ts/levels/getNumLevels.ts | 8 +- .../panel/sitekey/add/ts/levels/index.ts | 6 +- .../sitekey/add/ts/levels/levels.test.ts | 14 +- .../sitekey/add/ts/levels/updateLevel.ts | 18 +- .../add/ts/levels/validateLevel.test.ts | 8 +- .../sitekey/add/ts/levels/validateLevel.ts | 8 +- .../sitekey/add/ts/removeLevelButton/index.ts | 38 +- .../removeLevelButton.test.ts | 19 +- .../ts/removeLevelButton/updateDom/index.ts | 24 +- .../removeLevelButton/updateDom/setupTests.ts | 6 +- .../updateDom/updateInputs.test.ts | 16 +- .../updateDom/updateInputs.ts | 12 +- .../updateDom/updateLabel.test.ts | 27 +- .../updateDom/updateLabel.ts | 14 +- .../updateDom/updateLevelGroup.test.ts | 16 +- .../updateDom/updateLevelGroup.ts | 4 +- .../updateDom/updateRemoveButton.test.ts | 14 +- .../updateDom/updateRemoveButton.ts | 4 +- templates/panel/sitekey/add/ts/setupTests.ts | 34 +- templates/panel/sitekey/delete/index.ts | 20 +- templates/panel/sitekey/edit/edit.test.ts | 10 +- templates/panel/sitekey/edit/index.ts | 36 +- templates/panel/sitekey/list/ts/index.ts | 10 +- templates/panel/sitekey/view/ts/index.ts | 2 +- templates/panel/ts/index.ts | 6 +- templates/router.test.ts | 40 +- templates/router.ts | 20 +- templates/setUpTests.ts | 6 +- templates/utils/genJsonPayload.test.ts | 12 +- templates/utils/genJsonPayload.ts | 6 +- templates/utils/getFormUrl.test.ts | 16 +- templates/utils/getFormUrl.ts | 6 +- templates/utils/isBlankString.test.ts | 18 +- templates/utils/isBlankString.ts | 6 +- templates/utils/isNumber.test.ts | 12 +- templates/utils/isNumber.ts | 2 +- templates/utils/lazyElement.ts | 2 +- templates/views/v1/routes.ts | 28 +- templates/widget/index.ts | 2 +- templates/widget/js/const.ts | 41 +- templates/widget/js/fetchPoWConfig.ts | 30 +- templates/widget/js/index.ts | 22 +- templates/widget/js/prove.ts | 38 +- templates/widget/js/sendToParent.ts | 6 +- templates/widget/js/sendWork.ts | 10 +- templates/widget/js/tests/const.test.ts | 42 +- templates/widget/js/tests/setupTests.ts | 22 +- .../widget/js/utils/genJsonPayload.test.ts | 30 - templates/widget/js/utils/genJsonPayload.ts | 23 - yarn.lock | 2234 ++++++++++------- 91 files changed, 2158 insertions(+), 1677 deletions(-) create mode 100644 .eslintrc.js delete mode 100644 templates/widget/js/utils/genJsonPayload.test.ts delete mode 100644 templates/widget/js/utils/genJsonPayload.ts diff --git a/.eslintrc.js b/.eslintrc.js new file mode 100644 index 00000000..6ac5f378 --- /dev/null +++ b/.eslintrc.js @@ -0,0 +1,21 @@ +module.exports = { + env: { + browser: true, + es2021: true, + }, + extends: ["eslint:recommended", "plugin:@typescript-eslint/recommended"], + parser: "@typescript-eslint/parser", + parserOptions: { + ecmaVersion: 12, + sourceType: "module", + }, + plugins: ["@typescript-eslint"], + rules: { + "@typescript-eslint/no-explicit-any": "off", + "@typescript-eslint/ban-types": "off", + indent: ["error", 2], + "linebreak-style": ["error", "unix"], + quotes: ["error", "double"], + semi: ["error", "always"], + }, +}; diff --git a/.github/workflows/linux.yml b/.github/workflows/linux.yml index aa713c26..25260cf8 100644 --- a/.github/workflows/linux.yml +++ b/.github/workflows/linux.yml @@ -80,6 +80,9 @@ jobs: # - name: build frontend # run: make frontend + # + - name: lint frontend + run: yarn lint - name: run tests run: make test diff --git a/package.json b/package.json index b19b5821..807dfe7e 100644 --- a/package.json +++ b/package.json @@ -4,6 +4,7 @@ "version": "1.0.0", "scripts": { "build": "webpack --mode production", + "lint": "yarn run eslint templates", "start": "webpack-dev-server --mode development --progress --color", "test": "jest" }, @@ -12,10 +13,13 @@ "@types/jsdom": "^16.2.10", "@types/node": "^15.0.2", "@types/sinon": "^10.0.0", + "@typescript-eslint/eslint-plugin": "^4.31.2", + "@typescript-eslint/parser": "^4.31.2", "@wasm-tool/wasm-pack-plugin": "^1.4.0", "css-loader": "^5.2.4", "css-minimizer-webpack-plugin": "^2.0.0", "dart-sass": "^1.25.0", + "eslint": "^7.32.0", "jest": "^26.6.3", "jest-fetch-mock": "^3.0.3", "jsdom": "^16.5.3", diff --git a/src/api/v1/mcaptcha/levels.rs b/src/api/v1/mcaptcha/levels.rs index de4efdec..2773a4cc 100644 --- a/src/api/v1/mcaptcha/levels.rs +++ b/src/api/v1/mcaptcha/levels.rs @@ -17,7 +17,7 @@ use actix_identity::Identity; use actix_web::{web, HttpResponse, Responder}; use futures::future::try_join_all; -use libmcaptcha::{defense::Level, DefenseBuilder, master::messages::RemoveCaptcha}; +use libmcaptcha::{defense::Level, master::messages::RemoveCaptcha, DefenseBuilder}; use log::debug; use serde::{Deserialize, Serialize}; @@ -190,8 +190,16 @@ async fn update_levels( } try_join_all(futs).await?; - if let Err(ServiceError::CaptchaError(e)) = data.captcha.remove(RemoveCaptcha(payload.key.clone())).await { - log::error!("Deleting captcha key {} while updating it, error: {:?}", &payload.key, e) + if let Err(ServiceError::CaptchaError(e)) = data + .captcha + .remove(RemoveCaptcha(payload.key.clone())) + .await + { + log::error!( + "Deleting captcha key {} while updating it, error: {:?}", + &payload.key, + e + ) } Ok(HttpResponse::Ok()) } diff --git a/templates/api/v1/routes.ts b/templates/api/v1/routes.ts index 95b17644..26b36f8a 100644 --- a/templates/api/v1/routes.ts +++ b/templates/api/v1/routes.ts @@ -16,15 +16,15 @@ */ const ROUTES = { - registerUser: '/api/v1/signup', - loginUser: '/api/v1/signin', - signoutUser: '/api/v1/signout', - deleteAccount: '/api/v1/account/delete', - usernameExists: '/api/v1/account/username/exists', - emailExists: '/api/v1/account/email/exists', - healthCheck: '/api/v1/meta/health', - buildDetails: '/api/v1/meta/build', - markNotificationRead: '/api/v1/notifications/read', + registerUser: "/api/v1/signup", + loginUser: "/api/v1/signin", + signoutUser: "/api/v1/signout", + deleteAccount: "/api/v1/account/delete", + usernameExists: "/api/v1/account/username/exists", + emailExists: "/api/v1/account/email/exists", + healthCheck: "/api/v1/meta/health", + buildDetails: "/api/v1/meta/build", + markNotificationRead: "/api/v1/notifications/read", }; export default ROUTES; diff --git a/templates/auth/captcha/index.ts b/templates/auth/captcha/index.ts index d9ca71dc..0285bba6 100644 --- a/templates/auth/captcha/index.ts +++ b/templates/auth/captcha/index.ts @@ -14,6 +14,6 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ -import * as lib from 'mcaptcha-glue'; +import * as lib from "mcaptcha-glue"; -export const register = () => lib.init(); +export const register = (): void => lib.init(); diff --git a/templates/auth/login/ts/index.ts b/templates/auth/login/ts/index.ts index d31ef381..88722609 100644 --- a/templates/auth/login/ts/index.ts +++ b/templates/auth/login/ts/index.ts @@ -14,38 +14,38 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ -import {init} from 'mcaptcha-glue'; +import {init} from "mcaptcha-glue"; -import VIEWS from '../../../views/v1/routes'; +import VIEWS from "../../../views/v1/routes"; -import isBlankString from '../../../utils/isBlankString'; -import genJsonPayload from '../../../utils/genJsonPayload'; -import getFormUrl from '../../../utils/getFormUrl'; -import registerShowPassword from '../../../components/showPassword'; -import createError from '../../../components/error/index'; +import isBlankString from "../../../utils/isBlankString"; +import genJsonPayload from "../../../utils/genJsonPayload"; +import getFormUrl from "../../../utils/getFormUrl"; +import registerShowPassword from "../../../components/showPassword"; +import createError from "../../../components/error/index"; //import '../forms.scss'; -export const getPassword = () => { - const passwordElement = document.getElementById('password'); +export const getPassword = (): string | null => { + const passwordElement = document.getElementById("password"); if (passwordElement === null) { - console.debug('Password is null'); + console.debug("Password is null"); return; } return passwordElement.value; }; -const login = async (e: Event) => { +const login = async (e: Event): Promise => { e.preventDefault(); - const loginElement = document.getElementById('login'); + const loginElement = document.getElementById("login"); if (loginElement === null) { - console.debug('login element element is null'); + console.debug("login element element is null"); return; } const login = loginElement.value; - isBlankString(login, 'username', e); + isBlankString(login, "username", e); const password = getPassword(); @@ -65,9 +65,9 @@ const login = async (e: Event) => { } }; -export const index = () => { - const form = document.getElementById('form'); - form.addEventListener('submit', login, true); +export const index = (): void => { + const form = document.getElementById("form"); + form.addEventListener("submit", login, true); registerShowPassword(); init(); }; diff --git a/templates/auth/register/ts/emailExists.test.ts b/templates/auth/register/ts/emailExists.test.ts index 3725a3fb..fe83c016 100644 --- a/templates/auth/register/ts/emailExists.test.ts +++ b/templates/auth/register/ts/emailExists.test.ts @@ -15,13 +15,13 @@ * along with this program. If not, see . */ -import fetchMock from 'jest-fetch-mock'; +import fetchMock from "jest-fetch-mock"; -import emailExists from './emailExists'; +import emailExists from "./emailExists"; -import {mockAlert, getRegistrationFormHtml} from '../../../setUpTests'; +import {mockAlert, getRegistrationFormHtml} from "../../../setUpTests"; -import setup from '../../../components/error/setUpTests'; +import setup from "../../../components/error/setUpTests"; fetchMock.enableMocks(); mockAlert(); @@ -30,14 +30,14 @@ beforeEach(() => { fetchMock.resetMocks(); }); -it('finds exchange', async () => { +it("finds exchange", async () => { fetchMock.mockResponseOnce(JSON.stringify({exists: true})); document.body.innerHTML = getRegistrationFormHtml(); - document.querySelector('body').appendChild(setup()); + document.querySelector("body").appendChild(setup()); - const emailField = document.getElementById('email'); - emailField.setAttribute('value', 'test@a.com'); + const emailField = document.getElementById("email"); + emailField.setAttribute("value", "test@a.com"); expect(await emailExists()).toBe(true); diff --git a/templates/auth/register/ts/emailExists.ts b/templates/auth/register/ts/emailExists.ts index cb714b7f..76816829 100644 --- a/templates/auth/register/ts/emailExists.ts +++ b/templates/auth/register/ts/emailExists.ts @@ -15,15 +15,15 @@ * along with this program. If not, see . */ -import ROUTES from '../../../api/v1/routes'; +import ROUTES from "../../../api/v1/routes"; -import genJsonPayload from '../../../utils/genJsonPayload'; -import createError from '../../../components/error/index'; +import genJsonPayload from "../../../utils/genJsonPayload"; +import createError from "../../../components/error/index"; -const emailExists = async (element?: HTMLInputElement) => { +const emailExists = async (element?: HTMLInputElement): Promise => { let email; if (element === undefined || element === null) { - email = document.getElementById('email'); + email = document.getElementById("email"); } else { email = element; } @@ -37,7 +37,7 @@ const emailExists = async (element?: HTMLInputElement) => { if (res.ok) { const data = await res.json(); if (data.exists) { - email.className += ' form__in-field--warn'; + email.className += " form__in-field--warn"; createError(`Email "${val}" is already used`); return data.exists; } diff --git a/templates/auth/register/ts/index.ts b/templates/auth/register/ts/index.ts index 0376b55d..7610397e 100644 --- a/templates/auth/register/ts/index.ts +++ b/templates/auth/register/ts/index.ts @@ -15,33 +15,33 @@ * along with this program. If not, see . */ -import VIEWS from '../../../views/v1/routes'; +import VIEWS from "../../../views/v1/routes"; -import isBlankString from '../../../utils/isBlankString'; -import genJsonPayload from '../../../utils/genJsonPayload'; +import isBlankString from "../../../utils/isBlankString"; +import genJsonPayload from "../../../utils/genJsonPayload"; -import userExists from './userExists'; -import emailExists from './emailExists'; -import getFormUrl from '../../../utils/getFormUrl'; -import registerShowPassword from '../../../components/showPassword'; -import createError from '../../../components/error/index'; +import userExists from "./userExists"; +import emailExists from "./emailExists"; +import getFormUrl from "../../../utils/getFormUrl"; +import registerShowPassword from "../../../components/showPassword"; +import createError from "../../../components/error/index"; //import '../forms.scss'; -const usernameElement = document.getElementById('username'); -const emailElement = document.getElementById('email'); -const passwordElement = document.getElementById('password'); +const usernameElement = document.getElementById("username"); +const emailElement = document.getElementById("email"); +const passwordElement = document.getElementById("password"); -const registerUser = async (e: Event) => { +const registerUser = async (e: Event): Promise => { e.preventDefault(); const username = usernameElement.value; - isBlankString(username, 'username', e); + isBlankString(username, "username", e); //isBlankString(e);//, username, 'username'); const password = passwordElement.value; const passwordCheckElement = ( - document.getElementById('password-check') + document.getElementById("password-check") ); const passwordCheck = passwordCheckElement.value; if (password != passwordCheck) { @@ -54,7 +54,7 @@ const registerUser = async (e: Event) => { } let email: string | null = emailElement.value; - if (!email.replace(/\s/g, '').length) { + if (!email.replace(/\s/g, "").length) { email = null; } else { exists = await emailExists(); @@ -80,11 +80,11 @@ const registerUser = async (e: Event) => { } }; -export const index = () => { - const form = document.getElementById('form'); - form.addEventListener('submit', registerUser, true); +export const index = (): void => { + const form = document.getElementById("form"); + form.addEventListener("submit", registerUser, true); usernameElement.addEventListener( - 'input', + "input", async () => await userExists(), false, ); diff --git a/templates/auth/register/ts/userExists.test.ts b/templates/auth/register/ts/userExists.test.ts index 153d5b87..19ebd4da 100644 --- a/templates/auth/register/ts/userExists.test.ts +++ b/templates/auth/register/ts/userExists.test.ts @@ -14,13 +14,13 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ -import fetchMock from 'jest-fetch-mock'; +import fetchMock from "jest-fetch-mock"; -import userExists from './userExists'; +import userExists from "./userExists"; -import {mockAlert, getLoginFormHtml} from '../../../setUpTests'; +import {mockAlert, getLoginFormHtml} from "../../../setUpTests"; -import setup from '../../../components/error/setUpTests'; +import setup from "../../../components/error/setUpTests"; fetchMock.enableMocks(); mockAlert(); @@ -29,16 +29,16 @@ beforeEach(() => { fetchMock.resetMocks(); }); -it('finds exchange', async () => { +it("finds exchange", async () => { fetchMock.mockResponseOnce(JSON.stringify({exists: true})); document.body.innerHTML = getLoginFormHtml(); - document.querySelector('body').appendChild(setup()); - const usernameField = document.querySelector('#username'); - usernameField.value = 'test'; + document.querySelector("body").appendChild(setup()); + const usernameField = document.querySelector("#username"); + usernameField.value = "test"; expect(await userExists()).toBe(true); - usernameField.value = 'test'; + usernameField.value = "test"; fetchMock.mockResponseOnce(JSON.stringify({exists: true})); expect(await userExists(usernameField)).toBe(true); diff --git a/templates/auth/register/ts/userExists.ts b/templates/auth/register/ts/userExists.ts index ebb72233..b4ce95f7 100644 --- a/templates/auth/register/ts/userExists.ts +++ b/templates/auth/register/ts/userExists.ts @@ -15,16 +15,16 @@ * along with this program. If not, see . */ -import ROUTES from '../../../api/v1/routes'; +import ROUTES from "../../../api/v1/routes"; -import genJsonPayload from '../../../utils/genJsonPayload'; -import createError from '../../../components/error/index'; +import genJsonPayload from "../../../utils/genJsonPayload"; +import createError from "../../../components/error/index"; -const userExists = async (element?: HTMLInputElement) => { +const userExists = async (element?: HTMLInputElement): Promise => { console.log(element); let username; if (element === undefined) { - username = document.getElementById('username'); + username = document.getElementById("username"); } else { username = element; } @@ -37,7 +37,7 @@ const userExists = async (element?: HTMLInputElement) => { if (res.ok) { const data = await res.json(); if (data.exists) { - username.className += ' form__in-field--warn'; + username.className += " form__in-field--warn"; createError(`Username "${val}" taken`); } return data.exists; diff --git a/templates/auth/sudo/getForm.test.ts b/templates/auth/sudo/getForm.test.ts index 321ca766..49361c14 100644 --- a/templates/auth/sudo/getForm.test.ts +++ b/templates/auth/sudo/getForm.test.ts @@ -15,17 +15,17 @@ * along with this program. If not, see . */ -import form from './index'; +import form from "./index"; -it('sudo form works', () => { +it("sudo form works", () => { try { form.get(); } catch (e) { - expect(e.message).toBe('Element form is undefined'); + expect(e.message).toBe("Element form is undefined"); } - const element = document.createElement('form'); - element.id = 'form'; + const element = document.createElement("form"); + element.id = "form"; document.body.appendChild(element); expect(form.get()).toBe(element); }); diff --git a/templates/auth/sudo/index.ts b/templates/auth/sudo/index.ts index ce8b3109..1d6487bb 100644 --- a/templates/auth/sudo/index.ts +++ b/templates/auth/sudo/index.ts @@ -14,9 +14,9 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ -import LazyElement from '../../utils/lazyElement'; +import LazyElement from "../../utils/lazyElement"; -const ID = 'form'; +const ID = "form"; const FORM = new LazyElement(ID); export default FORM; diff --git a/templates/components/additional-data/getAdditionalData.test.ts b/templates/components/additional-data/getAdditionalData.test.ts index 7bece21e..fa12398e 100644 --- a/templates/components/additional-data/getAdditionalData.test.ts +++ b/templates/components/additional-data/getAdditionalData.test.ts @@ -15,9 +15,9 @@ * along with this program. If not, see . */ -import additionalData from './index'; +import additionalData from "./index"; -it('sudo form works', () => { +it("sudo form works", () => { try { additionalData(); } catch (e) { @@ -26,8 +26,8 @@ it('sudo form works', () => { ); } - const element = document.createElement('div'); - element.id = 'additional-data'; + const element = document.createElement("div"); + element.id = "additional-data"; document.body.appendChild(element); expect(additionalData()).toBe(element); }); diff --git a/templates/components/additional-data/index.ts b/templates/components/additional-data/index.ts index 84aad12d..31f85d86 100644 --- a/templates/components/additional-data/index.ts +++ b/templates/components/additional-data/index.ts @@ -15,9 +15,9 @@ * along with this program. If not, see . */ -const additionalData = () => { +const additionalData = (): HTMLElement => { let element = null; - const ID = 'additional-data'; + const ID = "additional-data"; if (element === null) { element = document.getElementById(ID); @@ -29,7 +29,7 @@ const additionalData = () => { return element; } } else { - element; + return element; } }; diff --git a/templates/components/clipboard/index.ts b/templates/components/clipboard/index.ts index 02ca75e8..8af6aea5 100644 --- a/templates/components/clipboard/index.ts +++ b/templates/components/clipboard/index.ts @@ -23,7 +23,7 @@ class CopyIcon { constructor( writeText: string, copyIcon: HTMLElement, - copyDoneIconClass: string, + copyDoneIconClass: string ) { this.copyIcon = copyIcon; this.copyDoneIconClass = copyDoneIconClass; @@ -32,24 +32,24 @@ class CopyIcon { this.__registerHandlers(); } - __registerHandlers() { - this.copyIcon.addEventListener('click', e => this.copySitekey(e)); + __registerHandlers(): void { + this.copyIcon.addEventListener("click", (e) => this.copySitekey(e)); } /* * Copy secret to clipboard */ - async copySitekey(e: Event) { + async copySitekey(e: Event): Promise { const image = e.target; const copyDoneIcon = ( image.parentElement.querySelector(`.${this.copyDoneIconClass}`) ); await navigator.clipboard.writeText(this.writeText); - image.style.display = 'none'; - copyDoneIcon.style.display = 'block'; + image.style.display = "none"; + copyDoneIcon.style.display = "block"; setTimeout(() => { - copyDoneIcon.style.display = 'none'; - image.style.display = 'block'; + copyDoneIcon.style.display = "none"; + image.style.display = "block"; }, 1200); } } diff --git a/templates/components/error/error.test.ts b/templates/components/error/error.test.ts index 2090464d..ff83cac4 100644 --- a/templates/components/error/error.test.ts +++ b/templates/components/error/error.test.ts @@ -15,16 +15,16 @@ * along with this program. If not, see . */ -import createError from './index'; -import * as e from './index'; +import createError from "./index"; +import * as e from "./index"; -import setup from './setUpTests'; +import setup from "./setUpTests"; -'use strict'; +"use strict"; jest.useFakeTimers(); -it('checks if error boxes work', () => { +it("checks if error boxes work", () => { document.body.append(setup()); const getMsg = (num: number) => `message ${num}`; @@ -32,21 +32,21 @@ it('checks if error boxes work', () => { let msg = document.querySelector(`.${e.ERR_MSG_CONTAINER}`); expect(msg.innerHTML).toContain(getMsg(1)); - let btn = msg.getElementsByClassName(e.ERR_CLOSE)[0]; + const btn = msg.getElementsByClassName(e.ERR_CLOSE)[0]; btn.click(); msg = document.querySelector(`.${e.ERR_MSG_CONTAINER}`); expect(msg).toEqual(null); - const errElement = document.createElement('p'); + const errElement = document.createElement("p"); errElement.appendChild(document.createTextNode(getMsg(2))); createError(errElement); - msg = document.querySelector(`.${e.ERR_MSG_CONTAINER}`).querySelector('p'); + msg = document.querySelector(`.${e.ERR_MSG_CONTAINER}`).querySelector("p"); expect(msg).toEqual(errElement); - let timeOutElement = document.createElement('p'); + const timeOutElement = document.createElement("p"); timeOutElement.appendChild(document.createTextNode(getMsg(2))); createError(timeOutElement, 200); - msg = document.querySelector(`.${e.ERR_MSG_CONTAINER}`).querySelector('p'); + msg = document.querySelector(`.${e.ERR_MSG_CONTAINER}`).querySelector("p"); expect(msg).toEqual(timeOutElement); jest.runOnlyPendingTimers(); msg = document.querySelector(`.${e.ERR_MSG_CONTAINER}`); diff --git a/templates/components/error/index.ts b/templates/components/error/index.ts index 958ef3cf..c6714582 100644 --- a/templates/components/error/index.ts +++ b/templates/components/error/index.ts @@ -15,9 +15,9 @@ * along with this program. If not, see . */ -export const ERR_CONTAINER_ID = 'err__container'; -export const ERR_MSG_CONTAINER = 'err__msg-container'; // class -export const ERR_CLOSE = 'err__close'; // class +export const ERR_CONTAINER_ID = "err__container"; +export const ERR_MSG_CONTAINER = "err__msg-container"; // class +export const ERR_CLOSE = "err__close"; // class export const DEFAULT_LIFETIME = 5000; @@ -41,11 +41,11 @@ const err = () => { const createError = ( message: string | HTMLElement, lifetime: number = DEFAULT_LIFETIME, -) => { - const box = document.createElement('div'); +): void => { + const box = document.createElement("div"); const msg = () => { - if (typeof message === 'string') { + if (typeof message === "string") { return document.createTextNode(message); } else { return message; @@ -55,8 +55,8 @@ const createError = ( box.className = ERR_MSG_CONTAINER; box.appendChild(msg()); - const deleteBtn = document.createElement('button'); - const deleteMsg = document.createTextNode('x'); + const deleteBtn = document.createElement("button"); + const deleteMsg = document.createTextNode("x"); deleteBtn.appendChild(deleteMsg); deleteBtn.className = ERR_CLOSE; box.appendChild(deleteBtn); @@ -71,7 +71,7 @@ const createError = ( box.remove(); }; - deleteBtn.addEventListener('click', e => deleteHandler(e)); + deleteBtn.addEventListener("click", e => deleteHandler(e)); }; export default createError; diff --git a/templates/components/error/setUpTests.ts b/templates/components/error/setUpTests.ts index 9435a286..2ea5fdfa 100644 --- a/templates/components/error/setUpTests.ts +++ b/templates/components/error/setUpTests.ts @@ -14,10 +14,10 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ -import * as e from './index'; +import * as e from "./index"; -const setup = () => { - let x = document.createElement('div'); +const setup = (): HTMLElement => { + const x = document.createElement("div"); x.id = e.ERR_CONTAINER_ID; return x; }; diff --git a/templates/components/showPassword/index.ts b/templates/components/showPassword/index.ts index 0f3b21a0..3bc39554 100644 --- a/templates/components/showPassword/index.ts +++ b/templates/components/showPassword/index.ts @@ -15,12 +15,12 @@ * along with this program. If not, see . */ -const showPasswordButtonClassHidden = 'show-password--hide'; -const showPasswordButtonClassShowing = 'show-password--show'; +const showPasswordButtonClassHidden = "show-password--hide"; +const showPasswordButtonClassShowing = "show-password--show"; -const container = 'show-password-container'; +const container = "show-password-container"; -let display = 'hidden'; +let display = "hidden"; const showPasswordButtons = () => { let buttons: NodeListOf; @@ -49,45 +49,45 @@ const hidePasswordButtons = () => { }; // e is click event from show password container -export const showPassword = () => { - const inputs = document.body.querySelectorAll('input'); +export const showPassword = (): void => { + const inputs = document.body.querySelectorAll("input"); - if (display == 'hidden') { - display = 'show'; + if (display == "hidden") { + display = "show"; inputs.forEach(element => { - if (element.type === 'password') { - element.type = 'text'; + if (element.type === "password") { + element.type = "text"; } }); showPasswordButtons().forEach((button: HTMLInputElement) => { - button.style.display = 'none'; + button.style.display = "none"; }); hidePasswordButtons().forEach((button: HTMLInputElement) => { - button.style.display = 'inline'; + button.style.display = "inline"; }); } else { - display = 'hidden'; + display = "hidden"; inputs.forEach(element => { - if (element.type === 'text' && element.name.includes('password')) { - element.type = 'password'; + if (element.type === "text" && element.name.includes("password")) { + element.type = "password"; } }); showPasswordButtons().forEach((button: HTMLInputElement) => { - button.style.display = 'inline'; + button.style.display = "inline"; }); hidePasswordButtons().forEach((button: HTMLInputElement) => { - button.style.display = 'none'; + button.style.display = "none"; }); } // posibily clicked on something else }; -export const registerShowPassword = () => { +export const registerShowPassword = (): void => { document.querySelectorAll(`.${container}`).forEach(container => { - container.addEventListener('click', showPassword); + container.addEventListener("click", showPassword); }); }; diff --git a/templates/components/showPassword/showpassword.test.ts b/templates/components/showPassword/showpassword.test.ts index aa375f05..686f9889 100644 --- a/templates/components/showPassword/showpassword.test.ts +++ b/templates/components/showPassword/showpassword.test.ts @@ -15,8 +15,8 @@ * along with this program. If not, see . */ -import registerShowPassword from './index'; -import {showPassword} from './index'; +import registerShowPassword from "./index"; +import {showPassword} from "./index"; const initial_content = `
@@ -41,49 +41,49 @@ const initial_content = `
`; -it('show password works', () => { +it("show password works", () => { document.body.innerHTML = initial_content; const container = ( - document.querySelector(`.show-password-container`) + document.querySelector(".show-password-container") ); - const hide = container.querySelector('.show-password--hide'); - const show = container.querySelector('.show-password--show'); - const password = document.getElementById('password'); - show.style.display = 'inline'; - hide.style.display = 'none'; + const hide = container.querySelector(".show-password--hide"); + const show = container.querySelector(".show-password--show"); + const password = document.getElementById("password"); + show.style.display = "inline"; + hide.style.display = "none"; showPassword(); - expect(hide.style.display).toEqual('inline'); - expect(show.style.display).toEqual('none'); - expect(password.type).toEqual('text'); + expect(hide.style.display).toEqual("inline"); + expect(show.style.display).toEqual("none"); + expect(password.type).toEqual("text"); showPassword(); - expect(show.style.display).toEqual('inline'); - expect(hide.style.display).toEqual('none'); - expect(password.type).toEqual('password'); + expect(show.style.display).toEqual("inline"); + expect(hide.style.display).toEqual("none"); + expect(password.type).toEqual("password"); }); -it('show password click works', () => { +it("show password click works", () => { document.body.innerHTML = initial_content; const container = ( - document.querySelector(`.show-password-container`) + document.querySelector(".show-password-container") ); - const hide = container.querySelector('.show-password--hide'); - const show = container.querySelector('.show-password--show'); - const password = document.getElementById('password'); - show.style.display = 'inline'; - hide.style.display = 'none'; + const hide = container.querySelector(".show-password--hide"); + const show = container.querySelector(".show-password--show"); + const password = document.getElementById("password"); + show.style.display = "inline"; + hide.style.display = "none"; registerShowPassword(); container.click(); - expect(hide.style.display).toEqual('inline'); - expect(show.style.display).toEqual('none'); - expect(password.type).toEqual('text'); + expect(hide.style.display).toEqual("inline"); + expect(show.style.display).toEqual("none"); + expect(password.type).toEqual("text"); container.click(); - expect(show.style.display).toEqual('inline'); - expect(hide.style.display).toEqual('none'); - expect(password.type).toEqual('password'); + expect(show.style.display).toEqual("inline"); + expect(hide.style.display).toEqual("none"); + expect(password.type).toEqual("password"); }); diff --git a/templates/index.ts b/templates/index.ts index ecd82520..ce30f589 100644 --- a/templates/index.ts +++ b/templates/index.ts @@ -15,39 +15,39 @@ * along with this program. If not, see . */ -import {Router} from './router'; +import {Router} from "./router"; -import * as login from './auth/login/ts/'; -import * as register from './auth/register/ts/'; -import * as panel from './panel/ts/index'; -import settings from './panel/settings/'; -import * as deleteAccount from './panel/settings/account/delete'; -import * as updateSecret from './panel/settings/secret/update'; -import * as addSiteKey from './panel/sitekey/add/ts'; -import * as editSitekey from './panel/sitekey/edit/'; -import * as deleteSitekey from './panel/sitekey/delete/'; -import * as listSitekeys from './panel/sitekey/list/ts'; -import * as notidications from './panel/notifications/ts'; -import {MODE} from './logger'; -import log from './logger'; +import * as login from "./auth/login/ts/"; +import * as register from "./auth/register/ts/"; +import * as panel from "./panel/ts/index"; +import settings from "./panel/settings/"; +import * as deleteAccount from "./panel/settings/account/delete"; +import * as updateSecret from "./panel/settings/secret/update"; +import * as addSiteKey from "./panel/sitekey/add/ts"; +import * as editSitekey from "./panel/sitekey/edit/"; +import * as deleteSitekey from "./panel/sitekey/delete/"; +import * as listSitekeys from "./panel/sitekey/list/ts"; +import * as notidications from "./panel/notifications/ts"; +import {MODE} from "./logger"; +import log from "./logger"; -import VIEWS from './views/v1/routes'; +import VIEWS from "./views/v1/routes"; -import './main.scss'; -import './auth/css/main.scss'; -import './components/details-footer/main.scss'; -import './components/error/main.scss'; -import './components/showPassword/main.scss'; -import './panel/css/main.scss'; -import './panel/navbar/main.scss'; -import './panel/settings/main.scss'; -import './panel/notifications/main.scss'; -import './panel/header/taskbar/main.scss'; -import './panel/help-banner/main.scss'; -import './panel/sitekey/add/css/main.scss'; -import './panel/sitekey/list/css/main.scss'; +import "./main.scss"; +import "./auth/css/main.scss"; +import "./components/details-footer/main.scss"; +import "./components/error/main.scss"; +import "./components/showPassword/main.scss"; +import "./panel/css/main.scss"; +import "./panel/navbar/main.scss"; +import "./panel/settings/main.scss"; +import "./panel/notifications/main.scss"; +import "./panel/header/taskbar/main.scss"; +import "./panel/help-banner/main.scss"; +import "./panel/sitekey/add/css/main.scss"; +import "./panel/sitekey/list/css/main.scss"; -import './errors/main.scss'; +import "./errors/main.scss"; log.setMode(MODE.production); @@ -62,8 +62,8 @@ router.register(VIEWS.loginUser, login.index); router.register(VIEWS.notifications, notidications.index); router.register(VIEWS.listSitekey, listSitekeys.index); router.register(VIEWS.addSiteKey, addSiteKey.index); -router.register(VIEWS.editSitekey('[A-Z),a-z,0-9]+'), editSitekey.index); -router.register(VIEWS.deleteSitekey('[A-Z),a-z,0-9]+'), deleteSitekey.index); +router.register(VIEWS.editSitekey("[A-Z),a-z,0-9]+"), editSitekey.index); +router.register(VIEWS.deleteSitekey("[A-Z),a-z,0-9]+"), deleteSitekey.index); try { router.route(); diff --git a/templates/mobile.ts b/templates/mobile.ts index 87d68662..76e60c46 100644 --- a/templates/mobile.ts +++ b/templates/mobile.ts @@ -15,13 +15,13 @@ * along with this program. If not, see . */ -import './mobile.scss'; -import './auth/css/mobile.scss'; -import './components/details-footer/mobile.scss'; -import './panel/css/mobile.scss'; -import './panel/settings/mobile.scss'; -import './panel/header/taskbar/mobile.scss'; -import './panel/navbar/mobile.scss'; -import './panel/help-banner/mobile.scss'; -import './panel/sitekey/add/css/mobile.scss'; -import './panel/sitekey/list/css/mobile.scss'; +import "./mobile.scss"; +import "./auth/css/mobile.scss"; +import "./components/details-footer/mobile.scss"; +import "./panel/css/mobile.scss"; +import "./panel/settings/mobile.scss"; +import "./panel/header/taskbar/mobile.scss"; +import "./panel/navbar/mobile.scss"; +import "./panel/help-banner/mobile.scss"; +import "./panel/sitekey/add/css/mobile.scss"; +import "./panel/sitekey/list/css/mobile.scss"; diff --git a/templates/panel/navbar/index.ts b/templates/panel/navbar/index.ts index ec88d740..8da79d7b 100644 --- a/templates/panel/navbar/index.ts +++ b/templates/panel/navbar/index.ts @@ -14,4 +14,4 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ -import './main.scss'; +import "./main.scss"; diff --git a/templates/panel/notifications/ts/index.ts b/templates/panel/notifications/ts/index.ts index 97104ccc..1a684bd2 100644 --- a/templates/panel/notifications/ts/index.ts +++ b/templates/panel/notifications/ts/index.ts @@ -15,13 +15,13 @@ * along with this program. If not, see . */ -import genJsonPayload from '../../../utils/genJsonPayload'; -import createError from '../../../components/error'; +import genJsonPayload from "../../../utils/genJsonPayload"; +import createError from "../../../components/error"; -import ROUTES from '../../../api/v1/routes'; +import ROUTES from "../../../api/v1/routes"; -const BTN = document.querySelectorAll('.notification__mark-read-btn'); -const TABLE_BODY = document.querySelector('.notification__body'); +const BTN = document.querySelectorAll(".notification__mark-read-btn"); +const TABLE_BODY = document.querySelector(".notification__body"); const notification_record = (id: number) => TABLE_BODY.querySelector(`#notification__item-${id}`); @@ -46,10 +46,10 @@ const markRead = async (e: Event) => { const addMarkReadEventListenet = () => { BTN.forEach(btn => { - btn.addEventListener('click', markRead, true); + btn.addEventListener("click", markRead, true); }); }; -export const index = () => { +export const index = (): void => { addMarkReadEventListenet(); }; diff --git a/templates/panel/settings/account/delete.ts b/templates/panel/settings/account/delete.ts index 8bcd3395..09b39f4f 100644 --- a/templates/panel/settings/account/delete.ts +++ b/templates/panel/settings/account/delete.ts @@ -15,15 +15,15 @@ * along with this program. If not, see . */ -import {getPassword} from '../../../auth/login/ts/'; -import FORM from '../../../auth/sudo/'; +import {getPassword} from "../../../auth/login/ts/"; +import FORM from "../../../auth/sudo/"; -import getFormUrl from '../../../utils/getFormUrl'; -import genJsonPayload from '../../../utils/genJsonPayload'; -import createError from '../../../components/error'; -import registerShowPassword from '../../../components/showPassword'; +import getFormUrl from "../../../utils/getFormUrl"; +import genJsonPayload from "../../../utils/genJsonPayload"; +import createError from "../../../components/error"; +import registerShowPassword from "../../../components/showPassword"; -import VIEWS from '../../../views/v1/routes'; +import VIEWS from "../../../views/v1/routes"; const submit = async (e: Event) => { e.preventDefault(); @@ -44,7 +44,7 @@ const submit = async (e: Event) => { } }; -export const index = () => { - FORM.get().addEventListener('submit', submit, true); +export const index = (): void => { + FORM.get().addEventListener("submit", submit, true); registerShowPassword(); }; diff --git a/templates/panel/settings/index.ts b/templates/panel/settings/index.ts index cd18f6c8..75cc6cd3 100644 --- a/templates/panel/settings/index.ts +++ b/templates/panel/settings/index.ts @@ -15,28 +15,28 @@ * along with this program. If not, see . */ -import registerShowPassword from '../../components/showPassword/'; -import CopyIcon from '../../components/clipboard/'; -import createError from '../../components/error/'; +import registerShowPassword from "../../components/showPassword/"; +import CopyIcon from "../../components/clipboard/"; +import createError from "../../components/error/"; -import emailExists from '../../auth/register/ts/emailExists'; -import userExists from '../../auth/register/ts/userExists'; +import emailExists from "../../auth/register/ts/emailExists"; +import userExists from "../../auth/register/ts/userExists"; -import LazyElement from '../../utils/lazyElement'; -import isBlankString from '../../utils/isBlankString'; -import getFormUrl from '../../utils/getFormUrl'; -import genJsonPayload from '../../utils/genJsonPayload'; +import LazyElement from "../../utils/lazyElement"; +import isBlankString from "../../utils/isBlankString"; +import getFormUrl from "../../utils/getFormUrl"; +import genJsonPayload from "../../utils/genJsonPayload"; -import VIEWS from '../../views/v1/routes'; +import VIEWS from "../../views/v1/routes"; -const SECRET_COPY_ICON = 'settings__secret-copy'; -const SECRET_COPY_DONE_ICON = 'settings__secret-copy-done'; +const SECRET_COPY_ICON = "settings__secret-copy"; +const SECRET_COPY_DONE_ICON = "settings__secret-copy-done"; // form IDs -const DELETE_FORM = 'settings__delete-form'; -const EMAIL_FORM = 'settings__email-form'; -const USERNAME_FORM = 'settings__username-form'; -const SECRET_FORM = 'settings__secret-form'; +const DELETE_FORM = "settings__delete-form"; +const EMAIL_FORM = "settings__email-form"; +const USERNAME_FORM = "settings__username-form"; +const SECRET_FORM = "settings__secret-form"; // form elements const deleteForm = new LazyElement(DELETE_FORM); @@ -45,8 +45,8 @@ const usernameForm = new LazyElement(USERNAME_FORM); const secretForm = new LazyElement(SECRET_FORM); // field IDs -const EMAIL = 'email'; -const USERNAME = 'username'; +const EMAIL = "email"; +const USERNAME = "username"; // field elements const emailField = new LazyElement(EMAIL); @@ -57,7 +57,7 @@ const updateEmail = async (e: Event) => { e.preventDefault(); const emailElement = emailField.get(); const email = emailElement.value; - isBlankString(email, 'email', e); + isBlankString(email, "email", e); if (await emailExists(emailElement)) { return; } else { @@ -80,7 +80,7 @@ const updateUsername = async (e: Event) => { e.preventDefault(); const usernameElement = usernameField.get(); const username = usernameElement.value; - isBlankString(username, 'username', e); + isBlankString(username, "username", e); if (await userExists(usernameElement)) { return; } else { @@ -101,7 +101,7 @@ const updateUsername = async (e: Event) => { const updateSecret = (e: Event) => { e.preventDefault(); const msg = - 'WARNING: updating secret will cause service disruption if old secret is still in use post update'; + "WARNING: updating secret will cause service disruption if old secret is still in use post update"; if (confirm(msg)) { window.location.assign(VIEWS.updateSecret); } @@ -118,14 +118,14 @@ const deleteAccount = (e: Event) => { // regist form event handlers const registerForms = () => { - deleteForm.get().addEventListener('submit', e => deleteAccount(e), true); - emailForm.get().addEventListener('submit', e => updateEmail(e), true); - usernameForm.get().addEventListener('submit', e => updateUsername(e), true); + deleteForm.get().addEventListener("submit", (e) => deleteAccount(e), true); + emailForm.get().addEventListener("submit", (e) => updateEmail(e), true); + usernameForm.get().addEventListener("submit", (e) => updateUsername(e), true); console.log(usernameField.get()); usernameField .get() - .addEventListener('input', async () => await userExists(), false); - secretForm.get().addEventListener('submit', e => updateSecret(e), true); + .addEventListener("input", async () => await userExists(), false); + secretForm.get().addEventListener("submit", (e) => updateSecret(e), true); }; // set up copying account secret to clipboard @@ -138,7 +138,7 @@ const initCopySecret = () => { }; /// TODO email update button should only change if email value has been changed -const index = () => { +const index = (): void => { registerShowPassword(); initCopySecret(); registerForms(); diff --git a/templates/panel/settings/secret/update.ts b/templates/panel/settings/secret/update.ts index 292609a7..f0020c53 100644 --- a/templates/panel/settings/secret/update.ts +++ b/templates/panel/settings/secret/update.ts @@ -15,15 +15,15 @@ * along with this program. If not, see . */ -import {getPassword} from '../../../auth/login/ts/'; -import FORM from '../../../auth/sudo/'; +import {getPassword} from "../../../auth/login/ts/"; +import FORM from "../../../auth/sudo/"; -import getFormUrl from '../../../utils/getFormUrl'; -import genJsonPayload from '../../../utils/genJsonPayload'; -import createError from '../../../components/error'; -import registerShowPassword from '../../../components/showPassword'; +import getFormUrl from "../../../utils/getFormUrl"; +import genJsonPayload from "../../../utils/genJsonPayload"; +import createError from "../../../components/error"; +import registerShowPassword from "../../../components/showPassword"; -import VIEWS from '../../../views/v1/routes'; +import VIEWS from "../../../views/v1/routes"; const submit = async (e: Event) => { e.preventDefault(); @@ -44,7 +44,7 @@ const submit = async (e: Event) => { } }; -export const index = () => { - FORM.get().addEventListener('submit', submit, true); +export const index = (): void => { + FORM.get().addEventListener("submit", submit, true); registerShowPassword(); }; diff --git a/templates/panel/sitekey/add/ts/addLevelButton.test.ts b/templates/panel/sitekey/add/ts/addLevelButton.test.ts index 5af3b58e..aba58657 100644 --- a/templates/panel/sitekey/add/ts/addLevelButton.test.ts +++ b/templates/panel/sitekey/add/ts/addLevelButton.test.ts @@ -15,16 +15,16 @@ * along with this program. If not, see . */ -import getNumLevels from './levels/getNumLevels'; -import {getAddForm, trim, addLevel} from './setupTests'; -import setup from '../../../../components/error/setUpTests'; +import getNumLevels from "./levels/getNumLevels"; +import {getAddForm, trim, addLevel} from "./setupTests"; +import setup from "../../../../components/error/setUpTests"; document.body.innerHTML = getAddForm(); document.body.appendChild(setup()); jest.useFakeTimers(); -it('addLevelButton works', () => { +it("addLevelButton works", () => { expect(getNumLevels()).toBe(1); // add a level addLevel(2, 4); @@ -36,7 +36,7 @@ it('addLevelButton works', () => { addLevel(4, 9); expect(getNumLevels()).toBe(3); - let a = document.body.innerHTML; + const a = document.body.innerHTML; expect(trim(a)).toBe(trim(finalHtml())); diff --git a/templates/panel/sitekey/add/ts/addLevelButton.ts b/templates/panel/sitekey/add/ts/addLevelButton.ts index 39ff75f5..6beacf7a 100644 --- a/templates/panel/sitekey/add/ts/addLevelButton.ts +++ b/templates/panel/sitekey/add/ts/addLevelButton.ts @@ -14,16 +14,16 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ -import validateLevel from './levels/validateLevel'; -import getNumLevels from './levels/getNumLevels'; -import * as UpdateLevel from './levels/updateLevel'; +import validateLevel from "./levels/validateLevel"; +import getNumLevels from "./levels/getNumLevels"; +import * as UpdateLevel from "./levels/updateLevel"; import { getRemoveButtonHTML, addRemoveLevelButtonEventListener, -} from './removeLevelButton'; -import CONST from './const'; +} from "./removeLevelButton"; +import CONST from "./const"; -import log from '../../../../logger'; +import log from "../../../../logger"; /** * Gets executed when 'Add' Button is clicked to add levels @@ -39,30 +39,30 @@ const addLevel = (e: Event) => { const isValid = validateLevel(onScreenLevel); log.debug(`[addLevelButton] isValid: ${isValid}`); if (!isValid) { - let error = `Aborting level ${onScreenLevel} addition`; + const error = `Aborting level ${onScreenLevel} addition`; return log.error(error); } FIELDSET.replaceChild(getRemoveButtonHTML(onScreenLevel), PARENT); const newLevelElement = getHtml(onScreenLevel + 1); - FIELDSET.insertAdjacentElement('afterend', newLevelElement); + FIELDSET.insertAdjacentElement("afterend", newLevelElement); UpdateLevel.register(onScreenLevel); addRemoveLevelButtonEventListener(onScreenLevel); addLevelButtonAddEventListener(); - const main = document.querySelector('body'); + const main = document.querySelector("body"); const style = main.style.display; - main.style.display = 'none'; + main.style.display = "none"; main.style.display = style; }; /** adds onclick event listener */ -const addLevelButtonAddEventListener = () => { +const addLevelButtonAddEventListener = (): void => { const addLevelButton = ( document.querySelector(`.${CONST.ADD_LEVEL_BUTTON}`) ); - addLevelButton.addEventListener('click', addLevel); + addLevelButton.addEventListener("click", addLevel); }; /** @@ -72,25 +72,25 @@ const addLevelButtonAddEventListener = () => { const getHtml = (level: number) => { log.debug(`[generating HTML getHtml]level: ${level}`); - const fieldset = document.createElement('fieldset'); // new HTMLFieldSetElement(); + const fieldset = document.createElement("fieldset"); // new HTMLFieldSetElement(); fieldset.className = CONST.LEVEL_CONTAINER_CLASS; fieldset.id = `${CONST.LEVEL_FIELDSET_ID_WITHOUT_LEVEL}${level}`; - const legend = document.createElement('legend'); // new HTMLLegendElement(); + const legend = document.createElement("legend"); // new HTMLLegendElement(); legend.className = CONST.LEGEND_CLASS; const legendText = document.createTextNode(`Level ${level}`); legend.appendChild(legendText); fieldset.appendChild(legend); - const vistitorLabel = document.createElement('label'); //document.createElement('label'); + const vistitorLabel = document.createElement("label"); //document.createElement('label'); vistitorLabel.className = CONST.LABEL_CLASS; - const visitorText = document.createTextNode('Visitor'); + const visitorText = document.createTextNode("Visitor"); vistitorLabel.appendChild(visitorText); - const visitor = document.createElement('input'); //document.createElement('input'); + const visitor = document.createElement("input"); //document.createElement('input'); const visitorId = `${CONST.VISITOR_WITHOUT_LEVEL}${level}`; visitor.className = CONST.LEVEL_INPUT_CLASS; - visitor.type = 'number'; + visitor.type = "number"; visitor.name = visitorId; visitor.id = visitorId; vistitorLabel.htmlFor = visitorId; @@ -98,13 +98,13 @@ const getHtml = (level: number) => { fieldset.appendChild(vistitorLabel); - const difficultyLabel = document.createElement('label'); + const difficultyLabel = document.createElement("label"); difficultyLabel.className = CONST.LABEL_CLASS; - const difficultyText = document.createTextNode('Difficulty'); + const difficultyText = document.createTextNode("Difficulty"); difficultyLabel.appendChild(difficultyText); - const difficulty = document.createElement('input'); + const difficulty = document.createElement("input"); const difficultyID = `${CONST.DIFFICULTY_WITHOUT_LEVEL}${level}`; - difficulty.type = 'number'; + difficulty.type = "number"; difficulty.name = difficultyID; difficulty.className = CONST.LEVEL_INPUT_CLASS; difficulty.id = difficultyID; @@ -113,18 +113,18 @@ const getHtml = (level: number) => { fieldset.appendChild(difficultyLabel); - const addLevelLabel = document.createElement('label'); + const addLevelLabel = document.createElement("label"); addLevelLabel.className = CONST.REMOVE_LEVEL_LABEL_CLASS; - const addLevel = document.createElement('input'); + const addLevel = document.createElement("input"); addLevel.className = CONST.ADD_LEVEL_BUTTON; - addLevel.type = 'button'; - const addLevelButtonID = 'add'; + addLevel.type = "button"; + const addLevelButtonID = "add"; addLevel.name = addLevelButtonID; addLevel.id = addLevelButtonID; addLevelLabel.htmlFor = addLevelButtonID; - const addText = document.createTextNode('Add level'); + const addText = document.createTextNode("Add level"); addLevelLabel.appendChild(addText); - addLevel.value = 'Add'; + addLevel.value = "Add"; addLevelLabel.appendChild(addLevel); fieldset.appendChild(addLevelLabel); diff --git a/templates/panel/sitekey/add/ts/const.ts b/templates/panel/sitekey/add/ts/const.ts index 334850b8..6e111d15 100644 --- a/templates/panel/sitekey/add/ts/const.ts +++ b/templates/panel/sitekey/add/ts/const.ts @@ -15,25 +15,25 @@ * along with this program. If not, see . */ -const LABEL_INNER_TEXT_WITHOUT_LEVEL = 'Level '; -const LABEL_CLASS = 'sitekey-form__level-label'; +const LABEL_INNER_TEXT_WITHOUT_LEVEL = "Level "; +const LABEL_CLASS = "sitekey-form__level-label"; -const INPUT_ID_WITHOUT_LEVEL = 'level'; -const LEVEL_INPUT_CLASS = 'sitekey-form__level-input'; +const INPUT_ID_WITHOUT_LEVEL = "level"; +const LEVEL_INPUT_CLASS = "sitekey-form__level-input"; -const VISITOR_WITHOUT_LEVEL = 'visitor'; -const DIFFICULTY_WITHOUT_LEVEL = 'difficulty'; +const VISITOR_WITHOUT_LEVEL = "visitor"; +const DIFFICULTY_WITHOUT_LEVEL = "difficulty"; -const LEVEL_CONTAINER_CLASS = 'sitekey__level-container'; -const LEVEL_FIELDSET_ID_WITHOUT_LEVEL = 'level-group-'; -const LEGEND_CLASS = 'sitekey__level-title'; +const LEVEL_CONTAINER_CLASS = "sitekey__level-container"; +const LEVEL_FIELDSET_ID_WITHOUT_LEVEL = "level-group-"; +const LEGEND_CLASS = "sitekey__level-title"; -const REMOVE_LEVEL_BUTTON_ID_WITHOUT_LEVEL = 'remove-level'; -const REMOVE_LEVEL_BUTTON_CLASS = 'sitekey-form__level-remove-level-button'; -const REMOVE_LEVEL_LABEL_TEXT = 'Remove Level'; -const REMOVE_LEVEL_LABEL_CLASS = 'sitekey-form__level-label--hidden'; +const REMOVE_LEVEL_BUTTON_ID_WITHOUT_LEVEL = "remove-level"; +const REMOVE_LEVEL_BUTTON_CLASS = "sitekey-form__level-remove-level-button"; +const REMOVE_LEVEL_LABEL_TEXT = "Remove Level"; +const REMOVE_LEVEL_LABEL_CLASS = "sitekey-form__level-label--hidden"; -const ADD_LEVEL_BUTTON = 'sitekey-form__level-add-level-button'; +const ADD_LEVEL_BUTTON = "sitekey-form__level-add-level-button"; const CONST = { LABEL_CLASS, diff --git a/templates/panel/sitekey/add/ts/form/index.ts b/templates/panel/sitekey/add/ts/form/index.ts index 0fd1ce85..9311ceed 100644 --- a/templates/panel/sitekey/add/ts/form/index.ts +++ b/templates/panel/sitekey/add/ts/form/index.ts @@ -15,30 +15,31 @@ * along with this program. If not, see . */ -import {LEVELS} from '../levels'; +import { LEVELS } from "../levels"; -import getFormUrl from '../../../../../utils/getFormUrl'; -import genJsonPayload from '../../../../../utils/genJsonPayload'; +import getFormUrl from "../../../../../utils/getFormUrl"; +import genJsonPayload from "../../../../../utils/genJsonPayload"; -import VIEWS from '../../../../../views/v1/routes'; +import VIEWS from "../../../../../views/v1/routes"; -import validateDescription from './validateDescription'; -import validateDuration from './validateDuration'; +import validateDescription from "./validateDescription"; +import validateDuration from "./validateDuration"; -import createError from '../../../../../components/error'; +import createError from "../../../../../components/error"; -export const SITE_KEY_FORM_CLASS = 'sitekey-form'; -export const FORM = document.querySelector(`.${SITE_KEY_FORM_CLASS}`); +export const SITE_KEY_FORM_CLASS = "sitekey-form"; +export const FORM = ( + document.querySelector(`.${SITE_KEY_FORM_CLASS}`) +); -export const addSubmitEventListener = () => { - FORM.addEventListener('submit', submit, true); -}; +export const addSubmitEventListener = (): void => + FORM.addEventListener("submit", submit, true); const submit = async (e: Event) => { e.preventDefault(); const description = validateDescription(e); - const duration = validateDuration(e); + const duration = validateDuration(); const formUrl = getFormUrl(FORM); diff --git a/templates/panel/sitekey/add/ts/form/validateDescription.test.ts b/templates/panel/sitekey/add/ts/form/validateDescription.test.ts index 9efbd01e..9c3e615d 100644 --- a/templates/panel/sitekey/add/ts/form/validateDescription.test.ts +++ b/templates/panel/sitekey/add/ts/form/validateDescription.test.ts @@ -15,11 +15,11 @@ * along with this program. If not, see . */ -import validateDescription from './validateDescription'; -import {getAddForm, fillDescription} from '../setupTests'; -import {mockAlert} from '../../../../../setUpTests'; +import validateDescription from "./validateDescription"; +import {getAddForm, fillDescription} from "../setupTests"; +import {mockAlert} from "../../../../../setUpTests"; -import setup from '../../../../../components/error/setUpTests'; +import setup from "../../../../../components/error/setUpTests"; mockAlert(); @@ -27,17 +27,17 @@ document.body.innerHTML = getAddForm(); const emptyErr = "can't be empty"; -it('validateDescription workds', () => { - document.querySelector('body').appendChild(setup()); +it("validateDescription workds", () => { + document.querySelector("body").appendChild(setup()); try { - const event = new Event('submit'); + const event = new Event("submit"); validateDescription(event); } catch (e) { expect(e.message).toContain(emptyErr); } // fill and validate - fillDescription('testing'); - const event = new Event('submit'); + fillDescription("testing"); + const event = new Event("submit"); validateDescription(event); }); diff --git a/templates/panel/sitekey/add/ts/form/validateDescription.ts b/templates/panel/sitekey/add/ts/form/validateDescription.ts index 3ceccf16..0a7702e2 100644 --- a/templates/panel/sitekey/add/ts/form/validateDescription.ts +++ b/templates/panel/sitekey/add/ts/form/validateDescription.ts @@ -15,12 +15,12 @@ * along with this program. If not, see . */ -import isBlankString from '../../../../../utils/isBlankString'; +import isBlankString from "../../../../../utils/isBlankString"; -const validateDescription = (e: Event) => { - const inputElement = document.getElementById('description'); +const validateDescription = (e: Event): string => { + const inputElement = document.getElementById("description"); const val = inputElement.value; - const filed = 'Description'; + const filed = "Description"; isBlankString(val, filed, e); return val; }; diff --git a/templates/panel/sitekey/add/ts/form/validateDuration.test.ts b/templates/panel/sitekey/add/ts/form/validateDuration.test.ts index 057c56c1..9e345636 100644 --- a/templates/panel/sitekey/add/ts/form/validateDuration.test.ts +++ b/templates/panel/sitekey/add/ts/form/validateDuration.test.ts @@ -14,8 +14,6 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ -import isNumber from '../../../../../utils/isNumber'; - //const validateDuration = (e: Event) => { // const duartionElement = document.getElementById('duration'); // const duration = parseInt(duartionElement.value); @@ -31,30 +29,28 @@ import isNumber from '../../../../../utils/isNumber'; // //export default validateDuration; -import validateDuration from './validateDuration'; -import {getAddForm, fillDuration} from '../setupTests'; +import validateDuration from "./validateDuration"; +import {getAddForm, fillDuration} from "../setupTests"; document.body.innerHTML = getAddForm(); const emptyErr = "can't be empty"; -const NaNErr = 'duration can contain nubers only'; -const zeroErr = 'duration must be greater than zero'; +const NaNErr = "duration can contain nubers only"; +const zeroErr = "duration must be greater than zero"; const duration = 30; -it('validateDuration workds', () => { +it("validateDuration workds", () => { try { - const event = new Event('submit'); - validateDuration(event); + validateDuration(); } catch (e) { expect(e.message).toContain(emptyErr); } // fill string error try { - fillDuration('testing'); - const event = new Event('submit'); - validateDuration(event); + fillDuration("testing"); + validateDuration(); } catch (e) { expect(e.message).toContain(NaNErr); } @@ -62,13 +58,11 @@ it('validateDuration workds', () => { // zero err try { fillDuration(0); - const event = new Event('submit'); - validateDuration(event); + validateDuration(); } catch (e) { expect(e.message).toContain(zeroErr); } fillDuration(duration); - const event = new Event('submit'); - expect(validateDuration(event)).toBe(duration); + expect(validateDuration()).toBe(duration); }); diff --git a/templates/panel/sitekey/add/ts/form/validateDuration.ts b/templates/panel/sitekey/add/ts/form/validateDuration.ts index 8de3e5c0..4aedea92 100644 --- a/templates/panel/sitekey/add/ts/form/validateDuration.ts +++ b/templates/panel/sitekey/add/ts/form/validateDuration.ts @@ -14,17 +14,17 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ -import isNumber from '../../../../../utils/isNumber'; +import isNumber from "../../../../../utils/isNumber"; -const validateDuration = (e: Event) => { - const duartionElement = document.getElementById('duration'); +const validateDuration = (): number => { + const duartionElement = document.getElementById("duration"); const duration = parseInt(duartionElement.value); if (!isNumber(duration) || Number.isNaN(duration)) { - throw new Error('duration can contain nubers only'); + throw new Error("duration can contain nubers only"); } if (duration <= 0) { - throw new Error('duration must be greater than zero'); + throw new Error("duration must be greater than zero"); } return duration; }; diff --git a/templates/panel/sitekey/add/ts/index.ts b/templates/panel/sitekey/add/ts/index.ts index 5e1efc05..39906758 100644 --- a/templates/panel/sitekey/add/ts/index.ts +++ b/templates/panel/sitekey/add/ts/index.ts @@ -15,10 +15,10 @@ * along with this program. If not, see . */ -import addLevelButtonAddEventListener from './addLevelButton'; -import addSubmitEventListener from './form'; +import addLevelButtonAddEventListener from "./addLevelButton"; +import addSubmitEventListener from "./form"; -export const index = () => { +export const index = (): void => { addLevelButtonAddEventListener(); addSubmitEventListener(); }; diff --git a/templates/panel/sitekey/add/ts/levels/getLevelFields.test.ts b/templates/panel/sitekey/add/ts/levels/getLevelFields.test.ts index 78cf7fbf..5cd43b7a 100644 --- a/templates/panel/sitekey/add/ts/levels/getLevelFields.test.ts +++ b/templates/panel/sitekey/add/ts/levels/getLevelFields.test.ts @@ -15,27 +15,27 @@ * along with this program. If not, see . */ -import getLevelFields from './getLevelFields'; +import getLevelFields from "./getLevelFields"; import { getAddForm, level1, level2, fillAddLevel, addLevel, -} from '../setupTests'; +} from "../setupTests"; document.body.innerHTML = getAddForm(); -const visNumErr = 'visitor can contain nubers only'; -const diffNumErr = 'difficulty can contain nubers only'; +const visNumErr = "visitor can contain nubers only"; +const diffNumErr = "difficulty can contain nubers only"; -it('get levels fields works', () => { +it("get levels fields works", () => { addLevel(level1.visitor_threshold, level1.difficulty_factor); expect(getLevelFields(1)).toEqual(level1); // NaN visitor try { - fillAddLevel('test', level2.difficulty_factor); + fillAddLevel("test", level2.difficulty_factor); getLevelFields(2); } catch (e) { expect(e.message).toBe(visNumErr); @@ -43,7 +43,7 @@ it('get levels fields works', () => { // Nan difficulty_factor try { - fillAddLevel(level2.visitor_threshold, 'fooasdads'); + fillAddLevel(level2.visitor_threshold, "fooasdads"); getLevelFields(2); } catch (e) { expect(e.message).toBe(diffNumErr); diff --git a/templates/panel/sitekey/add/ts/levels/getLevelFields.ts b/templates/panel/sitekey/add/ts/levels/getLevelFields.ts index e6d502a3..01ca933e 100644 --- a/templates/panel/sitekey/add/ts/levels/getLevelFields.ts +++ b/templates/panel/sitekey/add/ts/levels/getLevelFields.ts @@ -15,13 +15,13 @@ * along with this program. If not, see . */ -import {Level} from './index'; -import CONST from '../const'; +import { Level } from "./index"; +import CONST from "../const"; -import log from '../../../../../logger'; +import log from "../../../../../logger"; /** Fetches level from DOM using the ID passesd and validates */ -const getLevelFields = (id: number) => { +const getLevelFields = (id: number): Level => { log.debug(`[getLevelFields]: id: ${id}`); const visitorID = CONST.VISITOR_WITHOUT_LEVEL + id.toString(); const difficultyID = CONST.DIFFICULTY_WITHOUT_LEVEL + id.toString(); @@ -35,11 +35,11 @@ const getLevelFields = (id: number) => { const difficulty_factor = parseInt(difficultyElement.value); if (Number.isNaN(visitor_threshold)) { - throw new Error('visitor can contain nubers only'); + throw new Error("visitor can contain nubers only"); } if (Number.isNaN(difficulty_factor)) { - throw new Error('difficulty can contain nubers only'); + throw new Error("difficulty can contain nubers only"); } const level: Level = { @@ -48,7 +48,7 @@ const getLevelFields = (id: number) => { }; log.debug( - `[getLevelFields.ts] visitor: ${visitor_threshold} difficulty: ${difficulty_factor}`, + `[getLevelFields.ts] visitor: ${visitor_threshold} difficulty: ${difficulty_factor}` ); return level; diff --git a/templates/panel/sitekey/add/ts/levels/getNumLevels.test.ts b/templates/panel/sitekey/add/ts/levels/getNumLevels.test.ts index 6dfce4af..c896daf3 100644 --- a/templates/panel/sitekey/add/ts/levels/getNumLevels.test.ts +++ b/templates/panel/sitekey/add/ts/levels/getNumLevels.test.ts @@ -15,12 +15,12 @@ * along with this program. If not, see . */ -import getNumLevels from './getNumLevels'; -import {getAddForm, addLevel} from '../setupTests'; +import getNumLevels from "./getNumLevels"; +import {getAddForm, addLevel} from "../setupTests"; document.body.innerHTML = getAddForm(); -it('get num levels works', () => { +it("get num levels works", () => { expect(getNumLevels()).toBe(1); addLevel(2, 4); expect(getNumLevels()).toBe(2); diff --git a/templates/panel/sitekey/add/ts/levels/getNumLevels.ts b/templates/panel/sitekey/add/ts/levels/getNumLevels.ts index 9f271643..ec239b58 100644 --- a/templates/panel/sitekey/add/ts/levels/getNumLevels.ts +++ b/templates/panel/sitekey/add/ts/levels/getNumLevels.ts @@ -15,16 +15,16 @@ * along with this program. If not, see . */ -import CONST from '../const'; +import CONST from "../const"; -import log from '../../../../../logger'; +import log from "../../../../../logger"; /** returns number of level input fields currently in DOM */ -const getNumLevels = () => { +const getNumLevels = (): number => { let numLevels = 0; document .querySelectorAll(`.${CONST.LEVEL_CONTAINER_CLASS}`) - .forEach(_ => numLevels++); + .forEach(() => numLevels++); log.debug(`[getNumLevels]: numLevels: ${numLevels}`); return numLevels; }; diff --git a/templates/panel/sitekey/add/ts/levels/index.ts b/templates/panel/sitekey/add/ts/levels/index.ts index a502c623..1bd60b47 100644 --- a/templates/panel/sitekey/add/ts/levels/index.ts +++ b/templates/panel/sitekey/add/ts/levels/index.ts @@ -15,7 +15,7 @@ * along with this program. If not, see . */ -import log from '../../../../../logger'; +import log from "../../../../../logger"; /** Datatype represenging an mCaptcha level */ export type Level = { @@ -95,7 +95,7 @@ export const LEVELS = (function() { } } levels.levels = tmpLevel.levels; - log.debug(`post update:`); + log.debug("post update:"); LEVELS.print(); return true; } catch (e) { @@ -133,7 +133,7 @@ export const LEVELS = (function() { } } levels.levels = tmpLevel.levels; - log.debug('Post remove:'); + log.debug("Post remove:"); LEVELS.print(); return true; } catch (e) { diff --git a/templates/panel/sitekey/add/ts/levels/levels.test.ts b/templates/panel/sitekey/add/ts/levels/levels.test.ts index 1b446ae1..579b652f 100644 --- a/templates/panel/sitekey/add/ts/levels/levels.test.ts +++ b/templates/panel/sitekey/add/ts/levels/levels.test.ts @@ -15,14 +15,14 @@ * along with this program. If not, see . */ -import {LEVELS, Level} from './index'; -import {level1, level1visErr, level1diffErr, level2} from '../setupTests'; +import {LEVELS, Level} from "./index"; +import {level1, level1visErr, level1diffErr, level2} from "../setupTests"; -const visitorErr = 'visitor count should be greater than previous levels'; -const difficultyErr = 'difficulty should be greater than previous levels'; +const visitorErr = "visitor count should be greater than previous levels"; +const difficultyErr = "difficulty should be greater than previous levels"; -const zeroVisError = 'visitors must be greater than zero'; -const zeroDiffError = 'difficulty must be greater than zero'; +const zeroVisError = "visitors must be greater than zero"; +const zeroDiffError = "difficulty must be greater than zero"; const zeroVis: Level = { difficulty_factor: 10, @@ -34,7 +34,7 @@ const zeroDiff: Level = { visitor_threshold: 10, }; -it('LEVELS works', () => { +it("LEVELS works", () => { // add level LEVELS.add(level1); expect(LEVELS.getLevels()).toEqual([level1]); diff --git a/templates/panel/sitekey/add/ts/levels/updateLevel.ts b/templates/panel/sitekey/add/ts/levels/updateLevel.ts index fa9d9fc9..5fd141f4 100644 --- a/templates/panel/sitekey/add/ts/levels/updateLevel.ts +++ b/templates/panel/sitekey/add/ts/levels/updateLevel.ts @@ -15,14 +15,14 @@ * along with this program. If not, see . */ -import CONST from '../const'; -import getLevelFields from './getLevelFields'; -import {LEVELS} from './index'; +import CONST from "../const"; +import getLevelFields from "./getLevelFields"; +import { LEVELS } from "./index"; -import createError from '../../../../../components/error'; +import createError from "../../../../../components/error"; /** on-change event handler to update level */ -const updateLevel = (e: Event) => { +const updateLevel = (e: Event): void => { const target = e.target; const id = target.id; @@ -36,7 +36,7 @@ const updateLevel = (e: Event) => { } if (Number.isNaN(level)) { - console.error(`[updateLevel.ts] level # computed is not correct, got NaN`); + console.error("[updateLevel.ts] level # computed is not correct, got NaN"); } try { @@ -48,7 +48,7 @@ const updateLevel = (e: Event) => { }; /** registers on-change event handlers to update levels */ -export const register = (id: number) => { +export const register = (id: number): void => { const visitorID = CONST.VISITOR_WITHOUT_LEVEL + id.toString(); const difficultyID = CONST.DIFFICULTY_WITHOUT_LEVEL + id.toString(); @@ -57,6 +57,6 @@ export const register = (id: number) => { document.getElementById(difficultyID) ); - visitorElement.addEventListener('input', updateLevel, false); - difficultyElement.addEventListener('input', updateLevel, false); + visitorElement.addEventListener("input", updateLevel, false); + difficultyElement.addEventListener("input", updateLevel, false); }; diff --git a/templates/panel/sitekey/add/ts/levels/validateLevel.test.ts b/templates/panel/sitekey/add/ts/levels/validateLevel.test.ts index adc1d034..8daab97c 100644 --- a/templates/panel/sitekey/add/ts/levels/validateLevel.test.ts +++ b/templates/panel/sitekey/add/ts/levels/validateLevel.test.ts @@ -15,15 +15,15 @@ * along with this program. If not, see . */ -import validateLevel from './validateLevel'; -import {getAddForm, level1, fillAddLevel} from '../setupTests'; -import setup from '../../../../../components/error/setUpTests'; +import validateLevel from "./validateLevel"; +import {getAddForm, level1, fillAddLevel} from "../setupTests"; +import setup from "../../../../../components/error/setUpTests"; document.body.innerHTML = getAddForm(); document.body.appendChild(setup()); -it('validate levels fields works', () => { +it("validate levels fields works", () => { // null error expect(validateLevel(1)).toEqual(false); diff --git a/templates/panel/sitekey/add/ts/levels/validateLevel.ts b/templates/panel/sitekey/add/ts/levels/validateLevel.ts index 75cad2a0..dc1b9f62 100644 --- a/templates/panel/sitekey/add/ts/levels/validateLevel.ts +++ b/templates/panel/sitekey/add/ts/levels/validateLevel.ts @@ -15,15 +15,15 @@ * along with this program. If not, see . */ -import {LEVELS} from './index'; -import getLevelFields from './getLevelFields'; -import createError from '../../../../../components/error/'; +import {LEVELS} from "./index"; +import getLevelFields from "./getLevelFields"; +import createError from "../../../../../components/error/"; /** * Fetches level from DOM using the ID passesd and validates * its contents * */ -const validateLevel = (id: number) => { +const validateLevel = (id: number): boolean => { try { const level = getLevelFields(id); LEVELS.add(level); diff --git a/templates/panel/sitekey/add/ts/removeLevelButton/index.ts b/templates/panel/sitekey/add/ts/removeLevelButton/index.ts index e6462ffc..ebc9d6b8 100644 --- a/templates/panel/sitekey/add/ts/removeLevelButton/index.ts +++ b/templates/panel/sitekey/add/ts/removeLevelButton/index.ts @@ -14,13 +14,13 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ -import {LEVELS} from '../levels/index'; -import updateLevelNumbersOnDOM from './updateDom'; -import CONST from '../const'; +import { LEVELS } from "../levels/index"; +import updateLevelNumbersOnDOM from "./updateDom"; +import CONST from "../const"; -import log from '../../../../../logger'; +import log from "../../../../../logger"; -const REMOVE_LEVEL_BUTTON = 'sitekey-form__level-remove-level-button'; +const REMOVE_LEVEL_BUTTON = "sitekey-form__level-remove-level-button"; /** * Gets executed when 'Remove' Button is clicked to remove levels @@ -31,12 +31,12 @@ const removeLevel = (e: Event) => { const FIELDSET = PARENT.parentElement; const levelNum = parseInt( - eventTarget.id.slice(CONST.REMOVE_LEVEL_BUTTON_ID_WITHOUT_LEVEL.length), + eventTarget.id.slice(CONST.REMOVE_LEVEL_BUTTON_ID_WITHOUT_LEVEL.length) ); if (Number.isNaN(levelNum)) { const msg = - '[removeLevelButton.ts] error in parsing level number from remove button ID'; + "[removeLevelButton.ts] error in parsing level number from remove button ID"; //log.error(msg); throw new Error(msg); } @@ -47,19 +47,19 @@ const removeLevel = (e: Event) => { }; /** adds onclick event listener */ -export const addRemoveLevelButtonEventListener = (level: number) => { +export const addRemoveLevelButtonEventListener = (level: number): void => { const removeButton = document.getElementById( - `${CONST.REMOVE_LEVEL_BUTTON_ID_WITHOUT_LEVEL}${level}`, + `${CONST.REMOVE_LEVEL_BUTTON_ID_WITHOUT_LEVEL}${level}` ); - removeButton.addEventListener('click', removeLevel); + removeButton.addEventListener("click", removeLevel); }; /** adds onclick event listener to all remove buttons */ -export const addRemoveLevelButtonEventListenerAll = () => { +export const addRemoveLevelButtonEventListenerAll = (): void => { const removeButtons = document.querySelectorAll(`.${REMOVE_LEVEL_BUTTON}`); - removeButtons.forEach(button => - button.addEventListener('click', removeLevel), + removeButtons.forEach((button) => + button.addEventListener("click", removeLevel) ); }; @@ -67,20 +67,20 @@ export const addRemoveLevelButtonEventListenerAll = () => { * Generate Remove button HTML. On-click handler should be added * seprately */ -export const getRemoveButtonHTML = (level: number) => { +export const getRemoveButtonHTML = (level: number): HTMLLabelElement => { log.log(`[generating HTML getHtml]level: ${level}`); - const btn = document.createElement('input'); + const btn = document.createElement("input"); btn.className = CONST.REMOVE_LEVEL_BUTTON_CLASS; - btn.type = 'button'; + btn.type = "button"; const id = `${CONST.REMOVE_LEVEL_BUTTON_ID_WITHOUT_LEVEL}${level}`; btn.name = id; btn.id = id; - btn.value = 'x'; + btn.value = "x"; - const removeLabel = document.createElement('label'); + const removeLabel = document.createElement("label"); removeLabel.className = CONST.REMOVE_LEVEL_LABEL_CLASS; - const removeLabelText = document.createTextNode('RemoveLevel'); + const removeLabelText = document.createTextNode("RemoveLevel"); removeLabel.appendChild(removeLabelText); removeLabel.appendChild(btn); removeLabel.htmlFor = id; diff --git a/templates/panel/sitekey/add/ts/removeLevelButton/removeLevelButton.test.ts b/templates/panel/sitekey/add/ts/removeLevelButton/removeLevelButton.test.ts index c9c483c0..2f670799 100644 --- a/templates/panel/sitekey/add/ts/removeLevelButton/removeLevelButton.test.ts +++ b/templates/panel/sitekey/add/ts/removeLevelButton/removeLevelButton.test.ts @@ -15,17 +15,12 @@ * along with this program. If not, see . */ -import getNumLevels from '../levels/getNumLevels'; -import { - getAddForm, - getRemoveButtonHTMLForm, - trim, - addLevel, -} from '../setupTests'; -import CONST from '../const'; +import getNumLevels from "../levels/getNumLevels"; +import { getAddForm, addLevel } from "../setupTests"; +import CONST from "../const"; -import log from '../../../../../logger'; -import {MODE} from '../../../../../logger'; +import log from "../../../../../logger"; +import { MODE } from "../../../../../logger"; document.body.innerHTML = getAddForm(); @@ -46,13 +41,13 @@ const setUp = () => { log.setMode(MODE.none); -it('removeLevelButton works', () => { +it("removeLevelButton works", () => { setUp(); for (let i = 1; i < 4; i++) { const l1 = ( document.getElementById( - `${CONST.REMOVE_LEVEL_BUTTON_ID_WITHOUT_LEVEL}${1}`, + `${CONST.REMOVE_LEVEL_BUTTON_ID_WITHOUT_LEVEL}${1}` ) ); diff --git a/templates/panel/sitekey/add/ts/removeLevelButton/updateDom/index.ts b/templates/panel/sitekey/add/ts/removeLevelButton/updateDom/index.ts index 423a3e2d..a041678a 100644 --- a/templates/panel/sitekey/add/ts/removeLevelButton/updateDom/index.ts +++ b/templates/panel/sitekey/add/ts/removeLevelButton/updateDom/index.ts @@ -14,25 +14,25 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ -import getNumLevels from '../../levels/getNumLevels'; -import CONST from '../../const'; -import log from '../../../../../../logger'; +import getNumLevels from "../../levels/getNumLevels"; +import CONST from "../../const"; +import log from "../../../../../../logger"; -import updateLabels from './updateLabel'; -import updateInputs from './updateInputs'; -import updateRemoveButton from './updateRemoveButton'; -import updateLevelGroup from './updateLevelGroup'; +import updateLabels from "./updateLabel"; +import updateInputs from "./updateInputs"; +import updateRemoveButton from "./updateRemoveButton"; +import updateLevelGroup from "./updateLevelGroup"; /** * update level number on fieldset legends and their ids too * @param {number} id - level number that was ordered to remove. * All updates are made relative to id * */ -const updateLevelNumbersOnDOM = (id: number) => { +const updateLevelNumbersOnDOM = (id: number): void => { const numLevels = getNumLevels(); if (id == numLevels) { throw new Error( - "Can't remove the very fist element, it has to be first added to DOM", + "Can't remove the very fist element, it has to be first added to DOM" ); } @@ -42,7 +42,7 @@ const updateLevelNumbersOnDOM = (id: number) => { const newLevel = i - 1; const levelGroup = document.querySelector( - `#${CONST.LEVEL_FIELDSET_ID_WITHOUT_LEVEL}${i}`, + `#${CONST.LEVEL_FIELDSET_ID_WITHOUT_LEVEL}${i}` ); if (levelGroup === null) { @@ -53,9 +53,9 @@ const updateLevelNumbersOnDOM = (id: number) => { } // rename legend - const legend = levelGroup.getElementsByTagName('legend')[0]; + const legend = levelGroup.getElementsByTagName("legend")[0]; const legendText = document.createTextNode(`Level ${newLevel}`); - const newLegend = document.createElement('legend'); + const newLegend = document.createElement("legend"); newLegend.className = legend.className; newLegend.appendChild(legendText); legend.replaceWith(newLegend); diff --git a/templates/panel/sitekey/add/ts/removeLevelButton/updateDom/setupTests.ts b/templates/panel/sitekey/add/ts/removeLevelButton/updateDom/setupTests.ts index 2e06293c..c63acf36 100644 --- a/templates/panel/sitekey/add/ts/removeLevelButton/updateDom/setupTests.ts +++ b/templates/panel/sitekey/add/ts/removeLevelButton/updateDom/setupTests.ts @@ -15,12 +15,12 @@ * along with this program. If not, see . */ -import getNumLevels from '../../levels/getNumLevels'; -import {getAddForm, addLevel} from '../../setupTests'; +import getNumLevels from "../../levels/getNumLevels"; +import { getAddForm, addLevel } from "../../setupTests"; document.body.innerHTML = getAddForm(); -export const setupAddlevels = () => { +export const setupAddlevels = (): void => { expect(getNumLevels()).toBe(1); // add a level addLevel(2, 2); diff --git a/templates/panel/sitekey/add/ts/removeLevelButton/updateDom/updateInputs.test.ts b/templates/panel/sitekey/add/ts/removeLevelButton/updateDom/updateInputs.test.ts index d9fd338d..9d10c019 100644 --- a/templates/panel/sitekey/add/ts/removeLevelButton/updateDom/updateInputs.test.ts +++ b/templates/panel/sitekey/add/ts/removeLevelButton/updateDom/updateInputs.test.ts @@ -15,20 +15,20 @@ * along with this program. If not, see . */ -import {getAddForm, trim} from '../../setupTests'; -import updateInputs from './updateInputs'; -import CONST from '../../const'; +import {getAddForm, trim} from "../../setupTests"; +import updateInputs from "./updateInputs"; +import CONST from "../../const"; -import log from '../../../../../../logger'; -import {MODE} from '../../../../../../logger'; +import log from "../../../../../../logger"; +import {MODE} from "../../../../../../logger"; -import {setupAddlevels} from './setupTests'; +import {setupAddlevels} from "./setupTests"; document.body.innerHTML = getAddForm(); log.setMode(MODE.none); -it('updateInputs works', () => { +it("updateInputs works", () => { setupAddlevels(); // removing level 2 const level = 2; @@ -58,7 +58,7 @@ it('updateInputs works', () => { }); /** get initial form to test remove button functionality */ -export const update = () => { +export const update = (): string => { return `

diff --git a/templates/panel/sitekey/add/ts/removeLevelButton/updateDom/updateInputs.ts b/templates/panel/sitekey/add/ts/removeLevelButton/updateDom/updateInputs.ts index 886be119..d15d0e86 100644 --- a/templates/panel/sitekey/add/ts/removeLevelButton/updateDom/updateInputs.ts +++ b/templates/panel/sitekey/add/ts/removeLevelButton/updateDom/updateInputs.ts @@ -14,11 +14,11 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ -import CONST from '../../const'; -import log from '../../../../../../logger'; +import CONST from "../../const"; +import log from "../../../../../../logger"; /** update input IDs with new level */ -const updateInput = (levelGroup: Element, newLevel: number) => { +const updateInput = (levelGroup: Element, newLevel: number): void => { const inputs = >( levelGroup.querySelectorAll(`.${CONST.LEVEL_INPUT_CLASS}`) ); @@ -26,17 +26,17 @@ const updateInput = (levelGroup: Element, newLevel: number) => { inputs.forEach(input => { if (input.id.includes(CONST.VISITOR_WITHOUT_LEVEL)) { log.log(`${input.id}`); - log.log('changing visitor_threshold input'); + log.log("changing visitor_threshold input"); const id = `${CONST.VISITOR_WITHOUT_LEVEL}${newLevel}`; input.id = id; input.name = id; } else if (input.id.includes(CONST.DIFFICULTY_WITHOUT_LEVEL)) { - log.log('changing difficulty input'); + log.log("changing difficulty input"); const id = `${CONST.DIFFICULTY_WITHOUT_LEVEL}${newLevel}`; input.id = id; input.name = id; } else { - if (input.id != 'add') { + if (input.id != "add") { throw new Error(`Did you add an extra input to DOM? ${input.id} ${input.className} ${input.name}`); } } diff --git a/templates/panel/sitekey/add/ts/removeLevelButton/updateDom/updateLabel.test.ts b/templates/panel/sitekey/add/ts/removeLevelButton/updateDom/updateLabel.test.ts index 226d858c..750eeeea 100644 --- a/templates/panel/sitekey/add/ts/removeLevelButton/updateDom/updateLabel.test.ts +++ b/templates/panel/sitekey/add/ts/removeLevelButton/updateDom/updateLabel.test.ts @@ -15,18 +15,15 @@ * along with this program. If not, see . */ -import getNumLevels from '../../levels/getNumLevels'; -import {getAddForm, trim} from '../../setupTests'; -import updateLabels from './updateLabel'; -import CONST from '../../const'; +import { trim } from "../../setupTests"; +import updateLabels from "./updateLabel"; +import CONST from "../../const"; -import log from '../../../../../../logger'; -import {MODE} from '../../../../../../logger'; - -import {setupAddlevels} from './setupTests'; +import log from "../../../../../../logger"; +import { MODE } from "../../../../../../logger"; /** get initial form to test remove button functionality */ -export const labelLevel = (level: number) => { +export const labelLevel = (level: number): string => { return `
@@ -85,11 +82,11 @@ document.body.innerHTML = labelLevel(2); log.setMode(MODE.none); -it('addLevelButton works', () => { +it("addLevelButton works", () => { // removing level 2 const level = 2; const levelGroup = document.querySelector( - `#${CONST.LEVEL_FIELDSET_ID_WITHOUT_LEVEL}${level}`, + `#${CONST.LEVEL_FIELDSET_ID_WITHOUT_LEVEL}${level}` ); const newLevel = 20; @@ -100,22 +97,22 @@ it('addLevelButton works', () => { levelGroup.querySelectorAll(`.${CONST.LABEL_CLASS}`) ); log.log(labels); - labels.forEach(label => { + labels.forEach((label) => { log.log(`${label.htmlFor}`); if (label.htmlFor.includes(CONST.VISITOR_WITHOUT_LEVEL)) { expect(label.htmlFor).toBe(`${CONST.VISITOR_WITHOUT_LEVEL}${newLevel}`); } else if (label.htmlFor.includes(CONST.DIFFICULTY_WITHOUT_LEVEL)) { expect(label.htmlFor).toBe( - `${CONST.DIFFICULTY_WITHOUT_LEVEL}${newLevel}`, + `${CONST.DIFFICULTY_WITHOUT_LEVEL}${newLevel}` ); } else if ( label.htmlFor.includes(CONST.REMOVE_LEVEL_BUTTON_ID_WITHOUT_LEVEL) ) { expect(label.htmlFor).toBe( - `${CONST.REMOVE_LEVEL_BUTTON_ID_WITHOUT_LEVEL}${newLevel}`, + `${CONST.REMOVE_LEVEL_BUTTON_ID_WITHOUT_LEVEL}${newLevel}` ); } else { - throw new Error('Did you add an extra label to DOM?'); + throw new Error("Did you add an extra label to DOM?"); } }); diff --git a/templates/panel/sitekey/add/ts/removeLevelButton/updateDom/updateLabel.ts b/templates/panel/sitekey/add/ts/removeLevelButton/updateDom/updateLabel.ts index f3f4555f..72e2787b 100644 --- a/templates/panel/sitekey/add/ts/removeLevelButton/updateDom/updateLabel.ts +++ b/templates/panel/sitekey/add/ts/removeLevelButton/updateDom/updateLabel.ts @@ -14,17 +14,17 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ -import CONST from '../../const'; -import log from '../../../../../../logger'; +import CONST from "../../const"; +import log from "../../../../../../logger"; /** update level lables to match new level */ -const updateLabels = (levelGroup: Element, newLevel: number) => { +const updateLabels = (levelGroup: Element, newLevel: number): void => { // rename labels const labels = >( - levelGroup.querySelectorAll(`label`) + levelGroup.querySelectorAll("label") ); log.log(labels); - labels.forEach(label => { + labels.forEach((label) => { log.log(`${label.htmlFor}`); const currentFor = label.htmlFor; if (currentFor.includes(CONST.VISITOR_WITHOUT_LEVEL)) { @@ -36,9 +36,9 @@ const updateLabels = (levelGroup: Element, newLevel: number) => { ) { label.htmlFor = `${CONST.REMOVE_LEVEL_BUTTON_ID_WITHOUT_LEVEL}${newLevel}`; } else { - if (currentFor != 'add') { + if (currentFor != "add") { throw new Error( - `Did you add an extra label to DOM? Found label with for: ${currentFor}`, + `Did you add an extra label to DOM? Found label with for: ${currentFor}` ); } } diff --git a/templates/panel/sitekey/add/ts/removeLevelButton/updateDom/updateLevelGroup.test.ts b/templates/panel/sitekey/add/ts/removeLevelButton/updateDom/updateLevelGroup.test.ts index 0fbb21fa..451b65a0 100644 --- a/templates/panel/sitekey/add/ts/removeLevelButton/updateDom/updateLevelGroup.test.ts +++ b/templates/panel/sitekey/add/ts/removeLevelButton/updateDom/updateLevelGroup.test.ts @@ -15,18 +15,16 @@ * along with this program. If not, see . */ -import getNumLevels from '../../levels/getNumLevels'; -import {getAddForm, trim} from '../../setupTests'; -import updateLevelGroup from './updateLevelGroup'; -import CONST from '../../const'; +import { trim} from "../../setupTests"; +import updateLevelGroup from "./updateLevelGroup"; +import CONST from "../../const"; -import log from '../../../../../../logger'; -import {MODE} from '../../../../../../logger'; +import log from "../../../../../../logger"; +import {MODE} from "../../../../../../logger"; -import {setupAddlevels} from './setupTests'; /** get initial form to test remove button functionality */ -export const labelLevel = (level: number) => { +export const labelLevel = (level: number): string => { return `
@@ -85,7 +83,7 @@ document.body.innerHTML = labelLevel(2); log.setMode(MODE.none); -it('update levelGroup works', () => { +it("update levelGroup works", () => { // removing level 2 const level = 2; const levelGroup = document.querySelector( diff --git a/templates/panel/sitekey/add/ts/removeLevelButton/updateDom/updateLevelGroup.ts b/templates/panel/sitekey/add/ts/removeLevelButton/updateDom/updateLevelGroup.ts index c91152c5..7fbf9bdf 100644 --- a/templates/panel/sitekey/add/ts/removeLevelButton/updateDom/updateLevelGroup.ts +++ b/templates/panel/sitekey/add/ts/removeLevelButton/updateDom/updateLevelGroup.ts @@ -14,10 +14,10 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ -import CONST from '../../const'; +import CONST from "../../const"; /** update level grup to match new level */ -const updateLevelGroup = (levelGroup: Element, newLevel: number) => +const updateLevelGroup = (levelGroup: Element, newLevel: number): string => (levelGroup.id = `${CONST.LEVEL_FIELDSET_ID_WITHOUT_LEVEL}${newLevel}`); export default updateLevelGroup; diff --git a/templates/panel/sitekey/add/ts/removeLevelButton/updateDom/updateRemoveButton.test.ts b/templates/panel/sitekey/add/ts/removeLevelButton/updateDom/updateRemoveButton.test.ts index 51f4c98d..e744feaa 100644 --- a/templates/panel/sitekey/add/ts/removeLevelButton/updateDom/updateRemoveButton.test.ts +++ b/templates/panel/sitekey/add/ts/removeLevelButton/updateDom/updateRemoveButton.test.ts @@ -15,15 +15,15 @@ * along with this program. If not, see . */ -import {trim} from '../../setupTests'; -import updateRemoveButton from './updateRemoveButton'; -import CONST from '../../const'; +import {trim} from "../../setupTests"; +import updateRemoveButton from "./updateRemoveButton"; +import CONST from "../../const"; -import log from '../../../../../../logger'; -import {MODE} from '../../../../../../logger'; +import log from "../../../../../../logger"; +import {MODE} from "../../../../../../logger"; /** get initial form to test remove button functionality */ -export const labelLevel = (level: number) => { +export const labelLevel = (level: number): string => { return `
@@ -83,7 +83,7 @@ document.body.innerHTML = labelLevel(level); log.setMode(MODE.none); -it('update remove button works', () => { +it("update remove button works", () => { // removing level 2 const levelGroup = document.getElementById( diff --git a/templates/panel/sitekey/add/ts/removeLevelButton/updateDom/updateRemoveButton.ts b/templates/panel/sitekey/add/ts/removeLevelButton/updateDom/updateRemoveButton.ts index 813ed726..cb406030 100644 --- a/templates/panel/sitekey/add/ts/removeLevelButton/updateDom/updateRemoveButton.ts +++ b/templates/panel/sitekey/add/ts/removeLevelButton/updateDom/updateRemoveButton.ts @@ -14,10 +14,10 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ -import CONST from '../../const'; +import CONST from "../../const"; /** update remove level button's ID */ -const updateRemoveButton = (levelGroup: Element, newLevel: number) => { +const updateRemoveButton = (levelGroup: Element, newLevel: number): void => { // rename button const button = ( levelGroup.querySelector(`.${CONST.REMOVE_LEVEL_BUTTON_CLASS}`) diff --git a/templates/panel/sitekey/add/ts/setupTests.ts b/templates/panel/sitekey/add/ts/setupTests.ts index 7fa8586d..7ecfde44 100644 --- a/templates/panel/sitekey/add/ts/setupTests.ts +++ b/templates/panel/sitekey/add/ts/setupTests.ts @@ -14,13 +14,13 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ -import getNumLevels from './levels/getNumLevels'; -import {Level} from './levels/index'; -import CONST from './const'; -import addLevelButtonAddEventListener from './addLevelButton'; +import getNumLevels from "./levels/getNumLevels"; +import { Level } from "./levels/index"; +import CONST from "./const"; +import addLevelButtonAddEventListener from "./addLevelButton"; /** get rid of all whitespaces, useful when comparing DOM states */ -export const trim = (s: string) => s.replace(/\s/g, ''); +export const trim = (s: string): string => s.replace(/\s/g, ""); export const level1: Level = { difficulty_factor: 200, @@ -43,7 +43,7 @@ export const level2: Level = { }; /** add level to DOM by filling add level form and clicking "Add" button */ -export const addLevel = (visitor: number, diff: number) => { +export const addLevel = (visitor: number, diff: number): void => { fillAddLevel(visitor, diff); const addLevelButton = ( document.querySelector(`.${CONST.ADD_LEVEL_BUTTON}`) @@ -54,8 +54,8 @@ export const addLevel = (visitor: number, diff: number) => { /** Fill add level form without clicking add button */ export const fillAddLevel = ( visitor: number | string, - diff: number | string, -) => { + diff: number | string +): void => { addLevelButtonAddEventListener(); const level = getNumLevels(); @@ -71,7 +71,11 @@ export const fillAddLevel = ( }; /** Fill add level form without clicking add button */ -export const editLevel = (level: number, visitor?: number, diff?: number) => { +export const editLevel = ( + level: number, + visitor?: number, + diff?: number +): void => { if (visitor !== undefined) { const visitorField = ( document.getElementById(`${CONST.VISITOR_WITHOUT_LEVEL}${level}`) @@ -88,18 +92,18 @@ export const editLevel = (level: number, visitor?: number, diff?: number) => { }; /** Fill description in add level form */ -export const fillDescription = (description: string) => { - const inputElement = document.getElementById('description'); +export const fillDescription = (description: string): void => { + const inputElement = document.getElementById("description"); inputElement.value = description; }; /** Fill duration in add level form */ -export const fillDuration = (duration: number | string) => { - const inputElement = document.getElementById('duration'); +export const fillDuration = (duration: number | string): void => { + const inputElement = document.getElementById("duration"); inputElement.value = duration.toString(); }; -export const getAddForm = () => ` +export const getAddForm = (): string => `

Add Sitekey @@ -171,7 +175,7 @@ export const getAddForm = () => ` `; /** get initial form to test remove button functionality */ -export const getRemoveButtonHTMLForm = () => { +export const getRemoveButtonHTMLForm = (): string => { return `

diff --git a/templates/panel/sitekey/delete/index.ts b/templates/panel/sitekey/delete/index.ts index b3e3204c..c89fa90d 100644 --- a/templates/panel/sitekey/delete/index.ts +++ b/templates/panel/sitekey/delete/index.ts @@ -15,16 +15,16 @@ * along with this program. If not, see . */ -import {getPassword} from '../../../auth/login/ts/'; -import FORM from '../../../auth/sudo/'; -import additionalData from '../../../components/additional-data'; -import registerShowPassword from '../../../components/showPassword'; +import { getPassword } from "../../../auth/login/ts/"; +import FORM from "../../../auth/sudo/"; +import additionalData from "../../../components/additional-data"; +import registerShowPassword from "../../../components/showPassword"; -import getFormUrl from '../../../utils/getFormUrl'; -import genJsonPayload from '../../../utils/genJsonPayload'; -import createError from '../../../components/error'; +import getFormUrl from "../../../utils/getFormUrl"; +import genJsonPayload from "../../../utils/genJsonPayload"; +import createError from "../../../components/error"; -import VIEWS from '../../../views/v1/routes'; +import VIEWS from "../../../views/v1/routes"; const submit = async (e: Event) => { e.preventDefault(); @@ -47,7 +47,7 @@ const submit = async (e: Event) => { } }; -export const index = () => { - FORM.get().addEventListener('submit', submit, true); +export const index = (): void => { + FORM.get().addEventListener("submit", submit, true); registerShowPassword(); }; diff --git a/templates/panel/sitekey/edit/edit.test.ts b/templates/panel/sitekey/edit/edit.test.ts index 5ac25bde..17d4a6ff 100644 --- a/templates/panel/sitekey/edit/edit.test.ts +++ b/templates/panel/sitekey/edit/edit.test.ts @@ -15,17 +15,17 @@ * along with this program. If not, see . */ -import getNumLevels from '../add/ts/levels/getNumLevels'; -import {addLevel} from '../add/ts/setupTests'; -import setup from '../../../components/error/setUpTests'; -import * as SETUP from './setupTest'; +import getNumLevels from "../add/ts/levels/getNumLevels"; +import {addLevel} from "../add/ts/setupTests"; +import setup from "../../../components/error/setUpTests"; +import * as SETUP from "./setupTest"; document.body.innerHTML = SETUP.EDIT_FORM; document.body.appendChild(setup()); jest.useFakeTimers(); -it('edit sitekey works', () => { +it("edit sitekey works", () => { expect(getNumLevels()).toBe(2); // add a level addLevel(5, 6); diff --git a/templates/panel/sitekey/edit/index.ts b/templates/panel/sitekey/edit/index.ts index b067ac86..110175d1 100644 --- a/templates/panel/sitekey/edit/index.ts +++ b/templates/panel/sitekey/edit/index.ts @@ -14,31 +14,31 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ -import * as Add from '../add/ts/form/'; -import addLevelButtonAddEventListener from '../add/ts/addLevelButton'; -import {addRemoveLevelButtonEventListenerAll} from '../add/ts/removeLevelButton'; -import getNumLevels from '../add/ts/levels/getNumLevels'; -import validateLevel from '../add/ts/levels/validateLevel'; -import * as UpdateLevel from '../add/ts/levels/updateLevel'; -import validateDescription from '../add/ts/form/validateDescription'; -import validateDuration from '../add/ts/form/validateDuration'; -import {LEVELS} from '../add/ts/levels'; +import * as Add from "../add/ts/form/"; +import addLevelButtonAddEventListener from "../add/ts/addLevelButton"; +import { addRemoveLevelButtonEventListenerAll } from "../add/ts/removeLevelButton"; +import getNumLevels from "../add/ts/levels/getNumLevels"; +import validateLevel from "../add/ts/levels/validateLevel"; +import * as UpdateLevel from "../add/ts/levels/updateLevel"; +import validateDescription from "../add/ts/form/validateDescription"; +import validateDuration from "../add/ts/form/validateDuration"; +import { LEVELS } from "../add/ts/levels"; -import getFormUrl from '../../../utils/getFormUrl'; -import genJsonPayload from '../../../utils/genJsonPayload'; -import createError from '../../../components/error'; -import LazyElement from '../../../utils/lazyElement'; +import getFormUrl from "../../../utils/getFormUrl"; +import genJsonPayload from "../../../utils/genJsonPayload"; +import createError from "../../../components/error"; +import LazyElement from "../../../utils/lazyElement"; -import VIEWS from '../../../views/v1/routes'; +import VIEWS from "../../../views/v1/routes"; -const BTN_ID = 'sitekey-form__submit'; +const BTN_ID = "sitekey-form__submit"; const BTN = new LazyElement(BTN_ID); const submit = async (e: Event) => { e.preventDefault(); const description = validateDescription(e); - const duration = validateDuration(e); + const duration = validateDuration(); const formUrl = getFormUrl(Add.FORM); @@ -66,7 +66,7 @@ const submit = async (e: Event) => { }; const addSubmitEventListener = () => { - Add.FORM.addEventListener('submit', submit, true); + Add.FORM.addEventListener("submit", submit, true); }; const bootstrapLevels = () => { @@ -78,7 +78,7 @@ const bootstrapLevels = () => { } }; -export const index = () => { +export const index = (): void => { addSubmitEventListener(); addLevelButtonAddEventListener(); bootstrapLevels(); diff --git a/templates/panel/sitekey/list/ts/index.ts b/templates/panel/sitekey/list/ts/index.ts index 0a0ad9f1..a5e6bc44 100644 --- a/templates/panel/sitekey/list/ts/index.ts +++ b/templates/panel/sitekey/list/ts/index.ts @@ -14,17 +14,17 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ -import CopyIcon from '../../../../components/clipboard/'; +import CopyIcon from "../../../../components/clipboard/"; -const SITEKEY_COPY_ICON = `sitekey__copy-icon`; -const SITEKEY_COPY_DONE_ICON = `sitekey__copy-done-icon`; +const SITEKEY_COPY_ICON = "sitekey__copy-icon"; +const SITEKEY_COPY_DONE_ICON = "sitekey__copy-done-icon"; -export const index = () => { +export const index = (): void => { const image = document.querySelectorAll(`.${SITEKEY_COPY_ICON}`); image.forEach((img: HTMLElement) => { if (!img.classList.contains(SITEKEY_COPY_ICON)) { throw new Error( - 'This method should only be called when sitekey copy button/icon is clicked', + "This method should only be called when sitekey copy button/icon is clicked" ); } const sitekey = img.dataset.sitekey; diff --git a/templates/panel/sitekey/view/ts/index.ts b/templates/panel/sitekey/view/ts/index.ts index 458b1d19..7c215f1a 100644 --- a/templates/panel/sitekey/view/ts/index.ts +++ b/templates/panel/sitekey/view/ts/index.ts @@ -14,4 +14,4 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ -export const index = () => {}; +//export const index = () => {}; diff --git a/templates/panel/ts/index.ts b/templates/panel/ts/index.ts index 7263718e..596665f0 100644 --- a/templates/panel/ts/index.ts +++ b/templates/panel/ts/index.ts @@ -15,8 +15,6 @@ * along with this program. If not, see . */ -import * as listSitekeys from '../sitekey/list/ts/'; +import * as listSitekeys from "../sitekey/list/ts/"; -export const index = () => { - listSitekeys.index(); -}; +export const index = (): void => listSitekeys.index(); diff --git a/templates/router.test.ts b/templates/router.test.ts index 138f1545..e11d5eb1 100644 --- a/templates/router.test.ts +++ b/templates/router.test.ts @@ -15,29 +15,29 @@ * along with this program. If not, see . */ -import {Router} from './router'; +import {Router} from "./router"; -'use strict'; +"use strict"; const result = { - result: '', + result: "", }; -const panelResult = 'hello from panel'; -const panelRoute = '/panel'; +const panelResult = "hello from panel"; +const panelRoute = "/panel"; const panel = () => (result.result = panelResult); -const settingsRoute = '/sitekey/'; -const settingsResult = 'hello from settings'; +const settingsRoute = "/sitekey/"; +const settingsResult = "hello from settings"; const settings = () => (result.result = settingsResult); -const patternRoute = '/sitekey/[A-Z,a-z,0-9,_]+/'; -const examplePatternRoute = '/sitekey/alksdjakdjadajkhdjahrjke234/'; -const patterResult = 'hello from pattern route'; +const patternRoute = "/sitekey/[A-Z,a-z,0-9,_]+/"; +const examplePatternRoute = "/sitekey/alksdjakdjadajkhdjahrjke234/"; +const patterResult = "hello from pattern route"; const pattern = () => (result.result = patterResult); -const UriExistsErr = 'URI exists'; -const emptyUriErr = 'uri is empty'; +const UriExistsErr = "URI exists"; +const emptyUriErr = "uri is empty"; const unregisteredRouteErr = "Route isn't registered"; const router = new Router(); @@ -45,23 +45,23 @@ router.register(patternRoute, pattern); router.register(panelRoute, panel); router.register(settingsRoute, settings); -it('checks if Router works', () => { - window.history.pushState({}, '', examplePatternRoute); +it("checks if Router works", () => { + window.history.pushState({}, "", examplePatternRoute); router.route(); expect(result.result).toBe(patterResult); window.history.pushState( {}, - '', + "", examplePatternRoute.slice(0, examplePatternRoute.length - 1), ); router.route(); expect(result.result).toBe(patterResult); - window.history.pushState({}, 'Settings', settingsRoute); + window.history.pushState({}, "Settings", settingsRoute); router.route(); expect(result.result).toBe(settingsResult); - window.history.pushState({}, 'Panel', panelRoute); + window.history.pushState({}, "Panel", panelRoute); router.route(); expect(result.result).toBe(panelResult); @@ -74,14 +74,14 @@ it('checks if Router works', () => { // empty URI registration try { - router.register(' ', settings); + router.register(" ", settings); } catch (e) { expect(e.message).toBe(emptyUriErr); } // routing to unregistered route try { - window.history.pushState({}, `Page Doesn't Exist`, `/page/doesnt/exist`); + window.history.pushState({}, "Page Doesn't Exist", "/page/doesnt/exist"); router.route(); } catch (e) { expect(e.message).toBe(unregisteredRouteErr); @@ -89,7 +89,7 @@ it('checks if Router works', () => { // routing to unregistered route try { - window.history.pushState({}, `Page Doesn't Exist`, `/sitekey/;asd;lasdj`); + window.history.pushState({}, "Page Doesn't Exist", "/sitekey/;asd;lasdj"); router.route(); } catch (e) { expect(e.message).toBe(unregisteredRouteErr); diff --git a/templates/router.ts b/templates/router.ts index 229ccccf..74930fd2 100644 --- a/templates/router.ts +++ b/templates/router.ts @@ -19,11 +19,11 @@ const normalizeUri = (uri: string) => { uri = uri.trim(); if (uri.length == 0) { - throw new Error('uri is empty'); + throw new Error("uri is empty"); } - let uriLength = uri.length; - if (uri[uriLength - 1] == '/') { + const uriLength = uri.length; + if (uri[uriLength - 1] == "/") { uri = uri.slice(0, uriLength - 1); } return uri; @@ -51,14 +51,14 @@ export class Router { * @param {function} fn: - function to be registered when window.locatin.path * matches uri * */ - register(uri: string, fn: () => void) { + register(uri: string, fn: () => void): void { uri = normalizeUri(uri); - let pattern = new RegExp(`^${uri}$`); + const pattern = new RegExp(`^${uri}$`); - let patterString = pattern.toString(); + const patterString = pattern.toString(); if ( - this.routes.find(route => { + this.routes.find((route) => { if (route.pattern.toString() == patterString) { return true; } else { @@ -66,7 +66,7 @@ export class Router { } }) ) { - throw new Error('URI exists'); + throw new Error("URI exists"); } const route: routeTuple = { @@ -80,13 +80,13 @@ export class Router { * executes registered function with route * matches window.pathname.location * */ - route() { + route(): void { const path = normalizeUri(window.location.pathname); let fn: undefined | (() => void); if ( - this.routes.find(route => { + this.routes.find((route) => { if (path.match(route.pattern)) { fn = route.fn; return true; diff --git a/templates/setUpTests.ts b/templates/setUpTests.ts index 2be42567..2c76ddf9 100644 --- a/templates/setUpTests.ts +++ b/templates/setUpTests.ts @@ -16,7 +16,7 @@ */ /** get login form HTML */ -export const getLoginFormHtml = () => +export const getLoginFormHtml = (): string => `