How to make bootstrap cards be in the same line? Reactjs - reactjs

I've been using Bootstrap 4 for displaying data to the user.
Now I've had idea to display data in columns and rows. Frontend is ReactJS and it's fetching data from backend response. Right now I've managed to display data in the cards, but they are all over the place.
This is how my dashboard looks:
So as can you see, it's all over the place, where I need to each one be in the same line. (they are not sticking with each other)
Is there a way to fix this with only Bootstrap or is it needed to create some kind of my own css?
Here is Dashboard.js component:
import React, { useState, useEffect } from 'react';
import ArticleService from '../services/article.service';
import { Link } from 'react-router-dom';
import Pagination from 'react-js-pagination';
const Dashboard = () => {
const [content, setContent] = useState([]);
const [currentPage, setCurrentPage] = useState(1);
const [postsPerPage] = useState(10);
useEffect(() => {
const fetchPosts = async () => {
const res = await ArticleService.articles();
setContent(res.data);
};
fetchPosts();
}, []);
// Get current posts
const indexOfLastPost = currentPage * postsPerPage;
const indexOfFirstPost = indexOfLastPost - postsPerPage;
const currentPosts = content.slice(indexOfFirstPost, indexOfLastPost);
// Change page
const handlePageChange = (pageNumber) => {
setCurrentPage(pageNumber);
};
return (
<div className='container'>
<div className='row'>
<div className='col-sm'>
<h4>Opis artikla</h4>
{currentPosts &&
currentPosts.map((item) => (
<div key={item.id} className='card'>
<h3>{item.descr}</h3>
<h5>Broj kvadrata: {item.sqm}</h5>
</div>
))}
</div>
<div classname='col-sm'>
<h4>Cijena</h4>
{currentPosts &&
currentPosts.map((item) => (
<div key={item.id} className='card'>
<h3>{item.price}</h3>
<h5>Cijena po kvadratu: {item.ppm2}/m2</h5>
</div>
))}
</div>
<div classname='col-sm'>
<h4>Prikaži ponudu</h4>
{currentPosts &&
currentPosts.map((item) => (
<div key={item.id} className='card'>
<Link to={`/article/${item.id}`}>
<h5>
Prikaži<br></br>
<br></br>
<br></br>
</h5>
</Link>
</div>
))}
</div>
</div>
<nav>
<div className='w3-bar w3-xlarge'>
<ul className='pagination justify-content-center'>
<li className='page-item'>
<Pagination
hideDisabled
hideNavigation
hideFirstLastPages
currentPage={currentPage}
itemsCountPerPage={10}
totalItemsCount={content.length}
pageRangeDisplayed={indexOfLastPost}
onChange={handlePageChange}
/>
</li>
</ul>
</div>
</nav>
</div>
);
};
export default Dashboard;

