From 91f02cb90592b191c678ac8a0be3500d1ad2ef19 Mon Sep 17 00:00:00 2001 From: Danh Date: Wed, 1 Nov 2023 10:26:04 +0700 Subject: [PATCH 1/3] fix can not query campaign --- src/pages/Campaign/index.tsx | 15 ++++++++++++--- src/services/campaign.ts | 7 +++++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/pages/Campaign/index.tsx b/src/pages/Campaign/index.tsx index 7f073f2521..46070b2a99 100644 --- a/src/pages/Campaign/index.tsx +++ b/src/pages/Campaign/index.tsx @@ -1,7 +1,7 @@ import { useCallback, useEffect, useState } from 'react' import { useDispatch, useSelector } from 'react-redux' import { useLocation, useNavigate } from 'react-router-dom' -import { useGetCampaignsQuery, useGetLeaderboardQuery } from 'services/campaign' +import { useGetCampaignsQuery, useGetLeaderboardQuery, useLazyGetCampaignByIdQuery } from 'services/campaign' import { APP_PATHS, CAMPAIGN_LEADERBOARD_ITEM_PER_PAGE, EMPTY_ARRAY } from 'constants/index' import { useActiveWeb3React } from 'hooks' @@ -90,6 +90,7 @@ export default function CampaignsUpdater() { dispatch(setCampaignDataByPage({ campaigns: campaignData || [], isReset: queryParams.offset === 0 })) }, [campaignData, dispatch, queryParams.offset]) + const [getCampaignById] = useLazyGetCampaignByIdQuery() useEffect(() => { if (!currentCampaigns?.length) return const navigateFirsOne = () => { @@ -102,12 +103,20 @@ export default function CampaignsUpdater() { return } const selectedCampaign = currentCampaigns.find(campaign => campaign.id.toString() === selectedCampaignId) + if (selectedCampaign) { dispatch(setSelectedCampaign({ campaign: selectedCampaign })) } else { - navigateFirsOne() + getCampaignById(selectedCampaignId) + .unwrap() + .then(data => { + dispatch(setSelectedCampaign({ campaign: data })) + }) + .catch(() => { + navigateFirsOne() + }) } - }, [currentCampaigns, dispatch, selectedCampaignId, navigate]) + }, [currentCampaigns, dispatch, selectedCampaignId, navigate, getCampaignById]) useEffect(() => { if (isLoadingCampaignData === false) dispatch(setLoadingCampaignData(isLoadingCampaignData)) diff --git a/src/services/campaign.ts b/src/services/campaign.ts index e7440c4ffa..2884a31def 100644 --- a/src/services/campaign.ts +++ b/src/services/campaign.ts @@ -209,6 +209,12 @@ const campaignApi = createApi({ }), transformResponse: (data: any) => formatListCampaign(data?.data || []), }), + getCampaignById: builder.query({ + query: campaignId => ({ + url: `/${campaignId}`, + }), + transformResponse: (data: any) => formatListCampaign(data?.data ? [data.data] : [])?.[0], + }), getLeaderboard: builder.query< any, { pageSize: number; pageNumber: number; userAddress: string; lookupAddress: string; campaignId: number } @@ -255,6 +261,7 @@ export const { useGetLuckyWinnersQuery, useJoinCampaignMutation, useGetTxsCampaignQuery, + useLazyGetCampaignByIdQuery, } = campaignApi export default campaignApi From bc3015cd9927b5dbcc27f51c5e5ff2b4970653c7 Mon Sep 17 00:00:00 2001 From: Danh Date: Wed, 1 Nov 2023 10:35:40 +0700 Subject: [PATCH 2/3] chore refactor --- src/pages/Campaign/index.tsx | 16 +++++++--------- src/services/campaign.ts | 6 ++++-- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/pages/Campaign/index.tsx b/src/pages/Campaign/index.tsx index 46070b2a99..83cb03c5cb 100644 --- a/src/pages/Campaign/index.tsx +++ b/src/pages/Campaign/index.tsx @@ -106,16 +106,14 @@ export default function CampaignsUpdater() { if (selectedCampaign) { dispatch(setSelectedCampaign({ campaign: selectedCampaign })) - } else { - getCampaignById(selectedCampaignId) - .unwrap() - .then(data => { - dispatch(setSelectedCampaign({ campaign: data })) - }) - .catch(() => { - navigateFirsOne() - }) + return } + getCampaignById(selectedCampaignId) + .unwrap() + .then(data => { + dispatch(setSelectedCampaign({ campaign: data })) + }) + .catch(navigateFirsOne) }, [currentCampaigns, dispatch, selectedCampaignId, navigate, getCampaignById]) useEffect(() => { diff --git a/src/services/campaign.ts b/src/services/campaign.ts index 2884a31def..5afc204014 100644 --- a/src/services/campaign.ts +++ b/src/services/campaign.ts @@ -36,7 +36,9 @@ const formatRewards = (rewards: CampaignLeaderboardReward[]) => }), ) || [] -const formatListCampaign = (response: CampaignData[]) => { +const formatListCampaign = (data: CampaignData[]) => { + if (!data) return [] + const response = Array.isArray(data) ? data : [data] const campaigns: CampaignData[] = response.map((item: CampaignData) => ({ ...item, startTime: item.startTime * 1000, @@ -213,7 +215,7 @@ const campaignApi = createApi({ query: campaignId => ({ url: `/${campaignId}`, }), - transformResponse: (data: any) => formatListCampaign(data?.data ? [data.data] : [])?.[0], + transformResponse: (data: any) => formatListCampaign(data?.data)?.[0], }), getLeaderboard: builder.query< any, From 00e7d5b3a6e7105d3cf8abad66e952a18ad6169c Mon Sep 17 00:00:00 2001 From: Danh Date: Wed, 1 Nov 2023 11:47:09 +0700 Subject: [PATCH 3/3] prevent duplicate --- src/state/campaigns/reducer.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/state/campaigns/reducer.ts b/src/state/campaigns/reducer.ts index e9d12693dc..1bb05e7bfc 100644 --- a/src/state/campaigns/reducer.ts +++ b/src/state/campaigns/reducer.ts @@ -69,9 +69,15 @@ export default createReducer(initialState, builder => } }) .addCase(setCampaignDataByPage, (state, { payload: { campaigns, isReset } }) => { + const oldData = state.data + const newData = isReset + ? campaigns + : oldData.some(e => e.id === campaigns[0]?.id) + ? oldData + : oldData.concat(campaigns) return { ...state, - data: isReset ? campaigns : state.data.concat(campaigns), + data: newData, } }) .addCase(setLoadingCampaignData, (state, { payload: loading }) => {