Skip to content

Commit

Permalink
feat: add pull to refresh for PagedMemoList (#4128)
Browse files Browse the repository at this point in the history
  • Loading branch information
nlfox authored Nov 19, 2024
1 parent 1424036 commit 3cbccde
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 21 deletions.
1 change: 1 addition & 0 deletions web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"react-leaflet": "^4.2.1",
"react-redux": "^9.1.2",
"react-router-dom": "^6.27.0",
"react-simple-pull-to-refresh": "^1.3.3",
"react-use": "^17.5.1",
"tailwind-merge": "^2.5.4",
"tailwindcss": "^3.4.14",
Expand Down
14 changes: 14 additions & 0 deletions web/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 36 additions & 21 deletions web/src/components/PagedMemoList/PagedMemoList.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Button } from "@usememos/mui";
import { ArrowDownIcon, LoaderIcon } from "lucide-react";
import { useEffect, useState } from "react";
import PullToRefresh from "react-simple-pull-to-refresh";
import { DEFAULT_LIST_MEMOS_PAGE_SIZE } from "@/helpers/consts";
import { useMemoList, useMemoStore } from "@/store/v1";
import { Memo } from "@/types/proto/api/v1/memo_service";
Expand Down Expand Up @@ -28,7 +29,11 @@ const PagedMemoList = (props: Props) => {
nextPageToken: "",
});
const sortedMemoList = props.listSort ? props.listSort(memoList.value) : memoList.value;

const handleRefresh = async () => {
memoList.reset();
setState((state) => ({ ...state, nextPageToken: "" }));
fetchMoreMemos("");
};
const setIsRequesting = (isRequesting: boolean) => {
setState((state) => ({ ...state, isRequesting }));
};
Expand All @@ -53,28 +58,38 @@ const PagedMemoList = (props: Props) => {
}, [props.filter, props.pageSize]);

return (
<>
{sortedMemoList.map((memo) => props.renderer(memo))}
{state.isRequesting && (
<div className="w-full flex flex-row justify-center items-center my-4">
<LoaderIcon className="animate-spin text-zinc-500" />
</div>
)}
{!state.isRequesting && state.nextPageToken && (
<PullToRefresh
onRefresh={handleRefresh}
pullingContent={<></>}
refreshingContent={
<div className="w-full flex flex-row justify-center items-center my-4">
<Button variant="plain" onClick={() => fetchMoreMemos(state.nextPageToken)}>
{t("memo.load-more")}
<ArrowDownIcon className="ml-2 w-4 h-auto" />
</Button>
</div>
)}
{!state.isRequesting && !state.nextPageToken && sortedMemoList.length === 0 && (
<div className="w-full mt-12 mb-8 flex flex-col justify-center items-center italic">
<Empty />
<p className="mt-2 text-gray-600 dark:text-gray-400">{t("message.no-data")}</p>
<LoaderIcon className="animate-spin" />
</div>
)}
</>
}
>
<>
{sortedMemoList.map((memo) => props.renderer(memo))}
{state.isRequesting && (
<div className="w-full flex flex-row justify-center items-center my-4">
<LoaderIcon className="animate-spin text-zinc-500" />
</div>
)}
{!state.isRequesting && state.nextPageToken && (
<div className="w-full flex flex-row justify-center items-center my-4">
<Button variant="plain" onClick={() => fetchMoreMemos(state.nextPageToken)}>
{t("memo.load-more")}
<ArrowDownIcon className="ml-2 w-4 h-auto" />
</Button>
</div>
)}
{!state.isRequesting && !state.nextPageToken && sortedMemoList.length === 0 && (
<div className="w-full mt-12 mb-8 flex flex-col justify-center items-center italic">
<Empty />
<p className="mt-2 text-gray-600 dark:text-gray-400">{t("message.no-data")}</p>
</div>
)}
</>
</PullToRefresh>
);
};

Expand Down

0 comments on commit 3cbccde

Please sign in to comment.