add new site accepts duration

This commit is contained in:
realaravinth
2021-05-04 11:07:18 +05:30
parent e83a362e75
commit 1e1ec187dc
6 changed files with 38 additions and 7 deletions

View File

@@ -34,7 +34,7 @@ pub mod routes {
impl Docs { impl Docs {
pub const fn new() -> Self { pub const fn new() -> Self {
Docs { Docs {
home: "/docs", home: "/docs/",
spec: "/docs/openapi.json", spec: "/docs/openapi.json",
assets: "/docs/{_:.*}", assets: "/docs/{_:.*}",
} }

View File

@@ -28,6 +28,7 @@ pub struct IndexPage<'a> {
pub levels: usize, pub levels: usize,
pub form_title: &'a str, pub form_title: &'a str,
pub form_description: &'a str, pub form_description: &'a str,
pub form_duration: usize,
} }
const COMPONENT: &str = "Add Site Key"; const COMPONENT: &str = "Add Site Key";
@@ -40,6 +41,7 @@ impl<'a> Default for IndexPage<'a> {
levels: 1, levels: 1,
form_description: "", form_description: "",
form_title: "Add Site Key", form_title: "Add Site Key",
form_duration: 30,
} }
} }
} }

View File

@@ -14,6 +14,20 @@
/> />
</label> </label>
<label class="sitekey-form__label" for="duration">
Cooldown Duratoin(in seconds)
<input
class="sitekey-form__input"
type="number"
name="duration"
id="duration"
min=0
required
value="<.= form_duration .>"
/>
</label>
<. for level in 1..=levels { .> <. for level in 1..=levels { .>
<. if level == levels { .> <. if level == levels { .>
<. include!("./add-level.html"); .> <. include!("./add-level.html"); .>

View File

@@ -15,12 +15,12 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
import CONST from './const'; import {LEVELS} from './levels';
import getNumLevels from './levels/getNumLevels';
import isBlankString from '../../utils/isBlankString'; import isBlankString from '../../utils/isBlankString';
import getFormUrl from '../../utils/getFormUrl'; import getFormUrl from '../../utils/getFormUrl';
import genJsonPayload from '../../utils/genJsonPayload'; import genJsonPayload from '../../utils/genJsonPayload';
import {LEVELS} from './levels'; import isNumber from '../../utils/isNumber';
import VIEWS from '../../views/v1/routes'; import VIEWS from '../../views/v1/routes';
@@ -55,11 +55,24 @@ const validateDescription = (e: Event) => {
isBlankString(val, filed, e); isBlankString(val, filed, e);
}; };
const validateDuration = (e: Event) => {
const duartionElement = <HTMLInputElement>document.getElementById('duration');
const duration = parseInt(duartionElement.value);
if (!isNumber(duration) || Number.isNaN(duration)) {
throw new Error('duration can contain nubers only');
}
if (duration <= 0) {
throw new Error('duration must be greater than zero');
}
return duration;
};
const submit = async (e: Event) => { const submit = async (e: Event) => {
e.preventDefault(); e.preventDefault();
validateDescription(e); validateDescription(e);
// validateLevels(e); const duration = validateDuration(e);
const formUrl = getFormUrl(FORM); const formUrl = getFormUrl(FORM);
@@ -68,6 +81,7 @@ const submit = async (e: Event) => {
const payload = { const payload = {
levels: levels, levels: levels,
duration,
}; };
console.debug(`[form submition] json payload: ${JSON.stringify(payload)}`); console.debug(`[form submition] json payload: ${JSON.stringify(payload)}`);

View File

@@ -34,11 +34,11 @@ const getLevelFields = (id: number) => {
const visitor_threshold = parseInt(visitorElement.value); const visitor_threshold = parseInt(visitorElement.value);
const difficulty_factor = parseInt(difficultyElement.value); const difficulty_factor = parseInt(difficultyElement.value);
if (!isNumber(visitor_threshold) || Number.isNaN(visitor_threshold)) { if (Number.isNaN(visitor_threshold)) {
throw new Error('visitor can contain nubers only'); throw new Error('visitor can contain nubers only');
} }
if (!isNumber(difficulty_factor) || Number.isNaN(difficulty_factor)) { if (Number.isNaN(difficulty_factor)) {
throw new Error('difficulty can contain nubers only'); throw new Error('difficulty can contain nubers only');
} }

View File

@@ -98,6 +98,7 @@ export const LEVELS = (function() {
} }
return true; return true;
} catch (e) { } catch (e) {
console.log(e);
return false; return false;
} }
}, },