Skip to content

Commit

Permalink
calc page offset in cloud function
Browse files Browse the repository at this point in the history
* use pageNum param instead of offset
  • Loading branch information
jermay committed Apr 13, 2021
1 parent e378578 commit 47bb150
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 12 deletions.
10 changes: 2 additions & 8 deletions moralis-scan/src/hooks/pagination.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ export const usePagination = (
) => {
const [pageSize, setPageSize] = useState(10);
const [currPage, setCurrPage] = useState(1);
const [offset, setOffset] = useState(0);
const [totalPages, setTotalPages] = useState(1);
const [numResults, setNumResults] = useState(0);
const queryOptions = useMemo(()=> ({
Expand All @@ -36,10 +35,10 @@ export const usePagination = (
params: {
userAddress,
pageSize,
offset,
pageNum: currPage,
},
postProcess: options.postProcess,
}), [userAddress, pageSize, offset, options.postProcess])
}), [userAddress, pageSize, currPage, options.postProcess])
const { data, error, loading } = useMoralisCloudQuery(methodName, queryOptions);

useEffect(() => {
Expand All @@ -50,11 +49,6 @@ export const usePagination = (
}
}, [data, pageSize]);

useEffect(() => {
const nextOffset = (currPage - 1) * pageSize;
setOffset(nextOffset);
}, [currPage, pageSize]);

const nextPage = () => {
if (currPage >= totalPages) {
// already at the last page
Expand Down
11 changes: 7 additions & 4 deletions moralis-scan/src/queries/cloud-functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import Moralis from "moralis";

// Cloud Functions- copy these into the Moralis server instance
Moralis.Cloud.define("getTransactions", async (request) => {
const { userAddress, pageSize = 10, offset = 0 } = request.params;
const {userAddress, pageSize, pageNum } = request.params;
const offset = (pageNum - 1) * pageSize;

const fromQuery = new Moralis.Query("EthTransactions");
fromQuery.equalTo("from_address", userAddress);

Expand All @@ -11,17 +13,18 @@ Moralis.Cloud.define("getTransactions", async (request) => {

const query = Moralis.Query.or(fromQuery, toQuery);
query.descending("block_number");
query.limit(pageSize);
query.withCount();
if (offset) {
query.skip(offset);
}
query.limit(pageSize);
query.withCount();

return query.find();
});

Moralis.Cloud.define("getTokenTranfers", async (request) => {
const { userAddress, pageSize = 10, offset = 0 } = request.params;
const {userAddress, pageSize, pageNum } = request.params;
const offset = (pageNum - 1) * pageSize;
const output = {
results: [],
count: 0,
Expand Down

0 comments on commit 47bb150

Please sign in to comment.