Skip to content

Commit

Permalink
feat(blockslist): state preserving pagination (#225)
Browse files Browse the repository at this point in the history
  • Loading branch information
jagnani73 authored Oct 22, 2024
1 parent b1f66d4 commit f473998
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 39 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { BlocksList as BlocksListComponent } from "./BlocksList";
import { storyAction } from "@/utils/functions";
import { type Meta, type StoryObj } from "@storybook/react";

type Story = StoryObj<typeof BlocksListComponent>;
Expand All @@ -13,5 +14,11 @@ export default meta;
export const BlocksList: Story = {
args: {
chain_name: "eth-mainnet",
actionable_block: (block_height) => storyAction(block_height),
page_number: 0,
page_size: 10,
on_page_change: (updatedPagination) => {
console.log(updatedPagination);
},
},
};
67 changes: 31 additions & 36 deletions src/components/Molecules/Block/BlocksList/BlocksList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ import {
timestampParser,
} from "@covalenthq/client-sdk";
import { type ColumnDef } from "@tanstack/react-table";
import { useCallback, useEffect, useState } from "react";
import { useEffect, useState } from "react";

export const BlocksList: React.FC<BlocksListProps> = ({
chain_name,
page_number = 0,
page_size = 10,
actionable_block = () => null,
on_page_change = () => null,
}) => {
const { goldrushClient } = useGoldRush();
const [errorMessage, setErrorMessage] = useState<string | null>(null);
Expand All @@ -33,41 +35,34 @@ export const BlocksList: React.FC<BlocksListProps> = ({
updateResult(null);
}, [chain_name, page_size]);

const updateResult = useCallback(
async (_pagination: Pagination | null) => {
try {
setMaybeResult(None);
setErrorMessage(null);
const { data, ...error } =
await goldrushClient.BaseService.getBlockHeightsByPage(
chain_name,
timestampParser(new Date(), "YYYY MM DD"),
"2100-01-01",
{
pageNumber: _pagination?.page_number ?? 0,
pageSize: _pagination?.page_size ?? page_size,
},
);
if (error.error) {
throw error;
}
if (!data?.items) {
throw FALLBACK_ERROR;
}
setPagination(data.pagination);
setMaybeResult(new Some(data.items));
} catch (error: GoldRushResponse<null> | any) {
setErrorMessage(error?.error_message ?? DEFAULT_ERROR_MESSAGE);
setMaybeResult(new Some(null));
console.error(error);
const updateResult = async (_pagination: Pagination | null) => {
try {
setMaybeResult(None);
setErrorMessage(null);
const { data, ...error } =
await goldrushClient.BaseService.getBlockHeightsByPage(
chain_name,
timestampParser(new Date(), "YYYY MM DD"),
"2100-01-01",
{
pageNumber: _pagination?.page_number ?? page_number,
pageSize: _pagination?.page_size ?? page_size,
},
);
if (error.error) {
throw error;
}
},
[chain_name],
);

const handleOnChangePagination = (updatedPagination: Pagination) => {
setPagination(updatedPagination);
updateResult(updatedPagination);
if (!data?.items || !data?.pagination) {
throw FALLBACK_ERROR;
}
on_page_change(data.pagination);
setPagination(data.pagination);
setMaybeResult(new Some(data.items));
} catch (error: GoldRushResponse<null> | any) {
setErrorMessage(error?.error_message ?? DEFAULT_ERROR_MESSAGE);
setMaybeResult(new Some(null));
console.error(error);
}
};

const columns: ColumnDef<Block>[] = [
Expand Down Expand Up @@ -149,7 +144,7 @@ export const BlocksList: React.FC<BlocksListProps> = ({
errorMessage={errorMessage}
maybeData={maybeResult}
pagination={pagination}
onChangePaginationHandler={handleOnChangePagination}
onChangePaginationHandler={updateResult}
/>
);
};
9 changes: 6 additions & 3 deletions src/utils/types/molecules.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import {
type DECODED_ACTION,
type DECODED_EVENT_CATEGORY,
} from "../constants/shared.constants";
import { type ActionableType, type TransactionsProps } from "./shared.types";
import type {
ActionableType,
PaginationProps,
TransactionsProps,
} from "./shared.types";
import { type Option } from "@/utils/option";
import type {
GasPricesResponse,
Expand Down Expand Up @@ -60,9 +64,8 @@ export interface BlockDetailsProps {
height: number;
}

export interface BlocksListProps {
export interface BlocksListProps extends PaginationProps {
chain_name: Chain;
page_size?: number;
actionable_block?: (block_height: number | null) => ActionableType;
}

Expand Down
6 changes: 6 additions & 0 deletions src/utils/types/shared.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ export interface PaginationFooterProps {
onChangePaginationHandler: (updatedPagination: Pagination) => void;
}

export interface PaginationProps {
page_size?: number;
page_number?: number;
on_page_change?: (updated_pagination: Pagination) => void;
}

export interface HeadingProps
extends React.DetailedHTMLProps<
React.HTMLAttributes<HTMLHeadingElement>,
Expand Down

0 comments on commit f473998

Please sign in to comment.