Skip to content

Commit

Permalink
add delete post api route
Browse files Browse the repository at this point in the history
  • Loading branch information
kualta committed Jun 29, 2024
1 parent 021dabc commit aa1031a
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
37 changes: 37 additions & 0 deletions src/app/api/posts/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,43 @@ async function fetchData(
});
}

export async function DELETE(req: NextRequest) {
const { searchParams } = req.nextUrl;
const id = searchParams.get("id") || undefined;

if (!id) {
return NextResponse.json({ error: "Missing id" }, { status: 400 });
}

try {
const { client, isAuthenticated, profileId } = await getLensClient();

if (!isAuthenticated) {
throw new Error("Not authenticated");
}

const post = await client.publication.fetch({ forId: id });

if (post.by.id !== profileId) {
throw new Error("Not authorized");
}

let result: Result<void, CredentialsExpiredError | NotAuthenticatedError>;
if (!post.isHidden) {
result = await client.publication.hide({ for: id });
}

if (result.isFailure()) {
throw new Error(result.error.message);
}

return NextResponse.json({ result }, { status: 200 });
} catch (error) {
console.error(error);
return NextResponse.json({ error: `Failed to delete post: ${error.message}` }, { status: 500 });
}
}

export async function POST(req: NextRequest) {
try {
const { searchParams } = req.nextUrl;
Expand Down
5 changes: 2 additions & 3 deletions src/components/post/PostContextMenu.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
"use client";
import { type PropsWithChildren, useEffect, useState } from "react";
import { ServerSignedIn } from "../ServerSignedIn";
import { Card } from "../ui/card";
import type { Post } from "./Post";
import { PostMenuContent } from "./PostMenu";
import { PostMenu } from "./PostMenu";

export const PostContextMenu = (props: PropsWithChildren & { post: Post }) => {
const [clicked, setClicked] = useState(false);
Expand Down Expand Up @@ -35,7 +34,7 @@ export const PostContextMenu = (props: PropsWithChildren & { post: Post }) => {
{clicked && (
<div className="z-[40] absolute" style={{ top: `${points.y}px`, left: `${points.x}px` }}>
<Card className="flex flex-col w-max gap-1 p-1 hover:bg-card border">
<PostMenuContent post={props.post} profileId="" />
<PostMenu post={props.post} profileId="" />
</Card>
</div>
)}
Expand Down
2 changes: 1 addition & 1 deletion src/components/post/PostMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { toast } from "sonner";
import { Button } from "../ui/button";
import type { Post } from "./Post";

export const PostMenuContent = ({ post, profileId }: { post: Post; profileId: string }) => {
export const PostMenu = ({ post, profileId }: { post: Post; profileId: string }) => {
const router = useRouter();
const author = post.author;

Expand Down

0 comments on commit aa1031a

Please sign in to comment.