Currently I am writing a news-website as mern stack. I am using redux toolkit. In my mongoDb, I have a value "clicked", which is default 0, and I want to increase it, when the article is clicked, for finding out the ten most clicked articles. I have build a handleClick function, which gets the value "clicked" and I have a updateInlineNews in my slice. Now I want to send the increased value to mongoDb, but updating news I only allow to users, who are admins. Has anyone an idea, how I could use "clicked" independent? Thanks foy your help.
My code:
const InLineNews = () => {
const {inlineNews} = useSelector((state)=>state.inlineNews);
const dispatch = useDispatch();
useEffect(()=>{
dispatch(getAllInlineNews());
}, [dispatch]);
const handleClick = (section)=>{
section++;
const inlineNewsData = {
clicked: section,
}
dispatch(updateInlineNews(inlineNewsData));
}
return <Container>
<Article>
<ArticleTitle>InlineNews</ArticleTitle>
{
inlineNews.map((item)=>(
<Link to={{pathname:`/${item.ressort}`}} className="link" key={item._id}>
<ArticleWrapper key={item._id} onClick={()=>handleClick(item.clicked)}>
<ImgHolder>
<Image src={item.img} alt={item.title} title={item.title}/>
</ImgHolder>
<ContentHolder>
<Ressort>{item.ressort}</Ressort>
<Theme>{item.theme}</Theme>
<ContentTitle>{item.title}</ContentTitle>
<Content>{item.content}</Content>
</ContentHolder>
</ArticleWrapper>
</Link>
))
}
</Article>