Skip to content

Commit 97885c7

Browse files
authored
Merge pull request scratchfoundation#5198 from benjiwheeler/block-cloud-on-embed
simplify props of cloud-manager-hoc
2 parents 79f3f6f + a23dd3e commit 97885c7

File tree

2 files changed

+18
-55
lines changed

2 files changed

+18
-55
lines changed

src/lib/cloud-manager-hoc.jsx

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,10 @@ const cloudManagerHOC = function (WrappedComponent) {
5454
canUseCloud (props) {
5555
return !!(props.cloudHost && props.username && props.vm && props.projectId && props.hasCloudPermission);
5656
}
57-
shouldNotModifyCloudData (props) {
58-
return (props.hasEverEnteredEditor && !props.canSave);
59-
}
6057
shouldConnect (props) {
6158
return !this.isConnected() && this.canUseCloud(props) &&
6259
props.isShowingWithId && props.vm.runtime.hasCloudData() &&
63-
!this.shouldNotModifyCloudData(props);
60+
props.canModifyCloudData;
6461
}
6562
shouldDisconnect (props, prevProps) {
6663
return this.isConnected() &&
@@ -70,7 +67,7 @@ const cloudManagerHOC = function (WrappedComponent) {
7067
(props.projectId !== prevProps.projectId) ||
7168
(props.username !== prevProps.username) ||
7269
// Editing someone else's project
73-
this.shouldNotModifyCloudData(props)
70+
!props.canModifyCloudData
7471
);
7572
}
7673
isConnected () {
@@ -102,11 +99,11 @@ const cloudManagerHOC = function (WrappedComponent) {
10299
render () {
103100
const {
104101
/* eslint-disable no-unused-vars */
102+
canModifyCloudData,
105103
cloudHost,
106104
projectId,
107105
username,
108106
hasCloudPermission,
109-
hasEverEnteredEditor,
110107
isShowingWithId,
111108
onShowCloudInfo,
112109
/* eslint-enable no-unused-vars */
@@ -124,23 +121,30 @@ const cloudManagerHOC = function (WrappedComponent) {
124121
}
125122

126123
CloudManager.propTypes = {
127-
canSave: PropTypes.bool.isRequired,
124+
canModifyCloudData: PropTypes.bool.isRequired,
128125
cloudHost: PropTypes.string,
129126
hasCloudPermission: PropTypes.bool,
130-
hasEverEnteredEditor: PropTypes.bool,
131-
isShowingWithId: PropTypes.bool,
127+
isShowingWithId: PropTypes.bool.isRequired,
132128
onShowCloudInfo: PropTypes.func,
133129
projectId: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
134130
username: PropTypes.string,
135131
vm: PropTypes.instanceOf(VM).isRequired
136132
};
137133

138-
const mapStateToProps = state => {
134+
CloudManager.defaultProps = {
135+
cloudHost: null,
136+
hasCloudPermission: false,
137+
onShowCloudInfo: () => {},
138+
username: null
139+
};
140+
141+
const mapStateToProps = (state, ownProps) => {
139142
const loadingState = state.scratchGui.projectState.loadingState;
140143
return {
141-
hasEverEnteredEditor: state.scratchGui.mode.hasEverEnteredEditor,
142144
isShowingWithId: getIsShowingWithId(loadingState),
143-
projectId: state.scratchGui.projectState.projectId
145+
projectId: state.scratchGui.projectState.projectId,
146+
// if you're editing someone else's project, you can't modify cloud data
147+
canModifyCloudData: (!state.scratchGui.mode.hasEverEnteredEditor || ownProps.canSave)
144148
};
145149
};
146150

test/unit/util/cloud-manager-hoc.test.jsx

Lines changed: 2 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ describe('CloudManagerHOC', () => {
6060

6161
mount(
6262
<WrappedComponent
63-
canSave
6463
hasCloudPermission
6564
cloudHost="nonEmpty"
6665
store={store}
@@ -80,7 +79,6 @@ describe('CloudManagerHOC', () => {
8079
const WrappedComponent = cloudManagerHOC(Component);
8180
mount(
8281
<WrappedComponent
83-
canSave
8482
hasCloudPermission
8583
store={store}
8684
username="user"
@@ -98,7 +96,6 @@ describe('CloudManagerHOC', () => {
9896
const WrappedComponent = cloudManagerHOC(Component);
9997
mount(
10098
<WrappedComponent
101-
canSave
10299
hasCloudPermission
103100
cloudHost="nonEmpty"
104101
store={store}
@@ -115,7 +112,6 @@ describe('CloudManagerHOC', () => {
115112
const WrappedComponent = cloudManagerHOC(Component);
116113
mount(
117114
<WrappedComponent
118-
canSave
119115
hasCloudPermission
120116
cloudHost="nonEmpty"
121117
store={stillLoadingStore}
@@ -132,7 +128,6 @@ describe('CloudManagerHOC', () => {
132128
const WrappedComponent = cloudManagerHOC(Component);
133129
mount(
134130
<WrappedComponent
135-
canSave
136131
cloudHost="nonEmpty"
137132
hasCloudPermission={false}
138133
store={store}
@@ -153,7 +148,6 @@ describe('CloudManagerHOC', () => {
153148

154149
const mounted = mount(
155150
<WrappedComponent
156-
canSave
157151
hasCloudPermission
158152
cloudHost="nonEmpty"
159153
store={stillLoadingStore}
@@ -182,7 +176,6 @@ describe('CloudManagerHOC', () => {
182176
const WrappedComponent = cloudManagerHOC(Component);
183177
const mounted = mount(
184178
<WrappedComponent
185-
canSave
186179
hasCloudPermission
187180
cloudHost="nonEmpty"
188181
store={stillLoadingStore}
@@ -209,7 +202,6 @@ describe('CloudManagerHOC', () => {
209202
const WrappedComponent = cloudManagerHOC(Component);
210203
const mounted = mount(
211204
<WrappedComponent
212-
canSave
213205
hasCloudPermission
214206
cloudHost="nonEmpty"
215207
store={store}
@@ -235,7 +227,6 @@ describe('CloudManagerHOC', () => {
235227
const WrappedComponent = cloudManagerHOC(Component);
236228
const mounted = mount(
237229
<WrappedComponent
238-
canSave
239230
hasCloudPermission
240231
cloudHost="nonEmpty"
241232
store={store}
@@ -262,7 +253,6 @@ describe('CloudManagerHOC', () => {
262253
const WrappedComponent = cloudManagerHOC(Component);
263254
const mounted = mount(
264255
<WrappedComponent
265-
canSave
266256
hasCloudPermission
267257
cloudHost="nonEmpty"
268258
store={store}
@@ -293,7 +283,6 @@ describe('CloudManagerHOC', () => {
293283
const WrappedComponent = cloudManagerHOC(Component);
294284
mount(
295285
<WrappedComponent
296-
canSave
297286
hasCloudPermission
298287
cloudHost="nonEmpty"
299288
store={store}
@@ -315,7 +304,6 @@ describe('CloudManagerHOC', () => {
315304
const WrappedComponent = cloudManagerHOC(Component);
316305
mount(
317306
<WrappedComponent
318-
canSave
319307
hasCloudPermission
320308
cloudHost="nonEmpty"
321309
store={store}
@@ -343,7 +331,6 @@ describe('CloudManagerHOC', () => {
343331
const WrappedComponent = cloudManagerHOC(Component);
344332
mount(
345333
<WrappedComponent
346-
canSave
347334
hasCloudPermission
348335
cloudHost="nonEmpty"
349336
store={store}
@@ -364,12 +351,11 @@ describe('CloudManagerHOC', () => {
364351
});
365352

366353
// Editor Mode Connection/Disconnection Tests
367-
test('Entering editor mode and can\'t save project should disconnect cloud provider # 1', () => {
354+
test('Entering editor mode and can\'t save project should disconnect cloud provider', () => {
368355
const Component = () => <div />;
369356
const WrappedComponent = cloudManagerHOC(Component);
370357
const mounted = mount(
371358
<WrappedComponent
372-
canSave
373359
hasCloudPermission
374360
cloudHost="nonEmpty"
375361
store={store}
@@ -382,34 +368,7 @@ describe('CloudManagerHOC', () => {
382368
const requestCloseConnection = mockCloudProviderInstance.requestCloseConnection;
383369

384370
mounted.setProps({
385-
canSave: false,
386-
hasEverEnteredEditor: true
387-
});
388-
389-
expect(vm.setCloudProvider.mock.calls.length).toBe(2);
390-
expect(vm.setCloudProvider).toHaveBeenCalledWith(null);
391-
expect(requestCloseConnection).toHaveBeenCalledTimes(1);
392-
});
393-
394-
test('Entering editor mode and can\'t save project should disconnect cloud provider # 2', () => {
395-
const Component = () => <div />;
396-
const WrappedComponent = cloudManagerHOC(Component);
397-
const mounted = mount(
398-
<WrappedComponent
399-
hasCloudPermission
400-
canSave={false}
401-
cloudHost="nonEmpty"
402-
store={store}
403-
username="user"
404-
vm={vm}
405-
/>
406-
);
407-
408-
expect(CloudProvider).toHaveBeenCalled();
409-
const requestCloseConnection = mockCloudProviderInstance.requestCloseConnection;
410-
411-
mounted.setProps({
412-
hasEverEnteredEditor: true
371+
canModifyCloudData: false
413372
});
414373

415374
expect(vm.setCloudProvider.mock.calls.length).toBe(2);

0 commit comments

Comments
 (0)