error handling levels

This commit is contained in:
realaravinth
2021-06-29 23:20:54 +05:30
parent f10741d09f
commit 96fafb339c
5 changed files with 15 additions and 4 deletions

View File

@@ -25,6 +25,8 @@ import VIEWS from '../../../../../views/v1/routes';
import validateDescription from './validateDescription';
import validateDuration from './validateDuration';
import createError from '../../../../../components/error';
const SITE_KEY_FORM_CLASS = 'sitekey-form';
const FORM = <HTMLFormElement>document.querySelector(`.${SITE_KEY_FORM_CLASS}`);
@@ -53,12 +55,11 @@ const submit = async (e: Event) => {
const res = await fetch(formUrl, genJsonPayload(payload));
if (res.ok) {
alert('success');
const data = await res.json();
window.location.assign(VIEWS.listSitekey(data.key));
} else {
const err = await res.json();
alert(`error: ${err.error}`);
createError(err.error);
}
};

View File

@@ -19,6 +19,8 @@ import validateDescription from './validateDescription';
import {getAddForm, fillDescription} from '../setupTests';
import {mockAlert} from '../../../../../setUpTests';
import setup from '../../../../../components/error/setUpTests';
mockAlert();
document.body.innerHTML = getAddForm();
@@ -26,6 +28,7 @@ document.body.innerHTML = getAddForm();
const emptyErr = "can't be empty";
it('validateDescription workds', () => {
document.querySelector('body').appendChild(setup());
try {
const event = new Event('submit');
validateDescription(event);

View File

@@ -19,6 +19,8 @@ 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) => {
const target = <HTMLInputElement>e.target;
@@ -41,7 +43,7 @@ const updateLevel = (e: Event) => {
const updatedLevel = getLevelFields(level);
LEVELS.update(updatedLevel, level);
} catch (e) {
alert(e);
createError(e);
}
};