Skip to content

Commit

Permalink
[#12] 테스트
Browse files Browse the repository at this point in the history
  • Loading branch information
6mn12j committed Jul 20, 2022
1 parent 68a6e97 commit fdcbb1e
Show file tree
Hide file tree
Showing 10 changed files with 111 additions and 19 deletions.
10 changes: 10 additions & 0 deletions src/@types/type.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
type User = {
isLogin: boolean;
id: string;
state: "checkIn" | "checkOut" | null;
checkinAt: string | null;
checkoutAt: string | null;
profile: string;
isAdmin: boolean;
};

type User2 = {
isLogin: boolean;
id: string;
cardNum: string;
Expand Down
2 changes: 0 additions & 2 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ import AppRouter from "routes/AppRouter";
import useUser from "utils/hooks/useUser";
import { getCookieValue } from "utils/cookie";
import { version } from "../package.json";
// import useCluster from "./utils/hooks/useCluster";

const App = () => {
// const { setCluster, setOfficeHour, setOfficeLunchTime } = useCluster();
const { login, logout } = useUser();

useEffect(() => {
Expand Down
2 changes: 1 addition & 1 deletion src/api/baseAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import axios, { AxiosError, AxiosResponse } from "axios";

const API_URL = process.env.REACT_APP_API_URL;

export const VERSION_PATH = "v1";
export const VERSION_PATH = "v3";
export const makeAPIPath = (path: string) => `${VERSION_PATH}/${path}`;

export const instance = axios.create({
Expand Down
12 changes: 12 additions & 0 deletions src/api/userAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,18 @@ export type GetUsingCardResponse = {
};
export type PostCheckOutResponse = boolean;

export const getUserUsagePerDay = () => {
return instance.get(makeAPIPath(`usage/perday`));
};

export const getUserUsagePerWeek = () => {
return instance.get(makeAPIPath(`usage/perweek`));
};

export const getUserUsagePerMonth = () => {
return instance.get(makeAPIPath(`usage/permonth`));
};

export const getStatus = () => {
return instance.get(makeAPIPath(`user/status`));
};
Expand Down
34 changes: 23 additions & 11 deletions src/pages/CheckIn.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Backdrop, CircularProgress } from "@mui/material";
import React, { useCallback, useEffect, useState } from "react";
import { getClusterUsingInfo } from "api/configAPI";
import { getDailyUsage, getStatus } from "api/userAPI";
import { getDailyUsage, getStatus, getUserUsagePerDay } from "api/userAPI";
import ProfileCard from "components/Card/ProfileCard";
import TimeLogCard from "components/Card/TimeLogCard";
import ClusterStatusBoard from "components/common/ClusterStatusBoard";
Expand Down Expand Up @@ -56,23 +56,35 @@ const CheckIn = () => {
const [logs, setLogs] = useState<Log[]>([]);
const [isLoading, setIsLoading] = useState(false);

// const getUserData = useCallback(async () => {
// setIsLoading(true);
// try {
// const [getUserStatusData, getLogsData] = await Promise.all([getUserStatus(), getLogs()]);
// setUser(getUserStatusData.user);
// setAuth({ isAdmin: getUserStatusData.isAdmin });
// // setCurrentUserCount(getUserStatusData.cluster);
// setLogs(getLogsData);
// } catch (e) {
// setLogs([]);
// console.log(e);
// alert("유저 정보가 올바르지 않습니다.\n 반복될 경우 관리자에게 요청해주세요");
// removeCookieValue(process.env.REACT_APP_AUTH_KEY);
// logout();
// }
// setIsLoading(false);
// }, [logout, setAuth, setUser]);

const getUserData = useCallback(async () => {
setIsLoading(true);
try {
const [getUserStatusData, getLogsData] = await Promise.all([getUserStatus(), getLogs()]);
setUser(getUserStatusData.user);
setAuth({ isAdmin: getUserStatusData.isAdmin });
// setCurrentUserCount(getUserStatusData.cluster);
setLogs(getLogsData);
const data = await getUserUsagePerDay();

console.log(data);
} catch (e) {
setLogs([]);
console.log(e);
alert("유저 정보가 올바르지 않습니다.\n 반복될 경우 관리자에게 요청해주세요");
removeCookieValue(process.env.REACT_APP_AUTH_KEY);
logout();
}
setIsLoading(false);
}, [logout, setAuth, setUser]);
}, []);

const handleFlip = () => {
setIsCardFlipped((prev) => !prev);
Expand Down
2 changes: 1 addition & 1 deletion src/pages/Home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const Home = () => {
const handleLogin = () => {
window.location.href = `${
process.env.REACT_APP_API_URL
}/user/login?redirect=${encodeURIComponent(window.location.href)}`;
}/user/login/42?redirect=${encodeURIComponent(window.location.href)}`;
};

useEffect(() => {
Expand Down
59 changes: 59 additions & 0 deletions src/redux/modules/profile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import * as Sentry from "@sentry/react";
import { ActionType, createAction, Reducer } from "typesafe-actions";
import DEFAULT_PROFILE from "../../assets/user-default.png";
// actions
const LOGIN = "profile/LOGIN";
const LOGOUT = "profile/LOGOUT";
const SET_USER = "profile/SET_USER";
const SET_AUTH = "profile/SET_AUTH";

// action creators
export const login = createAction(LOGIN)();
export const logout = createAction(LOGOUT)();
export const setUser = createAction(SET_USER)<Omit<User, "isLogin" | "isAdmin">>();
export const setAuth = createAction(SET_AUTH)<{ isAdmin: boolean }>();

// type
const actions = { setUser, login, logout, setAuth };
type UserActions = ActionType<typeof actions>;

// initialState
export const initialState: User = {
isLogin: false,
id: "",
state: "checkOut",
checkinAt: null,
checkoutAt: null,
profile: DEFAULT_PROFILE,
isAdmin: false,
};

// reducer
const user: Reducer<User, UserActions> = (state = initialState, action): User => {
switch (action.type) {
case LOGIN:
return {
...state,
isLogin: true,
};
case LOGOUT:
return {
...state,
isLogin: false,
};
case SET_AUTH: {
const { isAdmin } = action.payload;
return { ...state, isAdmin };
}
case SET_USER: {
const { checkinAt, checkoutAt, id, profile, state: userState } = action.payload;
Sentry.setUser({ username: id });
return { ...state, id, state: userState, checkinAt, checkoutAt, profile };
}

default:
return state;
}
};

export default user;
6 changes: 3 additions & 3 deletions src/redux/modules/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const SET_AUTH = "user/SET_AUTH";
// action creators
export const login = createAction(LOGIN)();
export const logout = createAction(LOGOUT)();
export const setUser = createAction(SET_USER)<Omit<User, "isLogin" | "isAdmin">>();
export const setUser = createAction(SET_USER)<Omit<User2, "isLogin" | "isAdmin">>();
export const setCardNum = createAction(SET_CARD_NUM)<{ cardNum: string }>();
export const setAuth = createAction(SET_AUTH)<{ isAdmin: boolean }>();

Expand All @@ -20,7 +20,7 @@ const actions = { setCardNum, setUser, login, logout, setAuth };
type UserActions = ActionType<typeof actions>;

// initialState
export const initialState: User = {
export const initialState: User2 = {
isLogin: false,
id: "",
cardNum: "",
Expand All @@ -32,7 +32,7 @@ export const initialState: User = {
};

// reducer
const user: Reducer<User, UserActions> = (state = initialState, action): User => {
const user: Reducer<User2, UserActions> = (state = initialState, action): User2 => {
switch (action.type) {
case LOGIN:
return {
Expand Down
1 change: 1 addition & 0 deletions src/utils/cookie.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const getCookieValue = (key: string | undefined) => {
console.log(key);
if (key === undefined) return "";

return document.cookie
Expand Down
2 changes: 1 addition & 1 deletion src/utils/hooks/useUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const useUser = () => {
}, [dispatch]);

const setUser = useCallback(
(param: Omit<User, "isAdmin" | "isLogin">) => {
(param: Omit<User2, "isAdmin" | "isLogin">) => {
dispatch(userActions.setUser(param));
},
[dispatch],
Expand Down

0 comments on commit fdcbb1e

Please sign in to comment.