Skip to content

Commit 51a003a

Browse files
committed
fix: search and pagination
1 parent b5aabf7 commit 51a003a

File tree

4 files changed

+29
-53
lines changed

4 files changed

+29
-53
lines changed

src/ui/components/Pagination/Pagination.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React from 'react';
22
import './Pagination.css';
33

44
export default function Pagination({ currentPage, totalItems = 0, itemsPerPage, onPageChange }) {
5-
// Calculate the total number of pages
5+
66
const totalPages = Math.ceil(totalItems / itemsPerPage);
77

88
const handlePageClick = (page) => {

src/ui/components/Search/Search.jsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React, { useState } from 'react';
22
import PropTypes from 'prop-types';
3-
import './Search.css'; // Import the CSS file
3+
import './Search.css';
44

55
export default function Search({ placeholder = 'Search...', onSearch }) {
66
const [query, setQuery] = useState('');
@@ -15,7 +15,7 @@ export default function Search({ placeholder = 'Search...', onSearch }) {
1515

1616
const handleKeyDown = (event) => {
1717
if (event.key === 'Enter') {
18-
handleSearch(); // Trigger search on Enter
18+
handleSearch();
1919
}
2020
};
2121

@@ -26,8 +26,8 @@ export default function Search({ placeholder = 'Search...', onSearch }) {
2626
placeholder={placeholder}
2727
value={query}
2828
onChange={handleInputChange}
29-
onKeyDown={handleKeyDown} // Add keydown event
30-
className="search-input" // Apply the class
29+
onKeyDown={handleKeyDown}
30+
className="search-input"
3131
/>
3232
<button onClick={handleSearch} className="search-button">
3333
Search

src/ui/views/RepoList/Components/Repositories.jsx

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,10 @@ export default function Repositories(props) {
2424
const [, setAuth] = useState(true);
2525
const [isLoading, setIsLoading] = useState(false);
2626
const [isError, setIsError] = useState(false);
27-
const [currentPage, setCurrentPage] = useState(1); // Pagination state
28-
const itemsPerPage = 5; // Set items per page
27+
const [currentPage, setCurrentPage] = useState(1);
28+
const itemsPerPage = 5;
2929
const navigate = useNavigate();
3030
const { user } = useContext(UserContext);
31-
3231
const openRepo = (repo) => navigate(`/admin/repo/${repo}`, { replace: true });
3332

3433
useEffect(() => {
@@ -39,7 +38,7 @@ export default function Repositories(props) {
3938
}
4039
getRepos(setIsLoading, (data) => {
4140
setData(data);
42-
setFilteredData(data); // Set both data and filteredData
41+
setFilteredData(data);
4342
}, setAuth, setIsError, query);
4443
}, [props]);
4544

@@ -50,7 +49,7 @@ export default function Repositories(props) {
5049
};
5150

5251
const handleSearch = (query) => {
53-
setCurrentPage(1); // Reset to page 1 on new search
52+
setCurrentPage(1);
5453
if (!query) {
5554
setFilteredData(data);
5655
} else {
@@ -63,10 +62,7 @@ export default function Repositories(props) {
6362
);
6463
}
6564
};
66-
const handlePageChange = (page) => setCurrentPage(page); // Update current page
67-
68-
69-
// Calculate items for the current page
65+
const handlePageChange = (page) => setCurrentPage(page);
7066
const startIdx = (currentPage - 1) * itemsPerPage;
7167
const paginatedData = filteredData.slice(startIdx, startIdx + itemsPerPage);
7268

@@ -86,13 +82,13 @@ export default function Repositories(props) {
8682
key='x'
8783
classes={classes}
8884
openRepo={openRepo}
89-
data={paginatedData} // Use filteredData here
85+
data={paginatedData}
9086
repoButton={addrepoButton}
91-
onSearch={handleSearch} // Pass handleSearch
92-
currentPage={currentPage} // Pass current page
93-
totalItems={filteredData.length} // Pass total items for pagination
94-
itemsPerPage={itemsPerPage} // Pass items per page
95-
onPageChange={handlePageChange} // Pass page change handler
87+
onSearch={handleSearch}
88+
currentPage={currentPage}
89+
totalItems={filteredData.length}
90+
itemsPerPage={itemsPerPage}
91+
onPageChange={handlePageChange}
9692
/>
9793
);
9894
}
@@ -114,7 +110,7 @@ function GetGridContainerLayOut(props) {
114110
<GridContainer>
115111
{props.repoButton}
116112
<GridItem xs={12} sm={12} md={12}>
117-
{/* Add Search component */}
113+
118114
<Search onSearch={props.onSearch} />
119115

120116
<TableContainer

src/ui/views/UserList/Components/UserList.jsx

Lines changed: 12 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,9 @@ import styles from '../../../assets/jss/material-dashboard-react/views/dashboard
1515
import { getUsers } from '../../../services/user';
1616
import Pagination from '../../../components/Pagination/Pagination';
1717
import { CloseRounded, Check, KeyboardArrowRight } from '@material-ui/icons';
18-
1918
import Search from '../../../components/Search/Search';
2019

2120

22-
// const useStyles = makeStyles({
23-
// table: {
24-
// minWidth: 650,
25-
// },
26-
// });
2721

2822
const useStyles = makeStyles(styles);
2923

@@ -34,21 +28,12 @@ export default function UserList(props) {
3428
const [, setAuth] = useState(true);
3529
const [isLoading, setIsLoading] = useState(false);
3630
const [isError, setIsError] = useState(false);
37-
38-
39-
// Pagination states
40-
const [currentPage, setCurrentPage] = useState(1);
41-
const itemsPerPage = 5;
42-
31+
const [currentPage, setCurrentPage] = useState(1);
32+
const itemsPerPage = 5;
4333
const [searchQuery, setSearchQuery] = useState('');
4434

4535

4636

47-
48-
// const openUser = (username) => navigate(`/admin/user/${username}`, { replace: true });
49-
50-
51-
5237
useEffect(() => {
5338
const query = {};
5439

@@ -63,37 +48,32 @@ export default function UserList(props) {
6348
if (isError) return <div>Something went wrong...</div>;
6449

6550

66-
67-
68-
69-
// Filter users based on the search query
70-
const filteredUsers = data.filter(user =>
51+
const filteredUsers = data.filter(user =>
7152
user.displayName && user.displayName.toLowerCase().includes(searchQuery.toLowerCase()) ||
7253
user.username && user.username.toLowerCase().includes(searchQuery.toLowerCase())
7354
);
74-
// Calculate the items for the current page
75-
const indexOfLastItem = currentPage * itemsPerPage;
76-
const indexOfFirstItem = indexOfLastItem - itemsPerPage;
77-
const currentItems = filteredUsers.slice(indexOfFirstItem, indexOfLastItem);
78-
const totalItems = filteredUsers.length;
7955

80-
// Function to handle page change
56+
const indexOfLastItem = currentPage * itemsPerPage;
57+
const indexOfFirstItem = indexOfLastItem - itemsPerPage;
58+
const currentItems = filteredUsers.slice(indexOfFirstItem, indexOfLastItem);
59+
const totalItems = filteredUsers.length;
60+
61+
8162
const handlePageChange = (page) => {
8263
setCurrentPage(page);
8364
};
8465

8566

86-
// Function to handle search
8767
const handleSearch = (query) => {
8868
setSearchQuery(query);
89-
setCurrentPage(1); // Reset to the first page when searching
69+
setCurrentPage(1);
9070
};
9171

9272
return (
9373
<GridContainer>
9474
<GridItem xs={12} sm={12} md={12}>
9575

96-
{/* Search Component */}
76+
9777
<Search onSearch={handleSearch} placeholder="Search users..." />
9878

9979
<TableContainer component={Paper}>
@@ -135,7 +115,7 @@ const totalItems = filteredUsers.length;
135115
</Table>
136116
</TableContainer>
137117

138-
{/* Pagination Component */}
118+
139119
<Pagination
140120
currentPage={currentPage}
141121
totalItems={totalItems}

0 commit comments

Comments
 (0)