functions.ts
1.44 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
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 formatNumber(number: number): string {
if (`${number}`.length > 6) {
return `${Math.floor(number / 1000000)}m`;
} else if (`${number}`.length > 3) {
return `${Math.floor(number / 1000)}k`;
} else {
return `${number}`;
}
}
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();
}
}