// src/pages/ArticleDetail.jsx
import React, { useState, useEffect } from "react";
import axios from "axios";
import { useParams, Link, useLocation } from "react-router-dom";
import {
Container,
Typography,
Box,
Button,
Divider,
Chip,
TextField,
IconButton,
List,
ListItem,
ListItemText,
ListItemSecondaryAction,
ListItemAvatar,
Avatar,
} from "@mui/material";
import DeleteIcon from '@mui/icons-material/Delete';
import NewsletterSubscription from "../components/NewsletterSubscription";
const ArticleDetail = () => {
const { id } = useParams();
const { search } = useLocation();
const authorId = new URLSearchParams(search).get("authorId");
const [article, setArticle] = useState(null);
const [newComment, setNewComment] = useState("");
const [comments, setComments] = useState([]);
useEffect(() => {
const fetchArticle = async () => {
try {
const response = await axios.get(
`http://localhost:5000/api/articles/${id}`,
{
withCredentials: true,
}
);
console.log("ssss", response.data.author.name);
setArticle(response.data);
setComments(response.data.comments || []);
} catch (error) {
console.error("Error fetching article:", error);
}
};
fetchArticle();
}, [id]);
const handleAddComment = async () => {
try {
await axios.post(
`http://localhost:5000/api/articles/${id}/comments`,
{ content: newComment, author: authorId },
{ withCredentials: true }
);
// Re-fetch the article to get the updated comments with author info
const updatedArticle = await axios.get(
`http://localhost:5000/api/articles/${id}`,
{
withCredentials: true,
}
);
console.log(updatedArticle.data);
setComments(updatedArticle.data.comments || []);
setNewComment("");
} catch (error) {
console.error("Error adding comment:", error);
}
};
const handleDeleteComment = async (commentId) => {
try {
await axios.delete(
`http://localhost:5000/api/articles/${id}/comments/${commentId}`,
{ withCredentials: true }
);
setComments((prevComments) =>
prevComments.filter((comment) => comment._id !== commentId)
);
} catch (error) {
console.error("Error deleting comment:", error);
}
};
if (!article) return <Typography>Loading...</Typography>;
return (
<Container maxWidth="md">
<Typography variant="h3" gutterBottom>
{article.title}
</Typography>
<Typography variant="subtitle1" color="textSecondary" gutterBottom>
By {article.author.name} on{" "}
{new Date(article.createdAt).toLocaleDateString()}
</Typography>
<Divider sx={{ my: 2 }} />
<Typography variant="body1" paragraph>
{article.content}
</Typography>
<Box sx={{ mt: 2 }}>
<Typography variant="h6" gutterBottom>
Categories
</Typography>
<Box sx={{ mb: 2 }}>
{article.categories.map((category, index) => (
<Chip key={index} label={category} sx={{ mr: 1, mb: 1 }} />
))}
</Box>
<Typography variant="h6" gutterBottom>
Tags
</Typography>
<Box>
{article.tags.map((tag, index) => (
<Chip key={index} label={tag} sx={{ mr: 1, mb: 1 }} />
))}
</Box>
</Box>
<Box sx={{ mt: 2 }}>
<Typography variant="h6" gutterBottom>
Comments
</Typography>
<List>
{comments.map((comment) => (
<ListItem key={comment._id}>
<ListItemAvatar>
<Avatar>
{article.author.name[0]}
</Avatar>
</ListItemAvatar>
<ListItemText
primary={comment.content}
secondary={`By ${article.author.name}
on ${new Date(comment.date).toLocaleDateString()}`}
/>
<ListItemSecondaryAction>
<IconButton
edge="end"
aria-label="delete"
onClick={() => handleDeleteComment(comment._id)}
>
<DeleteIcon />
</IconButton>
</ListItemSecondaryAction>
</ListItem>
))}
</List>
<Box sx={{ display: "flex", mt: 2 }}>
<TextField
fullWidth
label="Add a comment"
variant="outlined"
value={newComment}
onChange={(e) => setNewComment(e.target.value)}
/>
<Button
variant="contained"
color="primary"
sx={{ ml: 2 }}
onClick={handleAddComment}
>
Post
</Button>
</Box>
</Box>
<Box sx={{ mt: 2 }}>
<Button
variant="contained"
color="primary"
component={Link}
to={`/update-article/${article._id}`}
>
Edit Article
</Button>
</Box>
<NewsletterSubscription/>
</Container>
);
};
export default ArticleDetail;