I have this navbar where the user can navigate through the site and logged in users can make posts on the main page or news posts on the news page. However, i have 3 levels of permissions made in the backend where the visitors are allowed to navigate the website, then authenticated users can make normal posts or news posts but the last one is admin only permission to make a program post. the question is how can i show the third button (which is for program post) not show for authenticated users and only pop up when an admin logs in
these buttons can be found in the code bellow under authLinks
import { Link, NavLink } from 'react-router-dom';
import { connect } from 'react-redux';
import { logout } from '../actions/auth';
import Alert from './Alert';
import PropTypes from 'prop-types';
import {Navbar, NavDropdown} from "react-bootstrap";
import Logo from '../assets/images/logo.png';
const navbar = ({ auth:{isAuthenticated, loading}, logout}) => {
const authLinks = (
<Fragment>
<NavLink className='btn btn-primary btn-sm m-2' to='/create-post'>Nytt innlegg</NavLink>
<NavLink className='btn btn-primary btn-sm m-2' to='/create-nyhet'>Nytt nyhetsinnlegg</NavLink>
<NavLink className='btn btn-primary btn-sm m-2' to='/create-program'>Nytt arrangement</NavLink>
<a className='btn btn-primary btn-sm m-2' onClick={logout} href='#!'>Logg ut</a>
</Fragment>
);
const guestLinks = (
<NavLink className='btn btn-primary btn-sm m-2' to='/login'>Logg inn</NavLink>
);
return(
<Fragment>
<Navbar bg="dark" variant="dark" expand="md" sticky="top">
<div className="container-fluid">
<Navbar.Brand href='/'><img className="img-responsive" src={Logo} width="220" height="55" alt="Sammfunnet Logo" /></Navbar.Brand>
<Navbar.Toggle aria-controls="basic-navbar-nav" />
<Navbar.Collapse id="basic-navbar-nav">
<ul className="navbar-nav p-2">
<li className="nav-item active">
<NavLink className="nav-link" exact to='/'>Hjem</NavLink>
</li>
<li className="nav-item">
<NavLink className="nav-link" exact to='/nyhet'>Nyhet</NavLink>
</li>
<li className="nav-item">
<NavLink className="nav-link" exact to='/program'>Program</NavLink>
</li>
<NavDropdown title="Kontakt Oss" id="basic-nav-dropdown">
<NavDropdown.Item href="/bli-frivillig">Bli Frivillig</NavDropdown.Item>
<NavDropdown.Item href="/presse">Presse</NavDropdown.Item>
<NavDropdown.Item href="#action/3.3">Samarbeid</NavDropdown.Item>
</NavDropdown>
<li className="nav-item">
<NavLink className="nav-link" exact to='/omoss'>Om oss</NavLink>
</li>
</ul>
</Navbar.Collapse>
<span className="nav_link">
{ !loading && (<Fragment>{ isAuthenticated ? authLinks : guestLinks }</Fragment>) }
</span>
</div>
</Navbar>
<Alert />
</Fragment>
);
};
navbar.propTypes = {
logout: PropTypes.func.isRequired,
auth: PropTypes.object.isRequired
}
const mapStateToProps = state => ({
auth: state.auth
});
export default connect(mapStateToProps, { logout })(navbar);
Do this then should work
const authLinks = (
<Fragment>
<NavLink className='btn btn-primary btn-sm m-2' to='/create-post'>Nytt innlegg</NavLink>
<NavLink className='btn btn-primary btn-sm m-2' to='/create-nyhet'>Nytt nyhetsinnlegg</NavLink>
{ isAuthenticated && (<NavLink className='btn btn-primary btn-sm m-2' to='/create-program'>Nytt arrangement</NavLink> ) }
<a className='btn btn-primary btn-sm m-2' onClick={logout} href='#!'>Logg ut</a>
</Fragment>
);
Related
i am trying to make a nav bar when a navlink is clicked i want the activelink to change background-color
i have used NavLink but it is not woeking for some reasome pls help
export class side extends Component {
render() {
return (
<div className="l-navbar " id="nav-bar">
<nav className="nav">
<NavLink
activeClassName="active3"
className="porpssidebar"
exact={true}
to="/"
>
<div>
<a href="#" className="nav_logo">
<img className="slogo" src={forur} />
<i className="fas fa-angry"></i>{" "}
</a>
</div>
</NavLink>
<NavLink activeClassName="active3" exact={true} to="/orders">
<div>
{" "}
<a href="#" className="nav_logo">
<Orders />
<i className="fas fa-angry"></i>{" "}
</a>
</div>
</NavLink>
<NavLink to="/customers" activeClassName="active3">
<div>
{" "}
<a href="#" className="nav_logo">
<Package />
<i className="fas fa-angry"></i>{" "}
</a>
</div>
</NavLink>
</div>
);
}
}
export default side;
in app.css i have a class called active3 for changing color
.active3 {
background: black;
}
Good evening I am learning modals but I ran into a problem, the openModal() function works just fine but I cant get it to close...
Code:
import React, { Component } from 'react';
import './Navbar.css';
import 'bootstrap/dist/css/bootstrap.min.css';
import { Container, Navbar, NavbarBrand, Nav, InputGroup, FormControl, Modal, Button, ButtonGroup } from 'react-bootstrap';
import { library } from '#fortawesome/fontawesome-svg-core';
import { fab } from '#fortawesome/free-brands-svg-icons';
import Foot from './Footer'
import Today from '../Main/Home'
import Account from '../AccountPage/Account'
import Pool from '../PoolPage/Pool'
import Stats from '../StatsPage/Stats'
import BondingCard from '../BondPage/BondCard'
import AdCard from '../AdPage/AdHome'
library.add(fab)
class Navy extends Component {
constructor(props) {
super(props);
this.state = {
content: <Today />,
showlogin: false
}
this.homepage = this.homepage.bind(this);
this.accountpage = this.accountpage.bind(this);
this.statspage = this.statspage.bind(this);
this.poolpage = this.poolpage.bind(this);
this.loanpage = this.loanpage.bind(this);
this.adpage = this.adpage.bind(this);
this.openModal = this.openModal.bind(this);
this.closeModal = this.closeModal.bind(this);
}
homepage() {
this.setState({content: <Today/>});
}
accountpage() {
this.setState({content: <Account/>})
}
statspage() {
this.setState({content: <Stats/>})
}
poolpage() {
this.setState({content: <Pool/>})
}
loanpage() {
this.setState({content: <BondingCard/>})
}
adpage() {
this.setState({content: <AdCard/>})
}
openModal() {
this.setState({ showlogin: true });
}
closeModal() {
this.setState({ showlogin: false });
}
render() {
return (
<div>
<Navbar bg='light' variant='light' >
<div className='logo'>
<Container >
<NavbarBrand className='brandcolor'>
<h1>ETHER<span>SHARE<i className="fab fa-ethereum"></i></span></h1>
</NavbarBrand>
</Container>
</div>
<div className='wbtn' onClick={this.openModal}>
<h3><i class="fas fa-exclamation-triangle"></i>Connect|Login<i class='fas fa-exchange-alt'></i></h3>
<Modal show={this.state.showlogin} onHide={this.closeModal} size='lg' centered>
<Modal.Header closeButton>
<Modal.Title className='cent'>
<ButtonGroup size="sm" className="mb-2">
<Button variant='dark'>Login</Button>
<Button variant='dark'>Register</Button>
</ButtonGroup>
</Modal.Title>
</Modal.Header>
<Modal.Body>Woohoo, you're reading this text in a modal!</Modal.Body>
<Modal.Footer>
<Button variant="secondary" onClick={this.closeModal}>
Close
</Button>
<Button variant="primary" onClick={this.closeModal}>
Save Changes
</Button>
</Modal.Footer>
</Modal>
</div>
<Container>
<InputGroup className="mb-3">
<InputGroup.Text id="basic-addon1"><i class="fas fa-user-plus" aria-hidden="true"></i></InputGroup.Text>
<FormControl
placeholder="#username (search)"
aria-label="Username"
aria-describedby="basic-addon1"
/>
</InputGroup>
</Container>
<Container >
<Nav >
<ul className='menuItems'>
<div className='mainmenu'>
<Nav.Link className='linkitem'>
<li onClick={this.homepage}>
<i class="fa fa-home" aria-hidden="true"></i>
Home
<div className='arrow'>
<i class="fa fa-angle-double-right" ></i>
</div>
</li>
</Nav.Link>
<Nav.Link className='linkitem'>
<li onClick={this.accountpage}>
<i class="fa fa-cog" aria-hidden="true"></i>
Account
<div className='arrow'>
<i class="fa fa-angle-right"></i>
</div>
</li>
</Nav.Link>
<Nav.Link className='linkitem'>
<li onClick={this.poolpage}>
<i class="fa fa-rocket" aria-hidden="true"></i>
Pool
<div className='arrow'>
<i class="fa fa-angle-right"></i>
</div>
</li>
</Nav.Link>
<Nav.Link className='linkitem'>
<li onClick={this.loanpage}>
<i class="fa fa-university" aria-hidden="true"></i>
Loan|Borrow
<div className='arrow'>
<i class="fa fa-angle-right"></i>
</div>
</li>
</Nav.Link>
<Nav.Link className='linkitem'>
<li onClick={this.adpage}>
<i class="fa fa-plus" aria-hidden="true"></i>
Ads(earn crypto)
<div className='arrow'>
<i class="fa fa-angle-right"></i>
</div>
</li>
</Nav.Link>
<Nav.Link className='linkitem'>
<li onClick={this.statspage}>
<i class="fa fa-signal" aria-hidden="true"></i>
Stats
<div className='arrow'>
<i class="fa fa-angle-right"></i>
</div>
</li>
</Nav.Link>
</div>
</ul>
</Nav>
</Container>
<div className='footy'>
< Foot />
</div>
</Navbar>
{this.state.content}
</div>
)
}
};
export default Navy;
Does anyone know what I did wrong? Error in my 2 functions? This is the entire file, everything works but closeModal()...
dddddddddddddddddddddddddddddddddddddddd
dddddddddddddddddddddddd
ddddddddddd
You need to use setTimeOut, please try my experiment
Here
closeModal() {
let vm = this;
setTimeout(() => {
vm.setState({ showlogin: false });
}, 100);
}
I have three different Navbars. Each of them is a different component.
<PublicNavbar /> ,which is for public pages such as LandingPage, DiscoverPage etc.
<AccessNavbar /> ,which is for SignInPage, SignUppage, VerificationPage etc.
<PrivateNavbar /> , which is for private pages such as NewCampaignPage etc.
How can I show them properly? If a user is logged in, I want to replace DiscoverPage's PublicNavbar with PrivateNavbar.
import React from "react";
import { BrowserRouter as Router, Route, Link, Switch } from "react-router-dom";
import "semantic-ui-css/semantic.min.css";
import "./App.css";
import PublicNavbar from "./components/Navbar/PublicNavbar";
import AccessNavbar from "./components/Navbar/AccessNavbar";
import PrivateNavbar from "./components/Navbar/PrivateNavbar";
import Footer from "./components/Footer";
import LandingPage from "./pages/LandingPage";
import SignIn from "./pages/SIgnInPage";
import DiscoverPage from "./pages/DiscoverPage";
import SignUp from "./pages/SignUpPage";
import Verification from "./pages/VerificationPage";
import Registration from "./pages/RegistrationPage";
import RegistrationComplete from "./pages/RegistrationCompletePage";
import NewCampaign from "./pages/NewCampaignPage";
function App() {
return (
<Router>
<div>
<div id="container">
<div id="main">
<Switch>
<Route path="/" exact component={LandingPage} />
<Route path="/discover" component={DiscoverPage} />
<Route path="/signIn" component={SignIn} />
<Route path="/signUp" component={SignUp} />
<Route path="/verification" component={Verification} />
<Route path="/registration" component={Registration} />
<Route
path="/registration-complete"
component={RegistrationComplete}
/>
<Route path="/new-campaign" component={NewCampaign} />
</Switch>
</div>
</div>
<Footer />
</div>
</Router>
);
}
export default App;
Sample Navbar code:
import React from "react";
const PublicNavbar = () => {
return (
<nav className="navbar navbar-expand-lg bg-white">
<div className="container-fluid navbar-container">
<a className="navbar-brand abs nav-bar-title" href="#">
AshoDaanKori
</a>
<button
className="navbar-toggler ms-auto custom-toggler"
type="button"
data-bs-toggle="collapse"
data-bs-target="#collapseNavbar"
>
<span className="navbar-toggler-icon"></span>
</button>
<div className="navbar-collapse collapse" id="collapseNavbar">
<ul className="navbar-nav ms-auto">
<li className="nav-item active">
<a
className="nav-link"
href=""
data-bs-target="#myModal"
data-bs-toggle="modal"
>
About
</a>
</li>
<li className="nav-item active">
<a
className="nav-link"
href=""
data-bs-target="#myModal"
data-bs-toggle="modal"
>
How it works
</a>
</li>
<li className="nav-item active">
<a
className="nav-link"
href=""
data-bs-target="#myModal"
data-bs-toggle="modal"
>
Discover
</a>
</li>
</ul>
<a
className="nav-link navbar-btn"
href=""
data-bs-target="#myModal"
data-bs-toggle="modal"
>
Start Campaign
</a>
</div>
</div>
</nav>
);
};
export default PublicNavbar;
Create a new Navbar component named something like NavBarController.
While calling the navbar controller component pass in the "Type" as prop. Type should be a state and should change depending on the user status.
<NavBarController type={1}></NavBarController>
Let NavBarController handle whichever navbar you want to display out of your three navbars
(where type will be 1,2 or 3).
Your NavBarController will return something like this:
return (props.type===1?<PublicNavbar/>
:props.type===2?<AccessNavbar/>
:props.type===3?<PrivateNavbar/>)
Solved it by conditional rendering. Without creating multiple Navbar components, I created one and changed the inner elements based on condition.
const Navbar = ({ isAuthenticated }) => {
return (
<nav
className={`autohide navbar navbar-expand-lg bg-white ${
isAuthenticated ? "private-navbar" : ""
}`}
>
<div className="container-fluid navbar-container">
<NavHashLink className="navbar-brand abs nav-bar-title" to="/">
AshoDaanKori
</NavHashLink>
<button
className="navbar-toggler ms-auto custom-toggler"
type="button"
data-bs-toggle="collapse"
data-bs-target="#main_nav"
aria-expanded="false"
aria-label="Toggle navigation"
>
<span className="navbar-toggler-icon"></span>
</button>
{isAuthenticated ? (
<div className="collapse navbar-collapse" id="main_nav">
<ul className="navbar-nav ms-auto">
<li className="nav-item active">
<NavHashLink to="/my-fundraisers" className="nav-link">
My fundraisers
</NavHashLink>
</li>
</ul>
<Link to="/start-campaign" className="nav-link navbar-btn">
Start a new campaign
</Link>
{/* User profile */}
<div className="btn-group nav-item">
<button
type="button"
className="btn "
data-bs-toggle="dropdown"
data-bs-display="static"
aria-expanded="false"
style={{ padding: "0" }}
>
<i aria-hidden="true" className="user circle huge icon"></i>
</button>
<ul className="dropdown-menu dropdown-menu-end">
<li>
<h3
style={{
textAlign: "center",
fontSize: 18,
fontWeight: 600,
color: "#6E6E6E",
}}
>
{userInfo.fullName || "User"}
</h3>
</li>
<li>
<hr className="dropdown-divider" />{" "}
</li>
<li>
<Link className="dropdown-item" to="/account">
Account Settings
</Link>
</li>
<li>
<Link
className="dropdown-item"
to="#"
>
Sign Out
</Link>
</li>
</ul>
</div>
</div> //If authenticated, rendered PrivateNavbar elements
) : (
<div className="collapse navbar-collapse" id="main_nav">
<ul className="navbar-nav ms-auto">
<li className="nav-item active">
<NavHashLink
smooth
to="/#our-story-section"
className="nav-link"
>
About
</NavHashLink>
</li>
<li className="nav-item active">
<NavHashLink to="/how-it-works" className="nav-link">
How it works
</NavHashLink>
</li>
<li className="nav-item active">
<NavHashLink to="/discover" className="nav-link">
Discover
</NavHashLink>
</li>
</ul>
<NavHashLink to="/sign-in" className="nav-link navbar-btn">
START CAMPAIGN
</NavHashLink>
</div> //If not authenticated, rendered PublicNavbar elements
)}
</div>
</nav>
);
};
export default Navbar;
This is my code for the queue please only answer if you personally know me and are fimiliar with the project.
import React, { Fragment } from 'react';
import { Link } from 'react-router-dom'
import InputQ from './InputQ';
import ListQ from './ListQ';
import './Admin.css';
const AdminQueue = () => {
return (
<Fragment>
<InputQ />
<Link to="/">
<i className="fas fa-home float-right mt-3 mb-3" aria-hidden="true" />
</Link>
<Link to="/signin">
<i className="fa fa-lock float-right mt-3 mb-3 mr-3" aria-hidden="true" />
</Link>
<ListQ />
</Fragment>
)
}
export default AdminQueue;
All you have to do is import this make sure download with npm:
import SGF from 'react-sgf-dom';
And Wrap what you want to SGF with the tags:
const AdminQueue = () => {
return (
<SGF>
<InputQ />
<Link to="/">
<i className="fas fa-home float-right mt-3 mb-3" aria-hidden="true" />
</Link>
<Link to="/signin">
<i className="fa fa-lock float-right mt-3 mb-3 mr-3" aria-hidden="true" />
</Link>
<ListQ />
</SGF>
)
}
I have a bootstrap navigation menu and I am trying to simulate a click of a nav item on a nav dropdown in React with Jest. I have the following dropdown:
Navbar.js
export default props => {
const { user, email, group } = props;
return (
<Nav className="navbar navbar-expand navbar-light bg-white topbar mb-4 static-top shadow">
<button
type="button"
id="sidebarToggleTop"
className="btn btn-link d-md-none rounded-circle mr-3"
>
<FontAwesomeIcon icon={faBars} />
</button>
<ul className="navbar-nav ml-auto">
<div className="topbar-divider d-none d-sm-block" />
<NavDropdown
data-testid="profile-dropdown"
className="no-arrow"
title={
<>
<span className="mr-2 d-none d-lg-inline text-gray-600 small">
{user}
</span>
{email && (
<Gravatar
email={email}
className="img-profile rounded-circle"
/>
)}
</>
}
id="userDropdown"
>
<div className="dropdown-item">Profile: {group}</div>
<Link className="dropdown-item" to="/refresh">
Refresh
</Link>
<NavDropdown.Divider />
<NavDropdown.Item data-testid="sign-out-button" eventKey="logout" onSelect={() => Auth.signOut()}>
SignOut
</NavDropdown.Item>
</NavDropdown>
</ul>
</Nav>
);
};
Can anyone help? The item is not present in the DOM on render so I cannot use data-testid.