Skip to content

Commit 0c1d652

Browse files
author
Lan Le
committed
feat: WIP add remove all peaks button
1 parent c896f88 commit 0c1d652

File tree

8 files changed

+137
-10
lines changed

8 files changed

+137
-10
lines changed

src/__tests__/units/actions/edit_peak.test.tsx

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { rmFromNegList, rmFromPosList } from "../../../actions/edit_peak";
1+
import { rmFromNegList, rmFromPosList, clearAllPeaks } from "../../../actions/edit_peak";
22
import { EDITPEAK } from "../../../constants/action_type";
33

44
describe('Test redux action for edit peaks', () => {
@@ -15,4 +15,11 @@ describe('Test redux action for edit peaks', () => {
1515
expect(type).toEqual(EDITPEAK.RM_NEGATIVE)
1616
expect(payload).toEqual(peak)
1717
})
18+
19+
it('Remove all peaks', () => {
20+
const curveIdx = 0
21+
const { type, payload } = clearAllPeaks(curveIdx)
22+
expect(type).toEqual(EDITPEAK.CLEAR_ALL)
23+
expect(payload).toEqual(curveIdx)
24+
})
1825
})

src/__tests__/units/components/cmd_bar/03_peak.test.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ const mockStore = configureStore([]);
1010
const store = mockStore({
1111
ui:{ sweepType: LIST_UI_SWEEP_TYPE.ZOOMIN },
1212
layout: LIST_LAYOUT.MS,
13+
curve: { curveIdx: 0 },
1314
});
1415
const nmrStore = mockStore({
1516
ui:{ sweepType: LIST_UI_SWEEP_TYPE.ZOOMIN },
1617
layout: LIST_LAYOUT.H1,
18+
curve: { curveIdx: 0 },
1719
});
1820

1921
const dispatchMock = () => Promise.resolve({});
@@ -37,7 +39,7 @@ describe('<Peak />', () => {
3739
const { queryByTestId } = render(renderer);
3840
const renderResult = queryByTestId('Peak');
3941
expect(renderResult).toBeInTheDocument();
40-
expect(renderResult.childElementCount).toEqual(3);
42+
expect(renderResult.childElementCount).toEqual(4);
4143
});
4244

4345
it('render when does not hav Set reference button', async () => {
@@ -49,6 +51,6 @@ describe('<Peak />', () => {
4951
const { queryByTestId } = render(renderer);
5052
const renderResult = queryByTestId('Peak');
5153
expect(renderResult).toBeInTheDocument();
52-
expect(renderResult.childElementCount).toEqual(2);
54+
expect(renderResult.childElementCount).toEqual(3);
5355
});
5456
})
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,84 @@
11
import undoableEditPeakReducer from "../../../reducers/reducer_edit_peak";
2+
import { EDITPEAK, MANAGER } from "../../../constants/action_type";
3+
import { editPeakReducer } from "../../../reducers/reducer_edit_peak";
24

