Skip to content

Commit 7d9a482

Browse files
author
Mingze
authored
fix(creator): Prevent creating during creator pending (#643)
* fix(creator): Prevent creating during creator pending * fix(creator): Address comments
1 parent 40be2ee commit 7d9a482

File tree

8 files changed

+63
-20
lines changed

8 files changed

+63
-20
lines changed

src/drawing/DrawingAnnotations.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ const DrawingAnnotations = (props: Props): JSX.Element => {
5757
onSelect={handleAnnotationActive}
5858
/>
5959

60-
{isCreating && hasDrawnPathGroups && (
60+
{hasDrawnPathGroups && (
6161
<DrawingSVG ref={setStagedRootEl} className="ba-DrawingAnnotations-target">
6262
{/* Group element to capture the bounding box around the drawn path groups */}
6363
<g data-ba-reference-id={uuidRef.current}>

src/drawing/DrawingAnnotationsContainer.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ import {
99
import { AnnotationDrawing, PathGroup } from '../@types';
1010
import {
1111
AppState,
12+
CreatorStatus,
1213
getActiveAnnotationId,
1314
getAnnotationMode,
1415
getAnnotationsForLocation,
16+
getCreatorStatus,
1517
isFeatureEnabled,
1618
Mode,
1719
setActiveAnnotationIdAction,
@@ -32,7 +34,10 @@ export const mapStateToProps = (state: AppState, { location }: { location: numbe
3234
activeAnnotationId: getActiveAnnotationId(state),
3335
annotations: getAnnotationsForLocation(state, location).filter(isDrawing),
3436
drawnPathGroups: getDrawingDrawnPathGroupsForLocation(state, location),
35-
isCreating: isFeatureEnabled(state, 'drawingCreate') && getAnnotationMode(state) === Mode.DRAWING,
37+
isCreating:
38+
isFeatureEnabled(state, 'drawingCreate') &&
39+
getAnnotationMode(state) === Mode.DRAWING &&
40+
getCreatorStatus(state) !== CreatorStatus.pending,
3641
};
3742
};
3843

src/drawing/__tests__/DrawingAnnotations-test.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,10 @@ describe('DrawingAnnotations', () => {
9191
});
9292

9393
test('should render staged drawing if present and allowed', () => {
94-
const wrapper = getWrapper({ isCreating: true, drawnPathGroups: pathGroups });
94+
const wrapper = getWrapper({ drawnPathGroups: pathGroups });
9595

9696
expect(wrapper.find(DrawingList).exists()).toBe(true);
9797
expect(wrapper.find(DrawingSVG).exists()).toBe(true);
98-
expect(wrapper.find(DrawingCreator).exists()).toBe(true);
9998
});
10099
});
101100
});

src/drawing/__tests__/DrawingAnnotationsContainer-test.tsx

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Store } from 'redux';
44
import { mount, ReactWrapper } from 'enzyme';
55
import DrawingAnnotations from '../DrawingAnnotations';
66
import DrawingAnnotationsContainer, { Props } from '../DrawingAnnotationsContainer';
7-
import { createStore } from '../../store';
7+
import { createStore, CreatorStatus, Mode } from '../../store';
88

99
jest.mock('../../common/withProviders');
1010

@@ -33,5 +33,25 @@ describe('DrawingAnnotationsContainer', () => {
3333
isCreating: false,
3434
});
3535
});
36+
37+
test.each`
38+
mode | status | isCreating
39+
${Mode.NONE} | ${CreatorStatus.staged} | ${false}
40+
${Mode.DRAWING} | ${CreatorStatus.staged} | ${true}
41+
${Mode.DRAWING} | ${CreatorStatus.pending} | ${false}
42+
${Mode.REGION} | ${CreatorStatus.staged} | ${false}
43+
`(
44+
'should pass down isCreating as $isCreating if mode is $mode and status is $status',
45+
({ mode, isCreating, status }) => {
46+
const store = createStore({
47+
common: { mode },
48+
creator: { status },
49+
options: { features: { drawingCreate: true } },
50+
});
51+
const wrapper = getWrapper({ store });
52+
53+
expect(wrapper.find(DrawingAnnotations).prop('isCreating')).toEqual(isCreating);
54+
},
55+
);
3656
});
3757
});