I think you can try using the bootstrap class - align-items-baseline on I guess the row div.
Check this answer from another post to know more about this align-items-baseline property.
If this does not work then I think you can try this:
<div className='container'>
<!-- First a row div with only the 3 column headings -->
<div className='row'>
<div className='col-4'>
<h4>Opis artikla</h4>
</div>
<div classname='col-4'>
<h4>Cijena</h4>
</div>
<div classname='col-4'>
<h4>Prikaži ponudu</h4>
</div>
</div>
<!-- Then the data of the 3 columns -->
{currentPosts && currentPosts.map((item) => (
<!-- A new row for each items data -->
<div className='row'>
<!-- For the first column -->
<div className='col-4'>
<div key={item.id} className='card'>
<h3>{item.descr}</h3>
<h5>Broj kvadrata: {item.sqm}</h5>
</div>
</div>
<!-- For the second column -->
<div className='col-4'>
<div key={item.id} className='card'>
<h3>{item.price}</h3>
<h5>Cijena po kvadratu: {item.ppm2}/m2</h5>
</div>
</div>
<!-- For the third column -->
<div className='col-4'>
<div key={item.id} className='card'>
<Link to={`/article/${item.id}`}>
<h5>
Prikaži<br></br>
<br></br>
<br></br>
</h5>
</Link>
</div>
</div>
</div>
))}
<nav>
<div className='w3-bar w3-xlarge'>
<ul className='pagination justify-content-center'>
<li className='page-item'>
<Pagination
hideDisabled
hideNavigation
hideFirstLastPages
currentPage={currentPage}
itemsCountPerPage={10}
totalItemsCount={content.length}
pageRangeDisplayed={indexOfLastPost}
onChange={handlePageChange}
/>
</li>
</ul>
</div>
</nav>
</div>
Update - I tried the above code and as you said, yes it didn't work.
But then I did the below code and it's working as you want it to or what I think you want it to work like:
import React from 'react';
import 'bootstrap/dist/css/bootstrap.min.css';
var currentPosts = [
{ sqm: 'One', ppm2: 'a', id: '1' },
{ sqm: 'Two', ppm2: 'b', id: '2' },
{ sqm: 'Three', ppm2: 'c', id: '3' }
];
const Dashboard = () => {
return (
<div className='container'>
<div className='row'>
<div className='col-4'>
<h4>Opis artikla</h4>
</div>
<div className='col-4'>
<h4>Cijena</h4>
</div>
<div className='col-4'>
<h4>Prikaži ponudu</h4>
</div>
</div>
{currentPosts && currentPosts.map((item) => (
<div className='row mt-4 align-items-baseline'>
<div className='col-4'>
<div key={item.id} className='card'>
<h3>{item.descr}</h3>
<h5>Broj kvadrata: {item.sqm}</h5>
</div>
</div>
<div className='col-4'>
<div key={item.id} className='card'>
<h3>{item.price}</h3>
<h5>Cijena po kvadratu: {item.ppm2}/m2</h5>
</div>
</div>
<div className='col-4'>
<div key={item.id} className='card'>
{/* <Link to={`/article/${item.id}`}> */}
<h5>
Prikaži<br></br>
<br></br>
<br></br>
</h5>
{/* </Link> */}
</div>
</div>
</div>
))}
</div>
);
};
export default Dashboard;
You can add the pagination and other code lines to this...
Actually the only change I made was to add the mt-4 and align-items-baseline classes to the row div that has the data.
The result I got from this code was:

Related

how to get product id from sanity.io for every button that clicked in reactjs?

i'm trying to make product detail page but i am struggle to solve this problem. I fetch products from sanity.io and return the data using map. i want whenever i click the detail btn it's direct me to detail page and get the information of product
here is my code
const Products = () => {
const [allProducts, setAllProducts] = useState([]);
useEffect(() => {
async function getAllProducts() {
const allProduct = '*[_type == "product"]';
const response = await client.fetch(allProduct);
setAllProducts(await response);
}
getAllProducts();
}, []);
return (
<>
<Navbar />
<div className="container-fluid ">
<div className="row g-3 mb-5 ">
{allProducts.map((product) => {
console.log(product);
return (
<div className="col-12 col-md-3" key={product._id}>
<div className="card">
<img
src={urlFor(product.image[0])}
className="card-img-top"
alt={product.name}
/>
<div className="card-body">
<h1>${product.price}</h1>
<h5 className="card-title">{product.name} </h5>
<h6>{product.detail}</h6>
<div>
<Link to="/detail" className="btn btn-danger m-2">
Detail
</Link>
</div>
</div>
</div>
</div>
);
})}
</div>
<Footer />
</div>
</>
);
};

How can I display my data dynamically onclick event in React

