From 508d21fcd6c0f9f3d54a0851f48dfa997fb4e7db Mon Sep 17 00:00:00 2001 From: Sarah Breen Date: Fri, 22 Sep 2023 16:56:46 -0400 Subject: [PATCH] fix(app): add modal close logic for remote run deletion for LPC (#13633) fix RQA-1703 --- .../LabwarePositionCheckComponent.tsx | 39 +++++++++++++++++++ .../__tests__/useLaunchLPC.test.tsx | 4 +- .../organisms/LabwarePositionCheck/index.tsx | 1 + .../LabwarePositionCheck/useLaunchLPC.tsx | 3 +- 4 files changed, 44 insertions(+), 3 deletions(-) diff --git a/app/src/organisms/LabwarePositionCheck/LabwarePositionCheckComponent.tsx b/app/src/organisms/LabwarePositionCheck/LabwarePositionCheckComponent.tsx index 831f1e4edf0..f774cb2a878 100644 --- a/app/src/organisms/LabwarePositionCheck/LabwarePositionCheckComponent.tsx +++ b/app/src/organisms/LabwarePositionCheck/LabwarePositionCheckComponent.tsx @@ -7,6 +7,7 @@ import { LabwareOffsetCreateData } from '@opentrons/api-client' import { useCreateLabwareOffsetMutation, useCreateMaintenanceCommandMutation, + useCurrentMaintenanceRun, } from '@opentrons/react-api-client' import { CompletedProtocolAnalysis, @@ -37,6 +38,7 @@ import type { Axis, Sign, StepSize } from '../../molecules/JogControls/types' import type { RegisterPositionAction, WorkingOffset } from './types' import { getGoldenCheckSteps } from './utils/getGoldenCheckSteps' +const RUN_REFETCH_INTERVAL = 5000 const JOG_COMMAND_TIMEOUT = 10000 // 10 seconds interface LabwarePositionCheckModalProps { onCloseClick: () => unknown @@ -45,6 +47,7 @@ interface LabwarePositionCheckModalProps { mostRecentAnalysis: CompletedProtocolAnalysis | null existingOffsets: LabwareOffset[] caughtError?: Error + setMaintenanceRunId: (id: string | null) => void } export const LabwarePositionCheckComponent = ( @@ -56,10 +59,46 @@ export const LabwarePositionCheckComponent = ( existingOffsets, runId, maintenanceRunId, + setMaintenanceRunId, } = props const { t } = useTranslation(['labware_position_check', 'shared']) const isOnDevice = useSelector(getIsOnDevice) const protocolData = mostRecentAnalysis + + // we should start checking for run deletion only after the maintenance run is created + // and the useCurrentRun poll has returned that created id + const [ + monitorMaintenanceRunForDeletion, + setMonitorMaintenanceRunForDeletion, + ] = React.useState(false) + + const { data: maintenanceRunData } = useCurrentMaintenanceRun({ + refetchInterval: RUN_REFETCH_INTERVAL, + enabled: maintenanceRunId != null, + }) + + // this will close the modal in case the run was deleted by the terminate + // activity modal on the ODD + React.useEffect(() => { + if ( + maintenanceRunId !== null && + maintenanceRunData?.data.id === maintenanceRunId + ) { + setMonitorMaintenanceRunForDeletion(true) + } + if ( + maintenanceRunData?.data.id !== maintenanceRunId && + monitorMaintenanceRunForDeletion + ) { + setMaintenanceRunId(null) + } + }, [ + maintenanceRunData?.data.id, + maintenanceRunId, + monitorMaintenanceRunForDeletion, + setMaintenanceRunId, + ]) + const [fatalError, setFatalError] = React.useState(null) const [ { workingOffsets, tipPickUpOffset }, diff --git a/app/src/organisms/LabwarePositionCheck/__tests__/useLaunchLPC.test.tsx b/app/src/organisms/LabwarePositionCheck/__tests__/useLaunchLPC.test.tsx index 2c0b0eb5820..76be303a872 100644 --- a/app/src/organisms/LabwarePositionCheck/__tests__/useLaunchLPC.test.tsx +++ b/app/src/organisms/LabwarePositionCheck/__tests__/useLaunchLPC.test.tsx @@ -80,7 +80,7 @@ describe('useLaunchLPC hook', () => { Promise.resolve({ data: { definitionUri: 'fakeDefUri' } }) ) mockDeleteMaintenanceRun = jest.fn((_data, opts) => { - opts?.onSuccess() + opts?.onSettled() }) const store = mockStore({ isOnDevice: false }) wrapper = ({ children }) => ( @@ -184,7 +184,7 @@ describe('useLaunchLPC hook', () => { expect(mockDeleteMaintenanceRun).toHaveBeenCalledWith( MOCK_MAINTENANCE_RUN_ID, { - onSuccess: expect.any(Function), + onSettled: expect.any(Function), } ) expect(result.current.LPCWizard).toBeNull() diff --git a/app/src/organisms/LabwarePositionCheck/index.tsx b/app/src/organisms/LabwarePositionCheck/index.tsx index cb0bef0d373..92e8ede4060 100644 --- a/app/src/organisms/LabwarePositionCheck/index.tsx +++ b/app/src/organisms/LabwarePositionCheck/index.tsx @@ -13,6 +13,7 @@ interface LabwarePositionCheckModalProps { existingOffsets: LabwareOffset[] mostRecentAnalysis: CompletedProtocolAnalysis | null caughtError?: Error + setMaintenanceRunId: (id: string | null) => void } // We explicitly wrap LabwarePositionCheckComponent in an ErrorBoundary because an error might occur while pulling in diff --git a/app/src/organisms/LabwarePositionCheck/useLaunchLPC.tsx b/app/src/organisms/LabwarePositionCheck/useLaunchLPC.tsx index fe329f80f06..bed968d9e63 100644 --- a/app/src/organisms/LabwarePositionCheck/useLaunchLPC.tsx +++ b/app/src/organisms/LabwarePositionCheck/useLaunchLPC.tsx @@ -29,7 +29,7 @@ export function useLaunchLPC( const handleCloseLPC = (): void => { if (maintenanceRunId != null) { deleteMaintenanceRun(maintenanceRunId, { - onSuccess: () => { + onSettled: () => { setMaintenanceRunId(null) }, }) @@ -70,6 +70,7 @@ export function useLaunchLPC( mostRecentAnalysis={mostRecentAnalysis} existingOffsets={runRecord?.data?.labwareOffsets ?? []} maintenanceRunId={maintenanceRunId} + setMaintenanceRunId={setMaintenanceRunId} /> ) : null, }