Skip to content

Commit

Permalink
Merge branch 'develop' into feature/195_syntax_highlight
Browse files Browse the repository at this point in the history
  • Loading branch information
mikiya1130 committed Mar 5, 2022
2 parents 179d79c + 2c59290 commit 004e1b4
Show file tree
Hide file tree
Showing 13 changed files with 136 additions and 94 deletions.
5 changes: 0 additions & 5 deletions src/components/AuthContext.tsx

This file was deleted.

40 changes: 40 additions & 0 deletions src/components/AuthProvider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { User } from "firebase/auth";
import { createContext, useEffect, useState } from "react";

import { auth } from "@/utils/firebase";

type AuthContextProps = {
currentUser: User | null | undefined;
signInCheck: boolean;
};

const AuthContext = createContext<AuthContextProps>({
currentUser: undefined,
signInCheck: false,
});

const AuthProvider = ({ children }: { children: JSX.Element }) => {
const [currentUser, setCurrentUser] = useState<User | null | undefined>(
undefined
);
const [signInCheck, setSignInCheck] = useState<boolean>(false);

useEffect(() => {
auth.onAuthStateChanged(async (user) => {
if (user) {
setCurrentUser(user);
setSignInCheck(true);
} else {
setSignInCheck(true);
}
});
}, []);

return (
<AuthContext.Provider value={{ currentUser, signInCheck }}>
{children}
</AuthContext.Provider>
);
};

export { AuthContext, AuthProvider };
5 changes: 4 additions & 1 deletion src/components/login/LoginButton.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import { Button } from "@chakra-ui/react";
import React from "react";
import React, { useContext } from "react";
import { DiGithubBadge } from "react-icons/di";

import { AuthContext } from "@/components/AuthProvider";
import { useAuth } from "@/hooks/useAuth";

