addEvent.ts
2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/**
* @author : wonseog
* @date : 2021/03/10
* @description : 버튼 이벤트, Route 이벤트 등록 모음
**/
import {BASE_URL} from "@src/config/url";
import {randomTransition} from "@src/components/pageTransition";
import {setState} from "@src/store/state";
export const addEvent = async (
e : Event,
$App : Element | null,
App : (pathname : string) => string
) => {
e.stopPropagation();
/* add navigation click event */
if((e.target as HTMLAnchorElement).matches("[data-link]")){
const href : string= (e.target as HTMLAnchorElement).href.split(BASE_URL)[1];
e.preventDefault();
setTimeout(()=>{
$App && ($App.innerHTML = App(href));
},1200);
randomTransition();
}
/* add GET event for button */
else if((e.target as HTMLButtonElement).id === 'search-button') {
let result: any= null;
const insta_id = (document.querySelector('#id-input') as HTMLInputElement).value;
if(insta_id){
try{
result = await (await fetch(BASE_URL + 'search?insta_id=' + insta_id)).json();
} catch (e){
console.log(e);
} finally {
console.log('res!',result);
result && $App && (()=>{
randomTransition();
setState(result);
setTimeout(()=>{
$App.innerHTML = App('main');
},1200)
})();
}
} else {
alert('아이디를 입력하세요');
}
}
/* add POST event for button */
else if((e.target as HTMLButtonElement).id === 'update-button') {
let result: any= null;
const insta_id = (document.querySelector('#id-input') as HTMLInputElement).value;
if(insta_id){
try{
result = await (await fetch(BASE_URL + 'update?insta_id=' + insta_id)).json();
} catch (e){
console.log(e);
} finally {
console.log(result);
result && $App && (()=>{
randomTransition();
setState(result);
setTimeout(()=>{
$App.innerHTML = App('main');
},1200)
})();
}
} else {
alert('아이디를 입력하세요');
}
}
}