Skip to content

Commit 2222f01

Browse files
Implement pagination on the search page for
users and badges from endpoints Signed-off-by: Olamide Ojo <peterojoolamide@gmail.com>
1 parent c2c395f commit 2222f01

File tree

17 files changed

+125
-53
lines changed

17 files changed

+125
-53
lines changed

frontend/src/features/call.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,12 @@ export const callUnit = createApi({
4343
providesTags: ["AccoList"],
4444
}),
4545
retrieveDiscover: builder.query({
46-
query: (discover) => ({
46+
query: ({ discover, page_badges, page_users, per_page }) => ({
4747
url: `search/${discover}`,
4848
method: "GET",
49+
params: { page_badges, page_users, per_page },
4950
}),
50-
providesTags: (result, error, discover) => [{ type: "Discover", id: discover }],
51+
providesTags: (result, error, { discover }) => [{ type: "Discover", id: discover }],
5152
}),
5253
retrieveRarities: builder.query({
5354
query: (rarities) => `rarities/${rarities}`,

frontend/src/routes/findpage.jsx

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useEffect } from "react";
1+
import { useEffect,useState } from "react";
22
import { Badge, Card, ListGroup } from "react-bootstrap";
33
import { useDispatch, useSelector } from "react-redux";
44
import { useParams } from "react-router";
@@ -12,7 +12,20 @@ import Mistaken from "./mistaken.jsx";
1212
export default function FindPage() {
1313
const dispatch = useDispatch();
1414
const { slugdata: findtext } = useParams();
15-
const { data: dict, isLoading, error } = useRetrieveDiscoverQuery(findtext, { skip: false });
15+
const [badgePage, setBadgePage] = useState(1);
16+
const [userPage, setUserPage] = useState(1);
17+
const { data: dict, isLoading, error } = useRetrieveDiscoverQuery({
18+
discover: findtext,
19+
page_badges: badgePage,
20+
page_users: userPage,
21+
per_page: 10,
22+
},
23+
{skip: false}
24+
);
25+
useEffect(() => {
26+
setBadgePage(1);
27+
setUserPage(1);
28+
}, [findtext]);
1629
const vibe = useSelector((data) => data.area.vibe);
1730

1831
// Show or Hide LoadNote
@@ -52,7 +65,7 @@ export default function FindPage() {
5265
<Card.Title className="mb-0 ps-2 dataelem" style={{ textTransform: "capitalize" }}>
5366
Badges
5467
</Card.Title>
55-
<Card.Text className="mb-0 ps-2 small">Found {dict.badges.length} badge(s)</Card.Text>
68+
<Card.Text className="mb-0 ps-2 small">Found {dict.pagination?.badges_total} badge(s)</Card.Text>
5669
<hr className="mt-2 mb-0" />
5770
<ListGroup variant="flush">
5871
{dict.badges.length > 0 ? (
@@ -74,6 +87,21 @@ export default function FindPage() {
7487
<VertItem head="No badges found" body="Try refining your search" />
7588
)}
7689
</ListGroup>
90+
<div className="d-flex justify-content-between p-2">
91+
<button
92+
disabled={!dict?.pagination?.badges?.has_prev}
93+
onClick={() => setBadgePage((prev) => prev - 1)}
94+
>
95+
Previous
96+
</button>
97+
98+
<button
99+
disabled={!dict?.pagination?.badges?.has_next}
100+
onClick={() => setBadgePage((prev) => prev + 1)}
101+
>
102+
Next
103+
</button>
104+
</div>
77105
</Card.Body>
78106
</Card>
79107
<Card className="mb-2 vibe-border" style={{ "--vibe": vibe }}>
@@ -103,6 +131,21 @@ export default function FindPage() {
103131
<VertItem head="No users found" body="Try refining your search" />
104132
)}
105133
</ListGroup>
134+
<div className="d-flex justify-content-between p-2">
135+
<button
136+
disabled={!dict?.pagination?.users?.has_prev}
137+
onClick={() => setUserPage((prev) => prev - 1)}
138+
>
139+
Previous
140+
</button>
141+
142+
<button
143+
disabled={!dict?.pagination?.users?.has_next}
144+
onClick={() => setUserPage((prev) => prev + 1)}
145+
>
146+
Next
147+
</button>
148+
</div>
106149
</Card.Body>
107150
</Card>
108151
</div>

tahrir/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,4 @@
44

55
import importlib.metadata
66

7-
87
__version__ = importlib.metadata.version("tahrir")

tahrir/admin.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
from .database import db
77

8-
98
admin = Admin(name="Database", url="/dbadmin", endpoint="dbadmin", template_mode="bootstrap4")
109

1110

tahrir/app.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
from tahrir.views import add_static_view, internal_server_error, page_not_found
2424
from tahrir.views import blueprint as root_bp
2525

26-
2726
# Forms
2827
csrf = CSRFProtect()
2928

tahrir/cli.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
from tahrir.database import db
1010
from tahrir.utils.badge import ISSUER
1111

12-
1312
tahrir_cli = AppGroup("tahrir")
1413
"""Commands for the Tahrir application."""
1514

tahrir/defaults.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import os
44

5-
65
BASE_PATH = os.path.dirname(os.path.abspath(__file__))
76

87

tahrir/endpoints/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from flask import Blueprint, jsonify
22

3-
43
blueprint = Blueprint("api", __name__)
54

65

tahrir/endpoints/admin/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from flask import Blueprint, jsonify
22

3-
43
blueprint = Blueprint("admin", __name__)
54

65

tahrir/endpoints/admin/authorizations.py

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,15 @@ def add_authorization():
3131
if not result:
3232
return abort(400, "Failed to add authorization")
3333

34-
return jsonify(
35-
{
36-
"message": f"Badge {data.get(required_fields[0])!r} authorized to {data.get(required_fields[1])!r}"
37-
}
38-
), 201
34+
return (
35+
jsonify(
36+
{
37+
"message": f"Badge {data.get(required_fields[0])!r} /"
38+
f" authorized to {data.get(required_fields[1])!r}"
39+
}
40+
),
41+
201,
42+
)
3943

4044

4145
@bp.route("/api/admin/authorization", methods=["DELETE"])
@@ -64,8 +68,12 @@ def remove_authorization():
6468
if not result:
6569
return abort(404, "Authorization not found or failed to remove")
6670

67-
return jsonify(
68-
{
69-
"message": f"Badge {data.get(required_fields[0])!r} authorization revoked from {data.get(required_fields[1])!r}"
70-
}
71-
), 200
71+
return (
72+
jsonify(
73+
{
74+
"message": f"Badge {data.get(required_fields[0])!r} authorization "
75+
f"revoked from {data.get(required_fields[1])!r}"
76+
}
77+
),
78+
200,
79+
)

0 commit comments

Comments
 (0)