diff --git a/src/components/Home.jsx b/src/components/Home.jsx index c2a27a7..0f9c16e 100644 --- a/src/components/Home.jsx +++ b/src/components/Home.jsx @@ -13,6 +13,7 @@ export default function Home() { const [articles, setArticles] = useState([]) const [loading, setLoading] = useState(false) const [articleByTag, setArticleByTag] = useState('') + const [message, setMessage] = useState('') const [articleByFollowing, setArticleByFollowing] = useState(false) const [meta, setMeta] = useState({ to: 0, @@ -24,18 +25,27 @@ export default function Home() { useEffect(() => { const fetchArticles = async () => { + setMessage('') setLoading(true) try { if (articleByTag) { const response = await axios.get(`${BASE_URL}/tag/${articleByTag}/articles`) - setArticles(response.data.data) - setMeta(response.data.meta) + if (response.data.data.length) { + setArticles(response.data.data) + setMeta(response.data.meta) + }else { + setMessage('No articles found.') + } setLoading(false) } else if (articleByFollowing) { const response = await axios.get(`${BASE_URL}/followings/articles`, getConfig(token)) - setArticles(response.data.data) - setMeta(response.data.meta) + if (response.data.data.length) { + setArticles(response.data.data) + setMeta(response.data.meta) + }else { + setMessage('No articles found.') + } setLoading(false) } else { const response = await axios.get(`${BASE_URL}/articles`) @@ -90,10 +100,19 @@ export default function Home() { setArticleByTag={setArticleByTag} /> } {/* display all the published articles */} - +
+ No articles found +
+ + : + + } {/* display all the tags */} { - articles?.length ? articles?.map(article => ( + articles?.map(article => ( )) - : -
- No article found. -
} ) diff --git a/src/components/search/SearchBox.jsx b/src/components/search/SearchBox.jsx index 8cce540..41a8ff5 100644 --- a/src/components/search/SearchBox.jsx +++ b/src/components/search/SearchBox.jsx @@ -1,13 +1,14 @@ import axios from 'axios' import React, { useState } from 'react' import { BASE_URL } from '../../helpers/config' -import { Link } from 'react-router-dom' +import { Link, useNavigate } from 'react-router-dom' export default function SearchBox() { const [searchTerm, setSearTerm] = useState('') const [message, setMessage] = useState('') const [articles, setArticles] = useState([]) const [loading, setLoading] = useState(false) + const navigate = useNavigate() const searchArticles = async (e) => { e.preventDefault() @@ -34,6 +35,11 @@ export default function SearchBox() { } } + const viewArticleDetails = (slug) => { + navigate(`/articles/${slug}`) + setArticles([]) + } + return (
@@ -84,10 +90,10 @@ export default function SearchBox() { { article.title } - +
)) diff --git a/src/components/user/articles/Bookmarked.jsx b/src/components/user/articles/Bookmarked.jsx index a426da6..ac11ddc 100644 --- a/src/components/user/articles/Bookmarked.jsx +++ b/src/components/user/articles/Bookmarked.jsx @@ -1,12 +1,14 @@ import React, { useEffect } from 'react' -import { useSelector } from 'react-redux' +import { useDispatch, useSelector } from 'react-redux' import { Link, useNavigate } from 'react-router-dom' import useTitle from '../../custom/useTitle' +import { bookmark } from '../../../redux/slices/bookmarkSlice' export default function Bookmarked() { const { isLoggedIn } = useSelector(state => state.user) const { bookmarked } = useSelector(state => state.bookmark) const navigate = useNavigate() + const dispatch = useDispatch() //change page title useTitle('Saved Articles') @@ -45,6 +47,10 @@ export default function Bookmarked() { to={`/articles/${article.slug}`}> + )) diff --git a/src/components/user/articles/UserArticles.jsx b/src/components/user/articles/UserArticles.jsx index e88ea3b..6511421 100644 --- a/src/components/user/articles/UserArticles.jsx +++ b/src/components/user/articles/UserArticles.jsx @@ -1,6 +1,6 @@ import React, { useEffect, useState } from 'react' import { useDispatch, useSelector } from 'react-redux' -import { Link, useNavigate } from 'react-router-dom' +import { Link } from 'react-router-dom' import { setCurrentUser } from '../../../redux/slices/userSlice' import { toast } from 'react-toastify' import axios from 'axios' @@ -10,20 +10,25 @@ import Spinner from '../../layouts/Spinner' export default function UserArticles() { const { token } = useSelector(state => state.user) const dispatch = useDispatch() - const navigate = useNavigate() const [articles, setArticles] = useState([]) const [loading, setLoading] = useState(false) + const [message, setMessage] = useState('') useEffect(() => { fetchUserArticles() }, []) const fetchUserArticles = async () => { + setMessage('') setLoading(true) try { const response = await axios.get(`${BASE_URL}/user/articles`, getConfig(token)) - setArticles(response.data.data) + if (response.data.data.length) { + setArticles(response.data.data) + }else { + setMessage('No articles found.') + } setLoading(false) } catch (error) { setLoading(false) @@ -55,7 +60,11 @@ export default function UserArticles() {
: - articles?.length ? + message ? +
+ { message } +
+ : @@ -111,10 +120,6 @@ export default function UserArticles() { }
List of published articles
- : -
- No articles found. -
} )