35
describe('Test redux reducer for edit peak', () => {
4-
// TODO: need more test implementation
5-
it('Just a dump test', () => {
6-
expect(1).toEqual(1)
6+
interface PeaksState {
7+
selectedIdx: number,
8+
peaks: Object[]
9+
}
10+
11+
interface PeaksAction {
12+
type: string
13+
payload: any
14+
}
15+
16+
let peaksState: PeaksState
17+
let action: PeaksAction
18+
19+
beforeEach(() => {
20+
peaksState = { peaks: [], selectedIdx: 0 }
21+
action = { type: "", payload: null }
22+
})
23+
24+
it('Get default state', () => {
25+
const newState = undoableEditPeakReducer(peaksState, action)
26+
expect(newState.present).toEqual(peaksState)
27+
})
28+
29+
it('Add new peak to positive peaks', () => {
30+
action.type = EDITPEAK.ADD_POSITIVE
31+
action.payload = { dataToAdd: {x: 1, y: 2}, curveIdx: 0 }
32+
33+
const newState = editPeakReducer(peaksState, action)
34+
const expectedValue = [action.payload.dataToAdd]
35+
expect(newState.peaks[0].pos).toEqual(expectedValue)
36+
})
37+
38+
it('Add new peak to negative peaks', () => {
39+
action.type = EDITPEAK.ADD_NEGATIVE
40+
action.payload = { dataToAdd: {x: 1, y: 2}, curveIdx: 0 }
41+
42+
const newState = editPeakReducer(peaksState, action)
43+
const expectedValue = [action.payload.dataToAdd]
44+
expect(newState.peaks[0].neg).toEqual(expectedValue)
45+
})
46+
47+
it('Remove a peak from positive peaks', () => {
48+
action.type = EDITPEAK.RM_POSITIVE
49+
action.payload = {x: 1, y: 2}
50+
51+
peaksState = { peaks: [{pos: [{x: 1, y: 2}, {x: 3, y: 4}]}], selectedIdx: 0 }
52+
53+
const newState = editPeakReducer(peaksState, action)
54+
const expectedValue = [{x: 3, y: 4}]
55+
expect(newState.peaks[0].pos).toEqual(expectedValue)
56+
})
57+
58+
it('Remove a peak from negative peaks', () => {
59+
action.type = EDITPEAK.RM_NEGATIVE
60+
action.payload = {x: 1, y: 2}
61+
62+
peaksState = { peaks: [{neg: [{x: 1, y: 2}, {x: 3, y: 4}]}], selectedIdx: 0 }
63+
64+
const newState = editPeakReducer(peaksState, action)
65+
const expectedValue = [{x: 3, y: 4}]
66+
expect(newState.peaks[0].neg).toEqual(expectedValue)
67+
})
68+
69+
it('Reset to default value', () => {
70+
action.type = MANAGER.RESETALL
71+
const expectedValue = {peaks: [{neg: [], pos: [], prevOffset: 0}], selectedIdx: 0}
72+
const newState = editPeakReducer(peaksState, action)
73+
expect(newState).toEqual(expectedValue)
74+
})
75+
76+
it('Clear all peaks', () => {
77+
action.type = EDITPEAK.CLEAR_ALL
78+
action.payload = { curveIdx: 0 }
79+
peaksState = { peaks: [{pos: [{x: 1, y: 2}, {x: 3, y: 4}]}], selectedIdx: 0 }
80+
const expectedValue = {peaks: [{neg: [], pos: [], prevOffset: 0}], selectedIdx: 0}
81+
const newState = editPeakReducer(peaksState, action)
82+
expect(newState).toEqual(expectedValue)
783
})
884
})

src/actions/edit_peak.js

+8
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,15 @@ const rmFromNegList = (payload) => (
1414
}
1515
);
1616

17+
const clearAllPeaks = (payload) => (
18+
{
19+
type: EDITPEAK.CLEAR_ALL,
20+
payload,
21+
}
22+
);
23+
1724
export {
1825
rmFromPosList,
1926
rmFromNegList,
27+
clearAllPeaks,
2028
};

src/components/cmd_bar/03_peak.js

+14-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import { setUiSweepType } from '../../actions/ui';
1515
import Cfg from '../../helpers/cfg';
1616
import { MuButton, commonStyle, focusStyle } from './common';
1717
import { LIST_UI_SWEEP_TYPE } from '../../constants/list_ui';
18+
import TriBtn from './tri_btn';
19+
import { clearAllPeaks } from '../../actions/edit_peak';
1820

1921
const styles = () => (
2022
Object.assign(
@@ -30,12 +32,14 @@ const Peak = ({
3032
isFocusSetRefSt, disableSetRefSt,
3133
isHandleMaxAndMinPeaksSt,
3234
cyclicVotaSt, curveSt,
35+
clearAllPeaksAct,
3336
}) => {
3437
let onSweepPeakAdd = () => setUiSweepTypeAct(LIST_UI_SWEEP_TYPE.PEAK_ADD);
3538
let onSweepPeakDELETE = () => setUiSweepTypeAct(LIST_UI_SWEEP_TYPE.PEAK_DELETE);
3639
let onSweepAnchorShift = () => setUiSweepTypeAct(LIST_UI_SWEEP_TYPE.ANCHOR_SHIFT);
40+
const { curveIdx } = curveSt;
41+
const onClearAll = () => clearAllPeaksAct({ curveIdx });
3742
if (isHandleMaxAndMinPeaksSt) {
38-
const { curveIdx } = curveSt;
3943
const { spectraList } = cyclicVotaSt;
4044
const spectra = spectraList[curveIdx];
4145
if (spectra) {
@@ -85,6 +89,13 @@ const Peak = ({
8589
</MuButton>
8690
</span>
8791
</Tooltip>
92+
<TriBtn
93+
content={{ tp: 'Clear All Peaks' }}
94+
cb={onClearAll}
95+
>
96+
<span className={classNames(classes.txt, 'txt-sv-bar-rmallpeaks')}>P</span>
97+
<span className={classNames(classes.txt, classes.txtIcon, 'txt-sv-bar-rmallpeaks')}>x</span>
98+
</TriBtn>
8899
{
89100
!disableSetRefSt ? (
90101
<Tooltip title={<span className="txt-sv-tp">Set Reference</span>}>
@@ -126,6 +137,7 @@ const mapStateToProps = (state, _) => ( // eslint-disable-line
126137
const mapDispatchToProps = (dispatch) => (
127138
bindActionCreators({
128139
setUiSweepTypeAct: setUiSweepType,
140+
clearAllPeaksAct: clearAllPeaks,
129141
}, dispatch)
130142
);
131143

@@ -141,6 +153,7 @@ Peak.propTypes = {
141153
isHandleMaxAndMinPeaksSt: PropTypes.bool.isRequired,
142154
cyclicVotaSt: PropTypes.object.isRequired,
143155
curveSt: PropTypes.object.isRequired,
156+
clearAllPeaksAct: PropTypes.func.isRequired,
144157
};
145158

146159
export default compose(

src/constants/action_type.js

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const EDITPEAK = {
1212
RM_NEGATIVE: 'RM_FROM_NEGATIVE_EDITPEAK_LIST',
1313
RM_POSITIVE: 'RM_FROM_POSITIVE_EDITPEAK_LIST',
1414
SHIFT: 'EDITPEAK_SHIFT',
15+
CLEAR_ALL: 'EDITPEAK_CLEAR_ALL',
1516
};
1617

1718
const STATUS = {

src/reducers/reducer_edit_peak.js

+20
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,21 @@ const processShift = (state, action) => {
141141
return Object.assign({}, state, { peaks: newPeaks });
142142
};
143143

144+
const clearAllPeaks = (state, action) => {
145+
const { curveIdx } = action.payload;
146+
const { peaks } = state;
147+
const selectedEditPeaks = peaks[curveIdx];
148+
149+
const { pos } = selectedEditPeaks;
150+
151+
const newSelectedEditPeaks = Object.assign({}, selectedEditPeaks, { pos: [], neg: [...pos] });
152+
const newPeaks = [...peaks];
153+
newPeaks[curveIdx] = newSelectedEditPeaks;
154+
return Object.assign({}, state, { peaks: newPeaks });
155+
};
156+
144157
const editPeakReducer = (state = initialState, action) => {
158+
console.log(action);
145159
switch (action.type) {
146160
case EDITPEAK.ADD_POSITIVE:
147161
return addToPos(state, action);
@@ -153,6 +167,8 @@ const editPeakReducer = (state = initialState, action) => {
153167
return rmFromNeg(state, action);
154168
case EDITPEAK.SHIFT:
155169
return processShift(state, action);
170+
case EDITPEAK.CLEAR_ALL:
171+
return clearAllPeaks(state, action);
156172
case MANAGER.RESETALL:
157173
return {
158174
selectedIdx: 0,
@@ -176,4 +192,8 @@ const undoableEditPeakReducer = undoable(
176192
undoRedoConfig,
177193
);
178194

195+
export {
196+
editPeakReducer,
197+
};
198+
179199
export default undoableEditPeakReducer;

src/reducers/undo_redo_config.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
const undoRedoActions = [
77
EDITPEAK.ADD_POSITIVE, EDITPEAK.ADD_NEGATIVE,
88
EDITPEAK.RM_POSITIVE, EDITPEAK.RM_NEGATIVE,
9-
EDITPEAK.SHIFT,
9+
EDITPEAK.SHIFT, EDITPEAK.CLEAR_ALL,
1010
MANAGER.RESETALL, MANAGER.RESETSHIFT, MANAGER.RESET_INIT_COMMON, MANAGER.RESET_INIT_NMR,
1111
MANAGER.RESET_INIT_MS, MANAGER.RESET_INIT_COMMON_WITH_INTERGATION,
1212
UI.SWEEP.SELECT_INTEGRATION,
@@ -26,12 +26,12 @@ const undoRedoConfig = {
2626
ignoreInitialState: true,
2727
filter: includeAction(undoRedoActions),
2828
clearHistoryType: [
29-
EDITPEAK.SHIFT,
29+
EDITPEAK.SHIFT, EDITPEAK.CLEAR_ALL,
3030
MANAGER.RESETALL, MANAGER.RESETSHIFT, MANAGER.RESET_INIT_COMMON, MANAGER.RESET_INIT_NMR,
3131
MANAGER.RESET_INIT_MS, MANAGER.RESET_INIT_COMMON_WITH_INTERGATION,
3232
],
3333
neverSkipReducer: [
34-
EDITPEAK.SHIFT,
34+
EDITPEAK.SHIFT, EDITPEAK.CLEAR_ALL,
3535
MANAGER.RESETALL, MANAGER.RESETSHIFT, MANAGER.RESET_INIT_COMMON, MANAGER.RESET_INIT_NMR,
3636
MANAGER.RESET_INIT_MS, MANAGER.RESET_INIT_COMMON_WITH_INTERGATION,
3737
],

0 commit comments

Comments
 (0)