-
Notifications
You must be signed in to change notification settings - Fork 10
/
MyPostWidget.jsx
173 lines (162 loc) · 4.92 KB
/
MyPostWidget.jsx
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import {
EditOutlined,
DeleteOutlined,
AttachFileOutlined,
GifBoxOutlined,
ImageOutlined,
MicOutlined,
MoreHorizOutlined,
} from "@mui/icons-material";
import {
Box,
Divider,
Typography,
InputBase,
useTheme,
Button,
IconButton,
useMediaQuery,
} from "@mui/material";
import FlexBetween from "components/FlexBetween";
import Dropzone from "react-dropzone";
import UserImage from "components/UserImage";
import WidgetWrapper from "components/WidgetWrapper";
import { useState } from "react";
import { useDispatch, useSelector } from "react-redux";
import { setPosts } from "state";
const MyPostWidget = ({ picturePath }) => {
const dispatch = useDispatch();
const [isImage, setIsImage] = useState(false);
const [image, setImage] = useState(null);
const [post, setPost] = useState("");
const { palette } = useTheme();
const { _id } = useSelector((state) => state.user);
const token = useSelector((state) => state.token);
const isNonMobileScreens = useMediaQuery("(min-width: 1000px)");
const mediumMain = palette.neutral.mediumMain;
const medium = palette.neutral.medium;
const handlePost = async () => {
const formData = new FormData();
formData.append("userId", _id);
formData.append("description", post);
if (image) {
formData.append("picture", image);
formData.append("picturePath", image.name);
}
const response = await fetch(`http://localhost:3001/posts`, {
method: "POST",
headers: { Authorization: `Bearer ${token}` },
body: formData,
});
const posts = await response.json();
dispatch(setPosts({ posts }));
setImage(null);
setPost("");
};
return (
<WidgetWrapper>
<FlexBetween gap="1.5rem">
<UserImage image={picturePath} />
<InputBase
placeholder="What's on your mind..."
onChange={(e) => setPost(e.target.value)}
value={post}
sx={{
width: "100%",
backgroundColor: palette.neutral.light,
borderRadius: "2rem",
padding: "1rem 2rem",
}}
/>
</FlexBetween>
{isImage && (
<Box
border={`1px solid ${medium}`}
borderRadius="5px"
mt="1rem"
p="1rem"
>
<Dropzone
acceptedFiles=".jpg,.jpeg,.png"
multiple={false}
onDrop={(acceptedFiles) => setImage(acceptedFiles[0])}
>
{({ getRootProps, getInputProps }) => (
<FlexBetween>
<Box
{...getRootProps()}
border={`2px dashed ${palette.primary.main}`}
p="1rem"
width="100%"
sx={{ "&:hover": { cursor: "pointer" } }}
>
<input {...getInputProps()} />
{!image ? (
<p>Add Image Here</p>
) : (
<FlexBetween>
<Typography>{image.name}</Typography>
<EditOutlined />
</FlexBetween>
)}
</Box>
{image && (
<IconButton
onClick={() => setImage(null)}
sx={{ width: "15%" }}
>
<DeleteOutlined />
</IconButton>
)}
</FlexBetween>
)}
</Dropzone>
</Box>
)}
<Divider sx={{ margin: "1.25rem 0" }} />
<FlexBetween>
<FlexBetween gap="0.25rem" onClick={() => setIsImage(!isImage)}>
<ImageOutlined sx={{ color: mediumMain }} />
<Typography
color={mediumMain}
sx={{ "&:hover": { cursor: "pointer", color: medium } }}
>
Image
</Typography>
</FlexBetween>
{isNonMobileScreens ? (
<>
<FlexBetween gap="0.25rem">
<GifBoxOutlined sx={{ color: mediumMain }} />
<Typography color={mediumMain}>Clip</Typography>
</FlexBetween>
<FlexBetween gap="0.25rem">
<AttachFileOutlined sx={{ color: mediumMain }} />
<Typography color={mediumMain}>Attachment</Typography>
</FlexBetween>
<FlexBetween gap="0.25rem">
<MicOutlined sx={{ color: mediumMain }} />
<Typography color={mediumMain}>Audio</Typography>
</FlexBetween>
</>
) : (
<FlexBetween gap="0.25rem">
<MoreHorizOutlined sx={{ color: mediumMain }} />
</FlexBetween>
)}
<Button
disabled={!post}
onClick={handlePost}
sx={{
color: palette.background.alt,
backgroundColor: palette.primary.main,
borderRadius: "3rem",
}}
>
POST
</Button>
</FlexBetween>
</WidgetWrapper>
);
};
export default MyPostWidget;