Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add pagination to members page #820

Merged
merged 3 commits into from
Aug 2, 2022

Conversation

SankhaChak
Copy link
Contributor

  • Increased limit for initial query
  • Restricted refetch query limit to 20
  • Introduced pagination
  • UI changes - Show All button text changed to Show More

Screenshots -

Before -

Screenshot from 2022-07-29 00-24-18

After -

Screenshot from 2022-07-29 00-23-51

@vercel
Copy link

vercel bot commented Jul 28, 2022

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated
wonderverse-web ✅ Ready (Inspect) Visit Preview Sep 1, 2022 at 9:03PM (UTC)
wondrous-app ✅ Ready (Inspect) Visit Preview Sep 1, 2022 at 9:03PM (UTC)

const refetchQueries = [GET_POD_BY_ID];
const showEmptyState = podUserMembershipRequests?.length === 0;
const initialLoad = useRef(true);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we actually need the ref here? I think we can use this without the ref and without showShowMoreButton state

we can do this: hasMore && podUserMembershipRequests ? <ShowMoreBtn ... /> : <... />

This way I think we can remove the ref and the additional state, let me know if this makes sense

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since we're using different limits for the initial query and refetch, the custom hook uses the initial limit to decide if there are more data, thus using the hasMore variable from the hook would not give us the correct data.

The ref here is used to prevent the data being lost during re-renders.

Does that make sense ?

Copy link
Collaborator

@juniusfree juniusfree Jul 30, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SankhaChak Please correct me if I'm wrong. To set the initial value of 'showShowMoreButton,' we can do the following:
const [showShowMoreButton, setShowShowMoreButton] = useState(hasMore);

As a result, we can remove the 'useRef' and also the 'useEffect' below.

Let's wait for adrian's comment, though.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe not, since the component does re-render a couple of times and the first time the hook is called with undefined id, the hasMore value is returned false which is then preserved across the re-renders and not changed even if the hasMore value changes.

Let me know if that makes sense

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, got it. 😅 I don't know if you tried this, but this works when I tested it even without using useRef.

useEffect(() => { setShowShowMoreButton(hasMore); }, [hasMore]);

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so I played a little with your hook and did few tests and it seems to work as expected: https://www.toptal.com/developers/hastebin/yapuveqiwe.js

so basically you set the state inside the hook and then check previousData from the query, if data exists then it means it's a refetch, else it's first mount. You can use this to check the LIMIT you want to use. then you can remove the initialLoad, showMoreButton state and also the initialLoad ref. Then in your component just check for the hasMore from the hook

Let me know what you think @SankhaChak @juniusfree

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const useGetOrgMemberRequests = (orgId) => { const [hasMore, setHasMore] = useState(false); const [getOrgUserMembershipRequests, { data, fetchMore, previousData }] = useLazyQuery(GET_ORG_MEMBERSHIP_REQUEST, { onCompleted: ({ getOrgMembershipRequest }) => { const limitToRefer = previousData ? REFETCH_QUERY_LIMIT : QUERY_LIMIT; setHasMore(getOrgMembershipRequest?.length >= limitToRefer); }, }); useEffect(() => { if (orgId) { getOrgUserMembershipRequests({ variables: { orgId, limit: QUERY_LIMIT, }, }); } }, [orgId, getOrgUserMembershipRequests]); // const hasMore = data?.getOrgMembershipRequest?.length >= QUERY_LIMIT; return { data: data?.getOrgMembershipRequest, fetchMore, hasMore }; }; also leave this here, tho the hastebin link is more readable imo

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if previousData is undefined it means it's the first time the query is called so we basically hook to this

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like a great solution to me @Lamperoyge

Added a few changes to this -
the getOrgMembershipRequest value returned in the onCompleted callback was containing all the requests till that point, so had to create another variable to calculate the updated data length.

Code -

const updatedDataLength = previousData ? getOrgMembershipRequest?.length - previousData?.getOrgMembershipRequest?.length : getOrgMembershipRequest?.length;

This has one issue though, let's say the last fetchMore returns exactly the number of requests as the limit, the next requests' onCompleted callback doesn't run for some reason.

I'm trying to debug this, let me know if you know any solution for the same

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have made the necessary changes. Thank you @Lamperoyge and @juniusfree for your valuable feedback, got to learn quite a lot while going through this PR :)

Copy link
Collaborator

@juniusfree juniusfree left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are some of my observations. Please let me know if you have questions.

Copy link
Collaborator

@juniusfree juniusfree left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm 👍👍

@terryli0095 terryli0095 merged commit b8e8097 into staging Aug 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants