Skip to content

Commit

Permalink
feat: add remove real time
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonioErdeljac committed May 1, 2023
1 parent e3b104c commit 230ac72
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
5 changes: 2 additions & 3 deletions app/actions/getConversations.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import prisma from "@/app/libs/prismadb";
import getCurrentUser from "./getCurrentUser";
import { cache } from "react";

const getConversations = cache(async () => {
const getConversations = async () => {
const currentUser = await getCurrentUser();

if (!currentUser?.id) {
Expand Down Expand Up @@ -34,6 +33,6 @@ const getConversations = cache(async () => {
} catch (error: any) {
return [];
}
});
};

export default getConversations;
20 changes: 20 additions & 0 deletions app/api/conversations/[conversationId]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import getCurrentUser from "@/app/actions/getCurrentUser";
import { NextResponse } from "next/server";

import prisma from "@/app/libs/prismadb";
import { pusherServer } from "@/app/libs/pusher";

interface IParams {
conversationId?: string;
Expand All @@ -19,6 +20,19 @@ export async function DELETE(
return NextResponse.json(null);
}

const existingConversation = await prisma.conversation.findUnique({
where: {
id: conversationId
},
include: {
users: true
}
});

if (!existingConversation) {
return new NextResponse('Invalid ID', { status: 400 });
}

const deletedConversation = await prisma.conversation.deleteMany({
where: {
id: conversationId,
Expand All @@ -28,6 +42,12 @@ export async function DELETE(
},
});

existingConversation.users.forEach((user) => {
if (user.email) {
pusherServer.trigger(user.email, 'conversation:remove', existingConversation);
}
});

return NextResponse.json(deletedConversation)
} catch (error) {
return NextResponse.json(null);
Expand Down
7 changes: 7 additions & 0 deletions app/conversations/components/ConversationList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,15 @@ const ConversationList: React.FC<ConversationListProps> = ({
});
}

const removeHandler = (conversation: FullConversationType) => {
setItems((current) => {
return [...current.filter((convo) => convo.id !== conversation.id)]
});
}

pusherClient.bind('conversation:update', updateHandler)
pusherClient.bind('conversation:new', newHandler)
pusherClient.bind('conversation:remove', removeHandler)
}, [pusherKey, router]);

return (
Expand Down

0 comments on commit 230ac72

Please sign in to comment.