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

Filter Items on Map and Results Bar #17

Merged
merged 3 commits into from
May 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 7 additions & 16 deletions packages/web/src/components/Map/Map.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ import { UserAuth } from "../../context/AuthContext";

import axios from "axios";

import { filterItem } from '../../utils/Utils.js';

/**
* Map is uses react-leaflet's API to communicate user actions to map entities and information
*
Expand Down Expand Up @@ -124,27 +126,16 @@ export default function Map({
const fuse = new Fuse(data, fuseOptions);
const results = fuse.search(search).map((result) => result.item);

const filterItem = useCallback(
(item) => {
return (
search.toLowerCase() === "" ||
(((findFilter.isLost && item.isLost) ||
(findFilter.isFound && !item.isLost)) &&
(findFilter.type === "everything" || findFilter.type === item.type) &&
(findFilter.uploadDate === "" ||
(item.itemDate && item.itemDate.includes(findFilter.uploadDate))) &&
(!findFilter.isYourPosts || item.email === user.email) &&
(findFilter.isShowReturned || !item.isResolved))
);
},
[search, findFilter, user]
const filterItemCallback = useCallback(
(item) => filterItem(item, findFilter, user),
[findFilter, user]
);

const markersData = results.length > 0 ? results : data;
const allMarkers = markersData.filter(filterItem).map((item) => {
const allMarkers = markersData.filter(filterItemCallback).map((item) => {
return (
<Marker
key={item.location}
key={item.key}
position={item.location}
eventHandlers={{
click: () => {
Expand Down
24 changes: 7 additions & 17 deletions packages/web/src/components/ResultsBar/ResultsBar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import DataContext from "../../context/DataContext";
import { UserAuth } from "../../context/AuthContext";
import Fuse from "fuse.js";

import { filterItem } from '../../utils/Utils.js';

export default function ResultsBar({
search,
findFilter,
Expand All @@ -18,21 +20,9 @@ export default function ResultsBar({

const [itemsonScreenLimit, setItemsOnScreenLimit] = useState(10);

// Define callback function to return filtered items (filtered according to search bar and filter markers)
const filterItem = useCallback(
(item) => {
return (
search.toLowerCase() === "" ||
(((findFilter.isLost && item.isLost) ||
(findFilter.isFound && !item.isLost)) &&
(findFilter.type === "everything" || findFilter.type === item.type) &&
(findFilter.uploadDate === "" ||
(item.itemDate && item.itemDate.includes(findFilter.uploadDate))) &&
(!findFilter.isYourPosts || item.email === user.email) &&
(findFilter.isShowReturned || !item.isResolved))
);
},
[search, findFilter, user]
const filterItemCallback = useCallback(
(item) => filterItem(item, findFilter, user),
[findFilter, user]
);

const mapItem = useCallback(
Expand Down Expand Up @@ -67,8 +57,8 @@ export default function ResultsBar({

let allResults =
search === ""
? data.filter(filterItem).map(mapItem)
: results.filter(filterItem).map(mapItem);
? data.filter(filterItemCallback).map(mapItem)
: results.filter(filterItemCallback).map(mapItem);

// Callback function that increases the number of items displayed on the screen by 10
const handleLoadMore = useCallback(() => {
Expand Down
13 changes: 13 additions & 0 deletions packages/web/src/utils/Utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Define callback function to return filtered items (filtered according to search bar and filter markers)
const filterItem = (item, findFilter, user) => {
return (((findFilter.islost && item.islost) ||
(findFilter.isFound && !item.islost)) &&
(findFilter.type === "everything" || findFilter.type === item.type) &&
(findFilter.uploadDate === "" ||
(item.itemDate && item.itemDate.includes(findFilter.uploadDate))) &&
(!findFilter.isYourPosts || item.email === user.email) &&
(findFilter.isShowReturned || !item.isResolved)
);
};

export { filterItem };