Skip to content

Commit

Permalink
fix: Add unsubscribe functionality to NewsletterModal (elie222#209)
Browse files Browse the repository at this point in the history
- Pass the `mutate` prop from parent component to the `NewsletterModal` component
- Uses the `useUnsubscribeButton` hook to manage the newsletter unsubscription
- On clicking the unsubscribe button from the modal, calls the `onUnsubscribe` function from the `useUnsubscribeButton` hook

Fixes elie222#209
  • Loading branch information
aryanprince committed Aug 11, 2024
1 parent 23504a2 commit 44be712
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 9 deletions.
13 changes: 8 additions & 5 deletions apps/web/app/(app)/bulk-unsubscribe/BulkUnsubscribeSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -223,11 +223,14 @@ export function BulkUnsubscribeSection(props: {
</LoadingContent>
)}
</Card>
<NewsletterModal
newsletter={openedNewsletter}
onClose={() => setOpenedNewsletter(undefined)}
refreshInterval={props.refreshInterval}
/>
{openedNewsletter && (
<NewsletterModal
newsletter={openedNewsletter}
onClose={() => setOpenedNewsletter(undefined)}
refreshInterval={props.refreshInterval}
mutate={mutate}
/>
)}
<PremiumModal />
</>
);
Expand Down
34 changes: 30 additions & 4 deletions apps/web/app/(app)/stats/NewsletterModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,46 @@ import { SectionHeader } from "@/components/Typography";
import { EmailList } from "@/components/email-list/EmailList";
import type { ThreadsResponse } from "@/app/api/google/threads/controller";
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs";
import { Button } from "@/components/ui/button";
import { Button, ButtonLoader } from "@/components/ui/button";
import { getGmailFilterSettingsUrl } from "@/utils/url";
import { Tooltip } from "@/components/Tooltip";
import { AlertBasic } from "@/components/Alert";
import { onAutoArchive } from "@/utils/actions/client";
import { MoreDropdown } from "@/app/(app)/bulk-unsubscribe/common";
import {
MoreDropdown,
useUnsubscribeButton,
} from "@/app/(app)/bulk-unsubscribe/common";
import { useLabels } from "@/hooks/useLabels";
import { Row } from "@/app/(app)/bulk-unsubscribe/types";
import { usePremium } from "@/components/PremiumAlert";
import { usePostHog } from "posthog-js/react";

export function NewsletterModal(props: {
newsletter?: Pick<Row, "name" | "lastUnsubscribeLink" | "autoArchived">;
newsletter: Pick<Row, "name" | "lastUnsubscribeLink" | "autoArchived">;
onClose: (isOpen: boolean) => void;
refreshInterval?: number;
mutate?: () => Promise<any>;
}) {
const { newsletter, refreshInterval, onClose } = props;
const mutate = props.mutate || (() => Promise.resolve()); // Set a default value for mutate if it's not provided

const session = useSession();
const email = session.data?.user.email;

const { userLabels } = useLabels();

const { hasUnsubscribeAccess, mutate: refetchPremium } = usePremium();

const posthog = usePostHog();

const { unsubscribeLoading, onUnsubscribe } = useUnsubscribeButton({
item: newsletter,
hasUnsubscribeAccess,
mutate,
posthog,
refetchPremium,
});

return (
<Dialog open={!!newsletter} onOpenChange={onClose}>
<DialogContent className="max-h-screen overflow-x-scroll overflow-y-scroll lg:min-w-[880px] xl:min-w-[1280px]">
Expand All @@ -57,8 +76,15 @@ export function NewsletterModal(props: {
href={newsletter.lastUnsubscribeLink || undefined}
target="_blank"
rel="noreferrer"
onClick={onUnsubscribe}
>
Unsubscribe
{!unsubscribeLoading && <span>Unsubscribe</span>}
{!!unsubscribeLoading && (
<div className="flex cursor-not-allowed items-center opacity-50">
<ButtonLoader />
<span>Unsubscribing...</span>
</div>
)}
</a>
</Button>
<Tooltip content="Auto archive emails using Gmail filters">
Expand Down

0 comments on commit 44be712

Please sign in to comment.