1seok2

refactor: seperate components by function

...@@ -3,8 +3,8 @@ ...@@ -3,8 +3,8 @@
3 .vscode/** 3 .vscode/**
4 **/.idea/ 4 **/.idea/
5 5
6 +crawler/chromedriver_88
6 crawler/chromedriver 7 crawler/chromedriver
7 -crawler/chromedriver.exe
8 **/*.exe 8 **/*.exe
9 package-lock.json 9 package-lock.json
10 __pycache__/ 10 __pycache__/
......
...@@ -4,27 +4,10 @@ ...@@ -4,27 +4,10 @@
4 * @description : 버튼 이벤트, Route 이벤트 등록 모음 4 * @description : 버튼 이벤트, Route 이벤트 등록 모음
5 **/ 5 **/
6 6
7 -import { linkEvent, searchEvent, updateEvent } from "./event.list"; 7 +export default function addEventWithElementByType(
8 - 8 + element: HTMLElement,
9 -export const addEvent = async ( 9 + eventType: string,
10 - e: Event, 10 + handler: (e: Event) => void
11 - $App: Element, 11 +) {
12 - App: (pathname: string) => string 12 + element.addEventListener(eventType, handler);
13 -) => { 13 +}
14 - e.stopPropagation();
15 -
16 - /* add navigation click event */
17 - if ((e.target as HTMLAnchorElement).matches("[data-link]")) {
18 - linkEvent(e);
19 - }
20 -
21 - if ((e.target as HTMLButtonElement).id === "search-button") {
22 - /* add GET event for button */
23 - searchEvent();
24 - }
25 -
26 - if ((e.target as HTMLButtonElement).id === "update-button") {
27 - /* add POST event for button */
28 - updateEvent();
29 - }
30 -};
......
...@@ -21,8 +21,8 @@ export function linkEvent(e: Event) { ...@@ -21,8 +21,8 @@ export function linkEvent(e: Event) {
21 randomTransition(); 21 randomTransition();
22 } 22 }
23 23
24 -export async function searchEvent() { 24 +export async function handleFetchById(url: string, cssSelector: string) {
25 - const insta_id = (getElement("#id-input") as HTMLInputElement).value; 25 + const insta_id = (getElement(cssSelector) as HTMLInputElement).value;
26 26
27 pendingFetch(); 27 pendingFetch();
28 28
...@@ -31,18 +31,5 @@ export async function searchEvent() { ...@@ -31,18 +31,5 @@ export async function searchEvent() {
31 return; 31 return;
32 } 32 }
33 33
34 - await fetchByURL("search?insta_id=" + insta_id); 34 + await fetchByURL(url + insta_id);
35 -}
36 -
37 -export async function updateEvent() {
38 - const insta_id = (getElement("#id-input") as HTMLInputElement).value;
39 -
40 - pendingFetch();
41 -
42 - if (!insta_id) {
43 - warningEmptyId();
44 - return;
45 - }
46 -
47 - await fetchByURL("update?insta_id=" + insta_id);
48 } 35 }
......
1 -export const formatNumber = (number : number) : string => {
2 - if(`${number}`.length > 6) {
3 - return `${Math.floor(number / 1000000)}m`
4 - }
5 - else if(`${number}`.length > 3) {
6 - return `${Math.floor(number / 1000)}k`
7 - } else {
8 - return `${number}`;
9 - }
10 -}
...\ No newline at end of file ...\ No newline at end of file
...@@ -4,6 +4,16 @@ import { togglePageLoader } from "@src/components/page.loader"; ...@@ -4,6 +4,16 @@ import { togglePageLoader } from "@src/components/page.loader";
4 import { randomTransition } from "@src/components/page.transition"; 4 import { randomTransition } from "@src/components/page.transition";
5 import { renderApp } from "@src/App"; 5 import { renderApp } from "@src/App";
6 6
7 +export function formatNumber(number: number): string {
8 + if (`${number}`.length > 6) {
9 + return `${Math.floor(number / 1000000)}m`;
10 + } else if (`${number}`.length > 3) {
11 + return `${Math.floor(number / 1000)}k`;
12 + } else {
13 + return `${number}`;
14 + }
15 +}
16 +
7 export function getElement(cssSelector: string) { 17 export function getElement(cssSelector: string) {
8 return document.querySelector(cssSelector); 18 return document.querySelector(cssSelector);
9 } 19 }
......
1 +export { default as color } from "./theme";
1 +export default {
2 + color: {
3 + black: "#333",
4 + },
5 +};
...@@ -4,23 +4,28 @@ ...@@ -4,23 +4,28 @@
4 * @description : 현재 pathname을 파악 후 App으로 전달 4 * @description : 현재 pathname을 파악 후 App으로 전달
5 **/ 5 **/
6 6
7 -import App, { renderApp } from "./App";
8 -import animatePage from "./components/page.transition";
9 -import { addEvent } from "@src/components/add.event";
10 import "./assets/style/PageTransition.scss"; 7 import "./assets/style/PageTransition.scss";
11 import "./assets/style/PageLoader.scss"; 8 import "./assets/style/PageLoader.scss";
12 -import createLoaderElement from "./components/page.loader"; 9 +
13 -import { getElement, getPathname } from "./components/functions"; 10 +import animatePage from "@src/components/page.transition";
11 +import createLoaderElement from "@src/components/page.loader";
12 +import addEventWithElementByType from "@src/components/add.event";
13 +import { getPathname } from "@src/components/functions";
14 +import { linkEvent } from "@src/components/event.list";
15 +
16 +import { renderApp } from "./App";
14 17
15 window.addEventListener("DOMContentLoaded", () => { 18 window.addEventListener("DOMContentLoaded", () => {
16 createLoaderElement(); 19 createLoaderElement();
17 animatePage(); 20 animatePage();
18 21
19 - const $App = getElement("#App") as HTMLDivElement;
20 const pathname = getPathname(); 22 const pathname = getPathname();
21 23
22 - document.body.addEventListener("click", async (e) => { 24 + addEventWithElementByType(document.body, "click", function (e: Event) {
23 - await addEvent(e, $App, App); 25 + /* add navigation click event */
26 + if ((e.target as HTMLAnchorElement).matches("[data-link]")) {
27 + linkEvent(e);
28 + }
24 }); 29 });
25 30
26 /* change url */ 31 /* change url */
......
...@@ -5,13 +5,22 @@ ...@@ -5,13 +5,22 @@
5 * 조회 / 업데이트 5 * 조회 / 업데이트
6 **/ 6 **/
7 7
8 -import Title from "@src/components/title"; 8 +import Title from "@src/components/Title";
9 -import '@src/assets/style/Intro.scss' 9 +import "@src/assets/style/Intro.scss";
10 +import addEventWithElementByType from "@src/components/add.event";
11 +import { handleFetchById } from "@src/components/event.list";
12 +
13 +const Intro = (): string => {
14 + addEventWithElementByType(document.body, "click", function (e: Event) {
15 + if ((e.target as HTMLButtonElement).id === "search-button") {
16 + /* add GET event for button */
17 + handleFetchById("search?insta_id=", "#id-input");
18 + }
19 + });
10 20
11 -const Intro = (): string =>{
12 return `<div class="intro"> 21 return `<div class="intro">
13 <label for="id-input"> 22 <label for="id-input">
14 - ${Title('SEARCH INSTAGRAM').Large} 23 + ${Title("SEARCH INSTAGRAM").Large}
15 <input 24 <input
16 id="id-input" 25 id="id-input"
17 type="text" 26 type="text"
...@@ -20,7 +29,7 @@ const Intro = (): string =>{ ...@@ -20,7 +29,7 @@ const Intro = (): string =>{
20 /> 29 />
21 <button id="search-button">SEARCH</button> 30 <button id="search-button">SEARCH</button>
22 </label> 31 </label>
23 - </div>` 32 + </div>`;
24 -} 33 +};
25 34
26 export default Intro; 35 export default Intro;
......
...@@ -5,10 +5,11 @@ ...@@ -5,10 +5,11 @@
5 * 업데이트하기 5 * 업데이트하기
6 * 다른 메뉴 보기 6 * 다른 메뉴 보기
7 **/ 7 **/
8 -import { getState } from "@src/store/state";
9 -import { formatNumber } from "@src/components/format.number";
10 import "@src/assets/style/Main.scss"; 8 import "@src/assets/style/Main.scss";
11 9
10 +import { getState } from "@src/store/state";
11 +import { formatNumber } from "@src/components/functions";
12 +
12 const Main = () => { 13 const Main = () => {
13 const state = getState(); 14 const state = getState();
14 let followers: string = ""; 15 let followers: string = "";
......
...@@ -2,16 +2,35 @@ ...@@ -2,16 +2,35 @@
2 * @author : wonseog 2 * @author : wonseog
3 * @date : 2021/03/08 3 * @date : 2021/03/08
4 * @description : 페이지 헤더 4 * @description : 페이지 헤더
5 -**/ 5 + **/
6 6
7 -import Title from "@src/components/title"; 7 +import "@src/assets/style/Header.scss";
8 -import '@src/assets/style/Header.scss'
9 -import {getState} from "@src/store/state";
10 8
11 -const Header = () : string => ` 9 +import { renderApp } from "@src/App";
10 +import addEventWithElementByType from "@src/components/add.event";
11 +import { handleFetchById } from "@src/components/event.list";
12 +
13 +import { getState } from "@src/store/state";
14 +
15 +const Header = (): string => {
16 + const { insta_id } = getState();
17 +
18 + addEventWithElementByType(document.body, "click", function (e: Event) {
19 + if ((e.target as HTMLButtonElement).id === "update-button") {
20 + /* add POST event for button */
21 + handleFetchById("update?insta_id=", "#id-input");
22 + renderApp("main");
23 + }
24 + });
25 +
26 + return `
12 <div class="header"> 27 <div class="header">
13 - ${Title(getState().insta_id || 'INSTAGRAM').Large} 28 + <div class="form-container">
29 + <input type="text" name="insta_id" id="id-input" placeholder="${insta_id}" value="${insta_id}" />
30 + <button id="update-button">UPDATE</button>
31 + </div>
14 </div> 32 </div>
15 `; 33 `;
34 +};
16 35
17 export default Header; 36 export default Header;
......
...@@ -6,6 +6,7 @@ from config.admin import ID, PW, LOCAL_PROJECT_PATH ...@@ -6,6 +6,7 @@ from config.admin import ID, PW, LOCAL_PROJECT_PATH
6 from config.URLs import INSTAGRAM_URL 6 from config.URLs import INSTAGRAM_URL
7 from config.firebase import update_data 7 from config.firebase import update_data
8 8
9 +
9 def check_people(driver, type): 10 def check_people(driver, type):
10 result = [] 11 result = []
11 navigations = driver.find_elements_by_class_name('-nal3') 12 navigations = driver.find_elements_by_class_name('-nal3')
...@@ -50,10 +51,10 @@ def get_list(insta_id, driver): ...@@ -50,10 +51,10 @@ def get_list(insta_id, driver):
50 51
51 # update at firebase 52 # update at firebase
52 data = { 53 data = {
53 - "followers" : followers_list, 54 + "followers": followers_list,
54 - "following" : following_list, 55 + "following": following_list,
55 - "insta_id" : insta_id, 56 + "insta_id": insta_id,
56 - "src" : src 57 + "src": src
57 } 58 }
58 update_data(insta_id, data) 59 update_data(insta_id, data)
59 60
...@@ -62,7 +63,7 @@ def get_list(insta_id, driver): ...@@ -62,7 +63,7 @@ def get_list(insta_id, driver):
62 63
63 def crawler_instagram(insta_id): 64 def crawler_instagram(insta_id):
64 options = Options() 65 options = Options()
65 - options.add_argument("--headless") 66 + # options.add_argument("--headless")
66 options.add_argument("window-size=1920,1080") 67 options.add_argument("window-size=1920,1080")
67 driver = webdriver.Chrome(chrome_options=options, executable_path=LOCAL_PROJECT_PATH + '/crawler/chromedriver') 68 driver = webdriver.Chrome(chrome_options=options, executable_path=LOCAL_PROJECT_PATH + '/crawler/chromedriver')
68 driver.get(url=INSTAGRAM_URL) 69 driver.get(url=INSTAGRAM_URL)
...@@ -70,10 +71,10 @@ def crawler_instagram(insta_id): ...@@ -70,10 +71,10 @@ def crawler_instagram(insta_id):
70 71
71 print('hi') 72 print('hi')
72 login(driver) 73 login(driver)
73 - print('by') 74 + print(insta_id)
74 time.sleep(2) 75 time.sleep(2)
75 76
76 - url="%s/%s"%(INSTAGRAM_URL, insta_id) 77 + url = "%s/%s" % (INSTAGRAM_URL, insta_id)
77 driver.get(url=url) 78 driver.get(url=url)
78 time.sleep(2) 79 time.sleep(2)
79 80
......