Skip to content

Commit

Permalink
adding type and number check for submitHandler and init function
Browse files Browse the repository at this point in the history
  • Loading branch information
kaleabAlemayehu committed Nov 3, 2022
1 parent 2306452 commit 976561b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
29 changes: 21 additions & 8 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,19 @@ const state = {
number: '',
fact: '',
};

const init = () => {
state.fact = '';
state.type = '';
state.number = '';
};
const randomHandler = async (e) => {
e.preventDefault();
const randomSearch = new Search('random', state.type);
try {
state.fact = await randomSearch.getFact();
if (state.fact.found) {
searchView.renderFact(state.fact.text);
init();
}
} catch (error) {
console.error(error);
Expand Down Expand Up @@ -45,16 +50,24 @@ types.forEach((type) => {
const submitHandler = async (e) => {
e.preventDefault();
state.number = dom.input.value;
if (state.number || state.number === 0) {
const submitSearch = new Search(state.number, state.type);
try {
state.fact = await submitSearch.getFact();
// searchView.renderFact(state.fact);
} catch (error) {
console.error(error);
if (state.type) {
if (state.number || state.number === 0) {
const submitSearch = new Search(state.number, state.type);
try {
state.fact = await submitSearch.getFact();
searchView.renderFact(state.fact);
} catch (error) {
console.error(error);
}
} else {
searchView.numberError();
}
} else {
searchView.typeError();
}
};
['click', 'touchstart'].forEach((e) => {
dom.submit.addEventListener(e, submitHandler);
});

init();
8 changes: 8 additions & 0 deletions src/views/searchView.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,11 @@ export const renderFact = (fact) => {
console.log(fact);
dom.fact.innerText = fact;
};

export const typeError = () => {
dom.fact.innerText = 'select type first broooooooo';
};

export const numberError = () => {
dom.fact.innerText = 'enter the number broooooooo';
};

0 comments on commit 976561b

Please sign in to comment.