functions.ts
1.2 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
import { BASE_URL } from "@src/config/url";
import { setState } from "@src/store/state";
import { togglePageLoader } from "@src/components/page.loader";
import { randomTransition } from "@src/components/page.transition";
import { renderApp } from "@src/App";
export function getElement(cssSelector: string) {
return document.querySelector(cssSelector);
}
export function getElementsAll(cssSelector: string) {
return document.querySelectorAll(cssSelector);
}
export function getPathname() {
return window.location.pathname.split("/")[1];
}
export function warningEmptyId() {
alert("아이디를 입력하세요");
togglePageLoader(false);
}
export function pendingFetch() {
togglePageLoader(true);
}
export function successFetch(result: any, pathname: string) {
randomTransition();
setState(result);
setTimeout(() => {
renderApp(pathname);
togglePageLoader(false);
}, 1200);
}
export function failFetch() {
togglePageLoader(false);
alert("fail to search");
}
export async function fetchByURL(url: string) {
const result = await (await fetch(BASE_URL + url)).json();
console.log("res!", result);
if (result) {
successFetch(result, "main");
} else {
failFetch();
}
}