Skip to content

Commit

Permalink
prompt users to join discord if campaign has ended for exocore
Browse files Browse the repository at this point in the history
  • Loading branch information
Lamperoyge committed May 14, 2024
1 parent 6f50787 commit c87163f
Show file tree
Hide file tree
Showing 5 changed files with 187 additions and 85 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { Masonry } from "@mui/lab";
import { Box } from "@mui/material";
import { useMemo } from "react";
import IndividualQuestComponent from "./IndividualQuestComponent";
import { useDiscordConnect } from "./utils/hooks";
import { SharedSecondaryButton } from "components/Shared/styles";
import { shouldDisplayJoinDiscordButton } from "./utils/customization";

const EndedCampaignPrompt = ({ orgId }) => {
const { isMember, handleJoinDiscord } = useDiscordConnect({
orgId,
});

const handleClick = () => {
return handleJoinDiscord({
shouldVerify: false,
});
};

return (
<Box height="100%">
<SharedSecondaryButton onClick={handleClick}>{isMember ? "Visit Discord" : "Join Discord"}</SharedSecondaryButton>
</Box>
);
};

const ReferralAction = ({ quests, onStartQuest, hasEnded = false, orgId }) => {
const masonryColumnsConfig = useMemo(() => {
if (quests?.length <= 1) {
return { xs: 1 };
} else if (quests?.length <= 2) {
return { xs: 1, sm: 2 };
}
return { xs: 1, sm: 2, md: 2, lg: 3 };
}, [quests?.length]);

const showDiscordButton = shouldDisplayJoinDiscordButton(orgId);
if (hasEnded && showDiscordButton) return <EndedCampaignPrompt orgId={orgId} />;
return (
<Box
bgcolor="#AF9EFF"
height="100%"
padding="32px 56px"
display="flex"
width="100%"
flex="1"
justifyContent="center"
alignItems="flex-start"
>
<Masonry
spacing={4}
columns={masonryColumnsConfig}
sx={{
alignContent: "center",
width: "100%",
}}
>
{quests?.map((quest, index) => (
<Box
width="100%"
display="flex"
justifyContent="center"
sx={{
maxWidth: "350px",
}}
>
<IndividualQuestComponent quest={quest} key={index} onStartQuest={onStartQuest} />
</Box>
))}
</Masonry>
</Box>
);
};

export default ReferralAction;
72 changes: 26 additions & 46 deletions wondrous-bot-admin/src/components/StartReferralQuests/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,20 @@ import useStartQuest from "./utils/hooks";
import SafeImage from "components/SafeImage";
import InactiveQuestInfoModal from "./InactiveQuest";
import moment from "moment";
import ReferralAction from "./ReferralAction";
import { shouldDisplayJoinDiscordButton } from "./utils/customization";

