Thanks to visit codestin.com
Credit goes to github.com

Skip to content

GitHub 로그인 기능 구현 #20

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

Merged
merged 14 commits into from
Jul 20, 2022
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
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
node_modules

.env

.env.development
.idea
20 changes: 20 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"@testing-library/jest-dom": "^5.16.4",
"@testing-library/react": "^13.3.0",
"@testing-library/user-event": "^13.5.0",
"prettier": "^2.7.1",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-router-dom": "^6.3.0",
Expand Down
7 changes: 7 additions & 0 deletions src/hooks/user/useSetUser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { useSetRecoilState } from "recoil";
import userState from "../../state/user";

const useSetUser = () => {
return useSetRecoilState(userState);
};
export default useSetUser;
26 changes: 26 additions & 0 deletions src/hooks/user/useUserLogin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import {
requestAccessTokenAPI,
requestLoginAPI,
} from "../../pages/solutionReportPage/utils/gitHubLogin";
import useSetUser from "./useSetUser";
import { useState } from "react";
import useUserValue from "./useUserValue";

const useUserLogin = () => {
const [isLoggedIn, setIsLoggedIn] = useState(false);
const userInfo = useUserValue();
const setUserInfo = useSetUser();
const requestLogin = (code) => {
requestAccessTokenAPI(code)
.then((response) => requestLoginAPI(response.access_token))
.then((response) => {
if (response.hasOwnProperty("login")) {
setIsLoggedIn(true);
setUserInfo(response);
}
if (userInfo) setIsLoggedIn(true);
});
};
return { isLoggedIn, requestLogin };
};
export default useUserLogin;
11 changes: 11 additions & 0 deletions src/hooks/user/useUserProfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import useUserValue from "./useUserValue";

const useUserProfile = () => {
const user = useUserValue();
return {
profileImg: user.avatar_url ?? "",
username: user.name ?? "",
gitHubUrl: user.html_url ?? "",
};
};
export default useUserProfile;
7 changes: 7 additions & 0 deletions src/hooks/user/useUserValue.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { useRecoilValue } from "recoil";
import userState from "../../state/user";

