-
Notifications
You must be signed in to change notification settings - Fork 17
/
DeckPropertiesEditor.js
228 lines (207 loc) · 8.84 KB
/
DeckPropertiesEditor.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
import React from 'react';
import classNames from 'classnames';
import {connectToStores} from 'fluxible-addons-react';
import {navigateAction} from 'fluxible-router';
import ContentUtil from '../../util/ContentUtil';
import DeckEditStore from '../../../../../stores/DeckEditStore';
import UserProfileStore from '../../../../../stores/UserProfileStore';
import saveDeckEdit from '../../../../../actions/saveDeckEdit';
import saveDeckRevision from '../../../../../actions/saveDeckRevision';
class DeckPropertiesEditor extends React.Component {
constructor(props) {
super(props);
this.state = this.getStateFromProps(props);
}
getStateFromProps(props) {
return {
validationErrors: {},
title: props.deckProps.title || '',
language: props.deckProps.language || '',
description: props.deckProps.description || '',
theme: props.deckProps.theme || '',
license: props.deckProps.license || ''
};
}
componentWillReceiveProps(newProps) {
//check if props have changed to reinitialize state (for handling route changes)
if (newProps.deckProps !== this.props.deckProps) {
this.setState(this.getStateFromProps(newProps));
}
}
handleCancel() {
this.context.executeAction(navigateAction, {
url: ContentUtil.makeNodeURL(this.props.selector, 'view')
});
}
handleSave(withNewRevision) {
const saveAction = withNewRevision ? saveDeckRevision : saveDeckEdit;
let validationErrors = {}, isValid = true;
if (this.state.title == null || this.state.title.length === 0) {
validationErrors.title = 'Please enter a title.';
isValid = false;
}
if (this.state.language == null || this.state.language.length !== 5) {
validationErrors.language = 'Please select a language.';
isValid = false;
}
if (this.state.license == null || this.state.license.length < 2) {
validationErrors.license = 'Please select a license.';
isValid = false;
}
this.setState({validationErrors: validationErrors});
if (isValid) {
swal({
title: 'Saving Deck Properties...',
text: '',
type: 'success',
timer: 1000,
showCloseButton: false,
showCancelButton: false,
allowEscapeKey: false,
showConfirmButton: false
});
this.context.executeAction(saveAction, {
deckId: this.props.selector.sid != null ? this.props.selector.sid : this.props.selector.id,
title: this.state.title,
language: this.state.language,
description: this.state.description,
theme: this.state.theme,
license: this.state.license,
tags: [],
selector: this.props.selector
});
}
}
handleChange(fieldName, event) {
let stateChange = {};
stateChange[fieldName] = event.target.value;
this.setState(stateChange);
}
render() {
let userid = this.props.UserProfileStore.userid;
let isUserEditor = false;
if (userid != null && userid !== '' && this.props.DeckEditStore.editors.includes(userid)) {
isUserEditor = true;
}
let titleFieldClass = classNames({
'required': true,
'field': true,
'error': this.state.validationErrors.title != null
});
let langFieldClass = classNames({
'required': true,
'field': true,
'error': this.state.validationErrors.language != null
});
let licenseFieldClass = classNames({
'required': true,
'field': true,
'error': this.state.validationErrors.license != null
});
let languageOptions = <select className="ui search dropdown" id="language" aria-labelledby="language"
aria-required="true"
value={this.state.language}
onChange={this.handleChange.bind(this, 'language')}>
<option>
Select Language
</option>
<option value="en_GB" >
English
</option>
<option value="de_DE" >
German
</option>
<option value="el_GR" >
Greek
</option>
<option value="it_IT" >
Italian
</option>
<option value="pt_PT" >
Portugese
</option>
<option value="sr_RS" >
Serbian
</option>
<option value="es_ES" >
Spanish
</option>
</select>;
let themeOptions = <select className="ui search dropdown" id="themes" aria-labelledby="theme"
selected={this.state.theme}
onChange={this.handleChange.bind(this, 'theme')}>
<option value="DefaultTheme">Default</option>
</select>;
let licenseOptions = <select className="ui search dropdown" id="license" aria-labelledby="license"
value={this.state.license}
onChange={this.handleChange.bind(this, 'license')}>
<option value="CC0">CC0</option>
<option value="CC BY">CC BY</option>
<option value="CC BY-SA">CC BY-SA</option>
</select>;
let saveDeckButton = isUserEditor ?
<button className='ui primary button'
onClick={this.handleSave.bind(this, false)}>Save</button> : '';
return (
<div className="ui container">
<div className="ui grid">
<div className="sixteen wide column">
<form className="ui form">
<div className="two fields">
<div className={titleFieldClass} data-tooltip={this.state.validationErrors.title}>
<label>
Title
</label>
<input type="text" name="deck-title" value={this.state.title}
onChange={this.handleChange.bind(this, 'title')} placeholder="Title"
aria-required="true"/>
</div>
<div className={langFieldClass} data-tooltip={this.state.validationErrors.language}>
<label id="language">
Language
</label>
{languageOptions}
</div>
</div>
<div className="field">
<label id="deck-description">Description</label>
<textarea rows="4" aria-labelledby="deck-description"
value={this.state.description}
onChange={this.handleChange.bind(this, 'description')}/>
</div>
<div className="two fields">
<div className="field disabled">
<label id="themes">Choose deck theme</label>
{themeOptions}
</div>
<div className={licenseFieldClass} data-tooltip={this.state.validationErrors.license}>
<label id="license">License</label>
{licenseOptions}
</div>
</div>
{saveDeckButton}
<button className='ui primary button'
onClick={this.handleSave.bind(this, true)}>
Save as new revision
</button>
<button className="ui secondary button"
onClick={this.handleCancel.bind(this)}>
Cancel
</button>
</form>
</div>
</div>
</div>
);
}
}
DeckPropertiesEditor.contextTypes = {
executeAction: React.PropTypes.func.isRequired
};
DeckPropertiesEditor = connectToStores(DeckPropertiesEditor, [DeckEditStore, UserProfileStore], (context, props) => {
return {
DeckEditStore: context.getStore(DeckEditStore).getState(),
UserProfileStore: context.getStore(UserProfileStore).getState()
};
});
export default DeckPropertiesEditor;