From f6c1ce75a6aabe419c88087219b0e6fc2d5c53cb Mon Sep 17 00:00:00 2001
From: Ategon <benjamin@barbeau.net>
Date: Fri, 24 Jan 2025 04:57:24 -0500
Subject: [PATCH] Add promotions and demotions

---
 src/app/p/[slug]/page.tsx         | 136 +++++++++++++++++++++++++++++-
 src/components/posts/PostCard.tsx | 134 ++++++++++++++++++++++++++++-
 2 files changed, 268 insertions(+), 2 deletions(-)

diff --git a/src/app/p/[slug]/page.tsx b/src/app/p/[slug]/page.tsx
index 9d76271..cb8f110 100644
--- a/src/app/p/[slug]/page.tsx
+++ b/src/app/p/[slug]/page.tsx
@@ -377,6 +377,37 @@ export default function PostPage() {
                                 key="promote-mod"
                                 startContent={<Shield />}
                                 description="Promote user to Mod"
+                                onPress={async () => {
+                                  const response = await fetch(
+                                    process.env.NEXT_PUBLIC_MODE === "PROD"
+                                      ? "https://d2jam.com/api/v1/mod"
+                                      : "http://localhost:3005/api/v1/mod",
+                                    {
+                                      body: JSON.stringify({
+                                        targetname: post.author.slug,
+                                        mod: true,
+                                        username: getCookie("user"),
+                                      }),
+                                      method: "POST",
+                                      headers: {
+                                        "Content-Type": "application/json",
+                                        authorization: `Bearer ${getCookie(
+                                          "token"
+                                        )}`,
+                                      },
+                                      credentials: "include",
+                                    }
+                                  );
+
+                                  if (response.ok) {
+                                    toast.success("Promoted User to Mod");
+                                    window.location.reload();
+                                  } else {
+                                    toast.error(
+                                      "Error while promoting user to Mod"
+                                    );
+                                  }
+                                }}
                               >
                                 Appoint as mod
                               </DropdownItem>
@@ -385,11 +416,39 @@ export default function PostPage() {
                             )}
                             {user?.admin &&
                             post.author.mod &&
-                            post.author.id !== user.id ? (
+                            !post.author.admin ? (
                               <DropdownItem
                                 key="demote-mod"
                                 startContent={<ShieldX />}
                                 description="Demote user from Mod"
+                                onPress={async () => {
+                                  const response = await fetch(
+                                    process.env.NEXT_PUBLIC_MODE === "PROD"
+                                      ? "https://d2jam.com/api/v1/mod"
+                                      : "http://localhost:3005/api/v1/mod",
+                                    {
+                                      body: JSON.stringify({
+                                        targetname: post.author.slug,
+                                        username: getCookie("user"),
+                                      }),
+                                      method: "POST",
+                                      headers: {
+                                        "Content-Type": "application/json",
+                                        authorization: `Bearer ${getCookie(
+                                          "token"
+                                        )}`,
+                                      },
+                                      credentials: "include",
+                                    }
+                                  );
+
+                                  if (response.ok) {
+                                    toast.success("Demoted User");
+                                    window.location.reload();
+                                  } else {
+                                    toast.error("Error while demoting user");
+                                  }
+                                }}
                               >
                                 Remove as mod
                               </DropdownItem>
@@ -401,12 +460,87 @@ export default function PostPage() {
                                 key="promote-admin"
                                 startContent={<ShieldAlert />}
                                 description="Promote user to Admin"
+                                onPress={async () => {
+                                  const response = await fetch(
+                                    process.env.NEXT_PUBLIC_MODE === "PROD"
+                                      ? "https://d2jam.com/api/v1/mod"
+                                      : "http://localhost:3005/api/v1/mod",
+                                    {
+                                      body: JSON.stringify({
+                                        targetname: post.author.slug,
+                                        admin: true,
+                                        username: getCookie("user"),
+                                      }),
+                                      method: "POST",
+                                      headers: {
+                                        "Content-Type": "application/json",
+                                        authorization: `Bearer ${getCookie(
+                                          "token"
+                                        )}`,
+                                      },
+                                      credentials: "include",
+                                    }
+                                  );
+
+                                  if (response.ok) {
+                                    toast.success("Promoted User to Admin");
+                                    window.location.reload();
+                                  } else {
+                                    toast.error(
+                                      "Error while promoting user to Admin"
+                                    );
+                                  }
+                                }}
                               >
                                 Appoint as admin
                               </DropdownItem>
                             ) : (
                               <></>
                             )}
+                            {user?.admin &&
+                            post.author.admin &&
+                            post.author.id !== user.id ? (
+                              <DropdownItem
+                                key="demote-admin"
+                                startContent={<ShieldX />}
+                                description="Demote user to mod"
+                                onPress={async () => {
+                                  const response = await fetch(
+                                    process.env.NEXT_PUBLIC_MODE === "PROD"
+                                      ? "https://d2jam.com/api/v1/mod"
+                                      : "http://localhost:3005/api/v1/mod",
+                                    {
+                                      body: JSON.stringify({
+                                        targetname: post.author.slug,
+                                        mod: true,
+                                        username: getCookie("user"),
+                                      }),
+                                      method: "POST",
+                                      headers: {
+                                        "Content-Type": "application/json",
+                                        authorization: `Bearer ${getCookie(
+                                          "token"
+                                        )}`,
+                                      },
+                                      credentials: "include",
+                                    }
+                                  );
+
+                                  if (response.ok) {
+                                    toast.success("Demoted User to Mod");
+                                    window.location.reload();
+                                  } else {
+                                    toast.error(
+                                      "Error while demoting user to mod"
+                                    );
+                                  }
+                                }}
+                              >
+                                Remove as admin
+                              </DropdownItem>
+                            ) : (
+                              <></>
+                            )}
                           </DropdownSection>
                         ) : (
                           <></>
diff --git a/src/components/posts/PostCard.tsx b/src/components/posts/PostCard.tsx
index 92e38c4..ef7ba1f 100644
--- a/src/components/posts/PostCard.tsx
+++ b/src/components/posts/PostCard.tsx
@@ -378,6 +378,37 @@ export default function PostCard({
                             key="promote-mod"
                             startContent={<Shield />}
                             description="Promote user to Mod"
+                            onPress={async () => {
+                              const response = await fetch(
+                                process.env.NEXT_PUBLIC_MODE === "PROD"
+                                  ? "https://d2jam.com/api/v1/mod"
+                                  : "http://localhost:3005/api/v1/mod",
+                                {
+                                  body: JSON.stringify({
+                                    targetname: post.author.slug,
+                                    mod: true,
+                                    username: getCookie("user"),
+                                  }),
+                                  method: "POST",
+                                  headers: {
+                                    "Content-Type": "application/json",
+                                    authorization: `Bearer ${getCookie(
+                                      "token"
+                                    )}`,
+                                  },
+                                  credentials: "include",
+                                }
+                              );
+
+                              if (response.ok) {
+                                toast.success("Promoted User to Mod");
+                                window.location.reload();
+                              } else {
+                                toast.error(
+                                  "Error while promoting user to Mod"
+                                );
+                              }
+                            }}
                           >
                             Appoint as mod
                           </DropdownItem>
@@ -386,11 +417,39 @@ export default function PostCard({
                         )}
                         {user?.admin &&
                         post.author.mod &&
-                        post.author.id !== user.id ? (
+                        !post.author.admin ? (
                           <DropdownItem
                             key="demote-mod"
                             startContent={<ShieldX />}
                             description="Demote user from Mod"
+                            onPress={async () => {
+                              const response = await fetch(
+                                process.env.NEXT_PUBLIC_MODE === "PROD"
+                                  ? "https://d2jam.com/api/v1/mod"
+                                  : "http://localhost:3005/api/v1/mod",
+                                {
+                                  body: JSON.stringify({
+                                    targetname: post.author.slug,
+                                    username: getCookie("user"),
+                                  }),
+                                  method: "POST",
+                                  headers: {
+                                    "Content-Type": "application/json",
+                                    authorization: `Bearer ${getCookie(
+                                      "token"
+                                    )}`,
+                                  },
+                                  credentials: "include",
+                                }
+                              );
+
+                              if (response.ok) {
+                                toast.success("Demoted User");
+                                window.location.reload();
+                              } else {
+                                toast.error("Error while demoting user");
+                              }
+                            }}
                           >
                             Remove as mod
                           </DropdownItem>
@@ -402,12 +461,85 @@ export default function PostCard({
                             key="promote-admin"
                             startContent={<ShieldAlert />}
                             description="Promote user to Admin"
+                            onPress={async () => {
+                              const response = await fetch(
+                                process.env.NEXT_PUBLIC_MODE === "PROD"
+                                  ? "https://d2jam.com/api/v1/mod"
+                                  : "http://localhost:3005/api/v1/mod",
+                                {
+                                  body: JSON.stringify({
+                                    targetname: post.author.slug,
+                                    admin: true,
+                                    username: getCookie("user"),
+                                  }),
+                                  method: "POST",
+                                  headers: {
+                                    "Content-Type": "application/json",
+                                    authorization: `Bearer ${getCookie(
+                                      "token"
+                                    )}`,
+                                  },
+                                  credentials: "include",
+                                }
+                              );
+
+                              if (response.ok) {
+                                toast.success("Promoted User to Admin");
+                                window.location.reload();
+                              } else {
+                                toast.error(
+                                  "Error while promoting user to Admin"
+                                );
+                              }
+                            }}
                           >
                             Appoint as admin
                           </DropdownItem>
                         ) : (
                           <></>
                         )}
+                        {user?.admin &&
+                        post.author.admin &&
+                        post.author.id !== user.id ? (
+                          <DropdownItem
+                            key="demote-admin"
+                            startContent={<ShieldX />}
+                            description="Demote user to mod"
+                            onPress={async () => {
+                              const response = await fetch(
+                                process.env.NEXT_PUBLIC_MODE === "PROD"
+                                  ? "https://d2jam.com/api/v1/mod"
+                                  : "http://localhost:3005/api/v1/mod",
+                                {
+                                  body: JSON.stringify({
+                                    targetname: post.author.slug,
+                                    mod: true,
+                                    username: getCookie("user"),
+                                  }),
+                                  method: "POST",
+                                  headers: {
+                                    "Content-Type": "application/json",
+                                    authorization: `Bearer ${getCookie(
+                                      "token"
+                                    )}`,
+                                  },
+                                  credentials: "include",
+                                }
+                              );
+
+                              if (response.ok) {
+                                toast.success("Demoted User to Mod");
+                                window.location.reload();
+                              } else {
+                                toast.error("Error while demoting user to mod");
+                              }
+                            }}
+                          >
+                            Remove as admin
+                          </DropdownItem>
+                        ) : (
+                          <></>
+                        )}
                       </DropdownSection>
                     ) : (
                       <></>