I'm creating a movies app with React and Redux, in each movie card I have some information about the movie like the title, image, and a button(buy a ticket). The idea is when I click on the button of each card I want to get the same image and title of the card and display it on the same page on another card that going to pop up so the user can choose the quantity and continue.
How can I get the data from the movie card onclick and transform it to another card as a pop-up?
what do you think
Single movie card Component
const SingleMovieCard = ({ id, title, poster_path, overview, toggleHandler }) => {
const [selected, isSelected] = useState(null);
return (
<article key={id} className="card">
<div key={id} onMouseEnter={() => isSelected(id)} onMouseLeave={() => isSelected(null)}>
<img src={`${ImgPath}` + poster_path} alt={title} className="image" />
{selected === id && <video src="./Trailers/SpaceJam.mp4" autoPlay={true} loop muted />}
</div>
<div className="body-card">
<h1>{title}</h1>
<p>{`${overview.substring(0, 200)}...`}</p>
</div>
<div className="services">
<FiShare2 className="icon" />
<FiHeart className="icon" />
<div className="btn-icon-container">
<BiShoppingBag className="btn-icon" />
<button onClick={() => toggleHandler()}>Buy Ticket</button>
</div>
</div>
</article>
)
}
export default SingleMovieCard;
Pop-up movie card
const PopUpMovie = ({showClass, toggleHandler}) => {
const moviesList = useSelector((state)=> state.allMovies.movies);
return (
<div className={`pop-up-container ${showClass}`}>
<nav className="pop-up">
<GrClose className="pop-up-close" onClick={()=> toggleHandler()}/>
<div className="product-details">
<div className="img-container">
<img src="./Pictures/FreeGuy.jpg" alt="FreeGuy" />
</div>
<div className="product info">
<h1 className="title">Free Guy movie</h1>
<div className="quantity">
<h4>Quantity</h4>
<span>4</span>
</div>
<h5 className="prix">11$</h5>
<button className="btn-checkout">Continue to checkout</button>
</div>
</div>
</nav>}
</div>
)
}
export default PopUpMovie;
you can use Modal from react-bootstrap
Example:
import { Modal } from "react-bootstrap";
const PopUpMovie = ({ showClass, toggleHandler }) => {
const modalContent = (
<div className={`pop-up-container ${showClass}`}>
<nav className="pop-up">
<GrClose className="pop-up-close" onClick={() => toggleHandler()} />
<div className="product-details">
<div className="img-container">
<img src="./Pictures/FreeGuy.jpg" alt="FreeGuy" />
</div>
<div className="product info">
<h1 className="title">Free Guy movie</h1>
<div className="quantity">
<h4>Quantity</h4>
<span>4</span>
</div>
<h5 className="prix">11$</h5>
<button className="btn-checkout">Continue to checkout</button>
</div>
</div>
</nav>
</div>
)
const moviesList = useSelector((state) => state.allMovies.movies);
return (
<Modal
id="order-modal-close"
backdrop="static"
show={showClass}
size={"md"}
dialogClassName="modal-90w"
onHide={toggleHandler}
>
<Modal.Header closeButton>
<Modal.Title>Movie</Modal.Title>
</Modal.Header>
<Modal.Body >{modalContent}</Modal.Body>
{modalFooter}
</Modal>
)
}

showing the applicants of a job in reactjs using id

Iam trying to make a react application whereby users are applying for a job and also creating a report that will show the profile of the users who have applied for the job. Iam welcome to any ideas that can be given to me since I would like to have a logic on how to implement that.
Here is my code below:
DriverCard.js
import React from "react";
import member2 from "../../../dist/images/user2.png";
const DriverCard = ({cardValues}) => {
return (
<>
<div className="col-lg-3 col-md-3 col-sm-4 col-6 p-0 mb-5">
<div className="text-center">
<div className="mb-2">
<img
src={cardValues ? `${cardValues.location.title}` : member2}
className="rounded-circle"
height="85px"
width="85px"
alt="members"
/>
</div>
<h6 className="mb-0 textHeading">
{cardValues ? cardValues.name : `John Doe`}
</h6>
<span className="text-muted textStyle"> #JohntheD 5h</span>
<hr
style={{
width: "50%",
marginTop: 4,
marginBottom: 1,
}}
/>
<h6 className="textStyle mt-1">
{cardValues ? `${cardValues.licenseAge} +years licence` : `NA`}
</h6>
<h6 className="textStyle">
{cardValues ? `${cardValues.experience} +years experience` : `NA`}
</h6>
</div>
</div>
</>
);
};
export default DriverCard;
This is the report file that is suppossed to show the jobcard and the profile for the applicants.
import React, { useEffect, useState } from "react";
import {useHistory} from "react-router-dom";
import map from "lodash/map"
import { getSpecificJobCard } from "../../Api/index";
import arrowBack from "../../dist/images/arrowBack.svg";
import DriverCard from "./Job_Components/DriverCard";
import JobCardDetails from "./Job_Components/JobCardDetails";
import NextPageButtons from "../Main/DashBoard_Buttons/NextPageButtons";
const JobDetails = ({jobpostId}) => {
const [jobCard, setjobCard] = useState([]);
const history = useHistory();
console.log("param val",jobpostId);
useEffect(() => {
(async () => {
let jobCardRespData = await getSpecificJobCard(jobpostId);
setjobCard(jobCardRespData);
console.log("jobcard response:", jobCardRespData);
})();
}, []);
console.log("resp list ----- ", jobCard);
return (
<div className="">
<div className="col-md-10 m-auto">
<span style={{ cursor: "pointer" }} className="">
<img src={arrowBack} alt="" className="img-fluid" />
</span>
<div className="row">
<div className="col-lg-4 mt-5">
<div className="row">
{}
<JobCardDetails job={jobCard} />
</div>
</div>
<div className="col-lg-8 mt-5">
<div className="row">
<DriverCard/>
{/* <DriverCard /> */}
</div>
</div>
</div>
<div className="row">
<div className="col-lg-12">
<div className="row">
</div>
</div>
</div>
</div>
<div className="col-lg-12 my-3 border-top">
<NextPageButtons/>
</div>
</div>
);
};
export default JobDetails;

