Skip to content

Commit da5486d

Browse files
committed
feature: articles loading and routing
1 parent 45e8d0e commit da5486d

File tree

6 files changed

+70
-7
lines changed

6 files changed

+70
-7
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import useAPI from "../../hooks/useAPI.js";
2+
import useOrderedStyles from "../../hooks/useOrderedStyles.js";
3+
import {useContext, useEffect, useState} from "react";
4+
import AuthContext from "../../contexts/AuthContext.js";
5+
import {useNavigate, useParams} from "react-router";
6+
import ArticleTile from "./ArticleTile.jsx";
7+
8+
export default function Article(props) {
9+
const {date, feast, saint, holiday, author, image, content, title} = props
10+
const {id} = useParams()
11+
const {loadArticles} = useAPI();
12+
const {addStyle} = useOrderedStyles()
13+
const {is_authenticated} = useContext(AuthContext)
14+
const [article, setArticle] = useState({});
15+
const navigate = useNavigate();
16+
17+
useEffect(() => {
18+
addStyle("/articles/article.css", "articles")
19+
}, []);
20+
21+
useEffect(() => {
22+
loadArticles(date, feast, saint, holiday, author, id).then(response =>{
23+
setArticle(
24+
response?.data[0]
25+
)
26+
}
27+
)
28+
},[date, feast, saint, holiday, author]);
29+
30+
return (
31+
<>
32+
<section className="single-article">
33+
<article>
34+
<figure>
35+
{!!article.image && (<img src={article.image} alt={article.title}/>)}
36+
{!article.image && (<img src={'/src/assets/images/articles/saint.webp'} alt="няма изображение"/>)}
37+
</figure>
38+
<main>
39+
<h2>{article.title}</h2>
40+
<p>{article.content}</p>
41+
<nav>
42+
<ul>
43+
{/*{% if article.is_own %}*/}
44+
{/*<li><a href="{% url "article-delete" article.pk %}">*/}
45+
{/* */}
46+
{/*</a></li>*/}
47+
{/*{% endif %}*/}
48+
{/*{% if article.can_change or article.is_own %}*/}
49+
{/*<li><a href="{% url "article-edit" article.pk %}">*/}
50+
{/* */}
51+
{/*</a></li>*/}
52+
{/*{% endif %}*/}
53+
</ul>
54+
</nav>
55+
</main>
56+
</article>
57+
</section>
58+
</>
59+
)
60+
}

src/components/articles/ArticleTile.jsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
import routes from "../../routes/routes.js";
12

23
export default function ArticleTile(props) {
34

4-
const {date, feast, saint, holiday, author, image, content, title} = props
5+
const {id, date, feast, saint, holiday, author, image, content, title, navigate} = props
56

67
return (
78
<>
8-
<article onClick="window.location.href='{{ article.article_url }}';">
9+
<article onClick={() => navigate(routes["article-detail"].replace(":id", id))}>
910
<figure>
1011
{!!image && (<img src={image} alt={title}/>)}
1112
{!image && (<img src={'/src/assets/images/articles/saint.webp'} alt="няма изображение"/>)}

src/components/articles/Articles.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export default function Articles(props) {
2525
useEffect(() => {
2626
loadArticles(date, feast, saint, holiday, author).then(response =>{
2727
setArticles(response.data.map(article => {
28-
return <ArticleTile key={article.id} {...article}/>
28+
return <ArticleTile key={article.id} {...article} navigate={navigate}/>
2929
}))
3030
}
3131
)

src/hooks/useAPI.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ function useAPI(){
3030
}
3131
}
3232

33-
async function loadArticles(date, feast, saint, holiday, author){
33+
async function loadArticles(date, feast, saint, holiday, author, id){
3434
try{
35-
return await api.get(`${apiAddress}api/articles/?date=${date||""}&feast=${feast||""}&saint=${saint||""}&holiday=${holiday||""}&author=${author||""}`)
35+
return await api.get(`${apiAddress}api/articles/?id=${id||""}&date=${date||""}&feast=${feast||""}&saint=${saint||""}&holiday=${holiday||""}&author=${author||""}`)
3636
} catch(err){
3737
console.log(err)
3838
throw new Error("Failed to fetch articles")

src/routes/config.jsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ import Calendar from "../components/calendar/Calendar.jsx";
44
import Login from "../components/accounts/Login.jsx";
55
import Logout from "../components/accounts/Logout.jsx";
66
import Articles from "../components/articles/Articles.jsx";
7+
import Article from "../components/articles/Article.jsx";
78

89
export const routeConfig = [
910
{ path: routes.home, element: <Base/>, nested: [
1011
{path: routes["home"], element: <Articles/>, nested:[], auth_required: false},
11-
{path: routes["articles-page"], element: <Articles/>, nested:[], auth_required: false},
12+
{path: routes["all-articles"], element: <Articles/>, nested:[], auth_required: false},
13+
{path: routes["article-detail"], element: <Article/>, nested:[], auth_required: false},
1214
{path: routes["login-page"], element: <Login/>, nested:[], auth_required: false},
1315
{path: routes["logout-page"], element: <Logout/>, nested:[], auth_required: true},
1416
{path: routes["calendar-page"], element: <Calendar/>, nested:[], auth_required: false},

src/routes/routes.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const routes = {
44
"calendar-page": "/calendar",
55
"login-page": "/login",
66
"logout-page": "/logout",
7-
"articles-page": "/articles",
7+
"all-articles": "/articles",
88
"article-detail": "/articles/:id",
99
"article-create": "/articles/create",
1010
}

0 commit comments

Comments
 (0)