Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: can not query campaign #2354

Merged
merged 3 commits into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions src/pages/Campaign/index.tsx
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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 = () => {
Expand All @@ -102,12 +103,18 @@ export default function CampaignsUpdater() {
return
}
const selectedCampaign = currentCampaigns.find(campaign => campaign.id.toString() === selectedCampaignId)

if (selectedCampaign) {
dispatch(setSelectedCampaign({ campaign: selectedCampaign }))
} else {
navigateFirsOne()
return
}
}, [currentCampaigns, dispatch, selectedCampaignId, navigate])
getCampaignById(selectedCampaignId)
.unwrap()
.then(data => {
dispatch(setSelectedCampaign({ campaign: data }))
})
.catch(navigateFirsOne)
}, [currentCampaigns, dispatch, selectedCampaignId, navigate, getCampaignById])

useEffect(() => {
if (isLoadingCampaignData === false) dispatch(setLoadingCampaignData(isLoadingCampaignData))
Expand Down
11 changes: 10 additions & 1 deletion src/services/campaign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -209,6 +211,12 @@ const campaignApi = createApi({
}),
transformResponse: (data: any) => formatListCampaign(data?.data || []),
}),
getCampaignById: builder.query<any, string>({
query: campaignId => ({
url: `/${campaignId}`,
}),
transformResponse: (data: any) => formatListCampaign(data?.data)?.[0],
}),
getLeaderboard: builder.query<
any,
{ pageSize: number; pageNumber: number; userAddress: string; lookupAddress: string; campaignId: number }
Expand Down Expand Up @@ -255,6 +263,7 @@ export const {
useGetLuckyWinnersQuery,
useJoinCampaignMutation,
useGetTxsCampaignQuery,
useLazyGetCampaignByIdQuery,
} = campaignApi

export default campaignApi
8 changes: 7 additions & 1 deletion src/state/campaigns/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,15 @@ export default createReducer<CampaignsState>(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 }) => {
Expand Down
Loading