All files / extensions/measurement-tracking/src/contexts/TrackedMeasurementsContext promptTrackNewStudy.ts

3.03% Statements 1/33
0% Branches 0/11
0% Functions 0/12
3.12% Lines 1/32

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135      35x                                                                                                                                                                                                                                                                      
import i18n from 'i18next';
import { measurementTrackingMode } from './promptBeginTracking';
 
const RESPONSE = {
  NO_NEVER: -1,
  CANCEL: 0,
  CREATE_REPORT: 1,
  ADD_SERIES: 2,
  SET_STUDY_AND_SERIES: 3,
  NO_NOT_FOR_SERIES: 4,
};
 
function promptTrackNewStudy({ servicesManager, extensionManager }: withAppTypes, ctx, evt) {
  const { uiViewportDialogService, customizationService } = servicesManager.services;
  // When the state change happens after a promise, the state machine sends the retult in evt.data;
  // In case of direct transition to the state, the state machine sends the data in evt;
  const { viewportId, StudyInstanceUID, SeriesInstanceUID } = evt.data || evt;
 
  return new Promise(async function (resolve, reject) {
    const appConfig = extensionManager._appConfig;
 
    const standardMode = appConfig?.measurementTrackingMode === measurementTrackingMode.STANDARD;
    const simplifiedMode =
      appConfig?.measurementTrackingMode === measurementTrackingMode.SIMPLIFIED;
    let promptResult = standardMode
      ? await _askTrackMeasurements(uiViewportDialogService, customizationService, viewportId)
      : RESPONSE.SET_STUDY_AND_SERIES;
 
    Iif (promptResult === RESPONSE.SET_STUDY_AND_SERIES) {
      promptResult =
        ctx.isDirty && (standardMode || simplifiedMode)
          ? await _askSaveDiscardOrCancel(uiViewportDialogService, customizationService, viewportId)
          : RESPONSE.SET_STUDY_AND_SERIES;
    }
 
    resolve({
      userResponse: promptResult,
      StudyInstanceUID,
      SeriesInstanceUID,
      viewportId,
      isBackupSave: false,
    });
  });
}
 
function _askTrackMeasurements(
  UIViewportDialogService: AppTypes.UIViewportDialogService,
  customizationService: AppTypes.CustomizationService,
  viewportId
) {
  return new Promise(function (resolve, reject) {
    const message = customizationService.getCustomization(
      'viewportNotification.trackNewStudyMessage'
    );
    const actions = [
      { type: 'cancel', text: i18n.t('MeasurementTable:No'), value: RESPONSE.CANCEL },
      {
        type: 'secondary',
        text: i18n.t('MeasurementTable:No, do not ask again'),
        value: RESPONSE.NO_NOT_FOR_SERIES,
      },
      {
        type: 'primary',
        text: i18n.t('MeasurementTable:Yes'),
        value: RESPONSE.SET_STUDY_AND_SERIES,
      },
    ];
    const onSubmit = result => {
      UIViewportDialogService.hide();
      resolve(result);
    };
 
    UIViewportDialogService.show({
      viewportId,
      type: 'info',
      message,
      actions,
      onSubmit,
      onOutsideClick: () => {
        UIViewportDialogService.hide();
        resolve(RESPONSE.CANCEL);
      },
      onKeyPress: event => {
        Iif (event.key === 'Enter') {
          const action = actions.find(action => action.value === RESPONSE.SET_STUDY_AND_SERIES);
          onSubmit(action.value);
        }
      },
    });
  });
}
 
function _askSaveDiscardOrCancel(
  UIViewportDialogService: AppTypes.UIViewportDialogService,
  customizationService: AppTypes.CustomizationService,
  viewportId
) {
  return new Promise(function (resolve, reject) {
    const message = customizationService.getCustomization(
      'viewportNotification.discardStudyMessage'
    );
    const actions = [
      { type: 'cancel', text: 'Cancel', value: RESPONSE.CANCEL },
      {
        type: 'secondary',
        text: 'No, discard previously tracked series & measurements',
        value: RESPONSE.SET_STUDY_AND_SERIES,
      },
      {
        type: 'primary',
        text: 'Yes',
        value: RESPONSE.CREATE_REPORT,
      },
    ];
    const onSubmit = result => {
      UIViewportDialogService.hide();
      resolve(result);
    };
 
    UIViewportDialogService.show({
      viewportId,
      type: 'warning',
      message,
      actions,
      onSubmit,
      onOutsideClick: () => {
        UIViewportDialogService.hide();
        resolve(RESPONSE.CANCEL);
      },
    });
  });
}
 
export default promptTrackNewStudy;