From 5aef4e4b4105a55f76978435f4d11f48c6fad4f9 Mon Sep 17 00:00:00 2001 From: Vin Bui Date: Thu, 12 Dec 2024 10:51:36 -0500 Subject: [PATCH] WEB-25: Add delete announcement route --- .../announcement/announcementModal.tsx | 29 ++++++++++++++----- src/components/landing/landing.tsx | 12 ++++++-- .../landing/landingActiveSection.tsx | 11 +++++-- 3 files changed, 41 insertions(+), 11 deletions(-) diff --git a/src/components/announcement/announcementModal.tsx b/src/components/announcement/announcementModal.tsx index 1945603..3c35dba 100644 --- a/src/components/announcement/announcementModal.tsx +++ b/src/components/announcement/announcementModal.tsx @@ -1,6 +1,7 @@ import AppIcon from "@/icons/appIcon"; import { Announcement } from "@/models/announcement"; import { DateFormat } from "@/models/enums/dateFormat"; +import ApiClient from "@/services/apiClient"; import { useUserStore } from "@/stores/useUserStore"; import { dateInRange, formatDate } from "@/utils/utils"; import { X } from "lucide-react"; @@ -12,16 +13,18 @@ import errorToast from "../system/errorToast"; import AnnouncementBanner from "./announcementBanner"; import AnnouncementIndicator from "./announcementIndicator"; -interface AnnouncementModalProps { - onClose: () => void; +interface Props { + onClose: (refetch: boolean) => void; announcement: Announcement | null; } -export default function AnnouncementModal({ onClose, announcement }: AnnouncementModalProps) { +export default function AnnouncementModal({ onClose, announcement }: Props) { + const apiClient = ApiClient.createInstance(); const { user } = useUserStore(); const [showDeleteAlert, setShowDeleteAlert] = useState(false); const [showEndAlert, setShowEndAlert] = useState(false); + const [isLoading, setIsLoading] = useState(false); if (!announcement) return null; @@ -42,10 +45,18 @@ export default function AnnouncementModal({ onClose, announcement }: Announcemen if (!user) return; try { - console.log("Deleting", announcement); + setIsLoading(true); + ApiClient.setAuthToken(apiClient, user.idToken); + + await ApiClient.delete(apiClient, `/announcements/${announcement.id}`); + + // Successful + setIsLoading(false); + onClose(true); } catch (err) { console.error(err); errorToast(); + setIsLoading(false); } }; @@ -76,7 +87,7 @@ export default function AnnouncementModal({ onClose, announcement }: Announcemen

{announcement.title}

-
@@ -106,9 +117,13 @@ export default function AnnouncementModal({ onClose, announcement }: Announcemen
{dateInRange(new Date(announcement.startDate), new Date(announcement.endDate), new Date()) ? ( - setShowEndAlert(true)} /> + setShowEndAlert(true)} isLoading={isLoading} /> ) : ( - setShowDeleteAlert(true)} /> + setShowDeleteAlert(true)} + isLoading={isLoading} + /> )} diff --git a/src/components/landing/landing.tsx b/src/components/landing/landing.tsx index b6afa1f..e55a1a7 100644 --- a/src/components/landing/landing.tsx +++ b/src/components/landing/landing.tsx @@ -73,7 +73,11 @@ export default function Landing() { /> setShowForm(true)} /> - + fetchAnnouncementsQuery.refetch()} + />
@@ -87,7 +91,11 @@ export default function Landing() {
- + fetchAnnouncementsQuery.refetch()} + />
diff --git a/src/components/landing/landingActiveSection.tsx b/src/components/landing/landingActiveSection.tsx index 0514538..91f25d6 100644 --- a/src/components/landing/landingActiveSection.tsx +++ b/src/components/landing/landingActiveSection.tsx @@ -9,9 +9,10 @@ import AnnouncementModal from "../announcement/announcementModal"; interface Props { announcements?: Announcement[]; onEditClick: (announcement: Announcement) => void; + onRefetch: () => void; } -export default function LandingActiveSection({ announcements, onEditClick }: Props) { +export default function LandingActiveSection({ announcements, onEditClick, onRefetch }: Props) { const activeAnnouncements = sortAnnouncementsByStartDate(filterActiveAnnouncements(announcements ?? [])); const [selectedAnnouncement, setSelectedAnnouncement] = useState(null); @@ -45,7 +46,13 @@ export default function LandingActiveSection({ announcements, onEditClick }: Pro ) : (

{Constants.text.empty}

)} - + { + if (refetch) onRefetch(); + closeModal(); + }} + announcement={selectedAnnouncement} + /> ); }