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

[점심 뭐 먹지 미션 Step 2] 우디(류정우) 미션 제출합니다. #85

Merged
merged 14 commits into from
Apr 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
14 commits
Select commit Hold shift + click to select a range
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
17 changes: 10 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,25 @@
"name": "react-lunch",
"version": "0.1.0",
"private": true,
"homepage": "https://evencoding.github.io/react-lunch/",
"dependencies": {
"@testing-library/jest-dom": "^5.16.5",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"@types/jest": "^27.5.2",
"@types/node": "^16.18.23",
"@types/react": "^18.0.34",
"@types/react-dom": "^18.0.11",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "^2.1.3",
"typescript": "^4.9.5",
"web-vitals": "^2.1.4"
"web-vitals": "^2.1.4",
"styled-components": "^5.3.9"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
"eject": "react-scripts eject",
"predeploy": "npm run build",
"deploy": "gh-pages -d build"
},
"eslintConfig": {
"extends": [
Expand All @@ -42,6 +42,9 @@
},
"devDependencies": {
"@types/styled-components": "^5.1.26",
"styled-components": "^5.3.9"
"@types/jest": "^27.5.2",
"@types/node": "^16.18.23",
"@types/react": "^18.0.34",
"@types/react-dom": "^18.0.11"
}
}
29 changes: 2 additions & 27 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,14 @@
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta
name="description"
content="Web site created using create-react-app"
/>
<meta name="description" content="Web site created using create-react-app" />
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
<!--
manifest.json provides metadata used when your web app is installed on a
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
-->
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<!--
Notice the use of %PUBLIC_URL% in the tags above.
It will be replaced with the URL of the `public` folder during the build.
Only files inside the `public` folder can be referenced from the HTML.

Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>React App</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.

You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.

To begin the development, run `npm start` or `yarn start`.
To create a production bundle, use `npm run build` or `yarn build`.
-->
<div id="modal-root"></div>
</body>
</html>
137 changes: 40 additions & 97 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,114 +1,57 @@
import { Category, Restaurant, SortingType } from './types/restaurant';
import { Restaurant } from './types/restaurant';

import React from 'react';
import { Header, RestaurantList, RestaurantDetail } from './components';
import React, { useState } from 'react';
import { Header, RestaurantList, RestaurantDetail, Filter } from './components';

import mockData from './mockData.json';
import { useRestaurantFilter } from './hooks/useFilter';
import { useLocalStorage } from './hooks/useLocalStorage';

interface Props {}

interface State {
restaurants: Restaurant[];
category: string;
sortingType: SortingType;
interface ModalState {
isModalOpen: boolean;
detailId: Restaurant['id'];
detailId?: Restaurant['id'];
}

class App extends React.Component<Props, State> {
constructor(props: Props) {
super(props);

if (!localStorage.getItem('restaurants')) {
localStorage.setItem('restaurants', JSON.stringify(mockData.restaurants));
}

const restaurants = JSON.parse(localStorage.getItem('restaurants') || '[]');
this.state = {
restaurants,
category: '전체',
sortingType: '이름순',
isModalOpen: false,
detailId: '1'
};

window.addEventListener('keyup', ({ key }) => {
if (this.state.isModalOpen && key === 'Escape') {
this.closeModal();
}
});
}
const App = () => {
const localRestaurantsData = useLocalStorage('restaurants');
const [modalState, setModalState] = useState<ModalState>({
isModalOpen: false,
});
const [restaurants, { setCategory, setSortingType }] = useRestaurantFilter(localRestaurantsData);

openModal = (id: Restaurant['id']) => {
this.setState({
const openModal = (id: Restaurant['id']) => {
setModalState({
...modalState,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

setState 의 파라미터를 함수로 정의할 수도 있습니다!

setState((prev) => ({ ...prev, ... }));

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

헤인트의 예시처럼 이전 상태를 참조하고 반환하는 방식이 더 안정적이겠군요!
제가 쓴 코드는 객체를 병합하고 있기 때문에, 이전 상태와 충돌할 가능성이 있겠네요

감사합니다🤩

detailId: id,
isModalOpen: true
});
};

closeModal = () => {
this.setState({
isModalOpen: false
});
};

setCategory = (category: Category) => {
this.setState({
category
});
};

setSortingType = (sortingType: SortingType) => {
this.setState({
sortingType
isModalOpen: true,
});
};

filterRestaurants = () => {
const { category, sortingType } = this.state;

const restaurants = this.state.restaurants.filter(
restaurant => category === '전체' || restaurant.category === category
);

const getPivot = (restaurant: Restaurant) => {
return sortingType === '이름순' ? restaurant.name : restaurant.distance;
};

return restaurants.sort((a, b) => {
const A = getPivot(a);
const B = getPivot(b);
if (A > B) return 1;
if (A < B) return -1;
return 0;
const closeModal = () => {
setModalState({
...modalState,
isModalOpen: false,
Comment on lines +30 to +32

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

setModalState 파라미터로 함수를 넘길 수도 있습니다!

setState((prev) => ({ ...prev, ... });

});
};

render() {
return (
<div className="App">
<Header />

<RestaurantList
restaurants={this.filterRestaurants()}
openModal={this.openModal}
setCategory={this.setCategory}
setSortingType={this.setSortingType}
return (
<div className="App">
<Header />

<main>
<Filter setCategory={setCategory} setSortingType={setSortingType} />
<RestaurantList restaurants={restaurants} openModal={openModal} />
</main>

{modalState.isModalOpen && (
<RestaurantDetail
closeModal={closeModal}
restaurant={
restaurants.find((restaurant) => restaurant.id === modalState.detailId) as Restaurant
}
/>

{this.state.isModalOpen && (
<RestaurantDetail
closeModal={this.closeModal}
restaurant={
mockData.restaurants.find(
restaurant => restaurant.id === this.state.detailId
) as Restaurant
}
/>
)}
</div>
);
}
}
)}
</div>
);
};

export default App;
2 changes: 1 addition & 1 deletion src/components/Button.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import styled from 'styled-components';

const Button = styled.button`
export const Button = styled.button`
width: 100%;
height: 44px;

Expand Down
42 changes: 0 additions & 42 deletions src/components/CategoryImage.tsx

This file was deleted.

21 changes: 21 additions & 0 deletions src/components/CategoryImage/CategoryImage.styles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import styled from 'styled-components';

export const CategoryImageWrapper = styled.div`
display: flex;
justify-content: center;
align-items: center;
width: 64px;
height: 64px;
min-width: 64px;
min-height: 64px;

margin-right: 16px;

border-radius: 50%;
background: var(--lighten-color);

img {
width: 36px;
height: 36px;
}
`;
20 changes: 20 additions & 0 deletions src/components/CategoryImage/CategoryImage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Category } from '../../types/restaurant';

import React from 'react';
import * as styled from './CategoryImage.styles';

import { CATEGORY_IMAGE_MAP } from '../../constants';

interface Props {
category: Category;
}

const CategoryImage = (props: Props) => {
return (
<styled.CategoryImageWrapper>
<img src={`./img/${CATEGORY_IMAGE_MAP[props.category]}`} alt={props.category} />
</styled.CategoryImageWrapper>
);
};

export default CategoryImage;
21 changes: 21 additions & 0 deletions src/components/Filter/Filter.styles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import styled from 'styled-components';

export const FilterContainer = styled.section`
display: flex;
justify-content: space-between;

margin-top: 24px;
padding: 0 16px;

select {
height: 44px;
min-width: 125px;

border: 1px solid #d0d5dd;
border-radius: 8px;
padding: 8px;

background: transparent;
font-size: 16px;
}
`;
Loading