How can I refractory mapping in react - reactjs

I am trying to refractory my codes in react, I have to duplicate code to make it works.
Here is my following code:
<div className="team-invitees-tab-panes">
{!loading &&
currentInvitee.invited.accepted &&
tabs[activeTab] === "Pending" &&
invitees.map((user, index) => (
<div
role="button"
className={
selected === index
? "member-list-item-container selected"
: index === selected - 1
? "member-list-item-container border-none"
: "member-list-item-container"
}
tabIndex={index}
key={`member-${index}`}
onClick={() => handleSelect(index)}
onKeyPress={() => {
return;
}}
>
<div className="member-list-item">
<div
className="avatar"
style={{ backgroundImage: `url(${user.picture})` }}
/>
<div className="name">{user.fullname}</div>
<div className="status">
<svg
width="12"
height="16"
viewBox="0 0 12 16"
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M11.1836 4.90431C11.338 4.79611 11.4632 4.65656 11.5496 4.49648C11.636 4.33641 11.6812 4.16012 11.6817 3.98132V1.40406C11.6813 1.09731"
fill="#102A42"
/>
</svg>
</div>
</div>
</div>
))}
{!loading &&
currentInvitee.invited.declined &&
tabs[activeTab] === "Declined" &&
invitees.map((user, index) => (
<div
role="button"
className={
selected === index
? "member-list-item-container selected"
: index === selected - 1
? "member-list-item-container border-none"
: "member-list-item-container"
}
tabIndex={index}
key={`member-${index}`}
onClick={() => handleSelect(index)}
onKeyPress={() => {
return;
}}
>
<div className="member-list-item">
<div
className="avatar"
style={{ backgroundImage: `url(${user.picture})` }}
/>
<div className="name">{user.fullname}</div>
<div className="status">
<svg
width="12"
height="16"
viewBox="0 0 12 16"
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M11.1836 4.90431C11.338 4.79611 11.4632 4.65656 11.5496 4.49648C11.636 4.33641 11.6812 4.16012 11.6817 3.98132V1.40406C11.6813 1.09731"
fill="#102A42"
/>
</svg>
</div>
</div>
</div>
))}
</div>
Here is my photo of how my code looks like in web page
Is there anything that help me not to duplicate code for pending, accepted and declined?
Thank you so much for helping me, I really appreaciate it

I think you should make sepatare component Invitation which would render element of invites
const Invite = ({ user, selected, index, handleSelect }) => {
return (
<div
role="button"
className={
selected === index
? "member-list-item-container selected"
: index === selected - 1
? "member-list-item-container border-none"
: "member-list-item-container"
}
tabIndex={index}
key={`member-${index}`}
onClick={() => handleSelect(index)}
onKeyPress={() => {
return;
}}
>
<div className="member-list-item">
<div
className="avatar"
style={{ backgroundImage: `url(${user.picture})` }}
/>
<div className="name">{user.fullname}</div>
<div className="status">
<svg
width="12"
height="16"
viewBox="0 0 12 16"
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M11.1836 4.90431C11.338 4.79611 11.4632 4.65656 11.5496 4.49648C11.636 4.33641 11.6812 4.16012 11.6817 3.98132V1.40406C11.6813 1.09731"
fill="#102A42"
/>
</svg>
</div>
</div>
</div>
)
}
and use it like
<div className="team-invitees-tab-panes">
{!loading &&
currentInvitee.invited.accepted &&
tabs[activeTab] === 'Pending' &&
invitees.map((user, index) => (
<Invite
index={index}
user={user}
handleSelect={handleSelect}
selected={selected}
/>
))}
{!loading &&
currentInvitee.invited.declined &&
tabs[activeTab] === 'Declined' &&
invitees.map((user, index) => (
<Invite
index={index}
user={user}
handleSelect={handleSelect}
selected={selected}
/>
))}
</div>
also it looks like you are using invitees array for each tab, is it going to change to seprate arrays in the future?

Related

is there a way to display each individual comment data without it being set to the recently clicked comment?

