mv add form into advance

This commit is contained in:
realaravinth
2021-12-10 06:16:38 +05:30
parent 5afa531bb8
commit b2297eab6d
42 changed files with 62 additions and 62 deletions

View File

@@ -0,0 +1,54 @@
/*
* Copyright (C) 2021 Aravinth Manivannan <realaravinth@batsense.net>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import getLevelFields from "./getLevelFields";
import {
getAddForm,
level1,
level2,
fillAddLevel,
addLevel,
} from "../setupTests";
document.body.innerHTML = getAddForm();
const visNumErr = "visitor can contain nubers only";
const diffNumErr = "difficulty can contain nubers only";
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);
getLevelFields(2);
} catch (e) {
expect(e.message).toBe(visNumErr);
}
// Nan difficulty_factor
try {
fillAddLevel(level2.visitor_threshold, "fooasdads");
getLevelFields(2);
} catch (e) {
expect(e.message).toBe(diffNumErr);
}
addLevel(level2.visitor_threshold, level2.difficulty_factor);
expect(getLevelFields(2)).toEqual(level2);
});

View File

@@ -0,0 +1,57 @@
/*
* Copyright (C) 2021 Aravinth Manivannan <realaravinth@batsense.net>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { Level } from "./index";
import CONST from "../const";
import log from "../../../../../../logger";
/** Fetches level from DOM using the ID passesd and validates */
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();
const visitorElement = <HTMLInputElement>document.getElementById(visitorID);
const difficultyElement = <HTMLInputElement>(
document.getElementById(difficultyID)
);
const visitor_threshold = parseInt(visitorElement.value);
const difficulty_factor = parseInt(difficultyElement.value);
if (Number.isNaN(visitor_threshold)) {
throw new Error("visitor can contain nubers only");
}
if (Number.isNaN(difficulty_factor)) {
throw new Error("difficulty can contain nubers only");
}
const level: Level = {
difficulty_factor,
visitor_threshold,
};
log.debug(
`[getLevelFields.ts] visitor: ${visitor_threshold} difficulty: ${difficulty_factor}`
);
return level;
};
export default getLevelFields;

View File

@@ -0,0 +1,29 @@
/*
* Copyright (C) 2021 Aravinth Manivannan <realaravinth@batsense.net>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import getNumLevels from "./getNumLevels";
import {getAddForm, addLevel} from "../setupTests";
document.body.innerHTML = getAddForm();
it("get num levels works", () => {
expect(getNumLevels()).toBe(1);
addLevel(2, 4);
expect(getNumLevels()).toBe(2);
addLevel(4, 9);
expect(getNumLevels()).toBe(3);
});

View File

@@ -0,0 +1,32 @@
/*
* Copyright (C) 2021 Aravinth Manivannan <realaravinth@batsense.net>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import CONST from "../const";
import log from "../../../../../../logger";
/** returns number of level input fields currently in DOM */
const getNumLevels = (): number => {
let numLevels = 0;
document
.querySelectorAll(`.${CONST.LEVEL_CONTAINER_CLASS}`)
.forEach(() => numLevels++);
log.debug(`[getNumLevels]: numLevels: ${numLevels}`);
return numLevels;
};
export default getNumLevels;

View File

@@ -0,0 +1,145 @@
/*
* Copyright (C) 2021 Aravinth Manivannan <realaravinth@batsense.net>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import log from "../../../../../../logger";
/** Datatype represenging an mCaptcha level */
export type Level = {
difficulty_factor: number;
visitor_threshold: number;
};
/** Datatype representing a collection of mCaptcha levels */
class Levels {
levels: Array<Level>;
constructor() {
this.levels = [];
}
add = (newLevel: Level) => {
log.debug(`[levels/index.ts] levels lenght: ${this.levels.length}`);
if (newLevel.difficulty_factor <= 0) {
throw new Error(
`Level ${this.levels.length}'s difficulty must be greater than zero`,
);
}
if (newLevel.visitor_threshold <= 0) {
throw new Error(
`Level ${this.levels.length}'s visitors must be greater than zero`,
);
}
if (this.levels.length == 0) {
this.levels.push(newLevel);
return true;
}
let count = 1;
this.levels.forEach(level => {
if (level.visitor_threshold >= newLevel.visitor_threshold) {
const msg = `Level ${this.levels.length}'s visitor count should be greater than previous levels(Level ${count} is greater)`;
throw new Error(msg);
} else if (level.difficulty_factor >= newLevel.difficulty_factor) {
const msg = `Level ${this.levels.length} difficulty should be greater than previous levels(Level ${count} is greater)`;
throw new Error(msg);
} else {
count++;
}
});
this.levels.push(newLevel);
};
get = () => this.levels;
}
/** Singleton that does manipulations on Levels object */
export const LEVELS = (function() {
const levels = new Levels();
return {
/** get levels */
getLevels: () => levels.get(),
/** add new level */
add: (newLevel: Level) => levels.add(newLevel),
/** update levels */
update: (updateLevel: Level, id: number) => {
const tmpLevel = new Levels();
id -= 1;
try {
for (let i = 0; i < levels.levels.length; i++) {
if (id == i) {
tmpLevel.add(updateLevel);
} else {
tmpLevel.add(levels.levels[i]);
}
}
levels.levels = tmpLevel.levels;
log.debug("post update:");
LEVELS.print();
return true;
} catch (e) {
log.debug(e);
return false;
}
},
print: () =>
levels.levels.forEach(level =>
log.debug(
`difficulty_factor: ${level.difficulty_factor} visitor ${level.visitor_threshold}`,
),
),
/** remove level */
remove: (id: number) => {
log.debug(`[LEVELS] received order to remove ${id} element`);
const tmpLevel = new Levels();
id -= 1;
try {
for (let i = 0; i < levels.levels.length; i++) {
if (id != i) {
tmpLevel.add(levels.levels[i]);
} else {
log.debug(`[LEVELS] removing ${i} element`);
const rmElement = levels.levels[i];
log.debug(
`[LEVELS] removing element:
difficulty_factor: ${rmElement.difficulty_factor}
visitor_threshold: ${rmElement.visitor_threshold}`,
);
}
}
levels.levels = tmpLevel.levels;
log.debug("Post remove:");
LEVELS.print();
return true;
} catch (e) {
log.debug(e);
return false;
}
},
};
})();