React Hover to conditional render component

this is my code so far
class PortfolioList extends Component{
render(){
const {column , styevariation } = this.props;
const list = PortfolioListContent.slice(0 , this.props.item);
return(
<React.Fragment>
{list.map((value , index) => (
<div className={`${column}`} key={index}>
<div className={`portfolio ${styevariation}`}>
<div className="thumbnail-inner" >
<div className={`thumbnail ${value.image}`}></div>
<div className={`bg-blr-image ${value.image}`}></div>
</div>
<div className="content" >
<div className="inner">
<p>{value.category}</p>
<h4>{value.title}</h4>
<div className="btn-container">
<div className="portfolio-button">
<a className="rn-btn" href="/portfolio-details"><FaGithub /> Git </a>
</div>
<div className="portfolio-button">
<a className="rn-btn" href="/portfolio-details"><FaExternalLinkAlt /> Live </a>
</div>
</div>
</div>
</div>
</div>
</div>
))}
</React.Fragment>
)
}
}
I want to conditionally render the div "content" and the child elements of content div when mouse is hovering over "thumbnail-inner" div. But hide content when mouse is not hovering over thumbnail-inner div.
How can i achieve this?
I didn't test this, but the idea is to add a variable in the state of the component which holds the current hovered item.
When a mouseEnter event enters to your thumbnail-inner, you update that variable with the current component index. And you set it to -1 when a mouseLeave events happens in your thumbnail-inner.
Then you simply render the content conditionally by checking if the this.state.selectedIndex === index.
class PortfolioList extends Component {
state = {
selectedItem: -1,
}
render(){
const {column , styevariation } = this.props;
const list = PortfolioListContent.slice(0 , this.props.item);
return(
<React.Fragment>
{list.map((value , index) => (
<div className={`${column}`} key={index}>
<div className={`portfolio ${styevariation}`}>
<div
className="thumbnail-inner"
onMouseEnter={ () => { this.setState({selectedItem: index}) } }
onMouseLeave={ () => { this.setState({selectedItem: -1}) } }>
<div className={`thumbnail ${value.image}`}></div>
<div className={`bg-blr-image ${value.image}`}></div>
</div>
{
this.state.selectedItem === index &&
<div className="content" >
<div className="inner">
<p>{value.category}</p>
<h4>{value.title}</h4>
<div className="btn-container">
<div className="portfolio-button">
<a className="rn-btn" href="/portfolio-details"><FaGithub /> Git </a>
</div>
<div className="portfolio-button">
<a className="rn-btn" href="/portfolio-details"><FaExternalLinkAlt /> Live </a>
</div>
</div>
</div>
</div>
}
</div>
</div>
))}
</React.Fragment>
)
}
First, you need to add state for condition of hover (ex: "onHover"), the state is for conditional rendering the <div className="content">.
Second you need create function for hover and leaveHover(onMouseEnter & onMouseLeave) we call it handleMouseEnter for onMouseEnter, and we call it handleMouseLeave for onMouseLeave.
class PortfolioList extends Component {
state = {
onHover: false,
}
handleMouseEnter() {
this.setState({onHover: true})
}
handleMouseLeave() {
this.setState({onHover: false})
}
render(){
const {column , styevariation } = this.props;
const list = PortfolioListContent.slice(0 , this.props.item);
return(
<React.Fragment>
{list.map((value , index) => (
<div className={`${column}`} key={index}>
<div className={`portfolio ${styevariation}`}>
<div
className="thumbnail-inner"
onMouseEnter={this.handleMouseEnter}
onMouseLeave={this.handleMouseLeave}>
<div className={`thumbnail ${value.image}`}></div>
<div className={`bg-blr-image ${value.image}`}></div>
</div>
{
this.state.onHover &&
<div className="content" >
<div className="inner">
<p>{value.category}</p>
<h4>{value.title}</h4>
<div className="btn-container">
<div className="portfolio-button">
<a className="rn-btn" href="/portfolio-details"><FaGithub /> Git </a>
</div>
<div className="portfolio-button">
<a className="rn-btn" href="/portfolio-details"><FaExternalLinkAlt /> Live </a>
</div>
</div>
</div>
</div>
}
</div>
</div>
))}
</React.Fragment>
)
}

React Hooks - UseState - Passing object from Child to Parent

I am trying to pass data between a child and a parent component using Hooks and Functional Components.
In my parent component I have the following:-
import React, {useState} from "react"
import Card from './Card';
const CardsBoard = () => {
const { ratingObj, setRatingObj } = useState({});
console.log(ratingObj);
const cardInfo1 = {
.......
}
const cardInfo2 = {
.......
}
return (
<div className="container-fluid justify-content-center">
<div className="row">
<div className="col-md-6">
<div>
<h4>Player 1</h4>
</div>
<Card cardInfo={cardInfo1} onClick={() => setRatingObj(ratingObj)} />
</div>
<div className="col-md-6">
<h4>Player 2</h4>
<Card cardInfo={cardInfo2} onClick={() => setRatingObj(ratingObj)}/>
</div>
</div>
<div className="row top-buffer">
<div className="col-md-12 text-center">
<button id="startGameBtn" name="StartGameButton" className="btn btn-secondary">START GAME</button>
</div>
</div>
</div>
)
}
export default CardsBoard
Then in my child component I have the following:-
import React from "react"
const Card = ({ cardInfo, onClick }) => {
return (
<div className="card text-center shadow">
<h4 className="card-title">{cardInfo.title}</h4>
<div className="overflow">
<img src={cardInfo.image} alt={cardInfo.image} className="card-image" />
</div>
<div className="card-body text-dark">
<div className="container-fluid justify-content-center card-rating-text">
{
cardInfo.ratings.map(row => (
<div className="row" key={row[0].title}>
<div className="col-md-4 text-left">{row[0].title}</div>
<div className="col-md-2 text-left card-rating-color" onClick={() => onClick(cardInfo.player, row[0].title, row[0].rating)} >{row[0].rating}</div>
<div className="col-md-4 text-left">{row[1].title}</div>
<div className="col-md-2 text-left card-rating-color" onClick={() => onClick(cardInfo.player, row[1].title, row[1].rating)}>{row[1].rating}</div>
</div>
))
}
</div>
</div>
</div>
)
}
export default Card
At the moment I am getting an error:-
Uncaught TypeError: setRatingObj is not a function
at onClick (CardsBoard.js:58)
at onClick (Card.js:30)
How can I pass the onClick object from the child to the parent?
Thanks for your help and time
Change
const { ratingObj, setRatingObj } = useState({});
to
const [ ratingObj, setRatingObj ] = useState({});
useState returns a two-element array in the shape of [state, setStateFunc], so you have to apply array destructuring to it.

Resources