1+ import useAPI from "../../hooks/useAPI.js" ;
2+ import { useContext , useEffect , useState } from "react" ;
3+ import useOrderedStyles from "../../hooks/useOrderedStyles.js" ;
4+ import ArticleTile from "./ArticleTile.jsx" ;
5+ import useAuth from "../../hooks/useAuth.js" ;
6+ import AuthContext from "../../contexts/AuthContext.js" ;
7+ import routes from "../../routes/routes.js" ;
8+ import { useNavigate } from "react-router" ;
9+
10+ export default function Articles ( props ) {
11+ const {
12+ date, feast, saint, holiday, author
13+ } = props ;
14+
15+ const { loadArticles} = useAPI ( ) ;
16+ const { addStyle} = useOrderedStyles ( )
17+ const { is_authenticated} = useContext ( AuthContext )
18+ const [ articles , setArticles ] = useState ( [ ] ) ;
19+ const navigate = useNavigate ( ) ;
20+
21+ useEffect ( ( ) => {
22+ addStyle ( "/articles/articles.css" , "articles" )
23+ } , [ ] ) ;
24+
25+ useEffect ( ( ) => {
26+ loadArticles ( date , feast , saint , holiday , author ) . then ( response => {
27+ setArticles ( response . data . map ( article => {
28+ return < ArticleTile key = { article . id } { ...article } />
29+ } ) )
30+ }
31+ )
32+ } , [ date , feast , saint , holiday , author ] ) ;
33+
34+ const final = [ ]
35+ let add ;
36+
37+ if ( is_authenticated ) {
38+ add = (
39+ < >
40+ < article className = "new-article-tile" onClick = { ( ) => navigate ( routes [ "article-create" ] ) } >
41+ < figure >
42+ < img src = { '/src/assets/images/articles/saint.webp' } alt = { "няма намерени картички" } />
43+ </ figure >
44+ < main >
45+ < h2 style = { { textTransform : "capitalize" } } > добави картичка</ h2 >
46+ < p className = "new-article-sign" > + </ p >
47+ </ main >
48+ </ article >
49+ </ >
50+ )
51+ }
52+
53+ final . push ( articles ? (
54+ < >
55+ < p style = { { "textTransform" : "capitalize" } } > Преглед на картички от статиите</ p >
56+ < section className = "article-list" >
57+ { articles }
58+ { add }
59+ </ section >
60+ </ >
61+ ) : (
62+ < >
63+ < article >
64+ < figure >
65+ < img src = { '/src/assets/images/articles/saint.webp' } alt = { "няма намерени картички" } />
66+ </ figure >
67+ < main >
68+ < h2 style = { { textTransform : "capitalize" } } > Няма картички за този ден</ h2 >
69+ < p > Все още няма добавени картички за този ден</ p >
70+ </ main >
71+ </ article >
72+ </ >
73+ ) )
74+
75+ return final
76+ }
0 commit comments