src/region/RegionCreation.tsx

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,22 +55,23 @@ export default class RegionCreation extends React.PureComponent<Props, State> {
5555

5656
render(): JSX.Element | null {
5757
const { isCreating, isDiscoverabilityEnabled, isRotated, staged } = this.props;
58-
const canCreate = isCreating && !isRotated;
5958

60-
if (!canCreate) {
59+
if (isRotated) {
6160
return null;
6261
}
6362

6463
return (
6564
<>
66-
<RegionCreator
67-
className={classNames('ba-RegionCreation-creator', {
68-
'is-discoverability-enabled': isDiscoverabilityEnabled,
69-
})}
70-
onAbort={this.handleAbort}
71-
onStart={this.handleStart}
72-
onStop={this.handleStop}
73-
/>
65+
{isCreating && (
66+
<RegionCreator
67+
className={classNames('ba-RegionCreation-creator', {
68+
'is-discoverability-enabled': isDiscoverabilityEnabled,
69+
})}
70+
onAbort={this.handleAbort}
71+
onStart={this.handleStart}
72+
onStop={this.handleStop}
73+
/>
74+
)}
7475

7576
{staged && (
7677
<div className="ba-RegionCreation-target">

src/region/RegionCreationContainer.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ import { connect } from 'react-redux';
22
import {
33
AppState,
44
CreatorItemRegion,
5+
CreatorStatus,
56
getAnnotationMode,
67
getCreatorStagedForLocation,
8+
getCreatorStatus,
79
getRotation,
810
isCreatorStagedRegion,
911
isFeatureEnabled,
@@ -27,7 +29,7 @@ export const mapStateToProps = (state: AppState, { location }: { location: numbe
2729
const staged = getCreatorStagedForLocation(state, location);
2830

2931
return {
30-
isCreating: getAnnotationMode(state) === Mode.REGION,
32+
isCreating: getAnnotationMode(state) === Mode.REGION && getCreatorStatus(state) !== CreatorStatus.pending,
3133
isDiscoverabilityEnabled: isFeatureEnabled(state, 'discoverability'),
3234
isRotated: !!getRotation(state),
3335
staged: isCreatorStagedRegion(staged) ? staged : null,

src/region/__tests__/RegionCreation-test.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,15 +99,13 @@ describe('RegionCreation', () => {
9999
test('should render a RegionAnnotation if one has been drawn', () => {
100100
const shape = getRect();
101101
const wrapper = getWrapper({
102-
isCreating: true,
103102
staged: {
104103
location: 1,
105104
message: null,
106105
shape,
107106
},
108107
});
109108

110-
expect(wrapper.exists(RegionCreator)).toBe(true);
111109
expect(wrapper.find(RegionRect).prop('shape')).toEqual(shape);
112110
});
113111

@@ -122,7 +120,9 @@ describe('RegionCreation', () => {
122120

123121
test('should not render creation components if file is rotated', () => {
124122
const wrapper = getWrapper({
125-
isRotated: false,
123+
isCreating: true,
124+
isRotated: true,
125+
staged: {},
126126
});
127127

128128
expect(wrapper.exists(RegionCreator)).toBe(false);

src/region/__tests__/RegionCreationContainer-test.tsx

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { IntlShape } from 'react-intl';
33
import { mount, ReactWrapper } from 'enzyme';
44
import RegionCreation from '../RegionCreation';
55
import RegionCreationContainer, { Props } from '../RegionCreationContainer';
6-
import { createStore } from '../../store';
6+
import { createStore, CreatorStatus, Mode } from '../../store';
77

88
jest.mock('../../common/withProviders');
99
jest.mock('../RegionCreation');
@@ -33,6 +33,22 @@ describe('RegionCreationContainer', () => {
3333
});
3434
});
3535

36+
test.each`
37+
mode | status | isCreating
38+
${Mode.NONE} | ${CreatorStatus.staged} | ${false}
39+
${Mode.HIGHLIGHT} | ${CreatorStatus.staged} | ${false}
40+
${Mode.REGION} | ${CreatorStatus.staged} | ${true}
41+
${Mode.REGION} | ${CreatorStatus.pending} | ${false}
42+
`(
43+
'should pass down isCreating as $isCreating if mode is $mode and status is $status',
44+
({ mode, isCreating, status }) => {
45+
const store = createStore({ common: { mode }, creator: { status } });
46+
const wrapper = getWrapper({ store });
47+
48+
expect(wrapper.find(RegionCreation).prop('isCreating')).toEqual(isCreating);
49+
},
50+
);
51+
3652
test.each`
3753
rotation | isRotated
3854
${null} | ${false}

0 commit comments

Comments
 (0)