forked from fortran-lang/registry
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request fortran-lang#16 from arteevraina/global-search-redux
feat: Global Search and Pagination
- Loading branch information
Showing
11 changed files
with
338 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { MDBListGroupItem } from "mdb-react-ui-kit"; | ||
import { Row, Col, Image } from "react-bootstrap"; | ||
|
||
const PackageItem = ({ packageEntity }) => { | ||
return ( | ||
<MDBListGroupItem id="list-item"> | ||
<Row> | ||
<Col md={1}> | ||
<Image | ||
src="https://fortran-lang.org/en/_static/fortran-logo-256x256.png" | ||
fluid | ||
width={60} | ||
height={60} | ||
/> | ||
</Col> | ||
<Col md={4}> | ||
<div> | ||
<h5 id="list-item-package-name">{packageEntity.name}</h5> | ||
</div> | ||
<h6 className="mb-2 text-muted">{packageEntity.description}</h6> | ||
<h6 className="mb-2 text-muted"> | ||
Namespace {packageEntity.namespace} | ||
</h6> | ||
</Col> | ||
<Col md={1} style={{ flex: 1 }}> | ||
<h6 | ||
style={{ | ||
textAlign: "right", | ||
}} | ||
> | ||
By {packageEntity.author} | ||
</h6> | ||
</Col> | ||
</Row> | ||
</MDBListGroupItem> | ||
); | ||
}; | ||
|
||
export default PackageItem; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { useDispatch, useSelector } from "react-redux"; | ||
import { | ||
MDBPagination, | ||
MDBPaginationItem, | ||
MDBPaginationLink, | ||
} from "mdb-react-ui-kit"; | ||
import { searchPackage } from "../store/actions/searchActions"; | ||
|
||
const Pagination = ({ currentPage, totalPages }) => { | ||
const query = useSelector((state) => state.search.query); | ||
const orderBy = useSelector((state) => state.search.orderBy); | ||
|
||
const maxVisibleItems = 5; | ||
let startPage = Math.max(currentPage - Math.floor(maxVisibleItems / 2), 1); | ||
let endPage = Math.min(startPage + maxVisibleItems - 1, totalPages); | ||
const dispatch = useDispatch(); | ||
|
||
const handlePageChange = (page) => { | ||
dispatch(searchPackage(query, page, orderBy)); | ||
}; | ||
|
||
return ( | ||
<nav aria-label="Pagination"> | ||
<MDBPagination className="mb-0"> | ||
<MDBPaginationItem | ||
disabled={currentPage + 1 === 1} | ||
onClick={() => handlePageChange(currentPage - 1)} | ||
> | ||
<MDBPaginationLink aria-disabled="true">Previous</MDBPaginationLink> | ||
</MDBPaginationItem> | ||
{Array.from( | ||
{ length: endPage - startPage + 1 }, | ||
(_, i) => i + startPage | ||
).map((page) => ( | ||
<MDBPaginationItem | ||
key={page} | ||
active={currentPage + 1 === page} | ||
onClick={() => handlePageChange(page - 1)} | ||
> | ||
<MDBPaginationLink>{page}</MDBPaginationLink> | ||
</MDBPaginationItem> | ||
))} | ||
<MDBPaginationItem | ||
disabled={currentPage + 1 === totalPages} | ||
onClick={() => handlePageChange(currentPage + 1)} | ||
> | ||
<MDBPaginationLink>Next</MDBPaginationLink> | ||
</MDBPaginationItem> | ||
</MDBPagination> | ||
</nav> | ||
); | ||
}; | ||
|
||
export default Pagination; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,69 @@ | ||
import React, { useState } from "react"; | ||
// import "./search.css"; | ||
import React from "react"; | ||
import { useSelector, useDispatch } from "react-redux"; | ||
import PackageItem from "../components/packageItem"; | ||
import { MDBListGroup } from "mdbreact"; | ||
import Pagination from "../components/pagination"; | ||
import Dropdown from "react-bootstrap/Dropdown"; | ||
import DropdownButton from "react-bootstrap/DropdownButton"; | ||
import { searchPackage, setOrderBy } from "../store/actions/searchActions"; | ||
|
||
const Search = () => { | ||
const [query, setQuery] = useState(""); | ||
const [results, setResults] = useState([]); | ||
const queryString = window.location.search; | ||
const urlParams = new URLSearchParams(queryString); | ||
const packages = useSelector((state) => state.search.packages); | ||
const error = useSelector((state) => state.search.error); | ||
const totalPages = useSelector((state) => state.search.totalPages); | ||
const currentPage = useSelector((state) => state.search.currentPage); | ||
const orderBy = useSelector((state) => state.search.orderBy); | ||
const query = useSelector((state) => state.search.query); | ||
|
||
const handleSubmit = (event) => { | ||
event.preventDefault(); | ||
const url = `${ | ||
process.env.REACT_APP_REGISTRY_API_URL | ||
}/packages?query=${encodeURIComponent(query)}`; | ||
fetch(url) | ||
.then((response) => response.json()) | ||
.then((data) => { | ||
setResults(data["packages"]); | ||
}); | ||
}; | ||
const dropdownOptions = ["None", "Date last updated"]; | ||
|
||
const loadQuery = () => { | ||
console.log("urlParams.get(query)"); | ||
setQuery(urlParams.get("query")); | ||
const url = `${ | ||
process.env.REACT_APP_REGISTRY_API_URL | ||
}/packages?query=${encodeURIComponent(query)}`; | ||
fetch(url) | ||
.then((response) => response.json()) | ||
.then((data) => { | ||
setResults(data["packages"]); | ||
}); | ||
}; | ||
const dispatch = useDispatch(); | ||
|
||
return ( | ||
<div class="container" onLoad={loadQuery}> | ||
fpm-registry Package Search | ||
<form onSubmit={handleSubmit}> | ||
<input | ||
type="text" | ||
id="query" | ||
name="query" | ||
placeholder="Enter your search query" | ||
value={query} | ||
onChange={(event) => setQuery(event.target.value)} | ||
/> | ||
<button type="submit">Search</button> | ||
</form> | ||
<div id="result"> | ||
{results.map((result) => ( | ||
<div> | ||
<h2>{result.name}</h2> | ||
<p>{result.description}</p> | ||
<p>{result.author}</p> | ||
<p>{result.description}</p> | ||
<p>{result.namespace}</p> | ||
</div> | ||
))} | ||
</div> | ||
return error !== null ? ( | ||
<div className="container"> | ||
<div id="result">{error}</div> | ||
</div> | ||
); | ||
) : packages !== null ? ( | ||
packages.length === 0 ? ( | ||
<div className="container"> | ||
<div>No packages found.</div> | ||
</div> | ||
) : ( | ||
<div className="container"> | ||
<br /> | ||
<div className="d-flex justify-content-end" id="dropdown-orderby"> | ||
<label>Order by</label> | ||
<DropdownButton title={orderBy}> | ||
{dropdownOptions.map((option) => ( | ||
<Dropdown.Item | ||
key={option} | ||
onClick={() => { | ||
dispatch(setOrderBy(option)); | ||
dispatch(searchPackage(query, 0, option)); | ||
}} | ||
> | ||
{option} | ||
</Dropdown.Item> | ||
))} | ||
</DropdownButton> | ||
</div> | ||
<MDBListGroup | ||
style={{ | ||
alignItems: "center", | ||
}} | ||
> | ||
{packages.map((packageEntity) => ( | ||
<PackageItem | ||
key={packageEntity.name + packageEntity.namespace} | ||
packageEntity={packageEntity} | ||
/> | ||
))} | ||
<br /> | ||
<Pagination currentPage={currentPage} totalPages={totalPages} /> | ||
</MDBListGroup> | ||
</div> | ||
) | ||
) : null; | ||
}; | ||
|
||
export default Search; |
Oops, something went wrong.