From a20400f6a29b26af0bf51a02a5ddef276df3c6d5 Mon Sep 17 00:00:00 2001 From: Arik Chakma Date: Mon, 15 Apr 2024 18:16:55 +0600 Subject: [PATCH] feat: implement sponsor (#5480) * feat: implement sponsor * wip: add view cookie * Update src/lib/jwt.ts --------- Co-authored-by: Kamran Ahmed --- src/components/PageSponsor.tsx | 24 ++++++++++++++++++++++-- src/lib/jwt.ts | 16 ++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/src/components/PageSponsor.tsx b/src/components/PageSponsor.tsx index 45d149236..4377fab29 100644 --- a/src/components/PageSponsor.tsx +++ b/src/components/PageSponsor.tsx @@ -1,8 +1,9 @@ import { useEffect, useState } from 'react'; -import { httpGet } from '../lib/http'; +import { httpGet, httpPatch, httpPost } from '../lib/http'; import { sponsorHidden } from '../stores/page'; import { useStore } from '@nanostores/react'; import { X } from 'lucide-react'; +import { setViewSponsorCookie } from '../lib/jwt'; export type PageSponsorType = { company: string; @@ -15,6 +16,7 @@ export type PageSponsorType = { }; type V1GetSponsorResponse = { + id?: string; href?: string; sponsor?: PageSponsorType; }; @@ -26,6 +28,8 @@ type PageSponsorProps = { export function PageSponsor(props: PageSponsorProps) { const { gaPageIdentifier } = props; const $isSponsorHidden = useStore(sponsorHidden); + + const [sponsorId, setSponsorId] = useState(null); const [sponsor, setSponsor] = useState(); const loadSponsor = async () => { @@ -59,6 +63,7 @@ export function PageSponsor(props: PageSponsorProps) { } setSponsor(response.sponsor); + setSponsorId(response?.id || null); window.fireEvent({ category: 'SponsorImpression', @@ -69,6 +74,20 @@ export function PageSponsor(props: PageSponsorProps) { }); }; + const clickSponsor = async (sponsorId: string) => { + const { response, error } = await httpPatch<{ status: 'ok' }>( + `${import.meta.env.PUBLIC_API_URL}/v1-view-sponsor/${sponsorId}`, + {}, + ); + + if (error || !response) { + console.error(error); + return; + } + + setViewSponsorCookie(sponsorId); + }; + useEffect(() => { window.setTimeout(loadSponsor); }, []); @@ -85,12 +104,13 @@ export function PageSponsor(props: PageSponsorProps) { target="_blank" rel="noopener sponsored nofollow" className="fixed bottom-[15px] right-[15px] z-50 flex max-w-[350px] bg-white shadow-lg outline-0 outline-transparent" - onClick={() => { + onClick={async () => { window.fireEvent({ category: 'SponsorClick', action: `${company} Redirect`, label: gaLabel || `${gaPageIdentifier} / ${company} Link`, }); + await clickSponsor(sponsorId || ''); }} >