View File

@@ -0,0 +1,82 @@
/*
* Copyright (C) 2021 Aravinth Manivannan <realaravinth@batsense.net>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
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 zeroVisError = "visitors must be greater than zero";
const zeroDiffError = "difficulty must be greater than zero";
const zeroVis: Level = {
difficulty_factor: 10,
visitor_threshold: 0,
};
const zeroDiff: Level = {
difficulty_factor: 0,
visitor_threshold: 10,
};
it("LEVELS works", () => {
// add level
LEVELS.add(level1);
expect(LEVELS.getLevels()).toEqual([level1]);
// add visitor count < prev level
try {
LEVELS.add(level1visErr);
} catch (e) {
expect(e.message).toContain(visitorErr);
}
// add difficulty < prev level
try {
LEVELS.add(level1diffErr);
} catch (e) {
expect(e.message).toContain(difficultyErr);
}
// add second level
LEVELS.add(level2);
expect(LEVELS.getLevels()).toEqual([level1, level2]);
// update level
const newLevel2 = level2;
newLevel2.difficulty_factor = 8000;
LEVELS.update(newLevel2, 2);
expect(LEVELS.getLevels()).toEqual([level1, newLevel2]);
// update second level
LEVELS.remove(1);
expect(LEVELS.getLevels()).toEqual([newLevel2]);
// visitor is 0
try {
LEVELS.add(zeroVis);
} catch (e) {
expect(e.message).toContain(zeroVisError);
}
// difficulty is 0
try {
LEVELS.add(zeroDiff);
} catch (e) {
expect(e.message).toContain(zeroDiffError);
}
});

View File

@@ -0,0 +1,62 @@
/*
* Copyright (C) 2021 Aravinth Manivannan <realaravinth@batsense.net>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import CONST from "../const";
import getLevelFields from "./getLevelFields";
import { LEVELS } from "./index";
import createError from "../../../../../../components/error";
/** on-change event handler to update level */
const updateLevel = (e: Event): void => {
const target = <HTMLInputElement>e.target;
const id = target.id;
let level;
if (id.includes(CONST.VISITOR_WITHOUT_LEVEL)) {
level = parseInt(id.slice(CONST.VISITOR_WITHOUT_LEVEL.length));
}
if (id.includes(CONST.DIFFICULTY_WITHOUT_LEVEL)) {
level = parseInt(id.slice(CONST.DIFFICULTY_WITHOUT_LEVEL.length));
}
if (Number.isNaN(level)) {
console.error("[updateLevel.ts] level # computed is not correct, got NaN");
}
try {
const updatedLevel = getLevelFields(level);
LEVELS.update(updatedLevel, level);
} catch (e) {
createError(e.message);
}
};
/** registers on-change event handlers to update levels */
export const register = (id: number): void => {
const visitorID = CONST.VISITOR_WITHOUT_LEVEL + id.toString();
const difficultyID = CONST.DIFFICULTY_WITHOUT_LEVEL + id.toString();
const visitorElement = <HTMLInputElement>document.getElementById(visitorID);
const difficultyElement = <HTMLInputElement>(
document.getElementById(difficultyID)
);
visitorElement.addEventListener("input", updateLevel, false);
difficultyElement.addEventListener("input", updateLevel, false);
};

View File

@@ -0,0 +1,40 @@
/*
* Copyright (C) 2021 Aravinth Manivannan <realaravinth@batsense.net>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
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", () => {
// null error
expect(validateLevel(1)).toEqual(false);
fillAddLevel(level1.visitor_threshold, level1.difficulty_factor);
expect(validateLevel(1)).toEqual(true);
// zero visitor error
fillAddLevel(0, level1.difficulty_factor);
expect(validateLevel(1)).toEqual(false);
// zero difficulty error
fillAddLevel(level1.visitor_threshold, 0);
expect(validateLevel(1)).toEqual(false);
});

View File

@@ -0,0 +1,37 @@
/*
* Copyright (C) 2021 Aravinth Manivannan <realaravinth@batsense.net>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
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): boolean => {
try {
const level = getLevelFields(id);
LEVELS.add(level);
return true;
} catch (e) {
createError(e.message);
return false;
}
};
export default validateLevel;