Skip to content

⚡️⚡️⚡️ Self-hosted collaborative bookmark manager to collect, read, annotate, and fully preserve what matters, all in one place.

Table & Delete modal

Isaac Wise3572e101fb3e
diff --git a/components/ModalContent/DeleteRssSubscriptionModal.tsx b/components/ModalContent/DeleteRssSubscriptionModal.tsx
new file mode 100644
index 00000000..e5c45f28
--- /dev/null
+++ b/components/ModalContent/DeleteRssSubscriptionModal.tsx
@@ -0,0 +1,56 @@
+import React, { useEffect, useState } from "react";
+import Modal from "../Modal";
+import Button from "../ui/Button";
+import { useTranslation } from "next-i18next";
+import toast from "react-hot-toast";
+import { RssSubscription } from "@prisma/client";
+import { useDeleteRssSubscription } from "@/hooks/store/rss";
+
+type Props = {
+ onClose: Function;
+ rssSubscription: RssSubscription;
+};
+
+export default function DeleteRssSubscriptionModal({ onClose, rssSubscription }: Props) {
+ const { t } = useTranslation();
+ const [subscription, setSubscription] = useState<RssSubscription>(rssSubscription);
+ const deleteRssSubscription = useDeleteRssSubscription();
+
+ useEffect(() => {
+ setSubscription(rssSubscription);
+ }, []);
+
+ const submit = async () => {
+ const load = toast.loading(t("deleting"));
+
+ await deleteRssSubscription.mutateAsync(subscription.id, {
+ onSettled: (_, error) => {
+ toast.dismiss(load);
+
+ if (error) {
+ toast.error(error.message);
+ } else {
+ onClose();
+ toast.success(t("rss_subscription_deleted"));
+ }
+ },
+ });
+ };
+
+ return (
+ <Modal toggleModal={onClose}>
+ <p className="text-xl font-thin text-red-500">{t("delete_link")}</p>
+
+ <div className="divider mb-3 mt-1"></div>
+
+ <div className="flex flex-col gap-3">
+ <p>{t("rss_deletion_confirmation")}</p>
+
+ <Button className="ml-auto" intent="destructive" onClick={submit}>
+ <i className="bi-trash text-xl" />
+ {t("delete")}
+ </Button>
+ </div>
+ </Modal>
+ );
+}
diff --git a/components/SettingsSidebar.tsx b/components/SettingsSidebar.tsx
index bfc1ed7a..3305f383 100644
--- a/components/SettingsSidebar.tsx
+++ b/components/SettingsSidebar.tsx
@@ -19,18 +19,16 @@ export default function SettingsSidebar({ className }: { className?: string }) {
return (
<div
- className={`bg-base-100 h-full w-64 overflow-y-auto border-solid border border-base-100 border-r-neutral-content p-5 z-20 flex flex-col gap-5 justify-between ${
- className || ""
- }`}
+ className={`bg-base-100 h-full w-64 overflow-y-auto border-solid border border-base-100 border-r-neutral-content p-5 z-20 flex flex-col gap-5 justify-between ${className || ""
+ }`}
>
<div className="flex flex-col gap-1">
<Link href="/settings/account">
<div
- className={`${
- active === "/settings/account"
- ? "bg-primary/20"
- : "hover:bg-neutral/20"
- } duration-100 py-5 px-2 cursor-pointer flex items-center gap-2 w-full rounded-md h-8`}
+ className={`${active === "/settings/account"
+ ? "bg-primary/20"
+ : "hover:bg-neutral/20"
+ } duration-100 py-5 px-2 cursor-pointer flex items-center gap-2 w-full rounded-md h-8`}
>
<i className="bi-person text-primary text-2xl"></i>
<p className="truncate w-full pr-7">{t("account")}</p>
@@ -39,24 +37,34 @@ export default function SettingsSidebar({ className }: { className?: string }) {
<Link href="/settings/preference">
<div
- className={`${
- active === "/settings/preference"
- ? "bg-primary/20"
- : "hover:bg-neutral/20"
- } duration-100 py-5 px-2 cursor-pointer flex items-center gap-2 w-full rounded-md h-8`}
+ className={`${active === "/settings/preference"
+ ? "bg-primary/20"
+ : "hover:bg-neutral/20"
+ } duration-100 py-5 px-2 cursor-pointer flex items-center gap-2 w-full rounded-md h-8`}
>
<i className="bi-sliders text-primary text-2xl"></i>
<p className="truncate w-full pr-7">{t("preference")}</p>
</div>
</Link>
+ <Link href="/settings/rss-subscriptions">
+ <div
+ className={`${active === "/settings/rss-subscriptions"
+ ? "bg-primary/20"
+ : "hover:bg-neutral/20"
+ } duration-100 py-5 px-2 cursor-pointer flex items-center gap-2 w-full rounded-md h-8`}
+ >
+ <i className="bi-rss text-primary text-2xl"></i>
+ <p className="truncate w-full pr-7">RSS Subscriptions</p>
+ </div>
+ </Link>
+
<Link href="/settings/access-tokens">
<div
- className={`${
- active === "/settings/access-tokens"
- ? "bg-primary/20"
- : "hover:bg-neutral/20"
- } duration-100 py-5 px-2 cursor-pointer flex items-center gap-2 w-full rounded-md h-8`}
+ className={`${active === "/settings/access-tokens"
+ ? "bg-primary/20"
+ : "hover:bg-neutral/20"
+ } duration-100 py-5 px-2 cursor-pointer flex items-center gap-2 w-full rounded-md h-8`}
>
<i className="bi-key text-primary text-2xl"></i>
<p className="truncate w-full pr-7">{t("access_tokens")}</p>
@@ -65,11 +73,10 @@ export default function SettingsSidebar({ className }: { className?: string }) {
<Link href="/settings/password">
<div
- className={`${
- active === "/settings/password"
- ? "bg-primary/20"
- : "hover:bg-neutral/20"
- } duration-100 py-5 px-2 cursor-pointer flex items-center gap-2 w-full rounded-md h-8`}
+ className={`${active === "/settings/password"
+ ? "bg-primary/20"
+ : "hover:bg-neutral/20"
+ } duration-100 py-5 px-2 cursor-pointer flex items-center gap-2 w-full rounded-md h-8`}
>
<i className="bi-lock text-primary text-2xl"></i>
<p className="truncate w-full pr-7">{t("password")}</p>
@@ -79,11 +86,10 @@ export default function SettingsSidebar({ className }: { className?: string }) {
{process.env.NEXT_PUBLIC_STRIPE && !user.parentSubscriptionId && (
<Link href="/settings/billing">
<div
- className={`${
- active === "/settings/billing"
- ? "bg-primary/20"
- : "hover:bg-neutral/20"
- } duration-100 py-5 px-2 cursor-pointer flex items-center gap-2 w-full rounded-md h-8`}
+ className={`${active === "/settings/billing"
+ ? "bg-primary/20"
+ : "hover:bg-neutral/20"
+ } duration-100 py-5 px-2 cursor-pointer flex items-center gap-2 w-full rounded-md h-8`}
>
<i className="bi-credit-card text-primary text-xl"></i>
<p className="truncate w-full pr-7">{t("billing")}</p>
diff --git a/hooks/store/rss.tsx b/hooks/store/rss.tsx
new file mode 100644
index 00000000..82cbbb1b
--- /dev/null
+++ b/hooks/store/rss.tsx
@@ -0,0 +1,68 @@
+import { RssSubscription } from "@prisma/client";
+import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
+import { useSession } from "next-auth/react";
+
+const useRssSubscriptions = () => {
+ const { status } = useSession();
+
+ return useQuery({
+ queryKey: ["rss-subscriptions"],
+ queryFn: async () => {
+ const response = await fetch("/api/v1/user/rss");
+ if (!response.ok) throw new Error("Failed to fetch rss subscriptions.");
+
+ const data = await response.json();
+ return data.response as RssSubscription[];
+ },
+ enabled: status === "authenticated",
+ });
+}
+
+const useAddRssSubscription = () => {
+ const queryClient = useQueryClient();
+
+ return useMutation({
+ mutationFn: async (body: Partial<RssSubscription>) => {
+ const response = await fetch("/api/v1/user/rss", {
+ body: JSON.stringify(body),
+ method: "POST",
+ headers: {
+ "Content-Type": "application/json",
+ },
+ });
+
+ const data = await response.json();
+ if (!response.ok) throw new Error(data.response);
+
+ return data.response;
+ },
+ onSuccess: (data) => {
+ queryClient.setQueryData(["rss-subscriptions"], (oldData: RssSubscription[]) => [
+ ...oldData,
+ data.rssSubscription,
+ ]);
+ },
+ })
+}
+
+const useDeleteRssSubscription = () => {
+ const queryClient = useQueryClient();
+
+ return useMutation({
+ mutationFn: async (rssSubscriptionId: number) => {
+ const response = await fetch(`/api/v1/user/rss/${rssSubscriptionId}`, {
+ method: "DELETE",
+ });
+
+ const data = await response.json();
+ if (!response.ok) throw new Error(data.response);
+ },
+ onSuccess: (_, rssSubscriptionId) => {
+ queryClient.setQueryData(["rss-subscriptions"], (oldData: RssSubscription[]) => {
+ return oldData.filter((rssSubscription) => rssSubscription.id !== rssSubscriptionId);
+ });
+ },
+ });
+}
+
+export { useRssSubscriptions, useAddRssSubscription, useDeleteRssSubscription };
\ No newline at end of file
diff --git a/pages/api/v1/user/rss/[id].ts b/pages/api/v1/user/rss/[id].ts
index 1b7953ac..f29854a7 100644
--- a/pages/api/v1/user/rss/[id].ts
+++ b/pages/api/v1/user/rss/[id].ts
@@ -14,9 +14,9 @@ export default async function handler(
if (req.method === "DELETE") {
const rssSubscription = await prisma.rssSubscription.findUnique({ where: { id: rssId } });
- if (!rssSubscription) return res.status(404).json({ error: "RSS subscription not found." });
+ if (!rssSubscription) return res.status(404).json({ response: "RSS subscription not found." });
- if (rssSubscription.ownerId !== user.id) return res.status(403).json({ error: "Permission denied." });
+ if (rssSubscription.ownerId !== user.id) return res.status(403).json({ response: "Permission denied." });
await prisma.rssSubscription.delete({ where: { id: rssId } });
diff --git a/pages/api/v1/user/rss/index.ts b/pages/api/v1/user/rss/index.ts
index 481bb545..ff7b7337 100644
--- a/pages/api/v1/user/rss/index.ts
+++ b/pages/api/v1/user/rss/index.ts
@@ -28,7 +28,7 @@ export default async function handler(
);
if (collectionIsAccessible?.ownerId !== user.id && !memberHasAccess) {
- return res.status(403).json({ error: "You do not have permission to add a link to this collection" });
+ return res.status(403).json({ response: "You do not have permission to add a link to this collection" });
}
const response = await prisma.rssSubscription.create({
diff --git a/pages/settings/rss-subscriptions.tsx b/pages/settings/rss-subscriptions.tsx
new file mode 100644
index 00000000..c55ea2f6
--- /dev/null
+++ b/pages/settings/rss-subscriptions.tsx
@@ -0,0 +1,81 @@
+import SettingsLayout from "@/layouts/SettingsLayout";
+import { useTranslation } from "next-i18next";
+import getServerSideProps from "@/lib/client/getServerSideProps";
+import { useRssSubscriptions } from "@/hooks/store/rss";
+import DeleteRssSubscriptionModal from "@/components/ModalContent/DeleteRssSubscriptionModal";
+import { useState } from "react";
+import { RssSubscription } from "@prisma/client";
+
+export default function RssSubscriptions() {
+ const { t } = useTranslation();
+ const { data: rssSubscriptions = [] } = useRssSubscriptions();
+
+ const [deleteSubscriptionModal, setDeleteSubscriptionModal] = useState(false);
+ const [selectedSubscription, setSelectedSubscription] = useState<RssSubscription | null>(null);
+
+ const openDeleteModal = (subscription: RssSubscription) => {
+ setSelectedSubscription(subscription);
+ setDeleteSubscriptionModal(true);
+ };
+
+ return (
+ <SettingsLayout>
+ <p className="capitalize text-3xl font-thin inline">
+ {t("rss_subscriptions")}
+ </p>
+
+ <div className="divider my-3"></div>
+ <div className="flex flex-col gap-3">
+ <p>{t("rss_subscriptions_desc")}</p>
+
+ <button
+ className={`btn ml-auto btn-accent dark:border-violet-400 text-white tracking-wider w-fit flex items-center gap-2`}
+ // onClick={() => {
+ // setNewTokenModal(true);
+ // }}
+ >
+ {t("new_rss_subscription")}
+ </button>
+ {rssSubscriptions.length > 0 && (
+ <table className="table mt-2 overflow-x-auto">
+ <thead>
+ <tr>
+ <th>{t("name")}</th>
+ <th>{t("link")}</th>
+ <th></th>
+ </tr>
+ </thead>
+ <tbody>
+ {rssSubscriptions.map((rssSubscription, i) => (
+ <tr key={i}>
+ <td>{rssSubscription.name}</td>
+ <td>{rssSubscription.url}</td>
+ <td>
+ <button
+ className="btn btn-sm btn-ghost btn-square hover:bg-red-500"
+ onClick={() => openDeleteModal(rssSubscription)}
+
+ >
+ <i className="bi-x text-lg"></i>
+ </button>
+ </td>
+ </tr>
+ ))}
+ </tbody>
+ </table>
+ )}
+ </div>
+ {deleteSubscriptionModal && selectedSubscription && (
+ <DeleteRssSubscriptionModal
+ onClose={() => {
+ setDeleteSubscriptionModal(false);
+ setSelectedSubscription(null);
+ }}
+ rssSubscription={selectedSubscription}
+ />
+ )}
+ </SettingsLayout>
+ );
+}
+
+export { getServerSideProps };
diff --git a/public/locales/en/common.json b/public/locales/en/common.json
index 256bfff0..e0de17d3 100644
--- a/public/locales/en/common.json
+++ b/public/locales/en/common.json
@@ -394,7 +394,7 @@
"upload_preview_image": "Upload Preview Image",
"columns": "Columns",
"default": "Default",
- "invalid_url_guide":"Please enter a valid Address for the Link. (It should start with http/https)",
+ "invalid_url_guide": "Please enter a valid Address for the Link. (It should start with http/https)",
"email_invalid": "Please enter a valid email address.",
"username_invalid_guide": "Username has to be at least 3 characters, no spaces and special characters are allowed.",
"team_management": "Team Management",
@@ -429,5 +429,10 @@
"reddit": "Reddit",
"lemmy": "Lemmy",
"people_recommendation": "Recommendation (Friend, Family, etc.)",
- "open_all_links": "Open all Links"
+ "open_all_links": "Open all Links",
+ "rss_subscriptions": "RSS Subscriptions",
+ "rss_subscriptions_desc": "RSS Subscriptions are a way to keep up with your favorite websites and blogs. Linkwarden will automatically fetch the latest articles from the feeds you provide.",
+ "rss_deletion_confirmation": "Are you sure you want to delete this RSS Subscription?",
+ "new_rss_subscription": "New RSS Subscription",
+ "rss_subscription_deleted": "RSS Subscription deleted!"
}
\ No newline at end of file