Skip to content

Commit

Permalink
Merge pull request #252 from luxfi/dev/newProd
Browse files Browse the repository at this point in the history
completed: paginations
  • Loading branch information
venuswhispers authored Nov 27, 2024
2 parents 1f9b71d + 2ad6ae6 commit 0b92727
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 38 deletions.
74 changes: 40 additions & 34 deletions app/bridge/src/components/SwapHistory/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,6 @@ function TransactionsHistory() {
}
}, [paramString])



const getSwaps = async (page?: number, status?: string | number) => {
try {
const {
Expand All @@ -89,7 +87,7 @@ function TransactionsHistory() {
!showAllSwaps ? `page=${page}` : ''
}${
status ? `&status=${status}` : ''
}&version=${BridgeApiClient.apiVersion}&teleport=${useTeleporter}`
}&version=${BridgeApiClient.apiVersion}&teleport=${useTeleporter}&pageSize=${PAGE_SIZE}`
)
return {
data: data,
Expand All @@ -108,35 +106,43 @@ function TransactionsHistory() {
if (Number(data?.length) > 0) setShowToggleButton(true)
}, [])

const fetchInitialSwaps = async () => {
const { data, error } = await getSwaps(
1,
SwapStatusInNumbers.SwapsWithoutCancelledAndExpired
)
if (error) {
notify(error, 'error')
throw ''
}
setSwaps(data)
setPage(1)
if (Number(data?.length) < PAGE_SIZE) {
setIsLastPage(true)
} else {
setIsLastPage(false)
}
}

const fetchAllSwaps = async () => {
const { data, error } = await getSwaps()
if (error) {
notify(error, 'error')
throw ''
}

setSwaps(data)
setPage(1)
setIsLastPage(true)
}

useAsyncEffect(async () => {
try {
setLoading(true)
if (!showAllSwaps) {
const { data, error } = await getSwaps(
1,
SwapStatusInNumbers.SwapsWithoutCancelledAndExpired
)
if (error) {
notify(error, 'error')
throw ""
}
setSwaps(data)
setPage(1)
if (Number(data?.length) < PAGE_SIZE) {
setIsLastPage(true)
} else {
setIsLastPage(false)
}
fetchInitialSwaps()
} else {
const { data, error } = await getSwaps()
if (error) {
notify(error, 'error')
throw ""
}

setSwaps(data)
setPage(1)
setIsLastPage(true)
fetchAllSwaps()
}
} catch (err) {
//
Expand All @@ -145,8 +151,12 @@ function TransactionsHistory() {
}
}, [showAllSwaps])

useEffect(() => {
setShowAllSwaps (false)
useAsyncEffect(async () => {
if (showAllSwaps) {
setShowAllSwaps(false)
} else {
fetchInitialSwaps()
}
}, [useTeleporter])

const handleLoadMore = async () => {
Expand All @@ -161,18 +171,14 @@ function TransactionsHistory() {

if (error) {
notify(error, 'error')
throw ""
throw ''
}

setSwaps((old) => [...(old ? old : []), ...(data ? data : [])])
setPage(nextPage)
if (Number(data?.length) < PAGE_SIZE) {
setIsLastPage(true)
}
// setSwaps(data)
// setPage(1)
// setIsLastPage(true)
// setLoading(false)
} catch (err) {
//
} finally {
Expand Down
5 changes: 2 additions & 3 deletions app/server/src/lib/swaps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -785,11 +785,10 @@ export async function handlerUpdateMpcSignAction(id: string, txHash: string, amo
* @param isMainnet
* @returns
*/
export async function handlerGetSwaps(isDeleted?: boolean, isMainnet? : boolean, isTeleport?: boolean, page?: number) {
console.log({ isTeleport, page, isMainnet })
export async function handlerGetSwaps(isDeleted?: boolean, isMainnet? : boolean, isTeleport?: boolean, page?: number, pageSize: number = 20) {
console.log(pageSize)
try {
if (page) {
const pageSize = 20
const swaps = await prisma.swap.findMany({
skip: (page - 1) * pageSize,
take: pageSize,
Expand Down
5 changes: 4 additions & 1 deletion app/server/src/routes/swaps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@ router.get("/", async (req: Request, res: Response) => {
// page
const _page = req.query?.page
const page = _page ? (isNaN(Number(_page)) ? 0 : Number(Number(_page))) : undefined
// pageSize
const _pageSize = req.query?.pageSize
const pageSize = _pageSize ? (isNaN(Number(_pageSize)) ? 0 : Number(Number(_pageSize))) : undefined

const result = await handlerGetSwaps(isDeleted, isMainnet, isTeleport, page)
const result = await handlerGetSwaps(isDeleted, isMainnet, isTeleport, page, pageSize)
res.status(200).json({ data: result })
} catch (error: any) {
res.status(500).json({ error: error?.message })
Expand Down

0 comments on commit 0b92727

Please sign in to comment.