im currently building a website similar to reddit but im having troubles with the comments, i can toggle the comment on and off on click but whenever more than one comment is clicked it just displays the comment data of the recent comment that was clicked.
this is my code:
const handleComments = (subText, id) => {
setCommentIds(prev => (prev.includes(id) ? prev.filter(cid => cid !== id) : [...prev, id]));
dispatch(getComments({ subText, id }));
};
const popularRendering = () => {
if (popular.isLoading) {
return Array(5)
.fill()
.map((_, index) => <FeedSkeleton key={index} />);
} else if (popular.data && popular.data.data) {
return popular.data.data.children.map((data, index) => (
<div className="box-container" key={index}>
<div className="data-container">
<div className="votes-container">
<TiArrowUpOutline
size={27}
className={`${upvoted[data.data.id] ? "up-voted" : "up-vote"}`}
onClick={() => handleUpVote(data.data.id)}
/>
<p className={upvoted[data.data.id] ? "up-voted" : downvoted[data.data.id] ? "down-voted" : ""}>{formatNumber(data.data.score)}</p>
<TiArrowDownOutline
size={27}
className={`${downvoted[data.data.id] ? "down-voted" : "down-vote"}`}
onClick={() => handleDownVote(data.data.id)}
/>
</div>
<div className="feed-header">
{subredditIconUrl[data.data.subreddit_name_prefixed] ? (
<img className="feed-icon-img" src={subredditIconUrl[data.data.subreddit_name_prefixed]} alt="subreddit-icon" />
) : null}
<p>{data.data.subreddit_name_prefixed}</p>
<span>Posted by u/{data.data.author} </span>
<span>{formatDate(data.data.created_utc)}</span>
</div>
<div className="feed-body">
<p>{data.data.title}</p>
{isImage(data.data.url) ? (
<img src={data.data.url} alt="subreddit" />
) : data.data.is_video ? (
<video height="auto" width="100%" controls>
<source src={data.data.media.reddit_video.fallback_url} />
<p className="error-media">Sorry, this content cannot be displayed.</p>
</video>
) : null}
</div>
<div className="footer">
<div className="comments" onClick={() => handleComments(data.data.subreddit_name_prefixed, data.data.id)}>
<GoComment size={25} className="comment-icon" />
<p>{formatNumber(data.data.num_comments)} Comments</p>
</div>
</div>
{subreddit.commentsLoading
? Array(5)
.fill()
.map((_, index) => <CommentsSkeleton key={index} />)
: commentIds.includes(data.data.id) && <Comments postId={data.data.id} />}
</div>
</div>
));
}
};
i tried using an array to store the ids and only display the ids of those comments but it just always resulted in issues and an infinite loop of headaches and errors.

Radio Button clicked value get the previous clicked value React

