Skip to content

Commit

Permalink
Merge remote-tracking branch 'refs/remotes/origin/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
TheoJustin committed Aug 23, 2024
2 parents 94ffad8 + 511c6e7 commit 692d706
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 40 deletions.
88 changes: 78 additions & 10 deletions src/ss_backend/main.mo
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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<Post>();

for (post in posts.vals()) {
tempPosts.add(post);
};

return #ok(Vector.toArray(tempPosts));
};

public query func getPostById(postId : Text) : async Result.Result<Post, Text> {
let post = posts.get(postId);
switch (post) {
case (?post) {
return #ok(post);
};
case (null) {
return #err("Post not found!");
};
};
};

};
86 changes: 56 additions & 30 deletions src/ss_frontend/src/components/TextInput.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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();
}
};

Expand All @@ -51,7 +67,6 @@ const TextInput = () => {
if (url) {
setImages((prevImages) => [...prevImages, url]);
console.log(images);

} else {
throw new Error("Failed to upload image.");
}
Expand All @@ -70,9 +85,16 @@ const TextInput = () => {
}
};

const { mutate: sendMutate, status: sendStatus } = useMutation({
mutationKey: ["checkSend"],
mutationFn: handleSend,
});

return (
<div className="mb-5">
<h1 className="pl-[1rem] text-xl font-medium">Any project you wanna share?</h1>
<h1 className="pl-[1rem] text-xl font-medium">
Any project you wanna share?
</h1>
<div className="flex justify-between items-center gap-5">
<input
type="text"
Expand All @@ -93,20 +115,24 @@ const TextInput = () => {
/>
<HiOutlinePaperAirplane
className="text-4xl cursor-pointer"
onClick={handleSend}
onClick={sendMutate}
/>
</div>
{images.length != 0 &&
{images.length != 0 && (
<div className="items-center w-full flex justify-start gap-4 p-4 overflow-x-scroll">
{images.map((imageUrl) => {
return(
<div className="">
<img src={imageUrl} className="max-w-52 max-h-36 rounded-lg" alt="" />
</div>
)
return (
<div className="">
<img
src={imageUrl}
className="max-w-52 max-h-36 rounded-lg"
alt=""
/>
</div>
);
})}
</div>
}
)}
</div>
);
};
Expand Down

0 comments on commit 692d706

Please sign in to comment.