From 0822cd510ddf787ddd5d5836b1395e4adccb9e01 Mon Sep 17 00:00:00 2001 From: dkmyta Date: Sat, 21 Dec 2024 13:19:27 -0800 Subject: [PATCH] Set threatsList and selectedThreats --- .../threats-modal/stories/index.stories.tsx | 4 +- .../threats-modal/threat-fix-confirmation.tsx | 71 ++++++++++++------- 2 files changed, 49 insertions(+), 26 deletions(-) diff --git a/projects/js-packages/components/components/threats-modal/stories/index.stories.tsx b/projects/js-packages/components/components/threats-modal/stories/index.stories.tsx index 46f4cd3f97002..ecc897a214d52 100644 --- a/projects/js-packages/components/components/threats-modal/stories/index.stories.tsx +++ b/projects/js-packages/components/components/threats-modal/stories/index.stories.tsx @@ -66,7 +66,7 @@ const Base = args => { fixedIn: '1.2.4', severity: 3, fixable: { fixer: 'update', target: '1.12.4', extensionStatus: 'inactive' }, - fixer: { status: 'in_progress', lastUpdated: new Date().toISOString() }, + fixer: { status: 'not_started' }, status: 'current', source: 'https://wpscan.com/vulnerability/733d8a02-0d44-4b78-bbb2-37e447acd2f3', extension: { @@ -85,7 +85,7 @@ const Base = args => { fixedIn: '2.22.22', severity: 3, fixable: { fixer: 'update', target: '1.12.4', extensionStatus: 'inactive' }, - fixer: { status: 'in_progress', lastUpdated: new Date( '1999-01-01' ).toISOString() }, + fixer: { status: 'not_started' }, status: 'current', source: 'https://wpscan.com/vulnerability/733d8a02-0d44-4b78-bbb2-37e447acd2f3', extension: { diff --git a/projects/js-packages/components/components/threats-modal/threat-fix-confirmation.tsx b/projects/js-packages/components/components/threats-modal/threat-fix-confirmation.tsx index 8f923a63d033c..16a5f8691b8b0 100644 --- a/projects/js-packages/components/components/threats-modal/threat-fix-confirmation.tsx +++ b/projects/js-packages/components/components/threats-modal/threat-fix-confirmation.tsx @@ -24,49 +24,65 @@ import { ThreatsModalContext } from '.'; const ThreatFixConfirmation = () => { const [ isSm ] = useBreakpointMatch( [ 'sm', 'lg' ], [ null, '<' ] ); - const { threatsList, isBulk, handleUpgradeClick } = useContext( ThreatsModalContext ); + const { threatsList, setThreatsList, isBulk, handleUpgradeClick } = + useContext( ThreatsModalContext ); const [ selectedThreats, setSelectedThreats ] = useState( threatsList ); const handleToggleThreat = useCallback( ( threat: Threat, isChecked: boolean ) => { setSelectedThreats( prevSelectedThreats => { if ( isChecked ) { - // Add the threat if it's not already in the list return [ ...prevSelectedThreats, threat ]; } - // Remove the threat if it exists in the list return prevSelectedThreats.filter( selectedThreat => selectedThreat.id !== threat.id ); } ); }, [] ); // Memoize toggle handlers for each threat const toggleHandlers = useMemo( () => { - return threatsList.reduce( ( handlers, threat ) => { - handlers[ threat.id ] = isChecked => handleToggleThreat( threat, isChecked ); - return handlers; - }, {} ); + const handlers: Record< string, ( value: boolean ) => void > = {}; + + threatsList.forEach( threat => { + handlers[ threat.id ] = isChecked => { + handleToggleThreat( threat, isChecked ); + }; + } ); + + return handlers; }, [ threatsList, handleToggleThreat ] ); - const viewIndividualThreat = useCallback( threat => { - console.log( threat ); - }, [] ); + const viewIndividualThreat = useCallback( + ( threat: Threat ) => { + setThreatsList( [ threat ] ); + setSelectedThreats( [ threat ] ); + }, + [ setThreatsList, setSelectedThreats ] + ); const handleThreatClick = useCallback( - threat => () => { - viewIndividualThreat( threat ); + ( threat: Threat ) => { + return () => { + viewIndividualThreat( threat ); + }; }, [ viewIndividualThreat ] ); - const handleKeyPress = useCallback( - threat => event => { - if ( event.key === 'Enter' ) { - viewIndividualThreat( threat ); - } + const handleThreatKeyPress = useCallback( + ( threat: Threat ) => { + return ( event: React.KeyboardEvent ) => { + if ( event.key === 'Enter' || event.key === ' ' ) { + viewIndividualThreat( threat ); + } + }; }, [ viewIndividualThreat ] ); + const stopPropagationHandler = useCallback( ( event: React.MouseEvent | React.KeyboardEvent ) => { + event.stopPropagation(); + }, [] ); + const renderBulkThreat = threat => { return (
{ onClick={ handleThreatClick( threat ) } role="button" tabIndex={ 0 } - onKeyDown={ handleKeyPress( threat ) } + onKeyDown={ handleThreatKeyPress( threat ) } >
@@ -89,12 +105,19 @@ const ThreatFixConfirmation = () => {
{ !! threat.severity && } - selectedThreat.id === threat.id ) } - onChange={ toggleHandlers[ threat.id ] } - /> +
+ selectedThreat.id === threat.id ) } + onChange={ toggleHandlers[ threat.id ] } + /> +
);