const LoginButton = () => {
const { signInCheck } = useContext(AuthContext);
const { login } = useAuth();

return (
<Button
leftIcon={<DiGithubBadge color="black" size={25} />}
bgColor="white"
color="black"
isLoading={!signInCheck}
onClick={login}
>
ログイン
Expand Down
6 changes: 3 additions & 3 deletions src/hooks/useApi.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Octokit } from "@octokit/rest";

import { useCurrentUser } from "@/hooks/useCurrentUser";
import { currentUserSelectors } from "@/store/currentUserState";

export const useApi = () => {
const { accessToken } = useCurrentUser();
const currentUser = currentUserSelectors.useCurrentUser();

const octokit = new Octokit({
auth: accessToken,
auth: currentUser.accessToken,
});

return { octokit };
Expand Down
8 changes: 3 additions & 5 deletions src/hooks/useAuth.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import { GithubAuthProvider, signInWithRedirect, signOut } from "firebase/auth";
import { useRouter } from "next/router";
import { useSetRecoilState } from "recoil";

import { currentUserState } from "@/store/currentUserState";
import { CurrentUserType } from "@/types/CurrentUserType";
import { currentUserActions } from "@/store/currentUserState";
import { auth } from "@/utils/firebase";

export const useAuth = () => {
const router = useRouter();
const setCurrentUser = useSetRecoilState<CurrentUserType>(currentUserState);
const updateCurrentUser = currentUserActions.useUpdateCurrentUser();
const provider = new GithubAuthProvider();
provider.addScope("repo");

Expand All @@ -18,7 +16,7 @@ export const useAuth = () => {

const logout = () => {
signOut(auth);
setCurrentUser({
updateCurrentUser({
isSignedIn: false,
username: "",
accessToken: "",
Expand Down
15 changes: 0 additions & 15 deletions src/hooks/useCurrentUser.ts

This file was deleted.

10 changes: 3 additions & 7 deletions src/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,16 @@ import { ChakraProvider } from "@chakra-ui/react";
import { NextPage } from "next";
import type { AppProps } from "next/app";
import Head from "next/head";
import { useReducer } from "react";
import { RecoilRoot } from "recoil";

import AuthContext from "@/components/AuthContext";
import authReducer from "@/utils/authReducer";
import { AuthProvider } from "@/components/AuthProvider";

import "prismjs/themes/prism.css";
import "@/style/difffile.css";

const MyApp: NextPage<AppProps> = ({ Component, pageProps }) => {
const [state] = useReducer(authReducer.reducer, authReducer.initialState);

return (
<AuthContext.Provider value={state}>
<AuthProvider>
<ChakraProvider>
<RecoilRoot>
<Head>
Expand All @@ -24,7 +20,7 @@ const MyApp: NextPage<AppProps> = ({ Component, pageProps }) => {
<Component {...pageProps} />
</RecoilRoot>
</ChakraProvider>
</AuthContext.Provider>
</AuthProvider>
);
};

Expand Down
31 changes: 13 additions & 18 deletions src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,24 @@ import { Octokit } from "@octokit/rest";
import { getRedirectResult, GithubAuthProvider } from "firebase/auth";
import React, { useEffect, useState } from "react";
import { BsCheckCircleFill } from "react-icons/bs";
import { useSetRecoilState } from "recoil";

import Layout from "@/components/Layout";
import NoPullsMessage from "@/components/top/NoPullsMessage";
import PullRequestList from "@/components/top/PullRequestList";
import withAuth from "@/hoc/withAuth";
import { useApi } from "@/hooks/useApi";
import { useCurrentUser } from "@/hooks/useCurrentUser";
import { currentUserState } from "@/store/currentUserState";
import { CurrentUserType } from "@/types/CurrentUserType";
import {
currentUserActions,
currentUserSelectors,
} from "@/store/currentUserState";
import { TopPullRequestType } from "@/types/PullRequestType";
import { auth } from "@/utils/firebase";

const TopPage = () => {
const [pulls, setPulls] = useState<TopPullRequestType[]>([]);
const { octokit } = useApi();
const { username } = useCurrentUser();
const setCurrentUser = useSetRecoilState<CurrentUserType>(currentUserState);
const currentUser = currentUserSelectors.useCurrentUser();
const updateCurrentUser = currentUserActions.useUpdateCurrentUser();

useEffect(() => {
getRedirectResult(auth).then((result) => {
Expand All @@ -34,27 +34,22 @@ const TopPage = () => {
});

octokit.request("GET /user").then((res) => {
setCurrentUser((prevState) => ({
...prevState,
updateCurrentUser({
username: res.data.login,
}));
isSignedIn: true,
accessToken: String(token),
});
});

setCurrentUser((prevState) => ({
...prevState,
isSignedIn: true,
accessToken: String(token),
}));
}
}
});
}, []);

useEffect(() => {
if (username) {
if (currentUser.username) {
octokit
.request("GET /search/issues", {
q: `is:pr+user-review-requested:${username}+state:open`,
q: `is:pr+user-review-requested:${currentUser.username}+state:open`,
})
.then((response) => {
const items = response.data.items;
Expand All @@ -77,7 +72,7 @@ const TopPage = () => {
setPulls(newPulls);
});
}
}, [username]);
}, [currentUser.username]);

return (
<Layout
Expand Down
14 changes: 14 additions & 0 deletions src/store/RecoilKeys.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export enum RecoilAtomKeys {
CURRENT_USER_STATE = "currentUserState",
LOGIN_BUTTON_LOADING_STATE = "loginButtonLoadingState",
}

export enum RecoilSelectorKeys {
CURRENT_USER = "currentUser",
LOGIN_BUTTON_LOADING = "loginButtonLoading",
}

export enum RecoilPersistenceKeys {
PERSISTED_CURRENT_USER = "persistedCurrentUser",
PERSISTED_LOGIN_BUTTON_LOADING = "persistedLoginButton",
}
57 changes: 51 additions & 6 deletions src/store/currentUserState.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,66 @@
import { atom } from "recoil";
import { useCallback } from "react";
import { atom, selector, useRecoilValue, useSetRecoilState } from "recoil";
import { recoilPersist } from "recoil-persist";

import { CurrentUserType } from "@/types/CurrentUserType";
import {
RecoilAtomKeys,
RecoilPersistenceKeys,
RecoilSelectorKeys,
} from "@/store/RecoilKeys";

type CurrentUserState = {
isSignedIn: boolean;
username: string;
accessToken: string;
reviewId: number;
};

const { persistAtom } = recoilPersist({
key: "persistedCurrentUserState",
key: RecoilPersistenceKeys.PERSISTED_CURRENT_USER,
});

const initialState: CurrentUserType = {
const initialState: CurrentUserState = {
isSignedIn: false,
username: "",
accessToken: "",
reviewId: 0,
};

export const currentUserState = atom<CurrentUserType>({
key: "currentUserState",
const currentUserState = atom<CurrentUserState>({
key: RecoilAtomKeys.CURRENT_USER_STATE,
default: initialState,
effects_UNSTABLE: [persistAtom],
});

type CurrentUserActions = {
useUpdateCurrentUser: () => (
partialCurrentUser: Partial<CurrentUserState>
) => void;
};

export const currentUserActions: CurrentUserActions = {
useUpdateCurrentUser: () => {
const setCurrentUser =
useSetRecoilState<CurrentUserState>(currentUserState);

return useCallback((partialCurrentUser) => {
setCurrentUser((prevState) => ({
...prevState,
...partialCurrentUser,
}));
}, []);
},
};

type CurrentUserSelectors = {
useCurrentUser: () => CurrentUserState;
};

const currentUserSelector = selector<CurrentUserState>({
key: RecoilSelectorKeys.CURRENT_USER,
get: ({ get }) => get(currentUserState),
});

export const currentUserSelectors: CurrentUserSelectors = {
useCurrentUser: () => useRecoilValue<CurrentUserState>(currentUserSelector),
};
6 changes: 0 additions & 6 deletions src/types/CurrentUserType.ts

This file was deleted.

26 changes: 0 additions & 26 deletions src/utils/authReducer.ts

This file was deleted.

7 changes: 5 additions & 2 deletions src/utils/firebase.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { initializeApp } from "firebase/app";
import { getApps, initializeApp } from "firebase/app";
import { getAuth } from "firebase/auth";

const firebaseConfig = {
Expand All @@ -11,5 +11,8 @@ const firebaseConfig = {
measurementId: process.env.NEXT_PUBLIC_MEASUREMENT_ID,
};

initializeApp(firebaseConfig);
if (!getApps().length) {
initializeApp(firebaseConfig);
}

export const auth = getAuth();

0 comments on commit 004e1b4

Please sign in to comment.