Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8주차 & 9주차] 실습 완료 #142

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
484 changes: 480 additions & 4 deletions week6/jiyeKa/mission/package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions week6/jiyeKa/mission/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"preview": "vite preview"
},
"dependencies": {
"json-server": "^1.0.0-beta.3",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-router-dom": "^6.28.0",
Expand Down
8 changes: 8 additions & 0 deletions week6/jiyeKa/mission/src/App.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import Router from "./pages/Router";
import { Normalize } from "styled-normalize";
import useCartStore from "./store/cartStore";
import { useEffect } from "react";


const App = () => {

const fetchCart = useCartStore((state) => state.fetchCart);
useEffect(() => {
fetchCart();
}, [])
return (
<>
<Normalize />
Expand Down
18 changes: 18 additions & 0 deletions week6/jiyeKa/mission/src/apis/cart.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export const getCart = async () => {
const response = await fetch("http://localhost:8080/cart");
const data = await response.json();
return data;
};

export const updateCart = async (store, menus) => {
return await fetch("http://localhost:8080/cart",{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
store,
menus,
})
});
};
11 changes: 11 additions & 0 deletions week6/jiyeKa/mission/src/apis/stores.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export const getStores = async() => {
const response = await fetch("http://localhost:8080/stores");
const data = await response.json();
return data;
}

export const getStore = async (storeId) => {
const response = await fetch(`http://localhost:8080/stores/${storeId}`);
const data = await response.json();
return data;
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const MainMenuCategory = () => {

const navigate = useNavigate();
const onClickhandle = () => {
navigate("/store");
navigate("/stores");
}

return (
Expand Down
24 changes: 12 additions & 12 deletions week6/jiyeKa/mission/src/components/MenuItem/MenuItem.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,24 @@ import {BestMenu, MenuItemContainer, MenuName, MenuPrice, MenuIngredients, MenuB
import roundIMG from "../../assets/roundIMG.svg"
import useCartStore from "../../store/cartStore.js"

const MenuItem = ({ menu }) => {
const MenuItem = ({ menu, handleAddMenu }) => {

const menus = useCartStore((state) => state.menus);
const addMenu = useCartStore((state) => state.addMenu);
const resetCart = useCartStore((state)=>state.resetCart)
const currentStore = useCartStore((state) => state.store);

const handleAddMenu = () => {
if (menus.length > 0 && (menus[0].storeID !== menu.storeID)) {
const alert = window.confirm("주문서에는 같은 가게만 담을 수 있어요. 새로 담고 이전에 담은 메뉴는 삭제할까요?");
if (alert) {
resetCart();
addMenu(menu);
}
} else {
addMenu(menu);
}
};
// const handleAddMenu = () => {
// if (menus.length > 0 && (menus[0].storeID !== menu.storeID)) {
// const alert = window.confirm("주문서에는 같은 가게만 담을 수 있어요. 새로 담고 이전에 담은 메뉴는 삭제할까요?");
// if (alert) {
// resetCart();
// addMenu(menu);
// }
// } else {
// addMenu(menu);
// }
// }; -> StoreInfo.jsx에서 구현


return (
Expand Down
20 changes: 17 additions & 3 deletions week6/jiyeKa/mission/src/components/StoreInfo/StoreInfo.jsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
import {useParams} from "react-router-dom";
import stores from "../../models/stores";
import MenuItem from "../../components/MenuItem/MenuItem";
import star from "../../assets/star.svg";
import {NoStoreWrapper, StoreInfoWrapper, InfoWrapper, MenuWrapper} from "./StoreInfo.styles";
import { useState, useEffect } from "react";
import { getStore } from "../../apis/stores";
import useCartStore from "../../store/cartStore";

const StoreInfo = () => {
const {storeId} = useParams();
const store = stores.find((store) => store.id.toString() === storeId);
// const store = stores.stores.find((store) => store.id.toString() === storeId);
const [store, setStore] = useState();
const addMenu = useCartStore((state) => state.addMenu);

useEffect(() => {
getStore(storeId).then(value => setStore(value)); //api 사용해서 id를 통한 store 찾기
}, [storeId])

const handleAddMenu = (menu) => {
//
addMenu(menu, store)
}


if (!store) {
return (
Expand Down Expand Up @@ -42,7 +56,7 @@ const StoreInfo = () => {
<div className="storeMenu">
{store.menus.map((menu) => {
return (
<MenuItem key={menu.id} menu={menu} />
<MenuItem key={menu.id} menu={menu} handleAddMenu={() => handleAddMenu(menu)}/>
);
})}
</div>
Expand Down
6 changes: 3 additions & 3 deletions week6/jiyeKa/mission/src/components/StoreList/StoreList.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import {StoresTitle, StoresWrapper, ListWrapper} from "./StoreList.styles";
import StoresDetail from "../StoresDetail/StoresDetail";
import useCartStore from '../../store/cartStore';

const StoresList = () => {
const storeList = useCartStore((state) => state.storeList);
const StoreList = () => {
const storeList = useCartStore((state) => state.storeList) || [];
const sortedStores = storeList.sort((a, b) => b.reviewCnt - a.reviewCnt); //리뷰 수 기준으로 정렬

const topThreeStores = sortedStores.slice(0, 3); // 1~3위만 따로 스타일링
Expand Down Expand Up @@ -77,4 +77,4 @@ const StoresList = () => {
</StoresWrapper>
);
};
export default StoresList;
export default StoreList;
Loading