-
Notifications
You must be signed in to change notification settings - Fork 274
Expand file tree
/
Copy pathBlogDetail.js
More file actions
130 lines (119 loc) · 3.5 KB
/
BlogDetail.js
File metadata and controls
130 lines (119 loc) · 3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import { Button, InputLabel, TextField, Typography } from "@mui/material";
import { Box } from "@mui/system";
import axios from "axios";
import React, { useEffect, useState, useCallback } from "react";
import { useNavigate, useParams } from "react-router-dom";
import config from "../config";
const labelStyles = { mb: 1, mt: 2, fontSize: "24px", fontWeight: "bold" };
const BlogDetail = () => {
const navigate = useNavigate();
const { id } = useParams();
const [inputs, setInputs] = useState({ title: "", description: "" });
const [blog, setBlog] = useState(null);
const handleChange = (e) => {
setInputs((prevState) => ({
...prevState,
[e.target.name]: e.target.value,
}));
};
// eslint-disable-next-line react-hooks/exhaustive-deps
const fetchDetails = useCallback(async () => {
try {
const res = await axios.get(`${config.BASE_URL}/api/blogs/${id}`);
const data = res.data;
setBlog(data.blog);
console.log(blog);
setInputs({
title: data.blog.title || "",
description: data.blog.description || "",
});
} catch (err) {
console.error("Failed to fetch blog details:", err);
}
}, [id]);
useEffect(() => {
fetchDetails();
}, [fetchDetails]);
const sendRequest = async () => {
try {
const res = await axios.put(`${config.BASE_URL}/api/blogs/update/${id}`, {
title: inputs.title,
description: inputs.description,
});
return res.data;
} catch (err) {
console.error("Failed to update blog:", err);
}
};
const handleSubmit = (e) => {
e.preventDefault();
sendRequest()
.then((data) => {
console.log("Blog updated:", data);
navigate("/myBlogs/");
});
};
return (
<div>
{blog ? (
<form onSubmit={handleSubmit}>
<Box
border={3}
borderColor="linear-gradient(90deg, rgba(58,75,180,1) 2%, rgba(116,49,110,1) 36%, rgba(2,0,161,1) 73%, rgba(69,92,252,1) 100%)"
borderRadius={10}
boxShadow="10px 10px 20px #ccc"
padding={3}
margin={"auto"}
marginTop={3}
display="flex"
flexDirection={"column"}
width={"80%"}
>
<Typography
fontWeight={"bold"}
padding={3}
color="grey"
variant="h2"
textAlign={"center"}
>
Update Blog
</Typography>
<InputLabel sx={labelStyles}>Title</InputLabel>
<TextField
name="title"
onChange={handleChange}
value={inputs.title}
margin="auto"
variant="outlined"
required
/>
<InputLabel sx={labelStyles}>Description</InputLabel>
<TextField
name="description"
onChange={handleChange}
value={inputs.description}
margin="auto"
variant="outlined"
multiline
rows={4}
required
/>
<Button
sx={{ mt: 2, borderRadius: 4 }}
variant="contained"
color="warning"
type="submit"
>
Submit
</Button>
</Box>
</form>
) : (
<Typography textAlign="center" mt={5} variant="h5">
Loading blog details...
</Typography>
)}
</div>
);
};
export default BlogDetail;