리엑트 블로그 만들기 10 - 회원가입 2

리엑트 블로그 만들기 10 - 회원가입 2

import React, { Fragment, useCallback, useEffect, useState } from "react";
import { useDispatch, useSelector } from "react-redux";
import { Link } from "react-router-dom";
import {
Button,
Collapse,
Container,
Form,
Nav,
Navbar,
NavbarToggler,
NavItem,
} from "reactstrap";
import { LOGOUT_REQUEST } from "../redux/types";
import LoginModal from "./auth/LoginModal";
import RegisterModal from "./auth/RegisterModal";

const AppNavbar = () => {
const [isOpen, setIsOpen] = useState(false);
const { isAuthenticated, user, userRole } = useSelector(
(state) => state.auth
);
console.log(userRole, "UserRole");

const dispatch = useDispatch();

const onLogout = useCallback(() => {
dispatch({
type: LOGOUT_REQUEST,
});
}, [dispatch]);

useEffect(() => {
setIsOpen(false);
}, [user]);

const handleToggle = () => {
setIsOpen(!isOpen);
};

const addPostClick = () => {};

const authLink = (
<Fragment>
<NavItem>
{userRole === "MainJuin" ? (
<Form className="col mt-2">
<Link
to="posts"
className="btn btn-success block text-white px-3"
onClick={addPostClick}
>
AddPost
</Link>
</Form>
) : (
""
)}
</NavItem>
<NavItem className="d-flex justify-content-center">
<Form className="col mt-2">
{user && user.name ? (
<Link>
<Button outline color="light" className="px-3" block>
<strong>{user ? `Welcom ${user.name}` : ""}</strong>
</Button>
</Link>
) : (
<Button outline color="light" className="px-3" block>
<strong>No User</strong>
</Button>
)}
</Form>
</NavItem>
<NavItem>
<Form className="col">
<Link onClick={onLogout} to="#">
<Button outline color="light" className="mt-2" block>
Logout
</Button>
</Link>
</Form>
</NavItem>
</Fragment>
);

const guestLink = (
<Fragment>
<NavItem>
<RegisterModal />
</NavItem>
<NavItem>
<LoginModal />
</NavItem>
</Fragment>
);

return (
<div>
<Navbar color="dark" dark expand="lg" className="sticky-top">
<Container>
<Link to="/" className="text-white text-decoration-none">
Victor의 블로그
</Link>
<NavbarToggler onClick={handleToggle} />
<Collapse isOpen={isOpen} navbar>
<Nav className="ml-auto d-flex justify-content-around" navbar>
{isAuthenticated ? authLink : guestLink}
</Nav>
</Collapse>
</Container>
</Navbar>
</div>
);
};

export default AppNavbar;
Share