Skip to content

Commit

Permalink
feat: triggering dataChange event when NMRium data Changed
Browse files Browse the repository at this point in the history
  • Loading branch information
hamed-musallam committed Jun 13, 2022
1 parent 8a0eb58 commit 9639440
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
12 changes: 8 additions & 4 deletions src/NMRiumWrapper.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
/** @jsxImportSource @emotion/react */
import { css } from '@emotion/react';
import NMRium, { NMRiumData } from 'nmrium';
import { useEffect, useState } from 'react';
import { useEffect, useState, useCallback } from 'react';
import events from './events';
import useActions from './hooks/useActions';
import { usePreferences } from './hooks/usePreferences';
import { useLoadSpectraFromURL } from './hooks/useLoadSpectraFromURL';

Expand All @@ -30,10 +29,15 @@ const styles = {
`,
};

export type { NMRiumData };

export default function NMRiumWrapper() {
const [data, setDate] = useState<NMRiumData>();
const { workspace, preferences } = usePreferences();
const actionHandler = useActions();
const dataChangeHandler = useCallback((nmriumData) => {
events.trigger('dataChange', nmriumData);
}, []);

const {
load: loadFromURLs,
isLoading,
Expand Down Expand Up @@ -74,7 +78,7 @@ export default function NMRiumWrapper() {
)}
<NMRium
data={data}
onDataChange={actionHandler}
onDataChange={dataChangeHandler}
preferences={preferences}
workspace={workspace}
/>
Expand Down
10 changes: 4 additions & 6 deletions src/actions/types.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
import { NMRiumData } from 'nmrium';
import { State } from 'nmrium/lib/component/reducer/Reducer';

type EventType = 'load' | 'test' | 'loadURLs' | 'error';
type EventType = 'load' | 'dataChange' | 'loadURLs' | 'error';

interface TestData {
testData: string;
}
interface LoadURLs {
urls: string[];
}

type EventData<T extends EventType> = T extends 'load'
? NMRiumData
: T extends 'test'
? TestData
: T extends 'dataChange'
? State
: T extends 'loadURLs'
? LoadURLs
: T extends 'error'
Expand Down

0 comments on commit 9639440

Please sign in to comment.