const TopBarMessage = ({ displayName, hasEnded }) => {
if (hasEnded) return <>This campaign has ended, but you can still complete the quests below!</>;
const TopBarMessage = ({ displayName, hasEnded, orgId }) => {
const showDiscordButton = shouldDisplayJoinDiscordButton(orgId);

if (hasEnded)
return (
<>
{showDiscordButton
? "This campaign is no longer active, but join our Discord to discover more quests!"
: "This campaign has ended, but you can still complete the quests below!"}
</>
);
return (
<>
<strong>{displayName}</strong> referred you, complete the quests below!
Expand Down Expand Up @@ -68,15 +79,6 @@ const StartReferralQuests = ({ referralCampaign, referralCode, referralCampaignE
const quests = referralCampaign?.quests || [];
const rewardsValue = constructRewards({ rewards: referredRewards || [] });

const masonryColumnsConfig = useMemo(() => {
if (quests?.length <= 1) {
return { xs: 1 };
} else if (quests?.length <= 2) {
return { xs: 1, sm: 2 };
}
return { xs: 1, sm: 2, md: 2, lg: 3 };
}, [quests?.length]);

const referralBannerImage = referralCampaign?.media?.[0]?.slug;

const endDate = referralCampaign?.endDate ? moment(referralCampaign?.endDate) : null;
Expand Down Expand Up @@ -110,7 +112,6 @@ const StartReferralQuests = ({ referralCampaign, referralCode, referralCampaignE
display="flex"
width="calc(100% - 40px)"
position="sticky"
flex="1 0 0"
alignItems="center"
justifyContent="center"
zIndex="10"
Expand All @@ -134,7 +135,11 @@ const StartReferralQuests = ({ referralCampaign, referralCode, referralCampaignE
>
<Box />
<Typography color="black" fontSize="14px" fontFamily="Poppins" fontWeight={500} textAlign={"center"}>
<TopBarMessage hasEnded={hasEnded} displayName={referralCodeInfo?.referrerDisplayName} />
<TopBarMessage
orgId={referralCampaign?.orgId}
hasEnded={hasEnded}
displayName={referralCodeInfo?.referrerDisplayName}
/>
</Typography>
<ButtonBase
onClick={() => setDisplayReferrer(false)}
Expand Down Expand Up @@ -222,7 +227,7 @@ const StartReferralQuests = ({ referralCampaign, referralCode, referralCampaignE
{org?.description}
</Typography>
) : null}
{referredRewards?.length ? (
{referredRewards?.length && !hasEnded ? (
<Box display="flex" gap="14px" alignItems="center" justifyContent="center" flexDirection="column">
<Typography fontFamily="Poppins" fontSize="13px" color="black" fontWeight={500}>
Earn rewards for completing these quests!
Expand All @@ -234,38 +239,13 @@ const StartReferralQuests = ({ referralCampaign, referralCode, referralCampaignE
) : null}
{/* //TODO: REPLACE THIS WITH NEWEST CARD */}

<Box
bgcolor="#AF9EFF"
height="100%"
padding="32px 56px"
display="flex"
width="100%"
flex="1"
justifyContent="center"
alignItems="flex-start"
>
<Masonry
spacing={4}
columns={masonryColumnsConfig}
sx={{
alignContent: "center",
width: "100%",
}}
>
{quests?.map((quest, index) => (
<Box
width="100%"
display="flex"
justifyContent="center"
sx={{
maxWidth: "350px",
}}
>
<IndividualQuestComponent quest={quest} key={index} onStartQuest={onStartQuest} />
</Box>
))}
</Masonry>
</Box>

<ReferralAction
hasEnded={hasEnded}
orgId={referralCampaign?.orgId}
quests={quests}
onStartQuest={onStartQuest}
/>
</Grid>
</>
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { EXOCORE_ORG_ID } from "utils/constants";

export const shouldDisplayJoinDiscordButton = (orgId) => {
return [EXOCORE_ORG_ID, '113598456040259806'].includes(orgId);
};
118 changes: 79 additions & 39 deletions wondrous-bot-admin/src/components/StartReferralQuests/utils/hooks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,72 @@ interface IProps {
discordUrlParams: any;
}

export const useDiscordConnect = ({ orgId, discordUrlParams = {} }) => {
const [getOrgDiscordInviteLink, { data: orgDiscordInviteLinkData }] = useLazyQuery(GET_ORG_DISCORD_INVITE_LINK);

const { setSnackbarAlertMessage, setSnackbarAlertOpen } = useAlerts();
const [isDiscordConneceted, setIsDiscordConnected] = useState(false);
const [isMember, setIsMember] = useState(false);
const [isConnectionLoading, setIsConnectionLoading] = useState(false);

const handleOnCheckMembershipCompleted = async (data) => {
if (data?.checkCmtyUserGuildMembership?.isMember !== isMember) {
setIsMember(data?.checkCmtyUserGuildMembership?.isMember);
}
if (data?.checkCmtyUserGuildMembership?.isMember) {
setIsConnectionLoading(false);
}
};

const [checkCmtyUserGuildMembership, { startPolling: membershipStartPolling, stopPolling, data: membershipData }] =
useLazyQuery(CHECK_CMTY_USER_GUILD_MEMBERSHIP, {
notifyOnNetworkStatusChange: true,
fetchPolicy: "no-cache",
nextFetchPolicy: "no-cache",
onCompleted: handleOnCheckMembershipCompleted,
});

const handleJoinDiscord = async ({ shouldVerify } = { shouldVerify: true }) => {
setIsConnectionLoading(true);
try {
const { data } = await getOrgDiscordInviteLink({
variables: {
orgId: orgId,
},
});
const inviteLink = data?.getOrgDiscordInviteLink?.link;
window.open(inviteLink);
shouldVerify && membershipStartPolling(1000);
} catch (error) {
setIsConnectionLoading(false);
setSnackbarAlertMessage("Something went wrong. Please try again.");
}
};

const handleOnConnect = () => {
setIsConnectionLoading(true);
const discordAuthUrl = getDiscordUrl(
"/discord/callback/cmty-user-connect",
`&state=${encodeURIComponent(JSON.stringify(discordUrlParams))}`
);
window.open(discordAuthUrl);
};

return {
isDiscordConneceted,
isMember,
isConnectionLoading,
handleJoinDiscord,
handleOnConnect,
orgDiscordInviteLinkData,
setIsConnectionLoading,
handleOnCheckMembershipCompleted,
setIsDiscordConnected,
setIsMember,
getOrgDiscordInviteLink,
};
};

const useStartQuest = ({
setInfoModalQuestId,
orgId,
Expand All @@ -33,22 +99,22 @@ const useStartQuest = ({
discordUrlParams,
}: IProps) => {
const [isQuestInactive, setIsQuestInactive] = useState(false);
const [isDiscordConneceted, setIsDiscordConnected] = useState(false);
const [isMember, setIsMember] = useState(false);
const [isConnectionLoading, setIsConnectionLoading] = useState(false);

const [getOrgDiscordInviteLink, { data: orgDiscordInviteLinkData }] = useLazyQuery(GET_ORG_DISCORD_INVITE_LINK);

const { setSnackbarAlertMessage } = useAlerts();

const handleOnCheckMembershipCompleted = async (data) => {
if (data?.checkCmtyUserGuildMembership?.isMember !== isMember) {
setIsMember(data?.checkCmtyUserGuildMembership?.isMember);
}
if (data?.checkCmtyUserGuildMembership?.isMember) {
setIsConnectionLoading(false);
}
};
const {
isDiscordConneceted,
isMember,
isConnectionLoading,
handleJoinDiscord,
handleOnConnect,
orgDiscordInviteLinkData,
setIsConnectionLoading,
handleOnCheckMembershipCompleted,
setIsDiscordConnected,
setIsMember,
getOrgDiscordInviteLink,
} = useDiscordConnect({ orgId, discordUrlParams });

const [checkCmtyUserGuildMembership, { startPolling: membershipStartPolling, stopPolling, data: membershipData }] =
useLazyQuery(CHECK_CMTY_USER_GUILD_MEMBERSHIP, {
Expand Down Expand Up @@ -211,32 +277,6 @@ const useStartQuest = ({
return () => window.removeEventListener("storage", onStorageChange);
}, []);

const handleJoinDiscord = async () => {
setIsConnectionLoading(true);
try {
const { data } = await getOrgDiscordInviteLink({
variables: {
orgId: orgId,
},
});
const inviteLink = data?.getOrgDiscordInviteLink?.link;
window.open(inviteLink);
membershipStartPolling(1000);
} catch (error) {
setIsConnectionLoading(false);
setSnackbarAlertMessage("Something went wrong. Please try again.");
}
};

const handleOnConnect = () => {
setIsConnectionLoading(true);
const discordAuthUrl = getDiscordUrl(
"/discord/callback/cmty-user-connect",
`&state=${encodeURIComponent(JSON.stringify(discordUrlParams))}`
);
window.open(discordAuthUrl);
};

const handleInfoModalClose = () => {
setInfoModalQuestId(null);
stopPolling?.();
Expand Down
2 changes: 2 additions & 0 deletions wondrous-bot-admin/src/utils/constants.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -556,3 +556,5 @@ export const LOCKED_PATHS = ["/store", "/store/items/create", "/store/items/:id"
export const LOCAL_STORAGE_ORG_ID_KEY = "default-org-id";

export const ALLOWLIST_ADMIN_IDS = ["46108748309069853", "54694658413953389"];

export const EXOCORE_ORG_ID = '113885254110937338'

0 comments on commit c87163f

Please sign in to comment.