Skip to content
Merged
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
24 changes: 24 additions & 0 deletions Week9/9-1/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

node_modules
dist
dist-ssr
*.local

# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
54 changes: 54 additions & 0 deletions Week9/9-1/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# React + TypeScript + Vite

This template provides a minimal setup to get React working in Vite with HMR and some ESLint rules.

Currently, two official plugins are available:

- [@vitejs/plugin-react](https://github.com/vitejs/vite-plugin-react/blob/main/packages/plugin-react) uses [Babel](https://babeljs.io/) for Fast Refresh
- [@vitejs/plugin-react-swc](https://github.com/vitejs/vite-plugin-react/blob/main/packages/plugin-react-swc) uses [SWC](https://swc.rs/) for Fast Refresh

## Expanding the ESLint configuration

If you are developing a production application, we recommend updating the configuration to enable type-aware lint rules:

```js
export default tseslint.config({
extends: [
// Remove ...tseslint.configs.recommended and replace with this
...tseslint.configs.recommendedTypeChecked,
// Alternatively, use this for stricter rules
...tseslint.configs.strictTypeChecked,
// Optionally, add this for stylistic rules
...tseslint.configs.stylisticTypeChecked,
],
languageOptions: {
// other options...
parserOptions: {
project: ['./tsconfig.node.json', './tsconfig.app.json'],
tsconfigRootDir: import.meta.dirname,
},
},
})
```

You can also install [eslint-plugin-react-x](https://github.com/Rel1cx/eslint-react/tree/main/packages/plugins/eslint-plugin-react-x) and [eslint-plugin-react-dom](https://github.com/Rel1cx/eslint-react/tree/main/packages/plugins/eslint-plugin-react-dom) for React-specific lint rules:

```js
// eslint.config.js
import reactX from 'eslint-plugin-react-x'
import reactDom from 'eslint-plugin-react-dom'

export default tseslint.config({
plugins: {
// Add the react-x and react-dom plugins
'react-x': reactX,
'react-dom': reactDom,
},
rules: {
// other rules...
// Enable its recommended typescript rules
...reactX.configs['recommended-typescript'].rules,
...reactDom.configs.recommended.rules,
},
})
```
28 changes: 28 additions & 0 deletions Week9/9-1/eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import js from '@eslint/js'
import globals from 'globals'
import reactHooks from 'eslint-plugin-react-hooks'
import reactRefresh from 'eslint-plugin-react-refresh'
import tseslint from 'typescript-eslint'

export default tseslint.config(
{ ignores: ['dist'] },
{
extends: [js.configs.recommended, ...tseslint.configs.recommended],
files: ['**/*.{ts,tsx}'],
languageOptions: {
ecmaVersion: 2020,
globals: globals.browser,
},
plugins: {
'react-hooks': reactHooks,
'react-refresh': reactRefresh,
},
rules: {
...reactHooks.configs.recommended.rules,
'react-refresh/only-export-components': [
'warn',
{ allowConstantExport: true },
],
},
},
)
13 changes: 13 additions & 0 deletions Week9/9-1/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite + React + TS</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/main.tsx"></script>
</body>
</html>
34 changes: 34 additions & 0 deletions Week9/9-1/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"name": "week9",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "tsc -b && vite build",
"lint": "eslint .",
"preview": "vite preview"
},
"dependencies": {
"@reduxjs/toolkit": "^2.8.2",
"@tailwindcss/vite": "^4.1.7",
"lucide-react": "^0.511.0",
"react": "^19.1.0",
"react-dom": "^19.1.0",
"react-redux": "^9.2.0",
"tailwindcss": "^4.1.7"
},
"devDependencies": {
"@eslint/js": "^9.25.0",
"@types/react": "^19.1.2",
"@types/react-dom": "^19.1.2",
"@vitejs/plugin-react-swc": "^3.9.0",
"eslint": "^9.25.0",
"eslint-plugin-react-hooks": "^5.2.0",
"eslint-plugin-react-refresh": "^0.4.19",
"globals": "^16.0.0",
"typescript": "~5.8.3",
"typescript-eslint": "^8.30.1",
"vite": "^6.3.5"
}
}
1 change: 1 addition & 0 deletions Week9/9-1/public/vite.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file added Week9/9-1/src/App.css
Empty file.
22 changes: 22 additions & 0 deletions Week9/9-1/src/App.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Provider } from "react-redux";
import "./App.css";
import CartList from "./components/Cart/CartList/CartList";
import Navbar from "./components/NavigationBar/Navbar";
import store from "./store/store";
import TotalPrice from "./components/Cart/TotalPrice/TotalPrice";
import ClearCartModal from "./components/Modal/ClearCartModal/ClearCartModal";

function App() {
return (
<Provider store={store}>
<Navbar />
<div className="max-w-175 mx-auto">
<CartList />
<TotalPrice />
</div>
<ClearCartModal />
</Provider>
);
}

export default App;
1 change: 1 addition & 0 deletions Week9/9-1/src/assets/react.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
56 changes: 56 additions & 0 deletions Week9/9-1/src/components/Cart/CartItem/CartItem.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { MinusIcon, PlusIcon } from "lucide-react";
import type { CartItemProps } from "./CartItem.types";
import { useDispatch } from "../../../hooks/useCustomRedux";
import { decrease, increase, removeItem } from "../../../slices/Cart/cartSlice";

