diff --git a/src/ss_backend/main.mo b/src/ss_backend/main.mo index 694e0d4..d3be075 100644 --- a/src/ss_backend/main.mo +++ b/src/ss_backend/main.mo @@ -33,18 +33,18 @@ actor { category : Text; timestamp : Time.Time; images : [Text]; - comments: [Text]; - positive: Nat; - negative: Nat; - positiveVotes: Nat; - negativeVotes: Nat; - likes: [Principal]; + comments : [Text]; + positive : Nat; + negative : Nat; + positiveVotes : Nat; + negativeVotes : Nat; + likes : [Principal]; }; type Comment = { - id: Text; - sender: Principal; - comment: Text; + id : Text; + sender : Principal; + comment : Text; }; type Friend = { @@ -121,6 +121,74 @@ actor { return #ok(Vector.toArray(allUsers)); }; - + // post + public func createPost(userId : Principal, description : Text, images : [Text]) : async Bool { + let newId = await generateUUID(); + + let currUser = await getUserById(userId); + + // id : Text; + // description : Text; + // sender : Principal; + // category : Text; + // timestamp : Time.Time; + // images : [Text]; + // comments: [Text]; + // positive: Nat; + // negative: Nat; + // positiveVotes: Nat; + // negativeVotes: Nat; + // likes: [Principal]; + + switch (currUser) { + case (#ok(currUser)) { + let post : Post = { + id = newId; + description = description; + sender = userId; + category = ""; + timestamp = Time.now(); + images = images; + comments = []; + positive = 0; + negative = 0; + positiveVotes = 0; + negativeVotes = 0; + likes = []; + }; + + posts.put(newId, post); + }; + + case (#err(_error)) { + return false; + }; + }; + + return true; + }; + + public query func getAllPosts() : async Result.Result<[Post], Text> { + + var tempPosts = Vector.Vector(); + + for (post in posts.vals()) { + tempPosts.add(post); + }; + + return #ok(Vector.toArray(tempPosts)); + }; + + public query func getPostById(postId : Text) : async Result.Result { + let post = posts.get(postId); + switch (post) { + case (?post) { + return #ok(post); + }; + case (null) { + return #err("Post not found!"); + }; + }; + }; }; diff --git a/src/ss_frontend/src/components/TextInput.jsx b/src/ss_frontend/src/components/TextInput.jsx index d83dad6..3f5da7d 100644 --- a/src/ss_frontend/src/components/TextInput.jsx +++ b/src/ss_frontend/src/components/TextInput.jsx @@ -2,8 +2,12 @@ import React, { useRef, useState } from "react"; import { HiOutlinePaperAirplane } from "react-icons/hi2"; import { LuImagePlus } from "react-icons/lu"; import { uploadImage } from "../../../config/cloudinary"; +import { ss_backend } from "../../../declarations/ss_backend"; +import { useAuth } from "../hooks/UseAuth"; +import { useMutation } from "@tanstack/react-query"; const TextInput = () => { + const { principal, isAuthenticated } = useAuth(); const [inputText, setInputText] = useState(""); const [images, setImages] = useState([]); const [loading, setLoading] = useState(false); @@ -15,28 +19,40 @@ const TextInput = () => { return; } - console.log(inputText) + if (!isAuthenticated) { + alert("You must be logged in before posting."); + return; + } - try { - const response = await fetch( - "https://web-production-d8ae.up.railway.app/analyze", - { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify({ comment: inputText }), - } - ); + console.log(inputText); - const data = await response.json(); - if (response.ok) { - alert(`Classification: ${data.classification}`); - } else { - alert(`Error: ${data.error}`); - } - } catch (error) { - alert("Failed to send comment. Please try again later."); + // try { + // const response = await fetch( + // "https://web-production-d8ae.up.railway.app/analyze", + // { + // method: "POST", + // headers: { + // "Content-Type": "application/json", + // }, + // body: JSON.stringify({ comment: inputText }), + // } + // ); + + // const data = await response.json(); + // if (response.ok) { + // alert(`Classification: ${data.classification}`); + // } else { + // alert(`Error: ${data.error}`); + // } + // } catch (error) { + // alert("Failed to send comment. Please try again later."); + // } + + const resp = await ss_backend.createPost(principal, inputText, images); + + if (resp) { + console.log("aman"); + window.location.reload(); } }; @@ -51,7 +67,6 @@ const TextInput = () => { if (url) { setImages((prevImages) => [...prevImages, url]); console.log(images); - } else { throw new Error("Failed to upload image."); } @@ -70,9 +85,16 @@ const TextInput = () => { } }; + const { mutate: sendMutate, status: sendStatus } = useMutation({ + mutationKey: ["checkSend"], + mutationFn: handleSend, + }); + return (
-

Any project you wanna share?

+

+ Any project you wanna share? +

{ />
- {images.length != 0 && + {images.length != 0 && (
{images.map((imageUrl) => { - return( -
- -
- ) + return ( +
+ +
+ ); })}
- } + )}
); };