-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ✨ adds filter CANList Pills (Tags) (#2917)
* feat: adds CANFilterTags
- Loading branch information
Showing
6 changed files
with
150 additions
and
3 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
89 changes: 89 additions & 0 deletions
89
frontend/src/pages/cans/list/CANFilterTags/CANFilterTags.hooks.js
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,89 @@ | ||
import { useState, useEffect, useCallback } from "react"; | ||
/** | ||
* @typedef {Object} FilterItem | ||
* @property {string} title | ||
*/ | ||
|
||
/** | ||
* @typedef {Object} Filters | ||
* @property {FilterItem[]} activePeriod | ||
* @property {FilterItem[]} portfolio | ||
* @property {FilterItem[]} transfer | ||
*/ | ||
|
||
/** | ||
* @typedef {Object} Tag | ||
* @property {string} tagText | ||
* @property {string} filter | ||
*/ | ||
|
||
/** | ||
* Custom hook for managing tags list | ||
* @param {Filters} filters | ||
* @returns {Tag[]} | ||
*/ | ||
export const useTagsList = (filters) => { | ||
const [tagsList, setTagsList] = useState([]); | ||
|
||
/** | ||
* @param {keyof Filters} filterKey | ||
* @param {string} filterName | ||
*/ | ||
const updateTags = useCallback( | ||
(filterKey, filterName) => { | ||
if (!Array.isArray(filters[filterKey])) return; | ||
|
||
const selectedTags = filters[filterKey].map((item) => ({ | ||
tagText: item.title, | ||
filter: filterName | ||
})); | ||
|
||
setTagsList((prevState) => [...prevState.filter((t) => t.filter !== filterName), ...selectedTags]); | ||
}, | ||
[filters] | ||
); | ||
|
||
useEffect(() => { | ||
updateTags("activePeriod", "activePeriod"); | ||
}, [filters.activePeriod, updateTags]); | ||
|
||
useEffect(() => { | ||
updateTags("portfolio", "portfolio"); | ||
}, [filters.portfolio, updateTags]); | ||
|
||
useEffect(() => { | ||
updateTags("transfer", "transfer"); | ||
}, [filters.transfer, updateTags]); | ||
|
||
return tagsList; | ||
}; | ||
|
||
/** | ||
* Removes a filter tag | ||
* @param {Tag} tag - The tag to remove | ||
* @param {function(function(Filters): Filters): void} setFilters - Function to update filters | ||
*/ | ||
export const removeFilter = (tag, setFilters) => { | ||
switch (tag.filter) { | ||
case "activePeriod": | ||
setFilters((prevState) => ({ | ||
...prevState, | ||
activePeriod: prevState.activePeriod.filter((period) => period.title !== tag.tagText) | ||
})); | ||
break; | ||
case "portfolio": | ||
setFilters((prevState) => ({ | ||
...prevState, | ||
portfolio: prevState.portfolio.filter((portfolio) => portfolio.title !== tag.tagText) | ||
})); | ||
break; | ||
case "transfer": | ||
setFilters((prevState) => ({ | ||
...prevState, | ||
transfer: prevState.transfer.filter((transfer) => transfer.title !== tag.tagText) | ||
})); | ||
break; | ||
default: | ||
console.warn(`Unknown filter type: ${tag.filter}`); | ||
} | ||
}; |
35 changes: 35 additions & 0 deletions
35
frontend/src/pages/cans/list/CANFilterTags/CANFilterTags.jsx
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,35 @@ | ||
import _ from "lodash"; | ||
import FilterTags from "../../../../components/UI/FilterTags"; | ||
import FilterTagsWrapper from "../../../../components/UI/FilterTags/FilterTagsWrapper"; | ||
import { useTagsList, removeFilter } from "./CANFilterTags.hooks"; | ||
|
||
/** | ||
* A filter tags component. | ||
* @param {Object} props - The component props. | ||
* @param {import('./CANFilterTags.hooks').Filters} props.filters - The current filters. | ||
* @param {() => void} props.setFilters - A function to call to set the filters. | ||
* @returns {JSX.Element|null} The filter tags component or null if no tags. | ||
*/ | ||
export const CANFilterTags = ({ filters, setFilters }) => { | ||
const tagsList = useTagsList(filters); | ||
|
||
const tagsListByFilter = _.groupBy(tagsList, "filter"); | ||
const tagsListByFilterMerged = Object.values(tagsListByFilter) | ||
.flat() | ||
.sort((a, b) => a.tagText.localeCompare(b.tagText)); | ||
|
||
if (tagsList.length === 0) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<FilterTagsWrapper> | ||
<FilterTags | ||
removeFilter={(tag) => removeFilter(tag, setFilters)} | ||
tagsList={tagsListByFilterMerged} | ||
/> | ||
</FilterTagsWrapper> | ||
); | ||
}; | ||
|
||
export default CANFilterTags; |
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 @@ | ||
export { default } from "./CANFilterTags"; |
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