I have MUI radio buttons which I use to get filter criteria and I filter an object. Then i render the filtered object in an owl carousal to show. But when I click on a radio button it always takes the previously clicked value of the radio button. I have used ternary conditions to check but it keeps happening I couldb't figure out following is my code.
<FormControl>
<RadioGroup
row
aria-labelledby="demo-row-radio-buttons-group-label"
name="row-radio-buttons-group"
value={movieType}
onChange={movieTypeChange}
>
<FormControlLabel
value="1"
control={<Radio color="error" />}
label="All"
/>
<FormControlLabel
value="2"
control={<Radio color="error" />}
label="Now Showing"
/>
<FormControlLabel
value="3"
control={<Radio color="error" />}
label="Upcoming"
/>
</RadioGroup>
</FormControl>
{filterdMovie.length > 0 ? (
<OwlCarousel
className="owl-theme"
loop={movieCount}
center={movieCount}
margin={1}
autoplay={true}
dots={false}
items={3}
touchDrag={true}
lazyLoad={true}
responsive={state.responsive}
// responsive={"0:{items:1,},600:{items:3,},1000:{items:5,}"}
animateOut={"fadeOut"}
>
{filterdMovie.map((movieBannertop, idx) => {
return (
<div key={idx}>
<div className="item">
<div className="prs_upcom_movie_box_wrapper">
<div className="prs_upcom_movie_img_box">
<img
src={
movieBannertop.thumbnail
// HTMLImageElement.complete
// ?
// `${process.env.REACT_APP_DEV_BASE_URL_IMAGE}/movie/${movieBannertop.id}/thumbnail.jpg`
// : placeholder
}
alt="movie_img"
/>
<div className="prs_upcom_movie_img_overlay"></div>
<div className="prs_upcom_movie_img_btn_wrapper">
<ul>
<li>
{movieBannertop.bookingOpen ? (
<a
href
onClick={() => viewMovie(movieBannertop)}
style={{ textDecoration: "none" }}
>
More Info
</a>
) : null}
</li>
<li>
{!movieBannertop.upcoming ? (
<a
href
onClick={() => viewMovie(movieBannertop)}
style={{ textDecoration: "none" }}
>
Now Showing
</a>
) : (
<a
href
style={{
textDecoration: "none",
backgroundColor: "#2488ed",
}}
onClick={() => viewMovie(movieBannertop)}
>
Coming Soon
</a>
)}
</li>
</ul>
</div>
</div>
<div className="prs_upcom_movie_content_box">
<div className="prs_upcom_movie_content_box_inner">
<Tooltip title={movieBannertop.name}>
<h2>
<a
href
onClick={() => viewMovie(movieBannertop)}
style={{ textDecoration: "none" }}
>
{" "}
{movieBannertop.name.length > 10
? `${movieBannertop.name.substring(0, 10)}...`
: movieBannertop.name}
</a>
</h2>
</Tooltip>
<p>{movieBannertop.genre}</p>
</div>
<div className="prs_upcom_movie_content_box_inner_icon">
{/* <ul>
<li>
<a href="movie_booking.html">
<i className="flaticon-cart-of-ecommerce"></i>
</a>
</li>
</ul> */}
</div>
</div>
</div>
</div>
</div>
);
})}
</OwlCarousel>
) : (
<>
<Stack
direction="column"
justifyContent="center"
alignItems="center"
>
<img src={notFound} alt="" width="50%" />
<h4>No Movies Found</h4>
</Stack>
</>
)}
const movieTypeChange = (e) => {
e.preventDefault();
setMovieType(e.target.value);
const value = e.target.value;
if (value === "1") {
setFileredMovies(onlineMovies);
}
if (value === "2") {
let shows = [];
onlineMovies.forEach((element) => {
if (!element.upcoming) {
shows.push(element);
}
});
setFileredMovies(shows);
console.log(shows);
}
if (value === "3") {
console.log("3");
let shows = [];
onlineMovies.forEach((element) => {
if (element.upcoming) {
shows.push(element);
}
});
setFileredMovies(shows);
}
};
const movieTypeChange = (e) => {
var shows = [];
const value = e.target.value;
if (value === "1") {
setFileredMovies(onlineMovies);
}
if (value === "2") {
onlineMovies.forEach((element) => {
if (!element.upcoming) {
shows.push(element);
}
});
setFileredMovies(shows);
}
if (value === "3") {
onlineMovies.forEach((element) => {
if (element.upcoming) {
shows.push(element);
}
});
setFileredMovies(shows);
}
setMovieType(value);
};

how to render a button for just the first element in the array in react

i want to render the download button just for the first element in the array and for the rest the button will be disabled
allFileVersions.map((eachVersion) => {
return (
<div className="d-flex">
<div className="">versions:</div>
<div className="">{eachVersion.FileName}</div>
{/* <div>DOwnload</div> */}
<div
className={
"saveblue mg-r-10 " +
(allFileVersions[0]
? ""
: "disabled")
}>
<i className="fas fa-download pd-r-5" /> Download</div>
</div>
);
})
allFileVersions.map((eachVersion, index) => {
return (
<div className="d-flex">
<div className="">versions:</div>
<div className="">{eachVersion.FileName}</div>
{/* <div>DOwnload</div> */}
<div className={"saveblue mg-r-10 " + (index === 0 ? "" : "disabled")}>
<i className="fas fa-download pd-r-5" /> Download</div>
</div>
);
})
allFileVersions.map((eachVersion,index) => {
return (
<div className="d-flex">
<div className="">versions:</div>
<div className="">{eachVersion.FileName}</div>
{index === 0 && (
<div>DOwnload</div>
)}
<div
className={
"saveblue mg-r-10 " +
(allFileVersions[0]
? ""
: "disabled")
}>
<i className="fas fa-download pd-r-5" /> Download</div>
</div>
);
})
The map function gives you two values. The current item of the iteration and the current index. You can use the index to check if it's the first item.
allFileVersions.map((eachVersion, index) => {
return (
versions:
{eachVersion.FileName}
{/* DOwnload */}
<div
className={
saveblue mg-r-10 ${index === 0 ? "" : "disabled"}
}>
Download
);
})
Use the index for your logic instead.

How to hide logo if there is no logo?

