This repository was archived by the owner on Dec 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 410
Expand file tree
/
Copy pathdialogs-controller.js
More file actions
170 lines (147 loc) · 4.09 KB
/
dialogs-controller.js
File metadata and controls
170 lines (147 loc) · 4.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import React from 'react';
import PropTypes from 'prop-types';
import InitDialog from '../views/init-dialog';
import CloneDialog from '../views/clone-dialog';
import CredentialDialog from '../views/credential-dialog';
import OpenIssueishDialog from '../views/open-issueish-dialog';
import OpenCommitDialog from '../views/open-commit-dialog';
import CreateDialog from '../views/create-dialog';
import {GithubLoginModelPropType} from '../prop-types';
const DIALOG_COMPONENTS = {
null: NullDialog,
init: InitDialog,
clone: CloneDialog,
credential: CredentialDialog,
issueish: OpenIssueishDialog,
commit: OpenCommitDialog,
create: CreateDialog,
publish: CreateDialog,
};
export default class DialogsController extends React.Component {
static propTypes = {
// Model
loginModel: GithubLoginModelPropType.isRequired,
request: PropTypes.shape({
identifier: PropTypes.string.isRequired,
isProgressing: PropTypes.bool.isRequired,
}).isRequired,
// Atom environment
currentWindow: PropTypes.object.isRequired,
workspace: PropTypes.object.isRequired,
commands: PropTypes.object.isRequired,
config: PropTypes.object.isRequired,
};
state = {
requestInProgress: null,
requestError: [null, null],
}
render() {
const DialogComponent = DIALOG_COMPONENTS[this.props.request.identifier];
return <DialogComponent {...this.getCommonProps()} />;
}
getCommonProps() {
const {request} = this.props;
const accept = request.isProgressing
? async (...args) => {
this.setState({requestError: [null, null], requestInProgress: request});
try {
const result = await request.accept(...args);
this.setState({requestInProgress: null});
return result;
} catch (error) {
this.setState({requestError: [request, error], requestInProgress: null});
return undefined;
}
} : (...args) => {
this.setState({requestError: [null, null]});
try {
return request.accept(...args);
} catch (error) {
this.setState({requestError: [request, error]});
return undefined;
}
};
const wrapped = wrapDialogRequest(request, {accept});
return {
loginModel: this.props.loginModel,
request: wrapped,
inProgress: this.state.requestInProgress === request,
currentWindow: this.props.currentWindow,
workspace: this.props.workspace,
commands: this.props.commands,
config: this.props.config,
error: this.state.requestError[0] === request ? this.state.requestError[1] : null,
};
}
}
function NullDialog() {
return null;
}
class DialogRequest {
constructor(identifier, params = {}) {
this.identifier = identifier;
this.params = params;
this.isProgressing = false;
this.accept = () => {};
this.cancel = () => {};
}
onAccept(cb) {
this.accept = cb;
}
onProgressingAccept(cb) {
this.isProgressing = true;
this.onAccept(cb);
}
onCancel(cb) {
this.cancel = cb;
}
getParams() {
return this.params;
}
}
function wrapDialogRequest(original, {accept}) {
const dup = new DialogRequest(original.identifier, original.params);
dup.isProgressing = original.isProgressing;
dup.onAccept(accept);
dup.onCancel(original.cancel);
return dup;
}
export const dialogRequests = {
null: {
identifier: 'null',
isProgressing: false,
params: {},
accept: () => {},
cancel: () => {},
},
init({dirPath}) {
return new DialogRequest('init', {dirPath});
},
clone(opts) {
return new DialogRequest('clone', {
sourceURL: '',
destPath: '',
...opts,
});
},
credential(opts) {
return new DialogRequest('credential', {
includeUsername: false,
includeRemember: false,
prompt: 'Please authenticate',
...opts,
});
},
issueish() {
return new DialogRequest('issueish');
},
commit() {
return new DialogRequest('commit');
},
create() {
return new DialogRequest('create');
},
publish({localDir}) {
return new DialogRequest('publish', {localDir});
},
};