selectedPatientSlice.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import { createSlice, PayloadAction } from '@reduxjs/toolkit';
  2. import { RootState } from '@/states/store';
  3. import { workSelectionSlice } from './workSlice';
  4. import { historySelectionSlice } from './history';
  5. /**
  6. * 选中患者的照片信息状态
  7. */
  8. interface SelectedPatientState {
  9. patientId: string | null;
  10. patientName: string | null;
  11. portraitStatus: string | null;
  12. portraitFile: string | null;
  13. }
  14. const initialState: SelectedPatientState = {
  15. patientId: null,
  16. patientName: null,
  17. portraitStatus: null,
  18. portraitFile: null,
  19. };
  20. /**
  21. * 选中患者照片状态管理 Slice
  22. *
  23. * 功能:
  24. * 1. 监听 workSelectionSlice 和 historySelectionSlice 的选中变化
  25. * 2. 自动更新选中患者的照片信息
  26. * 3. 提供清除选中状态的 action
  27. */
  28. const selectedPatientSlice = createSlice({
  29. name: 'selectedPatient',
  30. initialState,
  31. reducers: {
  32. /**
  33. * 手动更新选中患者信息
  34. */
  35. updateSelectedPatient: (
  36. state,
  37. action: PayloadAction<{
  38. patientId: string;
  39. patientName: string;
  40. portraitStatus?: string;
  41. portraitFile?: string;
  42. }>
  43. ) => {
  44. state.patientId = action.payload.patientId;
  45. state.patientName = action.payload.patientName;
  46. state.portraitStatus = action.payload.portraitStatus || null;
  47. state.portraitFile = action.payload.portraitFile || null;
  48. },
  49. /**
  50. * 清除选中患者信息
  51. */
  52. clearSelectedPatient: (state) => {
  53. state.patientId = null;
  54. state.patientName = null;
  55. state.portraitStatus = null;
  56. state.portraitFile = null;
  57. },
  58. },
  59. extraReducers: (builder) => {
  60. // 监听 worklist 选中变化
  61. builder.addCase(
  62. workSelectionSlice.actions.setSelectedIds,
  63. (state, action: PayloadAction<string[]>) => {
  64. const selectedIds = action.payload;
  65. if (selectedIds.length === 0) {
  66. // 如果没有选中项,清空状态
  67. state.patientId = null;
  68. state.patientName = null;
  69. state.portraitStatus = null;
  70. state.portraitFile = null;
  71. } else {
  72. // 选中了项目,但我们需要从 store 中获取完整的 Task 数据
  73. // 这里只保存 patientId,实际的照片信息会在组件中从 workEntities 中获取
  74. state.patientId = selectedIds[0]; // 只处理单选
  75. // 其他字段会在组件中从 entities 获取
  76. }
  77. }
  78. );
  79. // 监听 historylist 选中变化
  80. builder.addCase(
  81. historySelectionSlice.actions.setSelectedIds,
  82. (state, action: PayloadAction<string[]>) => {
  83. const selectedIds = action.payload;
  84. if (selectedIds.length === 0) {
  85. // 如果没有选中项,清空状态
  86. state.patientId = null;
  87. state.patientName = null;
  88. state.portraitStatus = null;
  89. state.portraitFile = null;
  90. } else {
  91. // 选中了项目
  92. state.patientId = selectedIds[0]; // 只处理单选
  93. // 其他字段会在组件中从 entities 获取
  94. }
  95. }
  96. );
  97. },
  98. });
  99. export const { updateSelectedPatient, clearSelectedPatient } = selectedPatientSlice.actions;
  100. // Selectors
  101. export const selectSelectedPatient = (state: RootState) => state.selectedPatient;
  102. export const selectPatientId = (state: RootState) => state.selectedPatient.patientId;
  103. export const selectPortraitInfo = (state: RootState) => ({
  104. portraitStatus: state.selectedPatient.portraitStatus,
  105. portraitFile: state.selectedPatient.portraitFile,
  106. });
  107. export default selectedPatientSlice.reducer;