const titleRef = useRef(null);
const contentRef = useRef(null);
const customTagRef = useRef(null);
const tagRef = useRef(null);
const photoRef = useRef(null);
const placeNameRef = useRef(null);
const placeAddressRef = useRef(null);
const handleCreate = async (e) => {
e.preventDefault();
const titleValue = titleRef.current.value;
const contentValue = contentRef.current.value;
const photoValue = photoRef.current.files;
const tagValue = tagRef.current.value;
const customTagValue = customTagRef.current.value;
if (!tagValue) {
toast("태그를 선택해주세요.", {
position: "top-center",
icon: "🚨",
ariaProps: {
role: "alert",
"aria-live": "polite",
},
});
return;
}
if (!photoValue[0]) {
toast("사진을 등록해주세요.", {
position: "top-center",
icon: "🚨",
ariaProps: {
role: "alert",
"aria-live": "polite",
},
});
return;
}
const formData = new FormData();
formData.append("title", titleValue);
formData.append("content", contentValue);
formData.append("location", placeNameRef.current);
formData.append("address", placeAddressRef.current);
formData.append("tag", tagValue);
formData.append("customTag", customTagValue);
formData.set("userId", pb.authStore.model.id);
if (photoValue) {
formData.append("photo", photoValue[0]);
}
try {
const contentData = await pb.collection("content").create(formData);
if (contentData) {
await pb
.collection("user")
.update(user.id, { "content+": contentData.id });
}
navigate("/content/list");
toast("등록되었습니다.", {
position: "top-center",
icon: "⭐",
ariaProps: {
role: "status",
"aria-live": "polite",
},
});
} catch (error) {
console.error(error);
alert("오류가 발생해 콘텐츠 등록에 실패했습니다. 다시 시도해주세요.");
}
};