<ButtonGroup
spacing="3"
justifyContent="space-between"
width={{ base: "full", md: "auto" }}
variant="secondary"
>
// check if the first array data in collection are
// the same with the first data in current userData array
// hide the prev button if the data are exactly the same
{checkFirst === checkEmail ? (
<></>
) : (
<Button onClick={() => getUserData("prev", first)}>
Previous
</Button>
)}
//check userData.length. if less than limit, hide the next button
{userData.length !== 10 ? (
<></>
) : (
<Button onClick={() => getUserData("next", getQuery)}>
Next
</Button>
)}
</ButtonGroup>
To fully understand the flow, the explanation will start from the third step, because the third step will load first when we access the page for the first time. let's start shall we
Getting initial data
// creating arr
let arr = [];
// query data from firestore
const q = query(
collection(db, "users"), // define collection
orderBy("name_user", "asc"), // set data order by field and index
limit(10) // limit the data being called from database
);
// get data
const querySnapshot = await getDocs(q);
// get last doc from query
const lastVisible3 = querySnapshot.docs[querySnapshot.docs.length - 1];
//get first doc from query
const firstVisible3 = querySnapshot.docs[querySnapshot.docs.length - 10];
// push data from query to arr
querySnapshot.forEach((doc) => {
arr.push(doc.data());
});
// set first data from curent query
setFirst(firstVisible3);
// set last data from curent query
setGetQuery(lastVisible3);
// set data from the very first doc in the very first query
// for pagination condition purpose
setCheckFirst(arr[0].email_user);
// set data to be use for showing the data into the table
setUserData(arr);
// set data checker from the first doc in curent query
// for pagination condition purpose
setCheckEmail(arr[0].email_user);
// return the data from the function
return arr;
is it confusing? well, if isn't, you can carry on into the next step. But, if you are confuse, let's talk a little bit more about it.
at first, we are preparing object to hold the data that we're gonna get from the database. latter on, this object will be used to show the data into the table.
let arr = [];
after that, we make data query from firestore collection
const q = query(
collection(db, "users"), // define collection
orderBy("name_user", "asc"), // set data order by field and index
limit(10) // limit the data being called from database
);
// get data
const querySnapshot = await getDocs(q);
here's the important part, we're going to define the first and last doc. where we gonna use both?
// get last doc from query
const lastVisible3 = querySnapshot.docs[querySnapshot.docs.length - 1];
//get first doc from query
const firstVisible3 = querySnapshot.docs[querySnapshot.docs.length - 10];
lastVisible will be use to get data for the next page, and firstVisible will be use to get data for the perv page
next, we need to push data that we got from database into arr object that we made earlier
// push data from query to arr
querySnapshot.forEach((doc) => {
arr.push(doc.data());
});
then, we set the data that we have into state
// set first data from curent query
setFirst(firstVisible3);
// set last data from curent query
setGetQuery(lastVisible3);
// set data from the very first doc in the very first query
// for pagination condition purpose
setCheckFirst(arr[0].email_user);
// set data to be use for showing the data into the table
setUserData(arr);
// set data checker from the first doc in curent query
// for pagination condition purpose
setCheckEmail(arr[0].email_user);
that's it, we have comleted the first step. Next, we will create the condition for the next and prev query.
Next Page
in this step, the first thing we are going to do is checking the status being thrown
if (status === "next") {
. . .
if the status match the condition, it will run the query for the next page that are being load
here, we're gonna use the 2nd parameter that being thrown, value
the rest are pretty the same with before
Setup Next and Prev Button
import {
Button,
ButtonGroup,
} from "@chakra-ui/react";
<ButtonGroup
spacing="3"
justifyContent="space-between"
width={{ base: "full", md: "auto" }}
variant="secondary"
>
// check if the first array data in collection are
// the same with the first data in current userData array
// hide the prev button if the data are exactly the same
{checkFirst === checkEmail ? (
<></>
) : (
<Button onClick={() => getUserData("prev", first)}>
Previous
</Button>
)}
//check userData.length. if less than limit, hide the next button
{userData.length !== 10 ? (
<></>
) : (
<Button onClick={() => getUserData("next", getQuery)}>
Next
</Button>
)}
</ButtonGroup>