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

Protect: Update use fixers query error prop handling #39498

Merged
merged 13 commits into from
Oct 2, 2024
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: changed

Adds handling for FixerStatus error props
14 changes: 14 additions & 0 deletions projects/plugins/protect/src/js/data/scan/use-fixers-mutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,20 @@ export default function useFixersMutation(): UseMutationResult {
return useMutation( {
mutationFn: API.fixThreats,
onSuccess: data => {
// Handle a top level error
if ( data?.error ) {
throw new Error( data.error );
}

const isThreatLevelError = Object.values( data?.threats ).every(
( threat: { error?: string } ) => Boolean( threat?.error )
);

// Handle a threat level error
if ( isThreatLevelError ) {
throw new Error();
}
nateweller marked this conversation as resolved.
Show resolved Hide resolved

// The data returned from the API is the same as the data we need to update the cache.
queryClient.setQueryData( [ QUERY_FIXERS_KEY ], data );

Expand Down
18 changes: 12 additions & 6 deletions projects/plugins/protect/src/js/data/scan/use-fixers-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,21 @@ export default function useFixersQuery( {
| FixersStatus
| undefined;

// Handle a top level error
if ( data?.error ) {
throw new Error( data?.error );
}

const successes: string[] = [];
const failures: string[] = [];

Object.keys( data?.threats || {} ).forEach( threatId => {
const threat = data.threats[ threatId ];
const threat = data?.threats[ threatId ];
const cachedThreat = cachedData?.threats?.[ threatId ];

if ( cachedThreat?.status === 'in_progress' ) {
// If still in progress
if ( threat.status === 'in_progress' ) {
if ( threat?.status === 'in_progress' ) {
if (
! fixerTimestampIsStale( cachedThreat.last_updated ) &&
fixerTimestampIsStale( threat.last_updated )
Expand All @@ -93,13 +98,14 @@ export default function useFixersQuery( {
}

// Handle completion of fixers
if ( threat.status !== 'in_progress' ) {
if ( threat?.status !== 'in_progress' ) {
queryClient.invalidateQueries( { queryKey: [ QUERY_SCAN_STATUS_KEY ] } );
queryClient.invalidateQueries( { queryKey: [ QUERY_HISTORY_KEY ] } );

if ( threat.status === 'fixed' ) {
if ( threat?.status === 'fixed' ) {
successes.push( threatId );
} else {
// Handle unsuccessful statuses and threat level errors
nateweller marked this conversation as resolved.
Show resolved Hide resolved
failures.push( threatId );
}
}
Expand All @@ -117,8 +123,8 @@ export default function useFixersQuery( {
return false;
}

const inProgressNotStale = Object.values( query.state.data.threats ).some(
( threat: { status: string; last_updated: string } ) =>
const inProgressNotStale = Object.values( query.state.data?.threats ).some(
( threat: { status?: string; last_updated?: string } ) =>
threat.status === 'in_progress' && ! fixerTimestampIsStale( threat.last_updated )
);

Expand Down
8 changes: 5 additions & 3 deletions projects/plugins/protect/src/js/types/fixers.ts
nateweller marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ export type FixerStatus = 'not_started' | 'in_progress' | 'fixed' | 'not_fixed';

export type FixersStatus = {
ok: boolean;
threats: {
error?: string;
threats?: {
[ key: number ]: ThreatFixStatus;
};
};

export type ThreatFixStatus = {
status: FixerStatus;
last_updated: string;
error?: string;
status?: FixerStatus;
last_updated?: string;
};