Skip to content

Commit

Permalink
fix: import molecules id on loading json data (#1725)
Browse files Browse the repository at this point in the history
  • Loading branch information
wadjih-bencheikh18 committed Aug 25, 2022
1 parent 9000808 commit 5c43d1f
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 22 deletions.
5 changes: 3 additions & 2 deletions src/component/reducer/actions/LoadActions.ts
Expand Up @@ -2,9 +2,10 @@ import { Draft } from 'immer';
import { buildCorrelationData, CorrelationData } from 'nmr-correlation';

import { addJcamps, addJDFs } from '../../../data/SpectraManager';
import { Molecule } from '../../../data/molecules/Molecule';
import * as MoleculeManager from '../../../data/molecules/MoleculeManager';
import { UsedColors } from '../../../types/UsedColors';
import { Molecules, NMRiumPreferences, Spectra } from '../../NMRium';
import { NMRiumPreferences, Spectra } from '../../NMRium';
import { DefaultTolerance } from '../../panels/SummaryPanel/CorrelationTable/Constants';
import { getInitialState, State } from '../Reducer';
import { INITIATE, LOAD_JSON_FILE, LOAD_NMREDATA_FILE } from '../types/Types';
Expand All @@ -25,7 +26,7 @@ function setData(
draft: Draft<State>,
data: {
spectra: Spectra;
molecules: Molecules;
molecules: Molecule[];
preferences: NMRiumPreferences;
correlations: CorrelationData;
usedColors: UsedColors;
Expand Down
32 changes: 15 additions & 17 deletions src/data/SpectraManager.ts
@@ -1,15 +1,19 @@
import { fromJEOL, fromJCAMP, fromBruker } from 'nmr-parser';

import { State } from '../component/reducer/Reducer';
import { DISPLAYER_MODE } from '../component/reducer/core/Constants';
import { NMRiumDataReturn } from '../types/NMRiumDataReturn';
import { Preferences } from '../types/Preferences';

import * as Data1DManager from './data1d/Data1DManager';
import { SpectraAnalysis } from './data1d/MultipleAnalysis';
import * as Datum1D from './data1d/Spectrum1D';
import * as Data2DManager from './data2d/Data2DManager';
import * as Datum2D from './data2d/Spectrum2D';
import { CURRENT_EXPORT_VERSION } from './migration/MigrationManager';
import * as Molecule from './molecules/Molecule';
import { Datum1D as Datum1DType } from './types/data1d';
import { Datum2D as Datum2DType } from './types/data2d';

export enum DataExportOptions {
ROW_DATA = 'ROW_DATA',
Expand Down Expand Up @@ -274,39 +278,33 @@ function getPreferences(state): Preferences {
type JSONTarget = 'nmrium' | 'onDataChange';

export function toJSON(
state,
state: State,
target: JSONTarget,
dataExportOption: DataExportOptionsType = DataExportOptions.DATA_SOURCE,
): NMRiumDataReturn {
const {
data,
molecules: mols,
correlations,
multipleAnalysis,
actionType,
} = state || {
data: [],
molecules: [],
correlations: {},
multipleAnalysis: {},
actionType: '',
};
data = [],
molecules: mols = [],
correlations = {},
spectraAnalysis: multipleAnalysis = [],
actionType = '',
} = state;
const spectra = data.map((ob) => {
return ob.info.dimension === 1
? Datum1D.toJSON(ob, dataExportOption)
: Datum2D.toJSON(ob, dataExportOption);
? (Datum1D.toJSON(ob as Datum1DType, dataExportOption) as Datum1DType)
: (Datum2D.toJSON(ob as Datum2DType, dataExportOption) as Datum2DType);
});

const preferences = getPreferences(state);
const molecules = mols.map((mol) => Molecule.toJSON(mol));
const molecules = mols.map((mol: Molecule.Molecule) => Molecule.toJSON(mol));

return {
...(target === 'onDataChange' ? { actionType } : {}),
version: CURRENT_EXPORT_VERSION,
spectra,
molecules,
correlations,
multipleAnalysis,
multipleAnalysis: multipleAnalysis as SpectraAnalysis,
preferences,
};
}
3 changes: 2 additions & 1 deletion src/data/molecules/MoleculeManager.ts
Expand Up @@ -2,7 +2,7 @@ import { Molecule as OCLMolecule } from 'openchemlib/full';

import { initMolecule, Molecule } from './Molecule';

export function fromJSON(mols: any[] = []) {
export function fromJSON(mols: Molecule[] = []) {
const reservedNumbers = extractLabelsNumbers(mols);

const molecules: Molecule[] = [];
Expand All @@ -14,6 +14,7 @@ export function fromJSON(mols: any[] = []) {
initMolecule({
molfile: fragment.toMolfileV3(),
label: mol.label ? mol.label : `P${getLabelNumber(reservedNumbers)}`,
id: mol.id,
}),
);
}
Expand Down
4 changes: 2 additions & 2 deletions src/types/NMRiumDataReturn.ts
@@ -1,7 +1,7 @@
import { CorrelationData } from 'nmr-correlation';

import { SpectraAnalysis } from '../data/data1d/MultipleAnalysis';
import { Molecule } from '../data/molecules/Molecule';
import { MoleculeObject } from '../data/molecules/Molecule';
import { Datum1D } from '../data/types/data1d';
import { Datum2D } from '../data/types/data2d';

Expand All @@ -11,7 +11,7 @@ export interface NMRiumDataReturn {
actionType?: string;
version: number;
spectra: (Datum1D | Datum2D)[];
molecules: Molecule[];
molecules: MoleculeObject[];
correlations: CorrelationData;
preferences: Preferences;
multipleAnalysis: SpectraAnalysis;
Expand Down

0 comments on commit 5c43d1f

Please sign in to comment.