-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(app): add useUpdateRecoveryPolicyWithStrategy
- Loading branch information
Showing
2 changed files
with
61 additions
and
0 deletions.
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
60 changes: 60 additions & 0 deletions
60
app/src/resources/runs/useUpdateRecoveryPolicyWithStrategy.ts
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,60 @@ | ||
import { | ||
useHost, | ||
useUpdateErrorRecoveryPolicy, | ||
} from '@opentrons/react-api-client' | ||
import { getErrorRecoveryPolicy } from '@opentrons/api-client' | ||
|
||
import type { | ||
HostConfig, | ||
ErrorRecoveryPolicyResponse, | ||
RecoveryPolicyRulesParams, | ||
} from '@opentrons/api-client' | ||
|
||
/** | ||
* append - Add a new policy rule to the end of the existing recovery policy. | ||
*/ | ||
export type UpdatePolicyStrategy = 'append' | ||
|
||
export interface UpdateErrorRecoveryPolicyWithStrategy { | ||
runId: string | ||
newPolicy: RecoveryPolicyRulesParams[number] | ||
strategy: UpdatePolicyStrategy | ||
} | ||
|
||
export function useUpdateRecoveryPolicyWithStrategy( | ||
runId: string | ||
): ( | ||
newPolicy: UpdateErrorRecoveryPolicyWithStrategy['newPolicy'], | ||
strategy: UpdateErrorRecoveryPolicyWithStrategy['strategy'] | ||
) => Promise<ErrorRecoveryPolicyResponse> { | ||
const host = useHost() | ||
|
||
const { | ||
mutateAsync: updateErrorRecoveryPolicy, | ||
} = useUpdateErrorRecoveryPolicy(runId) | ||
|
||
return ( | ||
newPolicy: UpdateErrorRecoveryPolicyWithStrategy['newPolicy'], | ||
strategy: UpdateErrorRecoveryPolicyWithStrategy['strategy'] | ||
) => | ||
getErrorRecoveryPolicy(host as HostConfig, runId).then(res => { | ||
const existingPolicyRules = res.data.data.policyRules.map(rule => ({ | ||
commandType: rule.matchCriteria.command.commandType, | ||
errorType: rule.matchCriteria.command.error.errorType, | ||
ifMatch: rule.ifMatch, | ||
})) | ||
|
||
const buildUpdatedPolicy = (): RecoveryPolicyRulesParams => { | ||
switch (strategy) { | ||
case 'append': | ||
return [...existingPolicyRules, newPolicy] | ||
default: { | ||
console.error('Handled policy strategy, using append.') | ||
return [...existingPolicyRules, newPolicy] | ||
} | ||
} | ||
} | ||
|
||
return updateErrorRecoveryPolicy(buildUpdatedPolicy()) | ||
}) | ||
} |