Skip to content
This repository was archived by the owner on Dec 15, 2022. It is now read-only.

Commit 9446510

Browse files
committed
Now DialogsController is simpler too
1 parent b4056d3 commit 9446510

File tree

2 files changed

+23
-55
lines changed

2 files changed

+23
-55
lines changed

lib/controllers/dialogs-controller.js

Lines changed: 21 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
import React from 'react';
22
import PropTypes from 'prop-types';
33

4-
import Panel from '../atom/panel';
54
import InitDialog from '../views/init-dialog';
65
import CloneDialog from '../views/clone-dialog';
76
import CredentialDialog from '../views/credential-dialog';
87
import OpenIssueishDialog from '../views/open-issueish-dialog';
98
import OpenCommitDialog from '../views/open-commit-dialog';
109

10+
const DIALOG_COMPONENTS = {
11+
null: NullDialog,
12+
init: InitDialog,
13+
clone: CloneDialog,
14+
credential: CredentialDialog,
15+
issueish: OpenIssueishDialog,
16+
commit: OpenCommitDialog,
17+
};
18+
1119
export default class DialogsController extends React.Component {
1220
static propTypes = {
1321
// Model
@@ -22,58 +30,16 @@ export default class DialogsController extends React.Component {
2230
config: PropTypes.object.isRequired,
2331
};
2432

25-
constructor(props) {
26-
super(props);
27-
28-
this.dialogRenderFns = {
29-
null: () => null,
30-
init: this.renderInitDialog,
31-
clone: this.renderCloneDialog,
32-
credential: this.renderCredentialDialog,
33-
issueish: this.renderIssueishDialog,
34-
commit: this.renderCommitDialog,
35-
};
36-
37-
this.state = {
38-
requestInProgress: null,
39-
requestError: [null, null],
40-
};
33+
state = {
34+
requestInProgress: null,
35+
requestError: [null, null],
4136
}
4237

4338
render() {
44-
return this.dialogRenderFns[this.props.request.identifier]();
39+
const DialogComponent = DIALOG_COMPONENTS[this.props.request.identifier];
40+
return <DialogComponent {...this.getCommonProps()} />;
4541
}
4642

47-
renderInitDialog = () => (
48-
<Panel workspace={this.props.workspace} location="modal">
49-
<InitDialog {...this.getCommonProps()} />
50-
</Panel>
51-
)
52-
53-
renderCloneDialog = () => (
54-
<Panel workspace={this.props.workspace} location="modal">
55-
<CloneDialog config={this.props.config} {...this.getCommonProps()} />
56-
</Panel>
57-
)
58-
59-
renderCredentialDialog = () => (
60-
<Panel workspace={this.props.workspace} location="modal">
61-
<CredentialDialog {...this.getCommonProps()} />
62-
</Panel>
63-
)
64-
65-
renderIssueishDialog = () => (
66-
<Panel workspace={this.props.workspace} location="modal">
67-
<OpenIssueishDialog {...this.getCommonProps()} />
68-
</Panel>
69-
)
70-
71-
renderCommitDialog = () => (
72-
<Panel workspace={this.props.workspace} location="modal">
73-
<OpenCommitDialog {...this.getCommonProps()} />
74-
</Panel>
75-
)
76-
7743
getCommonProps() {
7844
const {request} = this.props;
7945
const accept = request.isProgressing
@@ -99,14 +65,20 @@ export default class DialogsController extends React.Component {
9965
const wrapped = wrapDialogRequest(request, {accept});
10066

10167
return {
68+
config: this.props.config,
10269
commands: this.props.commands,
70+
workspace: this.props.workspace,
10371
inProgress: this.state.requestInProgress === request,
10472
error: this.state.requestError[0] === request ? this.state.requestError[1] : null,
10573
request: wrapped,
10674
};
10775
}
10876
}
10977

78+
function NullDialog() {
79+
return null;
80+
}
81+
11082
class DialogRequest {
11183
constructor(identifier, params = {}) {
11284
this.identifier = identifier;
@@ -135,7 +107,7 @@ class DialogRequest {
135107
}
136108

137109
function wrapDialogRequest(original, {accept}) {
138-
const dup = new DialogRequest(original.identifier, original.getParams());
110+
const dup = new DialogRequest(original.identifier, original.params);
139111
dup.isProgressing = original.isProgressing;
140112
dup.onAccept(accept);
141113
dup.onCancel(original.cancel);

test/controllers/dialogs-controller.test.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,14 @@ describe('DialogsController', function() {
3030
const wrapper = shallow(buildApp({
3131
request: dialogRequests.null,
3232
}));
33-
assert.isTrue(wrapper.isEmptyRender());
33+
assert.isTrue(wrapper.exists('NullDialog'));
3434
});
3535

3636
it('renders a chosen dialog when the appropriate DialogRequest is provided', function() {
3737
const wrapper = shallow(buildApp({
3838
request: dialogRequests.init({dirPath: __dirname}),
3939
}));
40-
41-
const panel = wrapper.find('Panel');
42-
assert.strictEqual(panel.prop('location'), 'modal');
43-
assert.strictEqual(panel.prop('workspace'), atomEnv.workspace);
44-
assert.isTrue(panel.exists('InitDialog'));
40+
assert.isTrue(wrapper.exists('InitDialog'));
4541
});
4642

4743
it('passes inProgress to the dialog when the accept callback is asynchronous', async function() {

0 commit comments

Comments
 (0)