Skip to content

Commit

Permalink
fix: bug when drag / drop second NMRium file
Browse files Browse the repository at this point in the history
close #1421
  • Loading branch information
hamed-musallam committed Mar 11, 2022
1 parent 7683a67 commit fe64cb1
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 18 deletions.
6 changes: 2 additions & 4 deletions src/component/1d/FooterBanner.tsx
Expand Up @@ -95,7 +95,7 @@ function FooterBannerInner({

const getYValue = useCallback(
(xPosition) => {
if (spectrum.data) {
if (spectrum) {
const data = get1DDataXY(spectrum);
const xIndex = xFindClosestIndex(data.x, scaleX().invert(xPosition));
return data.y[xIndex];
Expand Down Expand Up @@ -192,11 +192,9 @@ function FooterBannerInner({

const MemoizedFooterBanner = memo(FooterBannerInner);

const emptyData = { info: {}, data: {} };

export default function FooterBanner() {
const { margin, width, height, activeSpectrum, activeTab } = useChartData();
const spectrum = useSpectrum(emptyData) as Datum1D;
const spectrum = useSpectrum(null) as Datum1D;
return (
<MemoizedFooterBanner
{...{ margin, width, height, activeSpectrum, spectrum, activeTab }}
Expand Down
12 changes: 8 additions & 4 deletions src/component/reducer/Reducer.ts
Expand Up @@ -55,8 +55,7 @@ export interface Margin {
bottom: number;
left: number;
}

export const initialState: State = {
export const getInitialState = (): State => ({
actionType: '',
data: [],
contours: {} as Contours,
Expand Down Expand Up @@ -122,7 +121,9 @@ export const initialState: State = {
},
},
usedColors: { '1d': [], '2d': [] },
};
});

export const initialState = getInitialState();

export type VerticalAlignment = 'bottom' | 'center' | 'stack';
export interface VerticalAlign {
Expand Down Expand Up @@ -452,7 +453,10 @@ export function dispatchMiddleware(dispatch) {
}

function innerSpectrumReducer(draft: Draft<State>, action) {
draft.actionType = action.type;
if (![types.LOAD_JSON_FILE, types.LOAD_NMREDATA_FILE].includes(action.type)) {
draft.actionType = action.type;
}

switch (action.type) {
case types.INITIATE:
return LoadActions.initiate(draft, action);
Expand Down
34 changes: 24 additions & 10 deletions src/component/reducer/actions/LoadActions.ts
Expand Up @@ -6,7 +6,8 @@ import * as MoleculeManager from '../../../data/molecules/MoleculeManager';
import { UsedColors } from '../../../types/UsedColors';
import { Molecules, NMRiumPreferences, Spectra } from '../../NMRium';
import { DefaultTolerance } from '../../panels/SummaryPanel/CorrelationTable/Constants';
import { State } from '../Reducer';
import { getInitialState, State } from '../Reducer';
import { LOAD_JSON_FILE, LOAD_NMREDATA_FILE } from '../types/Types';

import { changeSpectrumVerticalAlignment } from './PreferencesActions';
import { setActiveTab } from './ToolsActions';
Expand Down Expand Up @@ -125,12 +126,18 @@ function loadJcampFile(draft: Draft<State>, actions) {
}

function handleLoadJsonFile(draft: Draft<State>, action) {
setData(draft, action.payload);
const preferences = action.payload?.preferences || {};
setActiveTab(draft, { tab: preferences?.activeTab || '' });
setPreferences(draft, preferences);
const state = getInitialState();

draft.isLoading = false;
setData(state, action.payload);
const preferences = action.payload?.preferences || {};
setActiveTab(state, { tab: preferences?.activeTab || '' });
setPreferences(state, preferences);

state.width = draft.width;
state.height = draft.height;
state.isLoading = false;
state.actionType = LOAD_JSON_FILE;
return state;
}

function handleLoadMOLFile(draft: Draft<State>, actions) {
Expand All @@ -153,10 +160,17 @@ function handleLoadZIPFile(draft: Draft<State>, action) {
}

function handleLoadNmredata(draft: Draft<State>, action) {
setData(draft, action.payload);
setActiveTab(draft);
changeSpectrumVerticalAlignment(draft, { align: 'auto-check' });
draft.isLoading = false;
const state = getInitialState();

setData(state, action.payload);
setActiveTab(state);
changeSpectrumVerticalAlignment(state, { align: 'auto-check' });
state.isLoading = false;
state.width = draft.width;
state.height = draft.height;
state.actionType = LOAD_NMREDATA_FILE;

return state;
}

export {
Expand Down

0 comments on commit fe64cb1

Please sign in to comment.