Skip to content

Commit

Permalink
Merge base
Browse files Browse the repository at this point in the history
  • Loading branch information
dkmyta committed Aug 28, 2024
2 parents 3695e1a + 4eac461 commit 25b5a39
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Button, Text } from '@automattic/jetpack-components';
import { __ } from '@wordpress/i18n';
import { useCallback, useState } from 'react';
import useFixersMutation from '../../data/scan/use-fixers-mutation';
import useFixers from '../../hooks/use-fixers';
import useModal from '../../hooks/use-modal';
import CredentialsGate from '../credentials-gate';
import ThreatFixHeader from '../threat-fix-header';
Expand All @@ -10,25 +10,25 @@ import styles from './styles.module.scss';

const FixAllThreatsModal = ( { threatList = [] } ) => {
const { setModal } = useModal();
const fixersMutation = useFixersMutation();
const { fixThreats, isLoading: isFixersLoading } = useFixers();

const [ threatIds, setThreatIds ] = useState( threatList.map( ( { id } ) => parseInt( id ) ) );

const handleCancelClick = () => {
const handleCancelClick = useCallback( () => {
return event => {
event.preventDefault();
setModal( { type: null } );
};
};
}, [ setModal ] );

const handleFixClick = () => {
const handleFixClick = useCallback( () => {
return async event => {
event.preventDefault();

await fixersMutation.mutateAsync( threatIds );
await fixThreats( threatIds );
setModal( { type: null } );
};
};
}, [ fixThreats, setModal, threatIds ] );

const handleCheckboxClick = useCallback(
( checked, threat ) => {
Expand Down Expand Up @@ -67,7 +67,7 @@ const FixAllThreatsModal = ( { threatList = [] } ) => {
{ __( 'Cancel', 'jetpack-protect' ) }
</Button>
<Button
isLoading={ fixersMutation.isLoading }
isLoading={ isFixersLoading }
onClick={ handleFixClick() }
disabled={ ! threatIds.length }
>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Button, Text } from '@automattic/jetpack-components';
import { __ } from '@wordpress/i18n';
import useFixersMutation from '../../data/scan/use-fixers-mutation';
import useFixers from '../../hooks/use-fixers';
import useModal from '../../hooks/use-modal';
import CredentialsGate from '../credentials-gate';
import ThreatFixHeader from '../threat-fix-header';
Expand All @@ -9,7 +9,7 @@ import styles from './styles.module.scss';

const FixThreatModal = ( { id, fixable, label, icon, severity } ) => {
const { setModal } = useModal();
const fixersMutation = useFixersMutation();
const { fixThreats, isLoading: isFixersLoading } = useFixers();

const handleCancelClick = () => {
return event => {
Expand All @@ -21,7 +21,7 @@ const FixThreatModal = ( { id, fixable, label, icon, severity } ) => {
const handleFixClick = () => {
return async event => {
event.preventDefault();
await fixersMutation.mutateAsync( [ id ] );
await fixThreats( [ id ] );
setModal( { type: null } );
};
};
Expand All @@ -47,7 +47,7 @@ const FixThreatModal = ( { id, fixable, label, icon, severity } ) => {
<Button variant="secondary" onClick={ handleCancelClick() }>
{ __( 'Cancel', 'jetpack-protect' ) }
</Button>
<Button isLoading={ fixersMutation.isLoading } onClick={ handleFixClick() }>
<Button isLoading={ isFixersLoading } onClick={ handleFixClick() }>
{ __( 'Fix threat', 'jetpack-protect' ) }
</Button>
</div>
Expand Down
3 changes: 2 additions & 1 deletion projects/plugins/protect/src/js/hooks/use-fixers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ export default function useFixers() {
const { fixableThreats } = status;
const fixersMutation = useFixersMutation();

const { data: fixersStatus } = useFixersQuery( { threatIds: fixableThreats } );
const { data: fixersStatus } = useFixersQuery( { threatIds: fixableThreats, usePolling: true } );

const fixThreats = async ( threatIds: number[] ) => fixersMutation.mutateAsync( threatIds );

return {
fixableThreats,
fixersStatus,
fixThreats,
isLoading: fixersMutation.isPending,
};
}

0 comments on commit 25b5a39

Please sign in to comment.