1seok2

refactor: change for fetch data type

......@@ -15,7 +15,7 @@ import { getElement } from "./shared/functions";
function App(pathname: string): string {
/* id 없을 시 id 입력으로 redirect */
if (!getState().insta_id) pathname = "";
if (getState().user[0] === "") pathname = "";
history.pushState("", "", pathname);
return `
......
......@@ -5,28 +5,20 @@
* 주로 instaId, followers, following 관리
**/
export interface StateType{
insta_id? : string,
followers? : Array<string>,
following? : Array<string>,
src? : string
}
const initialState = {
insta_id : '',
followers : [''],
following : [''],
src : '',
}
let state: StateType = initialState;
user: ["", "", ""],
follower: [["", "", ""]],
};
export const setState = (newState: StateType): void => {
let state = initialState;
export const setState = (newState): void => {
state = {
...state,
...newState
}
...newState,
};
};
export const getState = () => {
return state
}
\ No newline at end of file
return state;
};
......
......@@ -13,33 +13,23 @@ import { getState } from "@src/store/state";
const Main = () => {
const state = getState();
let followers: string = "";
let following: string = "";
state.followers?.sort().forEach((id, idx) => {
followers += `<div class="item item-${idx % 2}">${id}</div>`;
state.follower?.forEach((follower, idx) => {
followers += `<div class="item item-${idx % 2}">${follower[0]}</div>`;
});
state.following?.sort().forEach((id, idx) => {
following += `<div class="item item-${idx % 2}">${id}</div>`;
});
const followers_num = formatNumber(state.followers?.length || 0);
const followings_num = formatNumber(state.following?.length || 0);
const followers_num = formatNumber(state.follower?.length || 0);
return `
<div class="main">
<div class="profile">
<img src="${state.src}" alt="${state.insta_id}-image"/>
<img src="${state}" alt="${state.user[0]}-image"/>
</div>
<div class="list">
<div class="followers">
<h2 class="title">FOLLOWERS (${followers_num})</h2>
<div>${followers}</div>
</div>
<div class="following">
<h2 class="title">FOLLOWINGS (${followings_num})</h2>
<div>${following}</div>
</div>
</div>
</div>
`;
......
......@@ -12,7 +12,7 @@ import { getState } from "@src/store/state";
import { addEventWithElementByType, handleFetchById } from "@src/shared/event";
const Header = (): string => {
const { insta_id } = getState();
const { user } = getState();
addEventWithElementByType(document.body, "click", function (e: Event) {
if ((e.target as HTMLButtonElement).id === "update-button") {
......@@ -25,7 +25,7 @@ const Header = (): string => {
return `
<div class="header">
<div class="form-container">
<input type="text" name="insta_id" id="id-input" placeholder="${insta_id}" value="${insta_id}" />
<input type="text" name="insta_id" id="id-input" placeholder="${user[0]}" value="${user[0]}" />
<button id="update-button">UPDATE</button>
</div>
</div>
......