I am using react-bootstrap Thumbnails to show a bunch of logo.
If there is no logo then I am setting a default logo.
But now I want the default logo also should not show.
So,if I remove the default logo, then I am getting the breakup image.
Here is the code -->
function ExpoCard({
data
}) {
const {logo} = data;
let defaultLogo = 'defaultLogo.png';
const URL = `${CLOUDINARYURL}/${isDisplayPage ? displayImgConfig : imgConfig}/${IMAGE_URL}${logo || defaultLogo}`;
<Thumbnail
src={URL}
alt={name}
onClick={() => setData && setData(data)}
className={cx('expo-card-thumbnail', setData && !isDisplayPage && 'cursor exhibitor-logo-portal-page')}
>
{!hideBoothName && (
<div
className={cx(
boothSize === 'LARGE' || boothSize === 'MEDIUM' ? 'expo-title-large' : 'expo-title-small',
'ex-title-name',
)}
style={{ color: nameColor || '#000000' }}
>
<LinkWithTooltip isText tooltip={name} placement="top">
{name}
</LinkWithTooltip>
</div>
)}
{categoryName && <div className="ex-tag-line">{categoryName}</div>}
<div>
{shortDescription && (
<>
<LinkWithTooltip isText tooltip={shortDescription} placement="top">
<div className="expo-card-desc-container m-t-5">
<div
className={cx(
boothSize === 'LARGE'
? 'expo-card-desc-large'
: boothSize === 'MEDIUM'
? 'expo-card-desc-medium'
: 'expo-card-desc-small',
'expo-card-desc',
)}
style={{ color: shortDescriptionColor || '#000000' }}
>
{shortDescription}
</div>
</div>
</LinkWithTooltip>
</>
)}
</div>
<div className="card-footer">
{isShowAvailability && !isDisplayPage && !disableChat && !isSearching && status && (
<span className={cx('ex-status-label', status)} style={{ position: 'absolute' }}>
{status}
</span>
)}
{!isDisplayPage
? data.sponsorUrl !== '' && (
<a
className="viewExpoBtn"
role="button"
tabIndex="0"
aria-label={name}
onKeyPress={(e) => e.key === 'Enter' && setData && setData(data)}
>
<i className="ac-icon-arrow-right pull-right btn" />
</a>
)
: (data.linkToSite || data.sponsorUrl) && (
<a
className="viewExpoBtn"
role="button"
tabIndex="0"
aria-label={name}
onKeyPress={(e) => e.key === 'Enter' && setData && setData(data)}
>
<i className="ac-icon-arrow-right pull-right btn" />
</a>
)}
</div>
</Thumbnail>
}
I was thinking of creating a class display-none and when there is no logo, Thumbnail should add display-none class but if I do so,everything gets hidden including name.
There are a few options:
Instead of hiding the Thumbnail itself just hide the IMG element inside with CSS child selectors.
You can do something like:
.hide-thumbnail >img{
display:none;
}
Add this hide-thumbnail class to Thumbnail when you don't want a picture to show up.
Set a 0 size picture as the default logo:
You can create an empty picture in paint on Windows and use that as the default picture
Move everything out of Thumbnail into a different Component and show Thumbnail along the new component only when you need it.
Option 1 and 2 are not great but can work as a quick solution.

index.js:1 Warning: Received `true` for a non-boolean attribute `active`

Does someone can say me what caused this error and how to fix it?
I already tried to change active keyword but still got same warning.
Not sure if it's problem with Styled components or with just attributes.
Down is the error and code.
Actual error
26 row - div under return,
27 - , ...
Codeblock
return (
<div>
<div className="App">
<div className="portfolio__labels">
<a
href="/#"
active={filter === "all"}
onClick={() => setFilter("all")}
>
All
</a>
<a
href="/#"
active={filter === "frontend"}
onClick={() => setFilter("frontend")}
>
Frontend
</a>
<a
href="/#"
active={filter === "mobile"}
onClick={() => setFilter("mobile")}
>
Mobile
</a>
<a
href="/#"
active={filter === "ux-ui"}
onClick={() => setFilter("ux-ui")}
>
UX/UI
</a>
<a
href="/#"
active={filter === "others"}
onClick={() => setFilter("others")}
>
Others
</a>
</div>
<div className="portfolio__container">
{projects.map((item, key) =>
item.filtered === true ? (
<ProfileCard
key={key}
name={item.name}
title={item.title}
image={item.image}
className="border-box"
exerpt={item.exerpt}
git={item.git}
url={item.url}
category={item.category}
click="Push"
sans-serif
mb0-l
mb3
flex-none
w5
mr3
/>
) : (
""
)
)}
</div>
</div>
</div>

Resources