-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathRequest.js
238 lines (215 loc) · 6.83 KB
/
Request.js
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
import React, { PropTypes } from 'react';
import { connect } from 'react-redux';
import { DragSource, DropTarget } from 'react-dnd';
import { ListGroup } from 'react-bootstrap';
import flow from 'lodash.flow';
import IconButton from 'components/IconButton';
import * as CollectionActions from 'store/collections/actions';
import * as RequestActions from 'store/request/actions';
import * as ConfigActions from 'store/config/actions';
import { isDefaultCompact } from 'store/options/selectors';
import { getEditingRequest } from 'store/config/selectors';
import selectText from 'utils/selectText';
import { StyledRequest, RequestButtons, MainContentDiv } from './StyledComponents';
import * as Type from './dropTypes';
/**
* Specifies the drag source contract.
*/
const requestSource = {
canDrag(props) {
// Disallow drag if editing request
return !props.isEditing;
},
beginDrag({ request, index, collectionIndex }) {
return { request, index, collectionIndex };
},
};
const requestTarget = {
hover(props, monitor) {
const dragIndex = monitor.getItem().index;
const dragCollectionIndex = monitor.getItem().collectionIndex;
const dragUUID = monitor.getItem().request.id;
const hoverIndex = props.index;
const hoverCollectionIndex = props.collectionIndex;
const hoverUUID = props.request.id;
// Abort further processing if dragged item is hovering over itself
if (dragUUID === hoverUUID) {
return;
}
// Note: we're mutating the monitor item here.
// Generally it's better to avoid mutations,
// but it's good here for the sake of performance
// to avoid expensive index searches.
/* eslint-disable no-param-reassign */
monitor.getItem().index = hoverIndex;
monitor.getItem().collectionIndex = hoverCollectionIndex;
/* eslint-enable no-param-reassign */
// Update redux orders
props.reorderRequest({
// Source
collectionIndex: dragCollectionIndex,
requestIndex: dragIndex,
}, {
// Target
collectionIndex: hoverCollectionIndex,
requestIndex: hoverIndex,
});
},
};
class Request extends React.Component {
static propTypes = {
connectDragSource: PropTypes.func.isRequired,
connectDropTarget: PropTypes.func.isRequired,
isDragging: PropTypes.bool.isRequired,
index: PropTypes.number.isRequired,
collectionIndex: PropTypes.number.isRequired,
renameRequest: PropTypes.func.isRequired,
selectRequest: PropTypes.func.isRequired,
sendRequest: PropTypes.func.isRequired,
deleteRequest: PropTypes.func.isRequired,
toggleEditMode: PropTypes.func.isRequired,
isEditing: PropTypes.bool.isRequired,
defaultCompact: PropTypes.bool.isRequired,
request: PropTypes.shape({
id: PropTypes.string.isRequired,
name: PropTypes.string,
method: PropTypes.string.isRequired,
url: PropTypes.string.isRequired,
}).isRequired,
};
state = {
compact: this.props.defaultCompact,
};
toggleCompact = () => {
this.setState({ compact: !this.state.compact });
}
toggleEdit = () => {
this.props.toggleEditMode(this.props.request);
}
renameRequest = e => {
const { collectionIndex, index, renameRequest } = this.props;
e.preventDefault();
this.toggleEdit();
renameRequest(collectionIndex, index, this.nameRef.value);
}
renderRequest() {
const { compact } = this.state;
const { request, index, collectionIndex, isEditing } = this.props;
const { method, name, url } = request;
return (
<div>
{!compact && <h4>{method}</h4>}
{isEditing ? (
<form onSubmit={this.renameRequest}>
<label
className="sr-only"
htmlFor={`${collectionIndex}.${index}.RequestName`}
>
Request name
</label>
<input
autoFocus
onFocus={selectText}
id={`${collectionIndex}.${index}.RequestName`}
defaultValue={name}
ref={ref => { this.nameRef = ref; }}
/>
<IconButton
tooltip="Save"
icon="check"
/>
</form>
) : (
name || url
)}
</div>
);
}
render() {
const { compact } = this.state;
const {
connectDragSource,
connectDropTarget,
isDragging,
request,
collectionIndex,
selectRequest,
sendRequest,
deleteRequest,
isEditing,
} = this.props;
return connectDragSource(connectDropTarget(
<div>{/* Need a wrapper div for React DnD support */}
<StyledRequest isDragging={isDragging}>
<ListGroup componentClass="div">
<div className="list-group-item">
<RequestButtons compact={compact}>
{!compact && (
<IconButton
tooltip="Toggle edit"
icon="cog"
onClick={this.toggleEdit}
/>
)}
{compact && (
<IconButton
tooltip="Expand"
icon="plus"
onClick={this.toggleCompact}
/>
)}
{!compact && !isEditing && (
<IconButton
tooltip="Minimize"
icon="minus"
onClick={this.toggleCompact}
/>
)}
{!compact && isEditing && (
<IconButton
tooltip="Delete"
icon="trash"
onClick={() => deleteRequest(request.id, collectionIndex)}
/>
)}
</RequestButtons>
{isEditing ? (
<MainContentDiv compact={compact}>
{this.renderRequest()}
</MainContentDiv>
) : (
<MainContentDiv
compact={compact}
onClick={() => selectRequest(request)}
onDoubleClick={() => sendRequest(request)}
>
{this.renderRequest()}
</MainContentDiv>
)}
</div>
</ListGroup>
</StyledRequest>
</div>,
));
}
}
const mapStateToProps = (state, props) => ({
isEditing: getEditingRequest(state)
? getEditingRequest(state).id === props.request.id
: false,
defaultCompact: isDefaultCompact(state),
});
export default flow(
DropTarget(Type.Request, requestTarget, connector => ({
connectDropTarget: connector.dropTarget(),
})),
DragSource(Type.Request, requestSource, (connector, monitor) => ({
connectDragSource: connector.dragSource(),
isDragging: monitor.isDragging(),
})),
connect(mapStateToProps, {
...CollectionActions,
...RequestActions,
...ConfigActions,
}),
)(Request);