Skip to content

Commit

Permalink
WEB-25: Add delete announcement route
Browse files Browse the repository at this point in the history
  • Loading branch information
vinnie4k committed Dec 12, 2024
1 parent 0a8f967 commit 5aef4e4
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 11 deletions.
29 changes: 22 additions & 7 deletions src/components/announcement/announcementModal.tsx
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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<boolean>(false);
const [showEndAlert, setShowEndAlert] = useState<boolean>(false);
const [isLoading, setIsLoading] = useState<boolean>(false);

if (!announcement) return null;

Expand All @@ -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);
}
};

Expand Down Expand Up @@ -76,7 +87,7 @@ export default function AnnouncementModal({ onClose, announcement }: Announcemen
<div className="flex flex-col gap-1">
<div className="flex flex-row items-center justify-between gap-1">
<h4 className="text-neutral-800 break-all">{announcement.title}</h4>
<button className="size-[24px] opacity-hover" onClick={onClose}>
<button className="size-[24px] opacity-hover" onClick={() => onClose(false)}>
<X className="size-[24px] fill-neutral-400" />
</button>
</div>
Expand Down Expand Up @@ -106,9 +117,13 @@ export default function AnnouncementModal({ onClose, announcement }: Announcemen
</div>

{dateInRange(new Date(announcement.startDate), new Date(announcement.endDate), new Date()) ? (
<ButtonPrimary2 text="End Live Announcement" action={() => setShowEndAlert(true)} />
<ButtonPrimary2 text="End Live Announcement" action={() => setShowEndAlert(true)} isLoading={isLoading} />
) : (
<ButtonSecondary2 text="Delete Announcement" action={() => setShowDeleteAlert(true)} />
<ButtonSecondary2
text="Delete Announcement"
action={() => setShowDeleteAlert(true)}
isLoading={isLoading}
/>
)}
</div>
</div>
Expand Down
12 changes: 10 additions & 2 deletions src/components/landing/landing.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,11 @@ export default function Landing() {
/>
<LandingCreateAnnouncement action={() => setShowForm(true)} />
<LandingUpcomingSection announcements={fetchAnnouncementsQuery.data} />
<LandingActiveSection announcements={fetchAnnouncementsQuery.data} onEditClick={editAnnouncement} />
<LandingActiveSection
announcements={fetchAnnouncementsQuery.data}
onEditClick={editAnnouncement}
onRefetch={() => fetchAnnouncementsQuery.refetch()}
/>
<LandingPastSection announcements={fetchAnnouncementsQuery.data} onEditClick={editAnnouncement} />
</div>
<div className="max-lg:hidden flex flex-col gap-8">
Expand All @@ -87,7 +91,11 @@ export default function Landing() {
<LandingUpcomingSection announcements={fetchAnnouncementsQuery.data} />
</div>
<div className="flex flex-col gap-8 flex-1">
<LandingActiveSection announcements={fetchAnnouncementsQuery.data} onEditClick={editAnnouncement} />
<LandingActiveSection
announcements={fetchAnnouncementsQuery.data}
onEditClick={editAnnouncement}
onRefetch={() => fetchAnnouncementsQuery.refetch()}
/>
<LandingPastSection announcements={fetchAnnouncementsQuery.data} onEditClick={editAnnouncement} />
</div>
</div>
Expand Down
11 changes: 9 additions & 2 deletions src/components/landing/landingActiveSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<Announcement | null>(null);

Expand Down Expand Up @@ -45,7 +46,13 @@ export default function LandingActiveSection({ announcements, onEditClick }: Pro
) : (
<p className="b1 self-stretch text-neutral-400 text-center">{Constants.text.empty}</p>
)}
<AnnouncementModal onClose={closeModal} announcement={selectedAnnouncement} />
<AnnouncementModal
onClose={(refetch) => {
if (refetch) onRefetch();
closeModal();
}}
announcement={selectedAnnouncement}
/>
</div>
);
}

0 comments on commit 5aef4e4

Please sign in to comment.