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

[4주차] 내 근처 카페 찾기 #5

Open
wants to merge 4 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
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
README.md
README.md

*/node_modules
1 change: 1 addition & 0 deletions cafe-nearby/.env.local
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
REACT_APP_KAKAO_AK=61781324236f7aba9a27478cc490b2a3
66 changes: 66 additions & 0 deletions cafe-nearby/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
{
"name": "eslint-prettier-practice",
"version": "0.1.0",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^5.14.1",
"@testing-library/react": "^13.0.0",
"@testing-library/user-event": "^13.2.1",
"@types/jest": "^27.0.1",
"@types/node": "^16.7.13",
"@types/react": "^18.0.0",
"@types/react-dom": "^18.0.0",
"@types/styled-components": "^5.1.25",
"axios": "^0.27.2",
"react": "^18.0.0",
"react-dom": "^18.0.0",
"react-scripts": "5.0.1",
"styled-components": "^5.3.5",
"styled-reset": "^4.3.4",
"typescript": "^4.4.2",
"web-vitals": "^2.1.0"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"lint": "yarn lint-eslint && yarn lint-prettier",
"lint-eslint": "eslint --ignore-path .gitignore src/**/*{ts,tsx} --fix",
"lint-prettier": "prettier --write \"src/**/*.(ts|tsx)\""
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^5.20.0",
"@typescript-eslint/parser": "^5.20.0",
"eslint": "^8.13.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-prettier": "^4.0.0",
"husky": "^7.0.4",
"lint-staged": "^12.4.0",
"prettier": "^2.6.2"
},
"lint-staged": {
"src/**/*.{js,jsx,ts,tsx}": [
"eslint --fix",
"prettier --write"
]
}
}
Binary file added cafe-nearby/public/favicon.ico
Binary file not shown.
43 changes: 43 additions & 0 deletions cafe-nearby/public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<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"
/>
<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`.
-->
</body>
</html>
Binary file added cafe-nearby/public/logo192.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added cafe-nearby/public/logo512.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions cafe-nearby/public/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"short_name": "React App",
"name": "Create React App Sample",
"icons": [
{
"src": "favicon.ico",
"sizes": "64x64 32x32 24x24 16x16",
"type": "image/x-icon"
},
{
"src": "logo192.png",
"type": "image/png",
"sizes": "192x192"
},
{
"src": "logo512.png",
"type": "image/png",
"sizes": "512x512"
}
],
"start_url": ".",
"display": "standalone",
"theme_color": "#000000",
"background_color": "#ffffff"
}
3 changes: 3 additions & 0 deletions cafe-nearby/public/robots.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# https://www.robotstxt.org/robotstxt.html
User-agent: *
Disallow:
11 changes: 11 additions & 0 deletions cafe-nearby/src/App.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import GlobalStyle from './globalStyles';
import Main from './Main';

export default function App() {
return (
<div>
<GlobalStyle />
<Main />
</div>
);
}
89 changes: 89 additions & 0 deletions cafe-nearby/src/Main.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import axios from 'axios';
import { useEffect, useState } from 'react';

import SearchBar from './components/SearchBar';
import SearchResult from './components/SearchResult';

export interface CafeInfo {
name: string;
phone: string;
distance: string;
}

interface Position {
x: number;
y: number;
}

export type ResponseStatus = 'IDLE' | 'PENDING' | 'SUCCESS' | 'ERROR';

export default function Main() {
const [cafeList, setCafeList] = useState<CafeInfo[] | undefined>();
const [position, setPosition] = useState<Position | undefined>();
const [searchStatus, setSearchStatus] = useState<ResponseStatus>('IDLE');
const [isAutoPosition, setIsAutoPosition] = useState(false);
const getCafeList = async (searchWord: string) => {
if (!position) {
setSearchStatus('ERROR');
return;
}
setSearchStatus('PENDING');
try {
const result = await axios.get(
`https://dapi.kakao.com/v2/local/search/${
isAutoPosition ? 'category' : 'keyword'
}`,
{
headers: {
Authorization: `KakaoAK ${process.env.REACT_APP_KAKAO_AK}`,
},
params: {
x: position.x,
y: position.y,
radius: 1000,
query: searchWord,
category_group_code: 'CE7',
},
}
);
setCafeList(
result.data.documents.map((cafe: any) => ({
name: cafe.place_name,
phone: cafe.phone,
distance: cafe.distance,
}))
);
setSearchStatus('SUCCESS');
} catch (error) {
setSearchStatus('ERROR');
console.log(error);
}
};

const handleClickCheckBox = () => {
setIsAutoPosition((prev) => !prev);
};

useEffect(() => {
navigator.geolocation.getCurrentPosition((position) => {
setPosition({
x: position.coords.longitude,
y: position.coords.latitude,
});
});
}, []);

useEffect(() => console.log(isAutoPosition), [isAutoPosition]);
Copy link
Member Author

Choose a reason for hiding this comment

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

지우자..


return (
<div>
<div>카페 찾기</div>
<SearchBar
onSubmit={getCafeList}
isAutoPosition={isAutoPosition}
onClickCheckBox={handleClickCheckBox}
></SearchBar>
<SearchResult cafeList={cafeList} searchStatus={searchStatus} />
</div>
);
}
4 changes: 4 additions & 0 deletions cafe-nearby/src/assets/ic_checked.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions cafe-nearby/src/assets/ic_unchecked.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions cafe-nearby/src/components/CafeItem.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { CafeInfo } from '../Main';

export default function CafeItem({ cafe }: { cafe: CafeInfo }) {
const { name, phone, distance } = cafe;
return (
<div>
<div>{name}</div>
<div>
<div>{phone}</div>
<div>{distance}m</div>
</div>
</div>
);
}
59 changes: 59 additions & 0 deletions cafe-nearby/src/components/SearchBar.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { useState } from 'react';
import styled from 'styled-components';

import Checked from '../assets/ic_checked.svg';
import Unchecked from '../assets/ic_unchecked.svg';

interface SearchBarProps {
onSubmit: (searchWord: string) => void;
isAutoPosition: boolean;
onClickCheckBox: () => void;
}

export default function SearchBar(props: SearchBarProps) {
const { onSubmit, isAutoPosition, onClickCheckBox } = props;
const [searchWord, setSearchWord] = useState('');
return (
<div>
<StCheckBox>
<input
id="isLocal"
checked={isAutoPosition}
type="checkbox"
onClick={onClickCheckBox}
/>
<label htmlFor="isLocal" />
<span>현재 위치 자동으로 가져오기</span>
</StCheckBox>
<form
onSubmit={(e) => {
e.preventDefault();
onSubmit(searchWord);
}}
>
<input
value={searchWord}
onChange={(e) => setSearchWord(e.target.value)}
placeholder="현재 위치를 입력해주세요."
readOnly={isAutoPosition}
/>
<button>검색</button>
</form>
</div>
);
}

const StCheckBox = styled.div`
input[type='checkbox'] {
display: none;
}
input[type='checkbox'] + label {
cursor: pointer;
padding-left: 23px;
background-repeat: no-repeat;
background-image: ${`url(${Unchecked})`};
}
input[type='checkbox']:checked + label {
background-image: ${`url(${Checked})`};
}
`;
28 changes: 28 additions & 0 deletions cafe-nearby/src/components/SearchResult.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { CafeInfo, ResponseStatus } from '../Main';
import CafeItem from './CafeItem';

interface SearchResultProps {
cafeList: CafeInfo[] | undefined;
searchStatus: ResponseStatus;
}

export default function SearchResult(props: SearchResultProps) {
const { cafeList, searchStatus } = props;
if (searchStatus === 'PENDING') {
return <div>Loading...</div>;
}
return cafeList === undefined ? (
<></>
) : cafeList.length ? (
<div>
{cafeList.map((cafe) => (
<CafeItem key={cafe.name} cafe={cafe} />
))}
</div>
) : (
<div>
검색 결과가 없어요. <br />
입력하신 위치와 실제 위치가 다를지 몰라요.
</div>
);
}
Loading