aprSlice.ts 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import { createSlice, PayloadAction, Middleware } from '@reduxjs/toolkit';
  2. import { AprConfig, getAprExposureParams } from '../../API/exam/APRActions';
  3. interface AprState {
  4. aprConfig: AprConfig;
  5. bodysize: string;
  6. workstation: string;
  7. isAECEnabled: boolean;
  8. currentExposureMode: string;
  9. }
  10. const initialState: AprState = {
  11. aprConfig: {
  12. AECDensity: 0,
  13. AECField: '',
  14. AECFilm: 0,
  15. Dose: 0,
  16. ExposureMode: 0,
  17. Focus: 0,
  18. TOD: 0,
  19. TubeLoad: 0,
  20. kV: 0,
  21. mA: 0,
  22. mAs: 0,
  23. ms: 0,
  24. },
  25. bodysize: '',
  26. workstation: '',
  27. isAECEnabled: false,
  28. currentExposureMode: '',
  29. };
  30. const aprSlice = createSlice({
  31. name: 'apr',
  32. initialState,
  33. reducers: {
  34. setAprConfig: (state, action: PayloadAction<AprConfig>) => {
  35. state.aprConfig = action.payload;
  36. },
  37. setBodysize: (state, action: PayloadAction<string>) => {
  38. state.bodysize = action.payload;
  39. },
  40. setWorkstation: (state, action: PayloadAction<string>) => {
  41. state.workstation = action.payload;
  42. },
  43. setIsAECEnabled: (state, action: PayloadAction<boolean>) => {
  44. state.isAECEnabled = action.payload;
  45. },
  46. setCurrentExposureMode: (state, action: PayloadAction<string>) => {
  47. state.currentExposureMode = action.payload;
  48. },
  49. },
  50. });
  51. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  52. const aprMiddleware: Middleware = (store) => (next) => (action: any) => {
  53. if (
  54. action.type === aprSlice.actions.setBodysize.type ||
  55. action.type === aprSlice.actions.setWorkstation.type
  56. ) {
  57. const state = store.getState();
  58. const id =
  59. state.bodyPositionList.selectedBodyPosition?.view_id || 'default_id'; // Dynamically determined based on selectedBodyPosition
  60. const workStationId = parseInt(state.apr.workstation, 10);
  61. const patientSize = state.apr.bodysize;
  62. if (state.apr.bodysize && state.apr.workstation) {
  63. getAprExposureParams(id, workStationId, patientSize).then((data) => {
  64. store.dispatch(setAprConfig(data));
  65. });
  66. }
  67. }
  68. return next(action);
  69. };
  70. export const {
  71. setAprConfig,
  72. setBodysize,
  73. setWorkstation,
  74. setIsAECEnabled,
  75. setCurrentExposureMode,
  76. } = aprSlice.actions;
  77. export default aprSlice.reducer;
  78. export { aprMiddleware };