From 6ee1ccf0af9b5f23d2b4b90995ca134b30c5ad7c Mon Sep 17 00:00:00 2001 From: mahesh jamdade Date: Mon, 17 Jun 2024 13:29:09 -0400 Subject: [PATCH] minor fix --- src/app/(main)/_components/Preview.tsx | 1 - src/app/(main)/_components/SideBarItem.tsx | 8 ++++++-- src/app/(main)/_services/logService.ts | 20 +++++++++++++++++--- src/app/(publish)/logs/publish/[id]/page.tsx | 1 + 4 files changed, 24 insertions(+), 6 deletions(-) diff --git a/src/app/(main)/_components/Preview.tsx b/src/app/(main)/_components/Preview.tsx index 3c6ebd0..e31a134 100644 --- a/src/app/(main)/_components/Preview.tsx +++ b/src/app/(main)/_components/Preview.tsx @@ -21,7 +21,6 @@ const Preview = ({ logId }: { logId: string }) => { const [copied, setCopied] = useState(false); const [previewLog, setpreviewLog] = useState(null); const { theme } = useTheme(); - const router = useRouter(); const pathName = usePathname() const isPublishRoute = pathName.includes('/logs/publish'); const { isOpen, onOpen, onClose } = useDisclosure(); diff --git a/src/app/(main)/_components/SideBarItem.tsx b/src/app/(main)/_components/SideBarItem.tsx index 14d3f6f..ad6aab3 100644 --- a/src/app/(main)/_components/SideBarItem.tsx +++ b/src/app/(main)/_components/SideBarItem.tsx @@ -21,12 +21,12 @@ const SidebarItem: React.FC = ({ selected, id, log, onLogClick const { isOpen, onOpen, onClose } = useDisclosure(); function MoreOptions() { - const options = ['Share', 'Delete']; + const options = ['Share', 'Delete', 'Delete Local']; return ( + className="dropdown-class"> @@ -49,6 +49,10 @@ const SidebarItem: React.FC = ({ selected, id, log, onLogClick const logService = new LogService(); switch (key) { case '1': + await logService.deleteLogById(id); + onRefresh(); + break; + case '2': await logService.deleteLogFromLocal(id); onRefresh(); break; diff --git a/src/app/(main)/_services/logService.ts b/src/app/(main)/_services/logService.ts index 785f425..b18ac83 100644 --- a/src/app/(main)/_services/logService.ts +++ b/src/app/(main)/_services/logService.ts @@ -1,6 +1,6 @@ // src/services/LogService.ts -import { addDoc, collection, doc, getDoc, getDocs, updateDoc } from 'firebase/firestore'; +import { addDoc, collection, deleteDoc, doc, getDoc, getDocs, updateDoc } from 'firebase/firestore'; import { db } from '../../../utils/firebase'; import { Log } from '../_models/Log'; @@ -38,15 +38,17 @@ class LogService { try { const docRef = await addDoc(this.logCollection, log.toFirestore()); if (docRef.id) { + console.log("published at", docRef.id); await this.saveLogToLocal({ ...log, id: docRef.id, toFirestore: function () { throw new Error('Function not implemented.'); } }); - return docRef.id; + return docRef.id! } - return ''; + console.log("publish failed"); + return await this.fetchLogFromLocalById(docRef.id) ? docRef.id : ''; } catch (e) { return ''; } @@ -58,6 +60,18 @@ class LogService { await this.saveLogToLocal(log); } + async markExpiredById(id: string): Promise { + const docRef = doc(this.logCollection, id); + await updateDoc(docRef, { isExpired: true }); + await this.deleteLogFromLocal(id); + } + + async deleteLogById(id: string): Promise { + const docRef = doc(this.logCollection, id); + await deleteDoc(docRef); + await this.deleteLogFromLocal(id); + } + async deleteExpiredLogs(): Promise { const querySnapshot = await getDocs(this.logCollection); const updatePromises: Promise[] = []; diff --git a/src/app/(publish)/logs/publish/[id]/page.tsx b/src/app/(publish)/logs/publish/[id]/page.tsx index 12794f4..b6b7e4d 100644 --- a/src/app/(publish)/logs/publish/[id]/page.tsx +++ b/src/app/(publish)/logs/publish/[id]/page.tsx @@ -8,6 +8,7 @@ export const dynamicParams = true; export async function generateStaticParams() { const logService = new LogService(); const logs = await logService.fetchLogs(); + // logs.forEach(log => console.log(log.id)); return logs.map(log => ({ id: log.id })); } export default function PublishPage({ params }: { params: { id: string } }) {