-
Notifications
You must be signed in to change notification settings - Fork 273
Expand file tree
/
Copy pathBlogDetail.js
More file actions
115 lines (109 loc) · 3.12 KB
/
BlogDetail.js
File metadata and controls
115 lines (109 loc) · 3.12 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
import { Button, InputLabel, TextField, Typography } from "@mui/material";
import { Box } from "@mui/system";
import axios from "axios";
import React, { useEffect, useState } 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 [blog, setBlog] = useState();
const id = useParams().id;
console.log(id);
const [inputs, setInputs] = useState({});
const handleChange = (e) => {
setInputs((prevState) => ({
...prevState,
[e.target.name]: e.target.value,
}));
};
const fetchDetails = async () => {
const res = await axios
.get(`${config.BASE_URL}/api/blogs/${id}`)
.catch((err) => console.log(err));
const data = await res.data;
return data;
};
useEffect(() => {
fetchDetails().then((data) => {
setBlog(data.blog);
setInputs({
title: data.blog.title,
description: data.blog.description,
});
});
});
const sendRequest = async () => {
const res = await axios
.put(`${config.BASE_URL}/api/blogs/update/${id}`, {
title: inputs.title,
description: inputs.description,
})
.catch((err) => console.log(err));
const data = await res.data;
return data;
};
console.log(blog);
const handleSubmit = (e) => {
e.preventDefault();
console.log(inputs);
sendRequest()
.then((data) => console.log(data))
.then(() => navigate("/myBlogs/"));
};
return (
<div>
{inputs && (
<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"}
>
Post Your Blog
</Typography>
<InputLabel sx={labelStyles}>Title</InputLabel>
<TextField
name="title"
onChange={handleChange}
value={inputs.title}
margin="auto"
variant="outlined"
/>
<InputLabel sx={labelStyles}>Description</InputLabel>
<TextField
name="description"
onChange={handleChange}
value={inputs.description}
margin="auto"
variant="outlined"
/>
<Button
sx={{ mt: 2, borderRadius: 4 }}
variant="contained"
color="warning"
type="submit"
>
Submit
</Button>
</Box>
</form>
)}
</div>
);
};
export default BlogDetail;