This repository has been archived by the owner on Feb 20, 2020. It is now read-only.
/
edit_track_modal.jsx
165 lines (134 loc) · 4.44 KB
/
edit_track_modal.jsx
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
var React = require("react");
var History = require("react-router").History;
var Modal = require("react-bootstrap").Modal;
var Row = require("react-bootstrap").Row;
var Col = require("react-bootstrap").Col;
var Input = require("react-bootstrap").Input;
var Button = require("react-bootstrap").Button;
var Thumbnail = require("react-bootstrap").Thumbnail;
var TrackModalsStore = require("../../stores/track_modals_store");
var TrackActions = require("../../actions/track_actions");
var UpdateTrackImage = require("../utility/update_track_image");
var Errors = require("../utility/errors");
module.exports = React.createClass({
mixins: [History],
getInitialState: function () {
return {
disabled: true,
errors: TrackModalsStore.getErrors(),
isUpdating: TrackModalsStore.isUpdating(),
pathname: TrackModalsStore.getUpdatedTrackPathname(),
show: TrackModalsStore.showEditModal()
};
},
componentDidMount: function () {
this.listenerToken = TrackModalsStore.addListener(this._onChange);
},
componentWillReceiveProps: function (nextProps) {
var track = nextProps.track;
if (track) {
this.setState({
title: track.title,
description: track.description,
imgUrl: track.img_square
});
}
},
componentWillUnmount: function () {
this.listenerToken.remove();
},
_onChange: function () {
this.setState(this.getInitialState());
this._redirectIfTitleChange();
},
_buttonState: function () {
return (this.state.isUpdating ? "Updating Track..." : "Update Track");
},
_disabled: function () {
return (
(this.state.isUpdating) ||
(this.state.title === "") ||
(this.state.disabled)
);
},
_handleDescriptionChange: function () {
var description = this.refs.description.getValue();
this.setState({ disabled: false, description: description });
},
_handleTitleChange: function () {
var title = this.refs.title.getValue();
this.setState({ disabled: false, title: title });
},
_redirectIfTitleChange: function () {
var newPathname = this.state.pathname;
if (newPathname && (newPathname !== this.props.pathname)) {
setTimeout(function () {
this.history.pushState(null, newPathname);
}.bind(this), 300);
}
},
_setState: function (state) {
this.setState(state);
},
close: function () {
TrackActions.closeEditModal();
},
titleLabel: function () {
return <span className="required-label">Title</span>;
},
update: function () {
var trackId = this.props.track.id;
var formData = new FormData();
formData.append("track[title]", this.state.title);
formData.append("track[description]", this.state.description);
if (this.state.img) {
formData.append("track[img]", this.state.img);
}
TrackActions.updateTrack(trackId, formData);
},
render: function () {
return (
<Modal onHide={ this.close }
dialogClassName="edit-track-modal"
show={ this.state.show }>
<Modal.Header closeButton>
<Modal.Title>Edit Track</Modal.Title>
</Modal.Header>
<Modal.Body>
<Errors errors={ this.state.errors } />
<Row>
<Col xs={ 5 } sm={ 5 } md={ 5 }>
<UpdateTrackImage imgUrl={ this.state.imgUrl }
setState={ this._setState } />
</Col>
<Col xs={ 7 } sm={ 7 } md={ 7 }>
<Input type="text"
label={ this.titleLabel() }
ref="title"
placeholder="Edit your track title"
help="Changing your track title will also change its URL!"
value={ this.state.title }
onChange={ this._handleTitleChange } />
<Input type="textarea"
label="Description"
ref="description"
rows="4"
id="track-description"
placeholder="Oops! Your track has no description. Enter something to describe your track."
value={ this.state.description }
onChange={ this._handleDescriptionChange } />
</Col>
</Row>
</Modal.Body>
<Modal.Footer>
<Button onClick={ this.close }>Cancel</Button>
<Button bsStyle="primary"
disabled={ this._disabled() }
onClick={ this.update }>
{ this._buttonState() }
</Button>
</Modal.Footer>
</Modal>
);
}
});