-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcreatePin.jsx
217 lines (215 loc) · 6.35 KB
/
createPin.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
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
import { AiOutlineCloudUpload } from "react-icons/ai";
import { MdDelete } from "react-icons/md";
import { useNavigate } from "react-router-dom";
import { useForm } from "react-hook-form";
import { Spinner } from "../components";
import { useState } from "react";
import categories from "../utils/categories";
import { client } from "../sanity.config";
const CreatePin = ({ user }) => {
const [imageSrc, setImageSrc] = useState(null);
const navigate = useNavigate();
const {
register,
handleSubmit,
setError,
setValue,
clearErrors,
resetField,
formState: { errors, isSubmitting },
} = useForm();
const onSubmit = async data => {
const { imageUpload, title, about, destination, category } = data;
let imageAsset;
/* First Upload Image to Sanity (which stores it as imageAsset) and get their generated URL */
try {
const file = imageUpload;
const { type, name } = file;
imageAsset = await client.assets.upload("image", file, {
contentType: type,
filename: name,
});
} catch (error) {
console.log("Image Upload failed");
return;
}
/* Now create the pin including the above imageAsset */
const doc = {
_type: "pin",
title,
about,
destination,
category,
image: {
_type: "image",
asset: {
_type: "reference",
_ref: imageAsset?._id,
},
},
userId: user._id,
postedBy: {
_type: "postedBy",
_ref: user._id,
},
};
try {
await client.create(doc);
navigate("/");
} catch (error) {
console.log("Could not create a new pin", error);
}
};
const handleImageChange = e => {
const file = e.target.files[0];
const fileType = file.type;
if (!fileType.includes("image")) {
setError("imageUpload", {
type: "fileType",
message: "Only image files are allowed",
});
resetField("imageUpload");
} else {
clearErrors("imageUpload");
var reader = new FileReader();
reader.onload = function (e) {
setImageSrc(e.target.result);
};
reader.readAsDataURL(file);
setValue("imageUpload", file);
}
};
return (
<form
onSubmit={handleSubmit(onSubmit)}
className="flex flex-col justify-center items-center mt-5 lg:h-4/5">
{Object.keys(errors).length > 0 &&
errors?.imageUpload?.type !== "fileType" && (
<p className="text-red-500 mb-5 text-xl transition-all duration-150 ease-in ">
Please add all fields.
</p>
)}
<div className="flex flex-col lg:flex-row justify-center items-center bg-white p-3 lg:p-5 w-full lg:w-4/5">
{/* Image Upload */}
<div className="bg-secondaryColor p-3 flex flex-0.7 w-full">
<div className=" flex flex-col justify-center items-center border-2 border-dotted border-gray-300 p-3 w-full h-420">
{isSubmitting && <Spinner />}
{errors?.imageUpload?.type === "fileType" && (
<p className="text-center">
{errors?.imageUpload.message}
</p>
)}
{!imageSrc ? (
<label htmlFor="imageUpload">
<div className="flex flex-col items-center justify-center h-full">
<div className="flex flex-col justify-center items-center">
<p className="font-bold text-2xl">
<AiOutlineCloudUpload />
</p>
<p className="text-lg">
Click to upload
</p>
</div>
<p className="mt-32 text-gray-400">
Recommendation: Use high-quality JPG,
JPEG, SVG, PNG, GIF or TIFF less than
20MB
</p>
</div>
<input
type="file"
{...register("imageUpload", {
required: true,
})}
accept="image/*"
id="imageUpload"
onChange={handleImageChange}
className="w-0 h-0"
/>
</label>
) : (
<div className="relative h-full w-full">
<img
src={imageSrc}
alt="uploaded-pic"
className="h-full w-full object-cover"
/>
<button
type="button"
className="absolute bottom-3 right-3 p-3 rounded-full bg-white text-xl cursor-pointer outline-none hover:shadow-md transition-all duration-500 ease-in-out"
onClick={e => {
setImageSrc(null);
resetField("imageUpload");
}}>
<MdDelete />
</button>
</div>
)}
</div>
</div>
{/* Other Inputs */}
<div className="flex flex-1 flex-col gap-6 lg:pl-5 mt-5 w-full ">
<input
type="text"
{...register("title", { required: true })}
placeholder="Add your title"
className="outline-none text-2xl sm:text-3xl font-bold border-b-2 border-gray-200 p-2 focus:shadow-sm"
/>
<div className="flex items-center gap-2 mt-2 mb-2 bg-white rounded-lg ">
<img
src={user?.avatar}
className="w-10 h-10 rounded-full"
alt="user-profile"
/>
<p className="font-bold">{user?.userName}</p>
</div>
<textarea
rows={1}
{...register("about", { required: true })}
placeholder="Tell everyone what your Pin is about"
className="outline-none text-base sm:text-lg border-b-2 border-gray-200 p-2 overflow-hidden focus:shadow-sm"></textarea>
<input
type="url"
{...register("destination", { required: true })}
placeholder="Add a destination link"
className="outline-none text-base sm:text-lg border-b-2 border-gray-200 p-2 invalid:border-pink-500 invalid:text-pink-600"
/>
<div className="flex flex-col mt-2">
<div>
<p className="mb-2 font-semibold text:lg sm:text-xl">
Choose Pin Category
</p>
<select
{...register("category", {
required: true,
})}
className="outline-none w-4/5 text-base border-b-2 border-gray-200 p-2 rounded-md cursor-pointer">
<option
value="others"
className="sm:text-bg bg-white">
Select Category
</option>
{categories.map((item, ind) => (
<option
key={`${item.name}-${ind}`}
className="text-base border-0 outline-none capitalize bg-white text-black "
value={item.name}>
{item.name}
</option>
))}
</select>
</div>
<div className="flex justify-end items-end mt-5">
<button
type="submit"
className="bg-red-500 text-white font-bold p-2 rounded-full w-28 outline-none">
Save Pin
</button>
</div>
</div>
</div>
</div>
</form>
);
};
export default CreatePin;