Skip to content

Commit

Permalink
add unfollow actions, closes #36
Browse files Browse the repository at this point in the history
  • Loading branch information
kualta committed Jun 18, 2024
1 parent 5e752af commit 72ea878
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 7 deletions.
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
## Overview

**what?**

[pingpad](https://pingpad.io) is a minimalistic decentralized social platform, made for you and your community.

**why?**
- complexity is the enemy.
- clarity paves the way for brilliance.
- embrace simplicity by focusing on clear and efficient solutions.

clarity paves the way for brilliance. pingpad is focusing on clear and efficient solutions.

**how?**

built on top of [lens](https://lens.xyz) network

## Gallery
<img height="auto" width="700" style="border-radius:50px" src="https://github.com/pingpad-io/ping/assets/72769566/29e2ae6a-f89e-45cc-b17a-4d5c6a4dd841" />
Expand Down
39 changes: 39 additions & 0 deletions src/app/api/profile/unfollow/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { LensTransactionStatusType } from "@lens-protocol/client";
import { type NextRequest, NextResponse } from "next/server";
import { getLensClient } from "~/utils/getLensClient";

export const dynamic = "force-dynamic";

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

try {
const { client } = await getLensClient();

const result = await client.profile.unfollow({
unfollow: [id],
});

if (result.isFailure()) {
return NextResponse.json({ error: result.error.message }, { status: 500 });
}

if (result.value.__typename === "LensProfileManagerRelayError") {
return NextResponse.json({ error: result.value.reason }, { status: 500 });
}

const data = result.value;

const completion = await client.transaction.waitUntilComplete({ forTxId: data.txId });
if (completion?.status === LensTransactionStatusType.Failed) {
console.error(completion.reason);
return NextResponse.json({ error: completion.reason, extra: completion.extraInfo }, { status: 500 });
}

return NextResponse.json({ result: result.value }, { status: 200, statusText: "Success" });
} catch (error) {
console.error("Failed to unfollow profile: ", error.message);
return NextResponse.json({ error: `${error.message}` }, { status: 500 });
}
}
10 changes: 6 additions & 4 deletions src/components/FollowButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,23 @@ import { toast } from "sonner";
import type { User } from "./post/Post";
import { Button } from "./ui/button";

export const FollowButton = ({ user }: {user: User}) => {
export const FollowButton = ({ user }: { user: User }) => {
const [following, setFollowing] = useState(user.actions.followed);

const toggleFollow = async () => {
const endpoint = following ? "/api/profile/unfollow" : "/api/profile/follow";

setFollowing(!following);

const result = await fetch(`/api/profile/follow?id=${user.id}`, {
const result = await fetch(`${endpoint}?id=${user.id}`, {
method: "POST",
});

if (!result.ok) {
setFollowing(!following);
toast.error(`Failed to follow: ${result.statusText} `);
toast.error(`${following ? "Unfollow" : "Follow"} action failed: ${result.statusText} `);
} else {
toast.success("Followed successfully!");
toast.success(`${following ? "Unfollowed" : "Followed"} Successfully!`);
}
};

Expand Down

0 comments on commit 72ea878

Please sign in to comment.