generated from TreasureProject/web3-frontend-starter-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add pagination and filtering to pool activity table (#52)
* add pagination to pool activity table * fix get transactions filter * hook up pool activity filters
- Loading branch information
1 parent
50c466a
commit 5a5d88a
Showing
11 changed files
with
328 additions
and
204 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { useFetcher } from "@remix-run/react"; | ||
import { useEffect, useRef, useState } from "react"; | ||
import type { PoolTransaction } from "~/api/pools.server"; | ||
import type { FetchPoolTransactions } from "~/routes/resources.pools.$id.transactions"; | ||
import type { TransactionType } from ".graphclient"; | ||
|
||
type Props = { | ||
id: string; | ||
type?: TransactionType; | ||
resultsPerPage?: number; | ||
enabled?: boolean; | ||
}; | ||
|
||
const DEFAULT_STATE = { | ||
page: 1, | ||
isLoading: false, | ||
}; | ||
|
||
export const usePoolTransactions = ({ | ||
id, | ||
type, | ||
resultsPerPage = 25, | ||
enabled = true, | ||
}: Props) => { | ||
const { load, state, data } = useFetcher<FetchPoolTransactions>(); | ||
const [{ page, isLoading }, setState] = useState<{ | ||
page: number; | ||
isLoading: boolean; | ||
}>(DEFAULT_STATE); | ||
const lastType = useRef<TransactionType | undefined>(); | ||
const results = data?.ok ? data.results : []; | ||
const hasPreviousPage = page > 1; | ||
const hasNextPage = results.length === resultsPerPage; | ||
|
||
useEffect(() => { | ||
if (enabled) { | ||
if (type !== lastType.current) { | ||
lastType.current = type; | ||
setState(DEFAULT_STATE); | ||
} | ||
|
||
const params = new URLSearchParams({ | ||
page: page.toString(), | ||
resultsPerPage: resultsPerPage.toString(), | ||
}); | ||
|
||
if (type) { | ||
params.set("type", type); | ||
} | ||
|
||
setState((curr) => ({ ...curr, isLoading: true })); | ||
load(`/resources/pools/${id}/transactions?${params.toString()}`); | ||
setState((curr) => ({ ...curr, isLoading: false })); | ||
} else { | ||
setState(DEFAULT_STATE); | ||
} | ||
}, [enabled, id, type, page, resultsPerPage, load]); | ||
|
||
return { | ||
isLoading: isLoading || state === "loading", | ||
results, | ||
page, | ||
resultsPerPage, | ||
hasPreviousPage, | ||
hasNextPage, | ||
goToPreviousPage: () => | ||
setState((curr) => | ||
hasPreviousPage ? { ...curr, page: curr.page - 1 } : curr, | ||
), | ||
goToNextPage: () => | ||
setState((curr) => | ||
hasNextPage ? { ...curr, page: curr.page + 1 } : curr, | ||
), | ||
refetch: () => setState({ ...DEFAULT_STATE, isLoading: true }), | ||
error: !data?.ok ? data?.error : undefined, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.