businessFlowMiddlewareLogic.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import { Middleware, PayloadAction } from '@reduxjs/toolkit';
  2. import prepare, { unprepare } from '../domain/exam/prepare';
  3. import { BusinessFlowState, setBusinessFlow } from './BusinessFlowSlice';
  4. import { setFeedbackOpen } from './exam/largeScreenSlice';
  5. let continueBusinessFlow = '';
  6. const businessFlowMiddlewareLogic: Middleware =
  7. (store) => (next) => (action: PayloadAction<string>) => {
  8. //const result = next(action);
  9. // console.log(
  10. // `[businessFlowMiddleware] Action dispatched: ${action.type} ${action.payload}`
  11. // );
  12. if (action.type !== setBusinessFlow.type) {
  13. return next(action); // Only handle setBusinessFlow actions
  14. }
  15. const state = store.getState().BusinessFlow as BusinessFlowState;
  16. const currentKey = state.currentKey;
  17. console.log(`[businessFlowMiddleware] currentKey: ${currentKey}`);
  18. console.log(
  19. `[businessFlowMiddleware] Current business flow is now: ${state.currentKey} ; Last key was: ${state.lastKey}`
  20. );
  21. //进入检查
  22. if (currentKey === 'exam') {
  23. prepare();
  24. }
  25. if (isFromExamToView(action.payload, currentKey)) {
  26. //从检查进入图像处理,有可能是曝光导致的,怎么知道呢?看发生器状态
  27. if (store.getState().generatorMonitor.acquisitionState === 1) {
  28. //发生器正在采集
  29. console.log(
  30. `[businessFlowMiddleware] Exiting exam flow, but generator is still acquiring.`
  31. );
  32. return next(action); // 发生器正在采集,不能退出
  33. } else {
  34. //非曝光导致的从检查进入处理 // 说明从检查退出 , 执行清理
  35. console.log(
  36. `[businessFlowMiddleware] Exiting exam flow, last key was: ${state.lastKey}`
  37. );
  38. unprepare();
  39. }
  40. }
  41. if (
  42. isExitingExam(action.payload, currentKey) &&
  43. action.payload !== 'exitExamCompleted' &&
  44. action.payload !== 'exitExamSuspended'
  45. ) {
  46. console.log(
  47. `[businessFlowMiddleware] Exiting exam flow to go to : ${action.payload}`
  48. );
  49. const exposureStatus = store.getState().bodyPositionList.exposureStatus;
  50. console.log(
  51. `[businessFlowMiddleware] Exposure status: ${exposureStatus}`
  52. );
  53. if (exposureStatus === 'Half Exposed') {
  54. store.dispatch(setFeedbackOpen(true)); //显示反馈框
  55. // store.dispatch({ type: 'SET_CONTINUE_BUSINESS_FLOW', payload: currentKey });
  56. continueBusinessFlow = action.payload; // 保存退出检查时,要去哪个业务流程
  57. return; //阻止退出exam
  58. } else if (exposureStatus === 'Fully Exposed') {
  59. // Notify backend
  60. console.log(
  61. '[businessFlowMiddleware] Fully Exposed, notifying backend'
  62. );
  63. // Proceed with the action
  64. return next(action);
  65. } else if (exposureStatus === 'Not Exposed') {
  66. // Proceed with the action
  67. return next(action);
  68. }
  69. }
  70. //退出检查的中间过程
  71. if (action.payload === 'exitExamCompleted') {
  72. // Notify backend with different interfaces for completed and suspended
  73. console.log(
  74. `[businessFlowMiddleware] Notifying backend for ${action.payload}`
  75. );
  76. // todo 把所有未曝光的体位从study删除掉--服务端和slice。
  77. return next({ ...action, payload: continueBusinessFlow });
  78. }
  79. if (action.payload === 'exitExamSuspended') {
  80. // Notify backend with different interfaces for completed and suspended 暂时不通知后端
  81. console.log(
  82. `[businessFlowMiddleware] Notifying backend for ${action.payload}`
  83. );
  84. return next({ ...action, payload: continueBusinessFlow });
  85. }
  86. next(action);
  87. };
  88. /**
  89. * 从检查到图像处理
  90. */
  91. function isFromExamToView(currentAction: string, currentKey: string): boolean {
  92. return currentAction === 'view' && currentKey === 'exam';
  93. }
  94. function isExitingExam(currentAction: string, currentKey: string): boolean {
  95. console.log(
  96. `[businessFlowMiddleware] Checking if exiting exam: ${currentAction} vs ${currentKey}`
  97. );
  98. return currentAction !== 'exam' && currentKey === 'exam';
  99. }
  100. export default businessFlowMiddlewareLogic;