|
| 1 | +import { useEffect, useState } from 'react'; |
| 2 | +import { |
| 3 | + AiOutlineHome, |
| 4 | + AiOutlineSearch, |
| 5 | + AiOutlineCamera, |
| 6 | + AiOutlineUser, |
| 7 | + AiOutlineLogin, |
| 8 | +} from 'react-icons/ai'; |
| 9 | +import { IoIosAddCircleOutline } from 'react-icons/io'; |
| 10 | +import { useRouter } from 'next/router'; |
| 11 | +import { useAppSelector } from '../../ducks/store'; |
| 12 | + |
| 13 | +interface ImenusValue { |
| 14 | + name: string; |
| 15 | + icon: JSX.Element; |
| 16 | + dis: string; |
| 17 | + link: string; |
| 18 | +} |
| 19 | + |
| 20 | +export function MainBottomNav({ param }) { |
| 21 | + const [active, setActive] = useState(0); |
| 22 | + const [menus, setMenus] = useState<ImenusValue[]>([]); |
| 23 | + const router = useRouter(); |
| 24 | + const { isLogin } = useAppSelector((state) => state.loginIdentity); |
| 25 | + useEffect(() => { |
| 26 | + let currentActive: number; |
| 27 | + if (param === 'home') { |
| 28 | + currentActive = 0; |
| 29 | + } else if (param === 'search') { |
| 30 | + currentActive = 1; |
| 31 | + } else if (param === 'auth') { |
| 32 | + currentActive = 2; |
| 33 | + } else if (param === 'post') { |
| 34 | + currentActive = 3; |
| 35 | + } else if (param === 'mypage') { |
| 36 | + currentActive = 4; |
| 37 | + } |
| 38 | + setActive(currentActive); |
| 39 | + }, [param]); |
| 40 | + useEffect(() => { |
| 41 | + let menuArr = [ |
| 42 | + { |
| 43 | + name: 'Home', |
| 44 | + icon: <AiOutlineHome />, |
| 45 | + dis: 'translate-x-0', |
| 46 | + link: '/', |
| 47 | + }, |
| 48 | + { |
| 49 | + name: 'Search', |
| 50 | + icon: <AiOutlineSearch />, |
| 51 | + dis: 'translate-x-14', |
| 52 | + link: '/habit/search', |
| 53 | + }, |
| 54 | + { |
| 55 | + name: 'Auth', |
| 56 | + icon: <AiOutlineCamera />, |
| 57 | + dis: 'translate-x-28', |
| 58 | + link: '/auth', |
| 59 | + }, |
| 60 | + { |
| 61 | + name: 'Post', |
| 62 | + icon: <IoIosAddCircleOutline />, |
| 63 | + dis: 'translate-x-42', |
| 64 | + link: '/habit/post', |
| 65 | + }, |
| 66 | + ]; |
| 67 | + if (isLogin === true) { |
| 68 | + setMenus([ |
| 69 | + ...menuArr, |
| 70 | + { |
| 71 | + name: 'MyPage', |
| 72 | + icon: <AiOutlineUser />, |
| 73 | + dis: 'translate-x-56', |
| 74 | + link: '/user/mypage', |
| 75 | + }, |
| 76 | + ]); |
| 77 | + } else { |
| 78 | + setMenus([ |
| 79 | + ...menuArr, |
| 80 | + { |
| 81 | + name: 'login', |
| 82 | + icon: <AiOutlineLogin />, |
| 83 | + dis: 'translate-x-56', |
| 84 | + link: '/user/login', |
| 85 | + }, |
| 86 | + ]); |
| 87 | + } |
| 88 | + }, [isLogin]); |
| 89 | + const onClickHandle = (link: string) => { |
| 90 | + //버튼 별 페이지 경로 작성 해야함. |
| 91 | + |
| 92 | + const linkBoolean: boolean = menus.slice(2).some((el) => { |
| 93 | + return el.link.includes(link); |
| 94 | + }); |
| 95 | + if (isLogin === false && linkBoolean === true && link !== '/') { |
| 96 | + router.push('/user/login'); |
| 97 | + } else { |
| 98 | + router.push(link); |
| 99 | + } |
| 100 | + }; |
| 101 | + return ( |
| 102 | + <div className="flex bg-mainColor h-[50px] px-6 w-full fixed bottom-0 min-w[300px] justify-center"> |
| 103 | + <ul className="flex relative items-center justify-center"> |
| 104 | + <span |
| 105 | + className={`bg-subColor duration-500 ${menus[active]?.dis} border-4 border-white h-14 w-14 absolute -top-7 -left-0 rounded-full `} |
| 106 | + ></span> |
| 107 | + {menus.map((menu, i) => { |
| 108 | + return ( |
| 109 | + <li key={i} className="w-14"> |
| 110 | + <div |
| 111 | + className="flex flex-col text-center pt-6" |
| 112 | + onClick={() => setActive(i)} |
| 113 | + > |
| 114 | + <span |
| 115 | + className={`flex justify-center text-xl text-iconColor cursor-pointer duration-500 z-50 ${ |
| 116 | + i === active && '-mt-11 text-iconColor ' |
| 117 | + }`} |
| 118 | + onClick={() => onClickHandle(menu.link)} |
| 119 | + > |
| 120 | + {menu.icon} |
| 121 | + </span> |
| 122 | + <span |
| 123 | + className={` ${ |
| 124 | + active === i |
| 125 | + ? 'translate-y-4 duration-700 opacity-100 text-sm text-iconColor text-base' |
| 126 | + : 'opacity-0 translate-y-10 text-iconColor text-sm text-base' |
| 127 | + }`} |
| 128 | + > |
| 129 | + {menu.name} |
| 130 | + </span> |
| 131 | + </div> |
| 132 | + </li> |
| 133 | + ); |
| 134 | + })} |
| 135 | + </ul> |
| 136 | + </div> |
| 137 | + ); |
| 138 | +} |
0 commit comments