Skip to content

Commit fc0ee07

Browse files
committed
fix: 6. 상품 검색 적용
* 초반 로딩 화면 플래그 설정
1 parent 38d64c2 commit fc0ee07

2 files changed

Lines changed: 19 additions & 5 deletions

File tree

src/main.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,11 @@ const mainStatus = {
2828

2929
// 라우터 인스턴스를 전역적으로 접근 가능하도록 선언
3030
let appRouter;
31-
31+
let isInit = true;
3232
// API 호출 및 렌더링을 담당하는 함수
3333
async function loadProductsAndUpdateUI() {
34-
mainStatus.loading = true;
34+
isInit && (mainStatus.loading = true);
35+
3536
appRouter.updateStateAndRender(mainStatus); // 로딩 상태 먼저 반영
3637

3738
try {
@@ -45,16 +46,17 @@ async function loadProductsAndUpdateUI() {
4546
console.error("loadProductsAndUpdateUI", "데이터를 불러오는 중 오류가 발생했습니다:", error);
4647
} finally {
4748
mainStatus.loading = false;
49+
isInit = false;
4850
appRouter.updateStateAndRender(mainStatus); // 최종 상태 반영
4951
}
5052
}
5153

5254
// 이벤트 핸들러 설정 함수
5355
function setupEventListeners() {
5456
/** change */
57+
let shouldUpdate = false;
5558
document.body.addEventListener("change", (e) => {
5659
const target = e.target;
57-
let shouldUpdate = false;
5860

5961
if (target.id === "limit-select") {
6062
mainStatus.params.limit = parseInt(target.value, 10);
@@ -70,6 +72,18 @@ function setupEventListeners() {
7072
}
7173
});
7274

75+
document.body.addEventListener("keydown", (e) => {
76+
if (e.key === "Enter" && e.target.id === "search-input") {
77+
mainStatus.params.search = e.target.value;
78+
shouldUpdate = true;
79+
} else {
80+
return;
81+
}
82+
if (shouldUpdate) {
83+
mainStatus.params.page = 1; // 필터 변경 시 첫 페이지로 초기화
84+
loadProductsAndUpdateUI();
85+
}
86+
});
7387
// TODO: 그 외 다른 이벤트 설정.....
7488
}
7589

src/router.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ const routes = {
4747

4848
// 라우터 인스턴스 생성 및 관리 함수
4949
export const createRouter = (initialmainStatus) => {
50-
let currentmainStatus = initialmainStatus; // main.js에서 전달받은 mainStatus 객체
50+
let currentmainStatus = JSON.parse(JSON.stringify(initialmainStatus)); // main.js에서 전달받은 mainStatus 객체
5151
const appRoot = document.querySelector("#root"); // 콘텐츠가 렌더링될 DOM 요소
5252

5353
// 현재 URL 경로에 맵핑된 컴포넌트 렌더링 함수
@@ -70,7 +70,7 @@ export const createRouter = (initialmainStatus) => {
7070

7171
// 외부에서 mainStatus을 업데이트하고 렌더링을 트리거할 수 있는 함수
7272
const updateStateAndRender = (newmainStatus) => {
73-
currentmainStatus = newmainStatus; // 상태 업데이트
73+
currentmainStatus = JSON.parse(JSON.stringify(newmainStatus)); // 상태 업데이트
7474
render(); // 업데이트된 상태로 다시 렌더링
7575
};
7676

0 commit comments

Comments
 (0)