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

useUser custom hook 사용 #126

Merged
merged 9 commits into from
Mar 19, 2021
Merged
Show file tree
Hide file tree
Changes from 5 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
6 changes: 2 additions & 4 deletions src/components/Admin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,16 @@ import { useHistory, useParams } from "react-router-dom";
import PlayerListItem from "./PlayerListItem";
import AddMatch from "./AddMatch";
import { CollectionName } from "../collections";
import { useUser } from "../customHooks/useUser";

const Admin = (): JSX.Element => {
const history = useHistory();
const { id } = useParams<{ id: string }>();
const [user, setUser] = useState(firebase.auth().currentUser);
const [idToPlayers, setIdToPlayers] = useState<Map<string, string>>(
new Map()
);

useEffect(() => {
firebase.auth().onAuthStateChanged(setUser);
}, []);
const user = useUser();

useEffect(() => {
(async () => {
Expand Down
18 changes: 12 additions & 6 deletions src/components/Header.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ import { fireAntEvent } from "../setupTests";

describe("Test", () => {
test("Sign In", async () => {
const fakeUser = {} as firebase.User;
((firebase.auth as unknown) as jest.Mock).mockReturnValue({
currentUser: null,
onAuthStateChanged: jest.fn(),
onAuthStateChanged: (callback: (user: firebase.User | null) => void) => {
callback(null);
},
signInWithPhoneNumber: jest.fn().mockResolvedValue({
confirm: jest.fn().mockResolvedValue({
additionalUserInfo: { isNewUser: false },
user: {},
}),
confirm: jest.fn().mockImplementation((callback: (user: firebase.User | null) => void) => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

어떻게 confirm돌리는지 보면 callback아니라 code 남겨:

const { user } = await confirmationResult.confirm(code);

그래서 이런 목 만들면 안 될거야.

callback(fakeUser);
})
}),
});
((firebase.firestore as unknown) as jest.Mock).mockReturnValue({
Expand Down Expand Up @@ -42,10 +44,13 @@ describe("Test", () => {
});

test("Sign Out", async () => {
const fakeUser = null;
((firebase.auth as unknown) as jest.Mock).mockReturnValue({
onAuthStateChanged: jest.fn(),
currentUser: {},
signOut: jest.fn().mockResolvedValue(null),
signOut: jest.fn().mockImplementation((callback: (user: firebase.User | null) => void) => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Header코드 보면 signOut을 입력값 하나도 안 넘겨.

await firebase.auth().signOut();

그래서 이 목이 callback안 받을거야. 그래서 오류 날거야. 이 코드 같아:

function mockSignOut(callback) {
  callback(null);
}

mockSignOut();

돌리면 callback이 undefined이야. 그래서 callback() 돌리면 undefined() 돌릴거야. undefined이 함수 아니라서 "undefined is not a function"이라는 오류 날거야.

Copy link
Owner Author

Choose a reason for hiding this comment

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

아 await firebase.auth().signOut(); 이부분은 이해했어!
나중에 같이 코딩할때 어떻게 풀어야되는지 알려줘! 혼자하기 너무 어려워 ㅠㅠ

Copy link
Collaborator

Choose a reason for hiding this comment

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

응! 만날때 보여줄게

callback(null);
}),
});

render(<Header />);
Expand All @@ -57,6 +62,7 @@ describe("Test", () => {
test("Sign In Status", async () => {
const fakeUser = {} as firebase.User;
((firebase.auth as unknown) as jest.Mock).mockReturnValue({
currentUser: {},
onAuthStateChanged: (callback: (user: firebase.User) => void) => {
callback(fakeUser);
},
Expand Down
12 changes: 3 additions & 9 deletions src/components/Header.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
import React, { useState, useEffect } from "react";
import React, { useState } from "react";
import { Row, Col, Modal } from "antd";
import "antd/dist/antd.css";
import "./Header.css";
import firebase from "firebase";
import { useHistory, Link } from "react-router-dom";
import { CollectionName } from "../collections";
import { useUser } from "../customHooks/useUser";

let appVerifier: firebase.auth.ApplicationVerifier | null = null;

const Header = (): JSX.Element => {
const history = useHistory();
const [isModalVisible, setIsModalVisible] = useState(false);
const [phoneNumber, setPhoneNumber] = useState("");
const [user, setUser] = useState(firebase.auth().currentUser);
const [loading, setLoading] = useState(false);

useEffect(() => {
firebase.auth().onAuthStateChanged(setUser);
}, []);
const user = useUser();

const showModal = () => {
setIsModalVisible(true);
Expand Down Expand Up @@ -65,8 +62,6 @@ const Header = (): JSX.Element => {
hideModal();
if (isNewUser) {
history.push("/signup");
} else {
setUser(user);
}
} catch (error) {
window.alert(error);
Expand All @@ -77,7 +72,6 @@ const Header = (): JSX.Element => {
const signOut = async () => {
try {
await firebase.auth().signOut();
setUser(null);
} catch (error) {
window.alert(error);
}
Expand Down
7 changes: 2 additions & 5 deletions src/components/MatchDetail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,16 @@ import {
} from "../globalFunction";
import ReservationStatus from "./ReservationStatus";
import { CollectionName } from "../collections";
import { useUser } from "../customHooks/useUser";

const MatchDetail = (): JSX.Element | null => {
const history = useHistory();
const [match, setMatch] = useState<Match | null>(null);
const [user, setUser] = useState(firebase.auth().currentUser);
const [reservationStatus, setReservationStatus] = useState<Status>(
"신청가능"
);
const { id } = useParams<{ id: string }>();

useEffect(() => {
firebase.auth().onAuthStateChanged(setUser);
}, []);
const user = useUser();

useEffect(() => {
(async () => {
Expand Down
7 changes: 2 additions & 5 deletions src/components/MatchList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,14 @@ import { Match } from "../types";
import { Button, Spin } from "antd";
import { Link } from "react-router-dom";
import { CollectionName } from "../collections";
import { useUser } from "../customHooks/useUser";

const MatchList = (): JSX.Element => {
const [dateKeyToMatches, setDateKeyToMatches] = useState<
Map<string, Match[]>
>(new Map());
const [user, setUser] = useState(firebase.auth().currentUser);
const [isAdmin, setIsAdmin] = useState(false);

useEffect(() => {
firebase.auth().onAuthStateChanged(setUser);
}, []);
const user = useUser();

useEffect(() => {
async function getMatches() {
Expand Down
8 changes: 2 additions & 6 deletions src/components/MatchListItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,19 @@ import "antd/dist/antd.css";
import "./MatchListItem.css";
import { Match, Status } from "../types";
import { Link } from "react-router-dom";
import firebase from "firebase";
import { getReservationStatus } from "../globalFunction";
import ReservationStatus from "./ReservationStatus";
import { useUser } from "../customHooks/useUser";

const MatchListItem = (matchProps: {
match: Match;
isAdmin: boolean;
}): JSX.Element => {
const { match, isAdmin } = matchProps;
const [user, setUser] = useState(firebase.auth().currentUser);
const [reservationStatus, setReservationStatus] = useState<Status>(
"신청가능"
);

useEffect(() => {
firebase.auth().onAuthStateChanged(setUser);
}, []);
const user = useUser();

useEffect(() => {
(async () => {
Expand Down
12 changes: 12 additions & 0 deletions src/customHooks/useUser.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { useState, useEffect } from "react";
import firebase from "firebase";

export function useUser(): firebase.User | null {
const [user, setUser] = useState(firebase.auth().currentUser);

useEffect(() => {
firebase.auth().onAuthStateChanged(setUser);
}, []);

return user;
}