const useUserValue = () => {
return useRecoilValue(userState);
};
export default useUserValue;
32 changes: 23 additions & 9 deletions src/pages/errorReportPage/ErrorReport.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@ export default function ErrorReport() {
const [questionName, setQuestionName] = useState("");
const [detailContent, setDetailContent] = useState("");

const isQuestionNameVisible = errorCategory !== "" && errorCategory !== "error-notCopied";
const isQuestionNameVisible =
errorCategory !== "" && errorCategory !== "error-notCopied";
const isDetailContentVisible =
errorCategory !== "" && (errorCategory !== "error-wrongAnswer" || questionName !== "");
const isSubmitBtnDisabled = errorCategory === "error-other" && detailContent === "";
errorCategory !== "" &&
(errorCategory !== "error-wrongAnswer" || questionName !== "");
const isSubmitBtnDisabled =
errorCategory === "error-other" && detailContent === "";

function handleOtherErrorBtnClick() {
setSubmitted(false);
Expand Down Expand Up @@ -56,14 +59,19 @@ export default function ErrorReport() {
{submitted ? (
<>
<ThanksMsg>제보해주셔서 감사합니다.</ThanksMsg>
<OtherReportBtn onClick={handleOtherErrorBtnClick}>다른 오류 제보</OtherReportBtn>
<OtherReportBtn onClick={handleOtherErrorBtnClick}>
다른 오류 제보
</OtherReportBtn>
</>
) : (
<MainContetnWrapper>
<StepByStepInputItem>
<InputLabel>오류 유형</InputLabel>
<RadioInputWrapper>
<Label htmlFor="error-wrongAnswer" clicked={errorCategory === "error-wrongAnswer"}>
<Label
htmlFor="error-wrongAnswer"
clicked={errorCategory === "error-wrongAnswer"}
>
정답 통과가 안돼요
</Label>
<RadioInput
Expand All @@ -72,7 +80,10 @@ export default function ErrorReport() {
name="errorCategory"
onClick={handleErrorCategoryClick}
/>
<Label htmlFor="error-notCopied" clicked={errorCategory === "error-notCopied"}>
<Label
htmlFor="error-notCopied"
clicked={errorCategory === "error-notCopied"}
>
코드 복사가 안돼요
</Label>
<RadioInput
Expand All @@ -81,7 +92,10 @@ export default function ErrorReport() {
name="errorCategory"
onClick={handleErrorCategoryClick}
/>
<Label htmlFor="error-other" clicked={errorCategory === "error-other"}>
<Label
htmlFor="error-other"
clicked={errorCategory === "error-other"}
>
기타
</Label>
<RadioInput
Expand All @@ -101,7 +115,7 @@ export default function ErrorReport() {
placeholder="문제 이름을 검색하세요."
defaultValue={questionName}
onInput={handleQuestionNameInput}
></TextInput>
/>
<QuestionList id="questionsList">
<QuestionItem>
<QuestionBtn>1번문제</QuestionBtn>
Expand Down Expand Up @@ -140,7 +154,7 @@ export default function ErrorReport() {
cols="100"
onInput={handleDetailContentInput}
defaultValue={detailContent}
></TextArea>
/>
</StepByStepInputItem>

<StepByStepInputItem>
Expand Down
112 changes: 69 additions & 43 deletions src/pages/solutionReportPage/SolutionReport.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,41 @@
import { useState } from "react";
import { useEffect, useState } from "react";
import styled from "styled-components";
import Header from "../../components/Header";
import {
ThanksMsg,
OtherReportBtn,
InputLabel,
MainContetnWrapper,
OtherReportBtn,
StepByStepInputItem,
InputLabel,
TextInput,
QuestionList,
QuestionItem,
QuestionBtn,
TextArea,
SubmitBtn,
TextArea,
TextInput,
ThanksMsg,
} from "../../style/styledComponents";
import gitHubLogoSrc from "../../images/github-logo-white.png";
import { useSearchParams } from "react-router-dom";
import { LOGIN_URL } from "./utils/gitHubLogin";
import useUserProfile from "../../hooks/user/useUserProfile";
import useUserLogin from "../../hooks/user/useUserLogin";

export default function SolutionReport() {
const [submitted, setSubmitted] = useState(false);
const [questionName, setQuestionName] = useState("");
const [detailContent, setDetailContent] = useState("");
const [searchParams, setSearchParams] = useSearchParams();
const userInfo = useUserProfile();
const { isLoggedIn, requestLogin } = useUserLogin();

useEffect(() => {
if (searchParams.get("code")) {
const code = searchParams.get("code");
requestLogin(code);
}
}, [searchParams]);
const handleGitHubLogin = async () => {};
const isDetailContentVisible = questionName !== "";
const isSubmitBtnDisabled = detailContent === "";

function handleOtherSolutionBtnClick() {
console.log("!");
setSubmitted(false);
setQuestionName("");
setDetailContent("");
Expand All @@ -50,7 +60,9 @@ export default function SolutionReport() {
{submitted ? (
<>
<ThanksMsg>제보해주셔서 감사합니다.</ThanksMsg>
<OtherReportBtn onClick={handleOtherSolutionBtnClick}>다른 정답 제보</OtherReportBtn>
<OtherReportBtn onClick={handleOtherSolutionBtnClick}>
다른 정답 제보
</OtherReportBtn>
</>
) : (
<MainContetnWrapper>
Expand All @@ -61,40 +73,55 @@ export default function SolutionReport() {
placeholder="문제 이름을 검색하세요."
defaultValue={questionName}
onInput={handleQuestionNameInput}
></TextInput>
<QuestionList id="questionsList">
<QuestionItem>
<QuestionBtn>1번문제</QuestionBtn>
</QuestionItem>
<QuestionItem>
<QuestionBtn>2번문제</QuestionBtn>
</QuestionItem>
<QuestionItem>
<QuestionBtn>3번문제</QuestionBtn>
</QuestionItem>
<QuestionItem>
<QuestionBtn>4번문제</QuestionBtn>
</QuestionItem>
<QuestionItem>
<QuestionBtn>5번문제</QuestionBtn>
</QuestionItem>
<QuestionItem>
<QuestionBtn>6번문제</QuestionBtn>
</QuestionItem>
<QuestionItem>
<QuestionBtn>7번문제</QuestionBtn>
</QuestionItem>
<QuestionItem>
<QuestionBtn>8번문제</QuestionBtn>
</QuestionItem>
</QuestionList>
/>
{/*<QuestionList id="questionsList">*/}
{/* <QuestionItem>*/}
{/* <QuestionBtn>1번문제</QuestionBtn>*/}
{/* </QuestionItem>*/}
{/* <QuestionItem>*/}
{/* <QuestionBtn>2번문제</QuestionBtn>*/}
{/* </QuestionItem>*/}
{/* <QuestionItem>*/}
{/* <QuestionBtn>3번문제</QuestionBtn>*/}
{/* </QuestionItem>*/}
{/* <QuestionItem>*/}
{/* <QuestionBtn>4번문제</QuestionBtn>*/}
{/* </QuestionItem>*/}
{/* <QuestionItem>*/}
{/* <QuestionBtn>5번문제</QuestionBtn>*/}
{/* </QuestionItem>*/}
{/* <QuestionItem>*/}
{/* <QuestionBtn>6번문제</QuestionBtn>*/}
{/* </QuestionItem>*/}
{/* <QuestionItem>*/}
{/* <QuestionBtn>7번문제</QuestionBtn>*/}
{/* </QuestionItem>*/}
{/* <QuestionItem>*/}
{/* <QuestionBtn>8번문제</QuestionBtn>*/}
{/* </QuestionItem>*/}
{/*</QuestionList>*/}
</StepByStepInputItem>

{isDetailContentVisible && (
{/*isDetailContentVisible &&*/}
{
<>
<StepByStepInputItem>
<InputLabel>기여자 등록</InputLabel>
<GitHubLoginBtn id="gitHubLoginBtn">GitHub 로그인</GitHubLoginBtn>
{isLoggedIn ? (
<UserInfo>
이름: {userInfo.username}
이미지: {userInfo.profileImg}
</UserInfo>
) : (
<a href={LOGIN_URL}>
<GitHubLoginBtn
id="gitHubLoginBtn"
onClick={handleGitHubLogin}
>
GitHub 로그인
</GitHubLoginBtn>
</a>
)}
</StepByStepInputItem>
<StepByStepInputItem>
<InputLabel>내용</InputLabel>
Expand All @@ -104,7 +131,7 @@ export default function SolutionReport() {
cols="100"
onInput={handleDetailContentInput}
defaultValue={detailContent}
></TextArea>
/>
</StepByStepInputItem>

<StepByStepInputItem>
Expand All @@ -117,7 +144,7 @@ export default function SolutionReport() {
</SubmitBtn>
</StepByStepInputItem>
</>
)}
}
</MainContetnWrapper>
)}
</>
Expand All @@ -139,7 +166,6 @@ const GitHubLoginBtn = styled.button`
background-repeat: no-repeat;
cursor: pointer;
`;

// const Msg = styled.span`
// color: ${(props) => props.theme.programmersBlue};
// `;
Loading