Skip to content

Commit

Permalink
message notifaction bug fix, comment reply bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
tomyRomero committed Mar 5, 2024
1 parent 4ebde9f commit f2a7666
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 16 deletions.
2 changes: 1 addition & 1 deletion app/(root)/chat/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ async function Page({
}

return (
<section className='w-full h-screen flex flex-col bg-black rounded-xl'>
<section className='w-full flex flex-1 flex-col bg-black rounded-xl h-max-screen'>
<div className="flex">
<h1 className='text-heading1-bold text-light-1 ml-4 mt-2'>Chat</h1>
<Image
Expand Down
2 changes: 1 addition & 1 deletion components/Chatbox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Button } from './ui/button';

const Chatbox = ({ chats }: any) => {
const [page, setPage] = useState(0);
const itemsPerPage = 3;
const itemsPerPage = 2;

// Calculate the starting index for slicing the array
const startIndex = page * itemsPerPage;
Expand Down
3 changes: 1 addition & 2 deletions components/cards/ChatLogs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
import Link from "next/link"
import Image from "next/image"
import { useEffect, useState } from "react";
import { getImageData, getRes } from "@/lib/s3";
import pusherClient from "@/lib/pusher";
import { getRes } from "@/lib/s3";
import { getChatBySenderAndReceiver, revalData } from "@/lib/actions/chat.actions";
import { useAppContext } from "@/lib/AppContext";
import { useRouter } from "next/navigation";
Expand Down
3 changes: 2 additions & 1 deletion components/cards/UserCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { useRouter } from "next/navigation";
import { getImageData, getRes } from "@/lib/s3";
import { Button } from "../ui/button";
import { useEffect, useState } from "react";
import { getChatBySenderAndReceiver, sendMessage } from "@/lib/actions/chat.actions";
import { getChatBySenderAndReceiver, sendMessage, updateChatForOther } from "@/lib/actions/chat.actions";
import { getDateTime } from "@/lib/utils";
import { fetchPostById } from "@/lib/actions/post.actions";

Expand Down Expand Up @@ -81,6 +81,7 @@ function UserCard({ id, name, username, imgUrl, type, postId, sender}: Props) {
]

const didSend = await sendMessage(shareText, sender, timestamp, id, newMessages, "/");
const didUpdate = await updateChatForOther(id, sender, newMessages, "/");

if(didSend)
{
Expand Down
9 changes: 5 additions & 4 deletions components/forms/Comment.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -125,20 +125,21 @@ function Comment({ postId, currentUserImg, currentUserId, parentId }: Props) {
{!backLoading? <h1>Back</h1> : <Image src={"/assets/lineloader.svg"} alt="loading" width={44} height={34}/> }
</Button>


<Button type='submit' className='comment-form_btn max-md:hidden'>
{!loading? <h1>Reply</h1> : <Image src={"/assets/lineloader.svg"} alt="loading" width={44} height={34}/> }
</Button>
</form>
<div className="hidden w-full mt-6 ml-auto max-sm:flex justify-end gap-4">
<Button type="button" onClick={goBack} className="comment-form_btn">

<div className="hidden w-full ml-auto max-sm:flex justify-end gap-4">
<Button type="button" onClick={goBack} className="comment-form_btn">
{!backLoading? <h1>Back</h1> : <Image src={"/assets/lineloader.svg"} alt="loading" width={44} height={34}/> }
</Button>

<Button type='submit' className='comment-form_btn'>
{!loading? <h1>Reply</h1> : <Image src={"/assets/lineloader.svg"} alt="loading" width={44} height={34}/> }
</Button>
</div>
</form>

</Form>
);
}
Expand Down
48 changes: 41 additions & 7 deletions lib/actions/chat.actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ export const getPusher = async()=> {
});
}



export const sendMessage = async (text: string, sender: string, timestamp: string, receiver: string, messages: any[], pathname: string) => {
try {
const connection = connectDb('spark', "sendMessage");
Expand Down Expand Up @@ -150,8 +148,7 @@ export const getPusher = async()=> {
};



//Get Chat that fills the reciever UI chat
//Get Chat that fills the receiver UI chat
export const getChatBySenderAndReceiver = async (senderId: string, receiverId: string) => {
try {
const connection = connectDb('spark', "getChatBySenderAndReceiver");
Expand Down Expand Up @@ -237,7 +234,7 @@ export const revalData = (path : string)=> {


export const getNoti = async (userId : string) => {
const userChats = await getChatsWithUsersByUserId(userId);
const userChats = await getChatsWithUsersByUserIdSender(userId);

const readStatusArray = await Promise.all(
userChats.map(async (chat) => {
Expand All @@ -249,11 +246,16 @@ export const getNoti = async (userId : string) => {

const lastMessage = chatfromOtherSide.messages[ chatfromOtherSide.messages.length - 1]

if(lastMessage.receiver === user.id)
if(lastMessage.sender === userId)
{
return 1;
}else{
return chatfromOtherSide.read_status;
if(!chatfromOtherSide.read_status)
{
console.log(user)
}

return chatfromOtherSide.read_status
}
})
);
Expand All @@ -276,6 +278,38 @@ export const getNoti = async (userId : string) => {
};


export const getChatsWithUsersByUserIdSender = async (userId: string) => {
try {
const connection = connectDb('spark', `getChatsWithUsersByUserId`);
// Use a parameterized query to select chats with user information
const queryAsync = util.promisify(connection.query).bind(connection);

const query = `
SELECT
chat.*,
user.username AS user_username,
user.name AS user_name,
user.image AS user_image
FROM
chat
JOIN
user ON chat.sender_id = user.id
WHERE
chat.sender_id = ?
`;

//@ts-ignore
const results: any[] = await queryAsync(query, [userId]);

// Close the database connection
connection.end();
//console.log("User Chats with Users: ", results);
return results;
} catch (error) {
console.log(error);
return [];
}
};



0 comments on commit f2a7666

Please sign in to comment.