Skip to content

Commit

Permalink
handle user single activity
Browse files Browse the repository at this point in the history
  • Loading branch information
corlard3y committed Oct 22, 2024
1 parent 60b6f39 commit 3c745da
Show file tree
Hide file tree
Showing 12 changed files with 152 additions and 44 deletions.
15 changes: 9 additions & 6 deletions src/modules/rewards/components/BonusActivitiesItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { FC } from 'react';

// hooks
import { Activity, useGetRewardsActivity } from 'queries';
import { Activity, StakeActivityResponse, UsersActivity } from 'queries';
import { useAccount } from 'hooks';

// components
Expand All @@ -17,6 +17,9 @@ export type BonusActivitiesItemProps = {
isLoadingItem: boolean;
setErrorMessage: (errorMessage: string) => void;
isLocked: boolean;
allUsersActivity: StakeActivityResponse;
isAllActivitiesLoading: boolean;
refetchActivity: () => void;
};

const BonusActivitiesItem: FC<BonusActivitiesItemProps> = ({
Expand All @@ -25,12 +28,12 @@ const BonusActivitiesItem: FC<BonusActivitiesItemProps> = ({
isLoadingItem,
setErrorMessage,
isLocked,
allUsersActivity,
isAllActivitiesLoading,
refetchActivity,
}) => {
const {
data: usersSingleActivity,
isLoading,
refetch: refetchActivity,
} = useGetRewardsActivity({ userId, activityId: activity.id }, { enabled: !!userId });
const usersSingleActivity = allUsersActivity?.[activity?.activityType] as UsersActivity;
const isLoading = isAllActivitiesLoading;

const { isWalletConnected } = useAccount();

Expand Down
27 changes: 26 additions & 1 deletion src/modules/rewards/components/BonusActivitiesSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ import { FC, useState } from 'react';
import { css } from 'styled-components';

// hooks
import { useGetRewardsActivities, useGetUserRewardsDetails } from 'queries';
import {
StakeActivityResponse,
useGetRewardsActivities,
useGetRewardsActivity,
useGetUserRewardsDetails,
} from 'queries';
import { useAccount } from 'hooks';
import { useRewardsContext } from 'contexts/RewardsContext';

Expand Down Expand Up @@ -41,6 +46,23 @@ const BonusActivities: FC<BonusActivitiesSectionProps> = () => {

const { isLocked } = useRewardsContext();

// Combine all activities into a single array
const allActivities = [...bonusActivities];

// Extract the `activityType` from each activity and filter out any undefined values
const activityTypes = allActivities
.map((activity) => activity.activityType) // Extract `activityType`
.filter(Boolean); // Remove undefined/null values

const {
data: allUsersActivity,
isLoading: isAllActivitiesLoading,
refetch: refetchActivity,
} = useGetRewardsActivity(
{ userId: userDetails?.userId as string, activityTypes: activityTypes },
{ enabled: !!userDetails?.userId && activityTypes.length > 0 }
);

return (
<Box
display="flex"
Expand Down Expand Up @@ -82,6 +104,9 @@ const BonusActivities: FC<BonusActivitiesSectionProps> = () => {
isLoadingItem={isLoading}
setErrorMessage={setErrorMessage}
isLocked={isLocked}
allUsersActivity={allUsersActivity as StakeActivityResponse}
isAllActivitiesLoading={isAllActivitiesLoading}
refetchActivity={refetchActivity}
/>
))}
</Box>
Expand Down
17 changes: 11 additions & 6 deletions src/modules/rewards/components/DailyRewardsSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,19 @@ export type DailyRewardsSectionProps = {};
const DailyRewardsSection: FC<DailyRewardsSectionProps> = () => {
const [errorMessage, setErrorMessage] = useState('');

const { activeItem, activeDay, isActivityDisabled, isLoading, userDetails, dailyRewardsActivities, handleCheckIn } =
useDailyRewards();
const {
activeItem,
activeDay,
isActivityDisabled,
isLoading,
userDetails,
dailyRewardsActivities,
refetchSendActivities,
} = useDailyRewards();

const { isLocked } = useRewardsContext();

const isDailyRewardClaimed = useMemo(() => {
return isActivityDisabled && activeDay > 1 && userDetails;
}, [isActivityDisabled, activeDay, userDetails]);
const isDailyRewardClaimed = isActivityDisabled && activeDay > 1 && userDetails;

const displayDailyRewards = useMemo(() => {
return !isActivityDisabled && activeDay > 0 && activeItem && userDetails;
Expand Down Expand Up @@ -85,7 +90,7 @@ const DailyRewardsSection: FC<DailyRewardsSectionProps> = () => {
activityType={activeItem?.activityType as ActvityType}
userId={userDetails?.userId as string}
activityTypeId={activeItem?.id as string}
refetchActivity={() => handleCheckIn()}
refetchActivity={() => refetchSendActivities()}
setErrorMessage={setErrorMessage}
isLoadingActivity={false}
label="Check In"
Expand Down
34 changes: 33 additions & 1 deletion src/modules/rewards/components/RewardsActivitiesList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ import { css } from 'styled-components';
import { Box, Lock, Text } from 'blocks';
import { useAccount } from 'hooks';
import { walletToCAIP10 } from 'helpers/w2w';
import { Activity, useGetRewardsActivities, useGetUserRewardsDetails } from 'queries';
import {
Activity,
StakeActivityResponse,
useGetRewardsActivities,
useGetRewardsActivity,
useGetUserRewardsDetails,
} from 'queries';
import { useRewardsContext } from 'contexts/RewardsContext';

import { RewardsActivitiesListItem } from './RewardsActivitiesListItem';
Expand Down Expand Up @@ -43,6 +49,23 @@ const RewardsActivitiesList: FC<RewardActivitiesProps> = () => {

const { isLocked } = useRewardsContext();

// Combine all activities into a single array
const allActivities = [...socialActivities, ...platformRewardActivities, ...channelSubscriptionActivities];

// Extract the `activityType` from each activity and filter out any undefined values
const activityTypes = allActivities
.map((activity) => activity.activityType) // Extract `activityType`
.filter(Boolean); // Remove undefined/null values

const {
data: allUsersActivity,
isLoading: isAllActivitiesLoading,
refetch: refetchActivity,
} = useGetRewardsActivity(
{ userId: userDetails?.userId as string, activityTypes: activityTypes },
{ enabled: !!userDetails?.userId && activityTypes.length > 0 }
);

return (
<Box
display="flex"
Expand All @@ -57,6 +80,9 @@ const RewardsActivitiesList: FC<RewardActivitiesProps> = () => {
activity={activity}
isLoadingItem={isLoading}
isLocked={isLocked}
allUsersActivity={allUsersActivity as StakeActivityResponse}
isAllActivitiesLoading={isAllActivitiesLoading}
refetchActivity={refetchActivity}
/>
))}
{(isLocked || !isWalletConnected) && (
Expand Down Expand Up @@ -103,6 +129,9 @@ const RewardsActivitiesList: FC<RewardActivitiesProps> = () => {
activity={activity}
isLoadingItem={isLoading}
isLocked={isLocked}
allUsersActivity={allUsersActivity as StakeActivityResponse}
isAllActivitiesLoading={isAllActivitiesLoading}
refetchActivity={refetchActivity}
/>
))}

Expand All @@ -114,6 +143,9 @@ const RewardsActivitiesList: FC<RewardActivitiesProps> = () => {
activity={activity}
isLoadingItem={isLoading}
isLocked={isLocked}
allUsersActivity={allUsersActivity as StakeActivityResponse}
isAllActivitiesLoading={isAllActivitiesLoading}
refetchActivity={refetchActivity}
/>
))}
</Box>
Expand Down
17 changes: 10 additions & 7 deletions src/modules/rewards/components/RewardsActivitiesListItem.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { FC, useEffect, useMemo, useState } from 'react';

import { Activity, useGetRewardsActivity } from 'queries';
import { Activity, StakeActivityResponse, UsersActivity } from 'queries';
import { useAccount } from 'hooks';

import {
Expand All @@ -27,6 +27,9 @@ export type RewardActivitiesListItemProps = {
activity: Activity;
isLoadingItem: boolean;
isLocked: boolean;
allUsersActivity: StakeActivityResponse;
isAllActivitiesLoading: boolean;
refetchActivity: () => void;
};

const getUpdatedExpiryTime = (timestamp: number) => {
Expand All @@ -46,13 +49,13 @@ const RewardsActivitiesListItem: FC<RewardActivitiesListItemProps> = ({
activity,
isLoadingItem,
isLocked,
allUsersActivity,
isAllActivitiesLoading,
refetchActivity,
}) => {
const { isWalletConnected } = useAccount();
const {
data: usersSingleActivity,
isLoading,
refetch: refetchActivity,
} = useGetRewardsActivity({ userId, activityId: activity.id }, { enabled: !!userId });
const usersSingleActivity = allUsersActivity?.[activity?.activityType] as UsersActivity;
const isLoading = isAllActivitiesLoading;

const [errorMessage, setErrorMessage] = useState('');
const { getLockStatus } = useLockedStatus();
Expand All @@ -73,7 +76,7 @@ const RewardsActivitiesListItem: FC<RewardActivitiesListItemProps> = ({
if (activity.activityType == 'follow_push_on_discord' || activity.activityType == 'follow_push_on_twitter') {
getLockStatus();
}
}, [usersSingleActivity?.status, activity.activityType]);
}, [usersSingleActivity, activity.activityType]);

return (
<Skeleton isLoading={isLoadingItem}>
Expand Down
17 changes: 10 additions & 7 deletions src/modules/rewards/components/StakePushActivitiesListItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { FC } from 'react';

// hooks
import { Activity, useGetRewardsActivity } from 'queries';
import { Activity, StakeActivityResponse, UsersActivity } from 'queries';
import { useAccount } from 'hooks';

// components
Expand All @@ -17,6 +17,9 @@ export type StakeActivitiesItemProps = {
setErrorMessage: (errorMessage: string) => void;
isLocked: boolean;
hasEpochEnded?: boolean;
allUsersActivity: StakeActivityResponse;
isAllActivitiesLoading: boolean;
refetchActivity: () => void;
};
const StakePushActivitiesListItem: FC<StakeActivitiesItemProps> = ({
userId,
Expand All @@ -25,15 +28,15 @@ const StakePushActivitiesListItem: FC<StakeActivitiesItemProps> = ({
setErrorMessage,
isLocked,
hasEpochEnded,
allUsersActivity,
isAllActivitiesLoading,
refetchActivity,
}) => {
const {
data: usersSingleActivity,
isLoading,
refetch: refetchActivity,
} = useGetRewardsActivity({ userId, activityId: activity.id }, { enabled: !!userId });

const { isWalletConnected } = useAccount();

const usersSingleActivity = allUsersActivity?.[activity?.activityType] as UsersActivity;
const isLoading = isAllActivitiesLoading;

const hasActivityEndedUnclaimed = usersSingleActivity?.status !== 'COMPLETED' && hasEpochEnded;

const isLockedOrNotConnected = isLocked || !isWalletConnected;
Expand Down
25 changes: 24 additions & 1 deletion src/modules/rewards/components/StakePushSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { FC, useMemo, useState } from 'react';

// hooks
import { useAccount } from 'hooks';
import { useGetUserRewardsDetails } from 'queries';
import { StakeActivityResponse, useGetRewardsActivity, useGetUserRewardsDetails } from 'queries';
import { useRewardsContext } from 'contexts/RewardsContext';

// helpers
Expand Down Expand Up @@ -46,6 +46,23 @@ const StakePushSection: FC<StakePushPoints> = ({ title, subtitle, timeline, mult
return daysToReset != null && activityResetDate >= 0 && activityResetDate <= 7 && !multiplier && isWalletConnected;
}, [daysToReset, activityResetDate, multiplier, isWalletConnected]);

// Combine all activities into a single array
const allActivities = [...uniV2PushArray, ...stakePushArray];

// Extract the `activityType` from each activity and filter out any undefined values
const activityTypes = allActivities
.map((activity) => activity.activityType) // Extract `activityType`
.filter(Boolean); // Remove undefined/null values

const {
data: allUsersActivity,
isLoading: isAllActivitiesLoading,
refetch: refetchActivity,
} = useGetRewardsActivity(
{ userId: userDetails?.userId as string, activityTypes: activityTypes },
{ enabled: !!userDetails?.userId && activityTypes.length > 0 }
);

return (
<Box
display="flex"
Expand Down Expand Up @@ -165,6 +182,9 @@ const StakePushSection: FC<StakePushPoints> = ({ title, subtitle, timeline, mult
setErrorMessage={setErrorMessage}
isLocked={isLocked}
hasEpochEnded={hasEpochEnded}
allUsersActivity={allUsersActivity as StakeActivityResponse}
isAllActivitiesLoading={isAllActivitiesLoading}
refetchActivity={refetchActivity}
/>
))}
</Box>
Expand All @@ -184,6 +204,9 @@ const StakePushSection: FC<StakePushPoints> = ({ title, subtitle, timeline, mult
setErrorMessage={setErrorMessage}
isLocked={isLocked}
hasEpochEnded={hasEpochEnded}
allUsersActivity={allUsersActivity as StakeActivityResponse}
isAllActivitiesLoading={isAllActivitiesLoading}
refetchActivity={refetchActivity}
/>
))}
</Box>
Expand Down
7 changes: 4 additions & 3 deletions src/modules/rewards/hooks/useDailyRewards.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ const useDailyRewards = () => {
// Check if dailyRewardsActivities is available and all activityTitles are defined
const areActivitiesDefined = dailyRewardsActivities && activityTitles?.every((title) => title !== undefined);

const { data: sendRecentActivities } = useGetRewardActivityStatus(
const { data: sendRecentActivities, refetch: refetchSendActivities } = useGetRewardActivityStatus(
{
userId: userDetails?.userId as string,
activities: activityTitles as string[],
Expand All @@ -71,7 +71,7 @@ const useDailyRewards = () => {
}, []);

// Handle success response from sending recent activities
const handleCheckIn = () => {
const handleCheckIn = useCallback(() => {
if (!sendRecentActivities || Object.keys(sendRecentActivities).length === 0) return;
setIsLoadingRewards(true);

Expand Down Expand Up @@ -99,7 +99,7 @@ const useDailyRewards = () => {
setActiveDay(newDay);
setActiveItem(newDayData);
setIsLoadingRewards(false);
};
}, [sendRecentActivities]);

// Effect for handling check-in when user details change or wallet is connected
useEffect(() => {
Expand All @@ -122,6 +122,7 @@ const useDailyRewards = () => {
userDetails,
dailyRewardsActivities,
handleCheckIn,
refetchSendActivities,
resetState,
};
};
Expand Down
14 changes: 7 additions & 7 deletions src/queries/hooks/rewards/useGetRewardsActivity.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { UseQueryOptions, useQuery } from '@tanstack/react-query';
import { rewardsActivity } from '../../queryKeys';
import { getRewardsActivity } from '../../services/rewards';
import { UsersActivity } from '../../types';
import { StakeActivityResponse } from '../../types';

export const useGetRewardsActivity = (
{ userId, activityId }: { userId: string; activityId: string },
config?: Partial<UseQueryOptions<UsersActivity>>
{ userId, activityTypes }: { userId: string; activityTypes: string[] },
config?: Partial<UseQueryOptions<StakeActivityResponse>>
) =>
useQuery<UsersActivity>({
queryKey: [rewardsActivity, userId, activityId],
queryFn: () => getRewardsActivity(userId, activityId),
...config
useQuery<StakeActivityResponse>({
queryKey: [rewardsActivity, userId, activityTypes],
queryFn: () => getRewardsActivity(userId, activityTypes),
...config,
});
4 changes: 2 additions & 2 deletions src/queries/models/rewards/getRewardsActivityModelCreator.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { UsersActivity } from 'queries/types';
import { StakeActivityResponse } from 'queries/types';

export const getRewardsActivityModelCreator = (response: UsersActivity): UsersActivity => response;
export const getRewardsActivityModelCreator = (response: StakeActivityResponse): StakeActivityResponse => response;
Loading

0 comments on commit 3c745da

Please sign in to comment.