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

로그인한 사용자만 별(즐겨찾기) 클릭 가능하도록 구현 #137

Merged
merged 7 commits into from
Mar 25, 2022
Merged
8 changes: 7 additions & 1 deletion src/components/BestRestaurantItem/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,22 @@ import { IconButton } from '@/components';
import { Star } from '@/components/IconButton';
import theme from '@/styles/theme';
import { BestRestaurantType } from '@/components/BestRestaurants';
import { useAppSelector } from '@/store/hooks';

interface BestRestaurantItemType {
restaurant: BestRestaurantType;
}

const BestRestaurantItem = ({ restaurant }: BestRestaurantItemType) => {
const { isUserLogin } = useAppSelector(({ user }) => user);
const [starState, setStarState] = useState(false);

const changeStar = (e: MouseEvent) => {
setStarState(!starState);
if (isUserLogin) {
setStarState(!starState);
} else {
alert('로그인한 사용자만 사용할 수 있는 기능입니다.');
}
};

return (
Expand Down
5 changes: 1 addition & 4 deletions src/components/Header/Header.styled.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export const StyledHeader = styled.header<headerprops>`

a {
margin: 0 25px;
text-decoration-line: none;

img {
width: 100px;
Expand Down Expand Up @@ -145,10 +146,6 @@ export const HeaderInput = styled.div<headerInputprops>(({ isMain }) =>
},
);

export const headerLink = css`
text-decoration-line: none;
`;

export const searchIcon = css`
width: 25px;
height: 25px;
Expand Down
26 changes: 12 additions & 14 deletions src/components/Header/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import logo from '@/assets/img/logo.svg';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { faMagnifyingGlass, faUser } from '@fortawesome/free-solid-svg-icons';
import {
headerLink,
searchIcon,
blankDiv,
StyledHeader,
Expand All @@ -16,24 +15,26 @@ import {

import { auth } from '@/firebase';
import { onAuthStateChanged } from 'firebase/auth';

import { useAppDispatch, useAppSelector } from '@/store/hooks';
import { modalActions } from '@/store/modal/modal-slice';
import { userActions } from '@/store/user/user-slice';

const Header = () => {
const dispatch = useAppDispatch();
const { isOverlayModalOpen } = useAppSelector(({ modal }) => modal);
const { isSearchBackModalOpen } = useAppSelector(({ modal }) => modal);
const { isUserLogin } = useAppSelector(({ user }) => user);

const [showHeader, setShowHeader] = useState<boolean>(true);
const [isMainPage, setIsMainPage] = useState<boolean>(false);
const [isLogin, setIsLogin] = useState(false);
const [profileImage, setProfileImage] = useState<string | null>('');
const [scrollPosition, setScrollPosition] = useState(0);
const [scrollPosition, setScrollPosition] = useState<number>(0);

useEffect(() => {
window.addEventListener('scroll', updateScroll);
});

const { isSearchBackModalOpen } = useAppSelector(({ modal }) => modal);

const onClickToggleSearchBackModal = useCallback(() => {
dispatch(modalActions.handleSearchBackModal());
}, [dispatch]);
Expand All @@ -42,8 +43,6 @@ const Header = () => {
}, [dispatch]);

const { pathname } = useLocation();
// console.log(pathname);
// console.log(isLogin);

const updateScroll = () => {
setScrollPosition(window.scrollY || document.documentElement.scrollTop);
Expand All @@ -52,10 +51,9 @@ const Header = () => {
onAuthStateChanged(auth, (user) => {
if (user) {
setProfileImage(user?.providerData[0].photoURL);
setIsLogin(true);
dispatch(userActions.handleUserLogin(true));
} else {
// console.log('no user');
setIsLogin(false);
dispatch(userActions.handleUserLogin(false));
}
});

Expand Down Expand Up @@ -88,18 +86,18 @@ const Header = () => {
</HeaderInput>
<ul>
<li>
<Link to="/matjib_list" css={headerLink}>
<Link to="/matjib_list">
<span>맛집 리스트</span>
</Link>
</li>
<li>
<Link to="/" css={headerLink}>
<Link to="/">
<span>술집 리스트</span>
</Link>
</li>
</ul>
<div>
{isLogin ? (
{isUserLogin ? (
<button className="profileImgBtn" onClick={onClickToggleModal}>
<img src={profileImage!} alt="프로필이미지"></img>
</button>
Expand All @@ -120,7 +118,7 @@ const Header = () => {
{isOverlayModalOpen && (
<ProfileIcon
onClickToggleModal={onClickToggleModal}
isLogin={isLogin}
isLogin={isUserLogin}
scroll={scrollPosition}
></ProfileIcon>
)}
Expand Down
8 changes: 7 additions & 1 deletion src/components/ProfileIcon/List.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { useState, MouseEvent } from 'react';

import { Star } from '@/components/IconButton';
import theme from '@/styles/theme';
import { useAppSelector } from '@/store/hooks';

import foodImage from '@/assets/img/food.jpg';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
Expand All @@ -15,10 +16,15 @@ export interface ListProps {
}

const List = ({ name, address, category, score }: ListProps) => {
const { isUserLogin } = useAppSelector(({ user }) => user);
const [starState, setStarState] = useState(false);

const changeStar = (e: MouseEvent) => {
setStarState(!starState);
if (isUserLogin) {
setStarState(!starState);
} else {
alert('로그인한 사용자만 사용할 수 있는 기능입니다.');
}
};

return (
Expand Down
2 changes: 1 addition & 1 deletion src/firebase/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export interface PostsOther {
phone: string;
category: string;
time: string[];
break: string;
breakTime: string;
menu: [];
score: number;
description: string;
Expand Down
5 changes: 5 additions & 0 deletions src/store/auth/auth-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ export const login = createAsyncThunk(
async (loginType: SocialType, { rejectWithValue }) => {
try {
const result = await loginWithSocial(loginType);
console.log(result);
if (!result) {
return;
}

await registUser(result?.user?.uid);
return result?.user?.uid;
} catch (error: any) {
Expand Down
3 changes: 2 additions & 1 deletion src/store/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { configureStore } from '@reduxjs/toolkit';
import auth from './auth/auth-slice';
import modal from './modal/modal-slice';
import user from './user/user-slice';

const store = configureStore({
reducer: { auth, modal },
reducer: { auth, modal, user },
});

export type RootState = ReturnType<typeof store.getState>;
Expand Down
19 changes: 19 additions & 0 deletions src/store/user/user-slice.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { createSlice } from '@reduxjs/toolkit';

const initialState = {
isUserLogin: false,
};

const userSlice = createSlice({
name: 'user',
initialState,
reducers: {
handleUserLogin(state, action) {
state.isUserLogin = action.payload;
},
},
});

export const userActions = userSlice.actions;

export default userSlice.reducer;