-
Notifications
You must be signed in to change notification settings - Fork 0
/
Items.js
235 lines (225 loc) · 8.42 KB
/
Items.js
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import { useState, useEffect } from "react";
import {
collection,
addDoc,
getDocs,
doc,
updateDoc,
deleteDoc,
} from "firebase/firestore";
import { ref, uploadBytes, getDownloadURL } from "firebase/storage";
import { db, storage } from "./firebase";
function Items() {
const [items, setItems] = useState([]);
const [name, setName] = useState("");
const [description, setDescription] = useState("");
const [image, setImage] = useState(null);
const [video, setVideo] = useState(null);
const [loading, setLoading] = useState(false);
useEffect(() => {
const fetchItems = async () => {
const data = await getDocs(collection(db, "posts"));
setItems(data.docs.map((doc) => ({ ...doc.data(), id: doc.id })));
};
fetchItems();
}, []);
const handleSubmit = async (e) => {
e.preventDefault();
setLoading(true);
try {
const itemRef = await addDoc(collection(db, "posts"), {
createdAt: new Date().toISOString(),
name,
description,
});
if (image) {
const storageRef = ref(storage, `images/${itemRef.id}`);
await uploadBytes(storageRef, image);
const url = await getDownloadURL(storageRef);
await updateDoc(doc(db, "posts", itemRef.id), { imageUrl: url });
}
if (video) {
const storageRef = ref(storage, `videos/${itemRef.id}`);
await uploadBytes(storageRef, video);
const url = await getDownloadURL(storageRef);
await updateDoc(doc(db, "posts", itemRef.id), { videoUrl: url });
}
setName("");
setDescription("");
setImage(null);
setVideo(null);
} catch (error) {
console.error("Error adding item: ", error);
} finally {
setLoading(false);
}
};
const handleDelete = async (id) => {
setLoading(true);
try {
await deleteDoc(doc(db, "posts", id));
} catch (error) {
console.error("Error deleting item: ", error);
} finally {
setLoading(false);
}
};
const handleImageUpload = (e) => {
const file = e.target.files[0];
setImage(file);
};
const handleVideoUpload = (e) => {
const file = e.target.files[0];
setVideo(file);
};
return (
<div>
{loading && <div style={{ fontSize: 100, color: "red" }}>Loading...</div>}
<h1>Items</h1>
<form onSubmit={handleSubmit}>
<div class="space-y-12">
<div class="border-b border-gray-900/10 pb-12">
<div class="mt-10 grid grid-cols-1 gap-x-6 gap-y-8 sm:grid-cols-6">
<div class="sm:col-span-4">
<label
for="username"
class="block text-sm font-medium leading-6 text-gray-900"
>
Title
</label>
<div class="mt-2">
<div class="flex rounded-md shadow-sm ring-1 ring-inset ring-gray-300 focus-within:ring-2 focus-within:ring-inset focus-within:ring-indigo-600 sm:max-w-md">
<input
type="text"
placeholder="Title"
value={name}
onChange={(e) => setName(e.target.value)}
class="block flex-1 border-0 bg-transparent py-1.5 pl-1 text-gray-900 placeholder:text-gray-400 focus:ring-0 sm:text-sm sm:leading-6"
/>
</div>
</div>
<br />
<label
for="username"
class="block text-sm font-medium leading-6 text-gray-900"
>
Description
</label>
<div class="mt-2">
<div class="flex rounded-md shadow-sm ring-1 ring-inset ring-gray-300 focus-within:ring-2 focus-within:ring-inset focus-within:ring-indigo-600 sm:max-w-md">
<input
type="text"
placeholder="Description"
value={description}
onChange={(e) => setDescription(e.target.value)}
class="block flex-1 border-0 bg-transparent py-1.5 pl-1 text-gray-900 placeholder:text-gray-400 focus:ring-0 sm:text-sm sm:leading-6"
/>
</div>
</div>
</div>
<div class="col-span-full">
<label
for="cover-photo"
class="block text-sm font-medium leading-6 text-gray-900"
>
Image upload
</label>
<div class="mt-2 flex justify-center rounded-lg border border-dashed border-gray-900/25 px-6 py-10">
<div class="text-center">
<svg
class="mx-auto h-12 w-12 text-gray-300"
viewBox="0 0 24 24"
fill="currentColor"
aria-hidden="true"
>
<path
fill-rule="evenodd"
d="M1.5 6a2.25 2.25 0 012.25-2.25h16.5A2.25 2.25 0 0122.5 6v12a2.25 2.25 0 01-2.25 2.25H3.75A2.25 2.25 0 011.5 18V6zM3 16.06V18c0 .414.336.75.75.75h16.5A.75.75 0 0021 18v-1.94l-2.69-2.689a1.5 1.5 0 00-2.12 0l-.88.879.97.97a.75.75 0 11-1.06 1.06l-5.16-5.159a1.5 1.5 0 00-2.12 0L3 16.061zm10.125-7.81a1.125 1.125 0 112.25 0 1.125 1.125 0 01-2.25 0z"
clip-rule="evenodd"
/>
</svg>
<div class="mt-4 flex text-sm leading-6 text-gray-600">
<label
for="file-upload"
class="relative cursor-pointer rounded-md bg-white font-semibold text-indigo-600 focus-within:outline-none focus-within:ring-2 focus-within:ring-indigo-600 focus-within:ring-offset-2 hover:text-indigo-500"
>
<span>Upload a file</span>
<input
id="file-upload"
name="file-upload"
type="file"
class="sr-only"
accept="video/*"
onChange={handleVideoUpload}
/>
</label>
<p class="pl-1">or drag and drop</p>
</div>
<p class="text-xs leading-5 text-gray-600">
PNG, JPG, GIF up to 10MB
</p>
</div>
</div>
</div>
{/* <input type="file" accept="image/*" onChange={handleImageUpload} /> */}
{/* <input
type="file"
accept="video/*"
onChange={handleVideoUpload}
/> */}
<br /> <br /> <br /> <br />
</div>
<button
class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded"
type="submit"
>
Add Item
</button>
</div>
</div>
</form>
{items
// .sort((a, b) => b.createdAt.toDate() - a.createdAt.toDate())
.map((item) => (
<div
class="max-w-sm rounded overflow-hidden shadow-lg"
key={item.id}
style={{ float: "right", margin: 10 }}
>
{item.imageUrl && (
<img
src={item.imageUrl}
alt={item.name}
style={{ width: 400 }}
class="w-full"
/>
)}
{item.videoUrl && (
<video
src={item.videoUrl}
controls
style={{ width: 400 }}
></video>
)}
<div class="px-6 py-4">
<div class="font-bold text-xl mb-2">{item.name}</div>
<p class="text-gray-700 text-base">{item.description}</p>
</div>
<div class="px-6 pt-4 pb-2">
<span class="inline-block bg-gray-200 rounded-full px-3 py-1 text-sm font-semibold text-gray-700 mr-2 mb-2">
{item.createdAt}
</span>
</div>
<button
class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded"
onClick={() => handleDelete(item.id)}
style={{ margin: 10 }}
>
Delete
</button>
</div>
))}
</div>
);
}
export default Items;