const CartItem = ({ lp }: CartItemProps) => {
const dispatch = useDispatch();

const handleIncreaseCount = () => {
dispatch(increase({ id: lp.id }));
};

const handleDecreaseCount = () => {
if (lp.amount === 1) {
dispatch(removeItem({ id: lp.id }));

return;
}

dispatch(decrease({ id: lp.id }));
};

return (
<div className="flex items-center p-4 border-b border-gray-200">
<img
src={lp.img}
alt={`${lp.title}의 LP 이미지`}
className="w-20 h-20 object-cover rounded mr-4"
/>
<div className="flex-1">
<h3 className="text-lg">{lp.title}</h3>
<p className="text-sm text-gray-600">{lp.singer}</p>
<p className="text-sm font-bold text-gray-600">{lp.price} 원</p>
</div>
<div className="flex items-center ml-4">
<button
onClick={handleDecreaseCount}
className="px-1 py-1 bg-gray-300 text-gray-800 rounded-l hover:bg-gray-400 cursor-pointer"
>
<MinusIcon />
</button>
<span className="px-4 py-[3px] border-y border-gray-300">
{lp.amount}
</span>
<button
onClick={handleIncreaseCount}
className="px-1 py-1 bg-gray-300 text-gray-800 rounded-r hover:bg-gray-400 cursor-pointer"
>
<PlusIcon />
</button>
</div>
</div>
);
};

export default CartItem;
5 changes: 5 additions & 0 deletions Week9/9-1/src/components/Cart/CartItem/CartItem.types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type { LpDto } from "../../../types/cart";

export interface CartItemProps {
lp: LpDto;
}
17 changes: 17 additions & 0 deletions Week9/9-1/src/components/Cart/CartList/CartList.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import CartItem from "../CartItem/CartItem";
import { useSelector } from "../../../hooks/useCustomRedux";

const CartList = () => {
const { cartItems } = useSelector((state) => state.cart);
return (
<div className="flex flex-col items-center justify-center">
<ul>
{cartItems.map((item) => (
<CartItem key={item.id} lp={item} />
))}
</ul>
</div>
);
};

export default CartList;
20 changes: 20 additions & 0 deletions Week9/9-1/src/components/Cart/TotalPrice/TotalPrice.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { useDispatch, useSelector } from "../../../hooks/useCustomRedux";
import { openModal } from "../../../slices/Modal/modalSlice";

const TotalPrice = () => {
const { total } = useSelector((state) => state.cart);
const dispatch = useDispatch();
return (
<div className="flex justify-between items-center pt-12 pb-12">
<button
onClick={() => dispatch(openModal())}
className="border p-4 rounded-md cursor-pointer"
>
장바구니 초기화
</button>
<div>{`총 가격: ${total}원`}</div>
</div>
);
};

export default TotalPrice;
37 changes: 37 additions & 0 deletions Week9/9-1/src/components/Modal/ClearCartModal/ClearCartModal.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { useDispatch, useSelector } from "../../../hooks/useCustomRedux";
import { clearCart } from "../../../slices/Cart/cartSlice";
import { closeModal } from "../../../slices/Modal/modalSlice";

const ClearCartModal = () => {
const dispatch = useDispatch();
const { isOpen } = useSelector((state) => state.modal);

if (!isOpen) return null;

return (
<div className="fixed inset-0 bg-black/80 flex items-center justify-center z-50">
<div className="bg-white p-8 rounded-lg shadow-lg text-center">
<p className="mb-5 text-lg">정말 장바구니를 초기화하겠습니까?</p>
<div className="flex justify-around">
<button
onClick={() => dispatch(closeModal())}
className="px-4 py-2 bg-gray-300 rounded-xl hover:bg-gray-400"
>
아니요
</button>
<button
onClick={() => {
dispatch(clearCart());
dispatch(closeModal());
}}
className="px-4 py-2 rounded-xl bg-red-500 text-white hover:bg-red-600"
>
</button>
</div>
</div>
</div>
);
};

export default ClearCartModal;
32 changes: 32 additions & 0 deletions Week9/9-1/src/components/NavigationBar/Navbar.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { ShoppingCartIcon } from "lucide-react";
import { useDispatch, useSelector } from "../../hooks/useCustomRedux";
import { useEffect } from "react";
import { calculateTotals } from "../../slices/Cart/cartSlice";

const Navbar = () => {
const { amount, cartItems } = useSelector((state) => state.cart);
const dispatch = useDispatch();

useEffect(() => {
dispatch(calculateTotals());
}, [cartItems, dispatch]);

return (
<div className="flex justify-between items-center p-4 bg-gray-800 text-white">
<h1
onClick={() => {
window.location.href = "/";
}}
className="text-2x font-semibold cursor-pointer"
>
Jett LpCart
</h1>
<div className="flex items-center space-x-2">
<ShoppingCartIcon className="text-2xl" />
<span className="text-xt font-medium">{amount}</span>
</div>
</div>
);
};

export default Navbar;
Loading