From 8798958f3d8a60b3bf6f7ceefbc73d55b9719232 Mon Sep 17 00:00:00 2001 From: Zhang Ziqing <69516975+ziqing26@users.noreply.github.com> Date: Mon, 31 Oct 2022 15:42:51 +0800 Subject: [PATCH] [offers][feat] integrate isSaved API (#475) --- .../offersSubmission/OffersProfileSave.tsx | 43 ++++++++---- .../offers/profile/ProfileHeader.tsx | 69 +++++++++++++++---- .../pages/offers/profile/[offerProfileId].tsx | 8 ++- .../router/offers/offers-profile-router.ts | 39 ++++++----- 4 files changed, 113 insertions(+), 46 deletions(-) diff --git a/apps/portal/src/components/offers/offersSubmission/OffersProfileSave.tsx b/apps/portal/src/components/offers/offersSubmission/OffersProfileSave.tsx index a2f9f089..597c08cc 100644 --- a/apps/portal/src/components/offers/offersSubmission/OffersProfileSave.tsx +++ b/apps/portal/src/components/offers/offersSubmission/OffersProfileSave.tsx @@ -1,3 +1,4 @@ +import { signIn, useSession } from 'next-auth/react'; import { useState } from 'react'; import { DocumentDuplicateIcon } from '@heroicons/react/20/solid'; import { BookmarkSquareIcon, CheckIcon } from '@heroicons/react/24/outline'; @@ -20,6 +21,7 @@ export default function OffersProfileSave({ const { showToast } = useToast(); const { event: gaEvent } = useGoogleAnalytics(); const [isSaved, setSaved] = useState(false); + const { data: session, status } = useSession(); const saveMutation = trpc.useMutation( ['offers.user.profile.addToUserProfile'], @@ -32,7 +34,10 @@ export default function OffersProfileSave({ }); }, onSuccess: () => { - setSaved(true); + trpcContext.invalidateQueries([ + 'offers.profile.isSaved', + { profileId, userId: session?.user?.id }, + ]); showToast({ title: `Saved to your dashboard!`, variant: 'success', @@ -41,16 +46,30 @@ export default function OffersProfileSave({ }, ); + const isSavedQuery = trpc.useQuery( + [`offers.profile.isSaved`, { profileId, userId: session?.user?.id }], + { + onSuccess: (res) => { + setSaved(res); + }, + }, + ); + + const trpcContext = trpc.useContext(); const handleSave = () => { - saveMutation.mutate({ - profileId, - token: token as string, - }); - gaEvent({ - action: 'offers.profile_submission_save_to_profile', - category: 'engagement', - label: 'Save to profile in profile submission', - }); + if (status === 'unauthenticated') { + signIn(); + } else { + saveMutation.mutate({ + profileId, + token: token as string, + }); + gaEvent({ + action: 'offers.profile_submission_save_to_profile', + category: 'engagement', + label: 'Save to profile in profile submission', + }); + } }; return ( @@ -99,9 +118,9 @@ export default function OffersProfileSave({