Update Post Page
This commit is contained in:
@@ -1,7 +1,6 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
import { useDevice } from "@/lib/context/DeviceContext";
|
import { useDevice } from "@/lib/context/DeviceContext";
|
||||||
|
|
||||||
import React, { useEffect, useState } from "react";
|
import React, { useEffect, useState } from "react";
|
||||||
|
|
||||||
export default function PostsPage() {
|
export default function PostsPage() {
|
||||||
@@ -10,6 +9,7 @@ export default function PostsPage() {
|
|||||||
const [userReactions, setUserReactions] = useState<{
|
const [userReactions, setUserReactions] = useState<{
|
||||||
[index: number]: { liked: boolean; warned: boolean };
|
[index: number]: { liked: boolean; warned: boolean };
|
||||||
}>({});
|
}>({});
|
||||||
|
const [imageFile, setImageFile] = useState<File | null>(null);
|
||||||
const { isAuthenticated, session } = useDevice();
|
const { isAuthenticated, session } = useDevice();
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@@ -18,18 +18,37 @@ export default function PostsPage() {
|
|||||||
}
|
}
|
||||||
}, [isAuthenticated, session?.username]);
|
}, [isAuthenticated, session?.username]);
|
||||||
|
|
||||||
const handlePostSubmit = (e: React.FormEvent) => {
|
const handlePostSubmit = async (e: React.FormEvent) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
const newPost = {
|
|
||||||
text: postText,
|
if (!imageFile) {
|
||||||
date: new Date().toLocaleString(),
|
alert("Please select an image to upload.");
|
||||||
likes: 0,
|
return;
|
||||||
warnings: 0,
|
}
|
||||||
imageUrl: "", // placeholder for image logick
|
|
||||||
author: session.username, // add username to track authorship
|
const formData = new FormData();
|
||||||
};
|
formData.append("image", imageFile);
|
||||||
setPosts([newPost, ...posts]);
|
formData.append("text", postText);
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await fetch("/api/post", {
|
||||||
|
method: "POST",
|
||||||
|
body: formData,
|
||||||
|
});
|
||||||
|
|
||||||
|
const data = await response.json();
|
||||||
|
if (response.ok) {
|
||||||
|
alert("Post uploaded successfully!");
|
||||||
|
setPosts([data.postData, ...posts]);
|
||||||
setPostText("");
|
setPostText("");
|
||||||
|
setImageFile(null);
|
||||||
|
} else {
|
||||||
|
alert(data.message || "Failed to upload post.");
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error uploading post:", error);
|
||||||
|
alert("An error occurred while uploading the post.");
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleLike = (index: number) => {
|
const handleLike = (index: number) => {
|
||||||
@@ -83,6 +102,12 @@ export default function PostsPage() {
|
|||||||
onChange={(e) => setPostText(e.target.value)}
|
onChange={(e) => setPostText(e.target.value)}
|
||||||
rows={4}
|
rows={4}
|
||||||
/>
|
/>
|
||||||
|
<input
|
||||||
|
type="file"
|
||||||
|
accept="image/*"
|
||||||
|
className="w-full p-2 rounded bg-neutral-800 text-white"
|
||||||
|
onChange={(e) => setImageFile(e.target.files?.[0] || null)}
|
||||||
|
/>
|
||||||
<button
|
<button
|
||||||
type="submit"
|
type="submit"
|
||||||
className="px-4 py-2 bg-success-600 text-white rounded font-semibold"
|
className="px-4 py-2 bg-success-600 text-white rounded font-semibold"
|
||||||
|
|||||||
@@ -21,7 +21,9 @@ async function authenticateUser() {
|
|||||||
|
|
||||||
export async function POST(req: Request) {
|
export async function POST(req: Request) {
|
||||||
try {
|
try {
|
||||||
// if (!(await authenticateUser())) return;
|
|
||||||
|
let userData = await authenticateUser();
|
||||||
|
if (!userData) return NextResponse.json({ message: "User not found" }, { status: 404 });
|
||||||
|
|
||||||
const formData = await req.formData();
|
const formData = await req.formData();
|
||||||
const file = formData.get("image");
|
const file = formData.get("image");
|
||||||
@@ -39,7 +41,14 @@ export async function POST(req: Request) {
|
|||||||
const prompt = `Generate a 1-3 sentence description for this image.`;
|
const prompt = `Generate a 1-3 sentence description for this image.`;
|
||||||
|
|
||||||
const data = await gemini.generateDescription(prompt, buffer);
|
const data = await gemini.generateDescription(prompt, buffer);
|
||||||
let postData = await db.posts.create("6gi1f", data?.description);
|
let postData = await db.posts.create(userData.id, data?.description, buffer);
|
||||||
|
|
||||||
|
if (!postData) {
|
||||||
|
return NextResponse.json(
|
||||||
|
{ message: "Failed to create post" },
|
||||||
|
{ status: 500 }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
{ message: "Image uploaded successfully", postData },
|
{ message: "Image uploaded successfully", postData },
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ const postSchema = new mongoose.Schema({
|
|||||||
timeStamp: Date,
|
timeStamp: Date,
|
||||||
reactions: Array,
|
reactions: Array,
|
||||||
userId: reqString,
|
userId: reqString,
|
||||||
|
image: Buffer
|
||||||
});
|
});
|
||||||
|
|
||||||
export class Post {
|
export class Post {
|
||||||
@@ -31,13 +32,14 @@ export class Post {
|
|||||||
return result.join('');
|
return result.join('');
|
||||||
}
|
}
|
||||||
|
|
||||||
async create(userId:string, imageDes: string) {
|
async create(userId:string, imageDes: string, image: Buffer) {
|
||||||
const newEntry = new this.model({
|
const newEntry = new this.model({
|
||||||
id: this.makeId(5),
|
id: this.makeId(5),
|
||||||
imageDes: imageDes,
|
imageDes: imageDes,
|
||||||
timeStamp: new Date().toISOString(),
|
timeStamp: new Date().toISOString(),
|
||||||
reactions: [],
|
reactions: [],
|
||||||
userId: userId
|
userId: userId,
|
||||||
|
image
|
||||||
});
|
});
|
||||||
await newEntry.save();
|
await newEntry.save();
|
||||||
return newEntry;
|
return newEntry;
|
||||||
|
|||||||
Reference in New Issue
Block a user