This repository has been archived by the owner on Sep 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
[3주차] 유현우 미션 제출합니다. #5
Open
puba5
wants to merge
14
commits into
CEOS-Developers:master
Choose a base branch
from
puba5:VoteByWooyoo
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
7b7c840
로그인 인풋과 버튼 구현, 양식에 맞지 않게 입력할시 alert
puba5 e2ab412
아이디와 비밀번호 맞게 입력하면 Vote-form으로 컴포넌트 변경
puba5 44c50c8
후보자 명단을 받아와서 띄어주도록 생성
puba5 05637b0
네이밍 컨벤션을 위해 이름 변경 및 필수 사항 구현 완료
puba5 94926e5
로그인 에러 상황별 다른 alert
puba5 42d602b
Memo 추가, Readme 추가, 선택 사항 구현
puba5 6a973b4
loggedIn state 변수 이름 변경
puba5 c80db56
put의 인자를 제거 및 수정
puba5 707178c
put의 finally 블록 삭제
puba5 ad0930d
투표 성공 alert를 axios.put.then으로 이동
puba5 547a89c
post의 인자를 새로운 객체 생성에서 userData로 변경
puba5 44161a5
Error 처리 부분을 함수로 분리, Memo 부분을 export default로 변경
puba5 d9eecd4
가독성을 높히기 위해 CandidateListWrapper 수정
puba5 f12cfaf
Add Custom Hook and Add login& vote Pages
puba5 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,4 +28,5 @@ yarn-error.log* | |
.env.development.local | ||
.env.test.local | ||
.env.production.local | ||
.env* | ||
.env* | ||
.now |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"version": 2, | ||
"public": false, | ||
"builds": [{ "src": "next.config.js", "use": "@now/next" }], | ||
"build": { | ||
"env": { | ||
"NODE_ENV": "@react-vote-11th-node-env", | ||
"PORT": "@react-vote-11th-port", | ||
"API_HOST": "@react-vote-11th-api-host" | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,29 @@ | ||
import React from "react"; | ||
import React, { useState } from "react"; | ||
import styled from "styled-components"; | ||
|
||
import LoginForm from "../src/components/login-form"; | ||
import VoteForm from "../src/components/vote-form"; | ||
|
||
export default function Home() { | ||
const [isLoggedIn, setIsLoggedIn] = useState(false); | ||
|
||
return ( | ||
<Wrapper> | ||
리액트 투-표 | ||
<LoginForm /> | ||
<Title>리액트 투-표</Title> | ||
{!isLoggedIn && <LoginForm loginSuccess={setIsLoggedIn} />} | ||
{isLoggedIn && <VoteForm />} | ||
</Wrapper> | ||
); | ||
} | ||
|
||
const Wrapper = styled.div` | ||
font-size: 3rem; | ||
min-height: 100vh; | ||
padding: 10rem 40rem; | ||
background-color: Azure; | ||
`; | ||
|
||
const Title = styled.p` | ||
font-weight: bold; | ||
margin-bottom: 2rem; | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import React, { useState } from "react"; | ||
import styled from "styled-components"; | ||
import { useRouter } from "next/router"; | ||
import LoginForm from "../src/components/login-form"; | ||
|
||
import Link from "next/link"; | ||
|
||
export default function Home() { | ||
const router = useRouter(); | ||
const { userName } = router.query; | ||
return ( | ||
<Wrapper> | ||
<Title>리액트 투-표</Title> | ||
<LoginForm /> | ||
<Link href="/vote?userName=최수민"> | ||
<a>투표하러가기</a> | ||
</Link> | ||
</Wrapper> | ||
); | ||
} | ||
|
||
const Wrapper = styled.div` | ||
font-size: 3rem; | ||
min-height: 100vh; | ||
padding: 10rem 40rem; | ||
background-color: Azure; | ||
`; | ||
|
||
const Title = styled.p` | ||
font-weight: bold; | ||
margin-bottom: 2rem; | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import React, { useState } from "react"; | ||
import styled from "styled-components"; | ||
import { useRouter } from "next/router"; | ||
|
||
import VoteForm from "../src/components/vote-form"; | ||
|
||
export default function Home() { | ||
const router = useRouter(); | ||
const { userName } = router.query; | ||
return ( | ||
<Wrapper> | ||
<Title onClick={() => router.back()}>리액트 투-표</Title> | ||
<Name>{userName}</Name>님 안녕하세요! | ||
<VoteForm /> | ||
</Wrapper> | ||
); | ||
} | ||
|
||
const Name = styled.span` | ||
color: blue; | ||
`; | ||
|
||
const Wrapper = styled.div` | ||
font-size: 3rem; | ||
min-height: 100vh; | ||
padding: 10rem 40rem; | ||
background-color: Azure; | ||
`; | ||
|
||
const Title = styled.p` | ||
font-weight: bold; | ||
margin-bottom: 2rem; | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import React from "react"; | ||
import styled from "styled-components"; | ||
import axios from "axios"; | ||
|
||
export default function CandidateForm(props) { | ||
const { name, voteCount, rank, id, refetch } = props; | ||
|
||
const voteCandidate = async () => { | ||
await axios | ||
.put(process.env.API_HOST + `/candidates/${id}/vote/`) | ||
.then(function (response) { | ||
console.log(response); | ||
alert(name + "님에게 투표 완료!"); | ||
refetch(); | ||
// return response.data; | ||
}) | ||
.catch(function (error) { | ||
console.log(error); | ||
alert("투표 실패!"); | ||
}); | ||
}; | ||
return ( | ||
<Wrapper> | ||
<CandidateRank>{rank}위:</CandidateRank> | ||
<CandidateDesc> | ||
{name}[{voteCount}표] | ||
</CandidateDesc> | ||
|
||
<VoteBtn | ||
onClick={() => { | ||
voteCandidate(); | ||
}} | ||
> | ||
투표 | ||
</VoteBtn> | ||
</Wrapper> | ||
); | ||
} | ||
|
||
const Wrapper = styled.div` | ||
display: flex; | ||
justify-content: space-between; | ||
align-items: center; | ||
flex-direction: row; | ||
`; | ||
|
||
const CandidateRank = styled.p` | ||
font-weight: bolder; | ||
font-size: 2.5rem; | ||
border: none; | ||
margin: none; | ||
`; | ||
const CandidateDesc = styled.p` | ||
font-size: 2.5rem; | ||
width: 40%; | ||
border: none; | ||
margin: none; | ||
`; | ||
const VoteBtn = styled.button` | ||
background: blue; | ||
color: white; | ||
border: none; | ||
border-radius: 0.7rem; | ||
font-size: 2rem; | ||
height: 3.5rem; | ||
width: 5.5rem; | ||
margin: none; | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,137 @@ | ||
import React from "react"; | ||
import React, { useState } from "react"; | ||
import axios from "axios"; | ||
import styled from "styled-components"; | ||
|
||
export default function LoginForm() { | ||
return <Wrapper>안녕 나는 로그인 폼!</Wrapper>; | ||
function LoginForm(props) { | ||
//State에 로그인에 필요한 데이터 저장 | ||
|
||
const [userData, setUserData] = useState({ | ||
email: "[email protected]", | ||
password: "example1!", | ||
}); | ||
|
||
const { loginSuccess } = props; | ||
// 변수 이름 쉽게하기 위해 | ||
const { email, password } = userData; | ||
|
||
const checkBlank = () => { | ||
// 둘 중 하나라도 안 채워져 있을시 알림 | ||
if (email === "" || password === "") { | ||
alert("빈칸 채워주세요!!"); | ||
return false; | ||
} else { | ||
return true; | ||
} | ||
}; | ||
|
||
const initFormData = (status) => { | ||
const initializedUserData = { | ||
email: "", | ||
password: "", | ||
}; | ||
|
||
switch (status) { | ||
case 404: | ||
alert("이메일이 존재하지 않습니다!!!"); | ||
break; | ||
case 422: | ||
alert("비밀번호가 틀렸습니다!!!"); | ||
initializedUserData.email = email; | ||
break; | ||
default: | ||
alert("로그인을 다시 시도해주세요!"); | ||
} | ||
|
||
setUserData(initializedUserData); | ||
}; | ||
// 로그인 시도 | ||
const tryLogin = () => { | ||
if (checkBlank() === false) { | ||
console.log("실패"); | ||
return; | ||
} | ||
axios | ||
.post(process.env.API_HOST + "/auth/signin/", userData) | ||
.then(function (response) { | ||
alert("로그인에 성공하셨습니다!!!"); | ||
loginSuccess(true); | ||
console.log(response); | ||
}) | ||
.catch(function (error) { | ||
initFormData(error.response.status); | ||
}); | ||
}; | ||
|
||
// 값이 변경될 때 | ||
const handleFormChange = (e) => { | ||
const { name, value } = e.target; | ||
setUserData({ | ||
...userData, | ||
[name]: value, | ||
}); | ||
}; | ||
|
||
return ( | ||
<Wrapper> | ||
<LoginTitle>로그인</LoginTitle> | ||
<InputWrapper> | ||
<InputLabel>EMAIL</InputLabel> | ||
<DataInput name="email" value={email} onChange={handleFormChange} /> | ||
</InputWrapper> | ||
<InputWrapper> | ||
<InputLabel>PASSWORD</InputLabel> | ||
<DataInput name="password" type="password" value={password} onChange={handleFormChange} /> | ||
</InputWrapper> | ||
<LoginBtn onClick={tryLogin} type="submit"> | ||
로그인 | ||
</LoginBtn> | ||
</Wrapper> | ||
); | ||
} | ||
|
||
export default React.memo(LoginForm); | ||
export const MemoizedLoginForm = React.memo(LoginForm); | ||
|
||
const Wrapper = styled.div` | ||
width: 100%; | ||
min-height: 30rem; | ||
background-color: white; | ||
font-size: 18px; | ||
padding: 3rem 4rem; | ||
`; | ||
|
||
const LoginTitle = styled.p` | ||
font-weight: bold; | ||
font-size: 2rem; | ||
margin-bottom: 3rem; | ||
`; | ||
|
||
const InputWrapper = styled.div` | ||
width: 100%; | ||
display: flex; | ||
flex-direction: row; | ||
justify-content: space-between; | ||
align-items: flex-start; | ||
margin-bottom: 3rem; | ||
`; | ||
|
||
const InputLabel = styled.p` | ||
font-size: 1.5rem; | ||
padding: 0px; | ||
margin: 0px; | ||
`; | ||
|
||
const DataInput = styled.input` | ||
border: 1px solid rgb(97, 97, 97); | ||
padding: 0.5rem 0.8rem; | ||
width: 70%; | ||
`; | ||
|
||
const LoginBtn = styled.button` | ||
float: right; | ||
background: rgb(222, 222, 222); | ||
font-size: 1.5rem; | ||
padding: 0.5rem 1rem; | ||
border: none; | ||
border-radius: 0.3rem; | ||
`; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
login-form에서는 export 하는 컴포넌트가 하나 뿐이기때문에 default export를 하는 게 좋을 거 같아요. LoginForm의 default export를 React.memo적용한 후에 하는 게 어떨까요?
export default React.memo(LoginForm);