-
Notifications
You must be signed in to change notification settings - Fork 31
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
Analytic: Feature to change Air Quality standards on charts #2383
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
142 changes: 142 additions & 0 deletions
142
platform/src/common/components/Charts/components/StandardsMenu.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,142 @@ | ||
import React, { useState, useRef, useEffect } from 'react'; | ||
import { MdKeyboardArrowRight, MdKeyboardArrowDown } from 'react-icons/md'; | ||
import { useDispatch, useSelector } from 'react-redux'; | ||
import { setAqStandard } from '@/lib/store/services/charts/ChartSlice'; | ||
import { IoMdCheckmark } from 'react-icons/io'; | ||
|
||
const aqStandards = [ | ||
{ | ||
name: 'WHO', | ||
value: { | ||
pm2_5: 10, | ||
pm10: 20, | ||
no2: 40, | ||
}, | ||
}, | ||
{ | ||
name: 'NEMA', | ||
value: { | ||
pm2_5: 15, | ||
pm10: 45, | ||
no2: 25, | ||
}, | ||
}, | ||
{ | ||
name: 'KCCA', | ||
value: { | ||
pm2_5: 20, | ||
pm10: 50, | ||
no2: 30, | ||
}, | ||
}, | ||
{ | ||
name: 'LASEPA', | ||
value: { | ||
pm2_5: 25, | ||
pm10: 55, | ||
no2: 35, | ||
}, | ||
}, | ||
]; | ||
|
||
const StandardsMenu = () => { | ||
const [isOpen, setIsOpen] = useState(false); | ||
const [isMobile, setIsMobile] = useState(false); | ||
const menuRef = useRef(null); | ||
const dispatch = useDispatch(); | ||
const selectedStandard = useSelector((state) => state.chart.aqStandard); | ||
|
||
useEffect(() => { | ||
const handleClickOutside = (event) => { | ||
if (menuRef.current && !menuRef.current.contains(event.target)) { | ||
setIsOpen(false); | ||
} | ||
}; | ||
|
||
const handleResize = () => { | ||
setIsMobile(window.innerWidth < 640); // Adjust this breakpoint as needed | ||
}; | ||
|
||
document.addEventListener('mousedown', handleClickOutside); | ||
window.addEventListener('resize', handleResize); | ||
handleResize(); // Initial check | ||
|
||
return () => { | ||
document.removeEventListener('mousedown', handleClickOutside); | ||
window.removeEventListener('resize', handleResize); | ||
}; | ||
}, []); | ||
|
||
const handleSelectStandard = (standard) => { | ||
dispatch(setAqStandard(standard)); | ||
setIsOpen(false); | ||
}; | ||
|
||
const currentStandard = | ||
aqStandards.find((s) => s.name === selectedStandard?.name) || | ||
aqStandards[0]; | ||
|
||
return ( | ||
<div | ||
className="relative w-full border-t border-gray-200" | ||
ref={menuRef} | ||
onMouseEnter={() => setIsOpen(true)} | ||
onMouseLeave={() => setIsOpen(false)} | ||
> | ||
<button | ||
className="flex justify-between items-center w-full px-4 py-2 text-sm text-gray-600 hover:bg-gray-50" | ||
aria-haspopup="true" | ||
aria-expanded={isOpen} | ||
> | ||
<span>Air Quality Standard</span> | ||
<span className="text-gray-400 ml-2 transition-transform duration-200"> | ||
{isMobile ? ( | ||
<MdKeyboardArrowDown | ||
size={18} | ||
className={`transform ${isOpen ? 'rotate-180' : ''}`} | ||
/> | ||
) : ( | ||
<MdKeyboardArrowRight | ||
size={18} | ||
className={`transform ${isOpen ? 'rotate-180' : ''}`} | ||
/> | ||
)} | ||
</span> | ||
</button> | ||
<div | ||
className={` | ||
absolute bg-white shadow-md p-1 border border-gray-200 rounded-md | ||
transition-all duration-200 ease-in-out overflow-hidden z-10 | ||
${isOpen ? 'opacity-100 visible' : 'opacity-0 invisible'} | ||
${isMobile ? 'left-0 right-0 top-full mt-1' : 'right-full top-0 mt-0'} | ||
`} | ||
style={{ | ||
width: '100%', | ||
transform: isMobile ? 'none' : 'translateX(-8px)', | ||
}} | ||
> | ||
{aqStandards.map((standard, index) => ( | ||
<button | ||
key={standard.name} | ||
className={` | ||
flex flex-row justify-between items-center w-full px-4 py-2 | ||
text-sm text-gray-600 hover:bg-gray-50 | ||
${standard.name === currentStandard.name ? 'bg-gray-100' : ''} | ||
${index !== 0 ? 'border-t border-gray-200' : ''} | ||
`} | ||
onClick={() => handleSelectStandard(standard)} | ||
> | ||
<span className="font-medium">{standard.name}</span> | ||
{standard.name === currentStandard.name && ( | ||
<span className="text-green-500 ml-2"> | ||
<IoMdCheckmark size={18} /> | ||
</span> | ||
)} | ||
</button> | ||
))} | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export default StandardsMenu; |
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 |
---|---|---|
|
@@ -268,7 +268,7 @@ const renderCustomizedLegend = ({ payload }) => { | |
* @param {Object} props | ||
*/ | ||
const CustomReferenceLabel = (props) => { | ||
const { viewBox } = props; | ||
const { viewBox, name } = props; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Update PropTypes to include the new name prop. The CustomReferenceLabel.propTypes = {
viewBox: PropTypes.object,
+ name: PropTypes.string.isRequired,
}; Also applies to: 283-283 |
||
const x = viewBox.width + viewBox.x - 10; | ||
const y = viewBox.y + 3; | ||
|
||
|
@@ -280,7 +280,7 @@ const CustomReferenceLabel = (props) => { | |
style={{ backgroundColor: 'red' }} | ||
className="rounded-[2px] py-[4px] px-[6px] flex justify-center text-center text-white text-[14px] tracking-[0.16px] font-normal leading-[16px]" | ||
> | ||
WHO | ||
{name} | ||
</div> | ||
</foreignObject> | ||
</g> | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Add error handling for missing standards.
The current standard selection logic should handle cases where the standard is not found in the array.
📝 Committable suggestion