2016-11-14 17:32:36 +13:00
|
|
|
|
2019-09-05 15:36:11 +12:00
|
|
|
import React from 'react';
|
|
|
|
|
import { connect } from 'react-redux';
|
|
|
|
|
import { bindActionCreators } from 'redux';
|
2018-07-08 21:12:18 +12:00
|
|
|
import Modal from './Modal';
|
2019-09-05 15:36:11 +12:00
|
|
|
import * as coreActions from '../../services/core/actions';
|
|
|
|
|
import * as uiActions from '../../services/ui/actions';
|
|
|
|
|
import * as mopidyActions from '../../services/mopidy/actions';
|
|
|
|
|
import * as spotifyActions from '../../services/spotify/actions';
|
2020-03-03 16:39:37 +13:00
|
|
|
import { uriSource } from '../../util/helpers';
|
2020-07-19 21:00:00 +12:00
|
|
|
import { i18n, I18n } from '../../locale';
|
2020-08-16 20:00:10 +12:00
|
|
|
import Button from '../../components/Button';
|
2016-11-14 17:32:36 +13:00
|
|
|
|
2020-03-03 16:39:37 +13:00
|
|
|
class EditPlaylist extends React.Component {
|
2019-09-05 15:36:11 +12:00
|
|
|
constructor(props) {
|
|
|
|
|
super(props);
|
|
|
|
|
this.state = {
|
2020-03-12 21:32:50 +13:00
|
|
|
loaded: false,
|
2019-09-05 15:36:11 +12:00
|
|
|
error: null,
|
|
|
|
|
name: '',
|
|
|
|
|
description: '',
|
|
|
|
|
image: null,
|
|
|
|
|
public: false,
|
|
|
|
|
collaborative: false,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-03 16:39:37 +13:00
|
|
|
componentDidMount() {
|
2020-07-19 21:00:00 +12:00
|
|
|
this.props.uiActions.setWindowTitle(i18n('modal.edit_playlist.title'));
|
2019-09-05 15:36:11 +12:00
|
|
|
|
|
|
|
|
if (this.props.playlist) {
|
|
|
|
|
this.setState({
|
2020-03-12 21:32:50 +13:00
|
|
|
loaded: true,
|
2019-09-05 15:36:11 +12:00
|
|
|
name: this.props.playlist.name,
|
|
|
|
|
description: this.props.playlist.description,
|
|
|
|
|
public: (this.props.playlist.public == true),
|
|
|
|
|
collaborative: (this.props.playlist.collaborative == true),
|
|
|
|
|
});
|
|
|
|
|
} else {
|
2020-03-12 21:32:50 +13:00
|
|
|
switch (uriSource(this.props.uri)) {
|
2019-09-05 15:36:11 +12:00
|
|
|
case 'spotify':
|
|
|
|
|
this.props.spotifyActions.getPlaylist(this.props.uri);
|
|
|
|
|
this.props.spotifyActions.following(this.props.uri);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
2020-08-12 08:11:39 +12:00
|
|
|
this.props.mopidyActions.getPlaylist(this.props.uri);
|
2019-09-05 15:36:11 +12:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-12 21:32:50 +13:00
|
|
|
componentDidUpdate = ({
|
2020-08-12 08:06:24 +12:00
|
|
|
playlist: prevPlaylist,
|
2020-03-12 21:32:50 +13:00
|
|
|
}) => {
|
|
|
|
|
const {
|
|
|
|
|
uri,
|
|
|
|
|
playlist,
|
|
|
|
|
mopidyActions: {
|
|
|
|
|
getPlaylist,
|
|
|
|
|
},
|
|
|
|
|
} = this.props;
|
|
|
|
|
|
2020-08-12 08:06:24 +12:00
|
|
|
if (playlist !== prevPlaylist) {
|
2020-03-12 21:32:50 +13:00
|
|
|
getPlaylist(uri);
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-09-05 15:36:11 +12:00
|
|
|
|
2020-03-12 21:32:50 +13:00
|
|
|
static getDerivedStateFromProps({ playlist }, state) {
|
|
|
|
|
if (playlist && !state.loaded) {
|
|
|
|
|
return {
|
|
|
|
|
loaded: true,
|
|
|
|
|
name: playlist.name,
|
|
|
|
|
description: playlist.description,
|
|
|
|
|
public: (playlist.public === true),
|
|
|
|
|
collaborative: (playlist.collaborative === true),
|
|
|
|
|
};
|
2019-09-05 15:36:11 +12:00
|
|
|
}
|
2020-03-12 21:32:50 +13:00
|
|
|
return null;
|
2019-09-05 15:36:11 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
savePlaylist(e) {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
|
|
if (!this.state.name || this.state.name == '') {
|
2020-07-19 21:00:00 +12:00
|
|
|
this.setState({ error: i18n('modal.edit_playlist.name_required') });
|
2019-09-05 15:36:11 +12:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
this.props.coreActions.savePlaylist(
|
|
|
|
|
this.props.uri,
|
|
|
|
|
this.state.name,
|
|
|
|
|
this.state.description,
|
|
|
|
|
this.state.public,
|
|
|
|
|
this.state.collaborative,
|
|
|
|
|
this.state.image,
|
|
|
|
|
);
|
|
|
|
|
window.history.back();
|
2020-03-03 16:39:37 +13:00
|
|
|
return false;
|
2019-09-05 15:36:11 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setImage(e) {
|
|
|
|
|
const self = this;
|
|
|
|
|
|
|
|
|
|
// Create a file-reader to import the selected image as a base64 string
|
|
|
|
|
const file_reader = new FileReader();
|
|
|
|
|
|
2018-07-08 20:28:18 +12:00
|
|
|
// Once the image is loaded, convert the result
|
2019-09-05 15:36:11 +12:00
|
|
|
file_reader.addEventListener('load', (e) => {
|
|
|
|
|
const image_base64 = e.target.result.replace('data:image/jpeg;base64,', '');
|
|
|
|
|
self.setState({ image: image_base64 });
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// This calls the filereader to load the file
|
|
|
|
|
file_reader.readAsDataURL(e.target.files[0]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
renderFields() {
|
2020-03-03 16:39:37 +13:00
|
|
|
switch (uriSource(this.props.uri)) {
|
2019-09-05 15:36:11 +12:00
|
|
|
case 'spotify':
|
|
|
|
|
return (
|
|
|
|
|
<div>
|
|
|
|
|
<div className="field text">
|
2020-07-19 21:00:00 +12:00
|
|
|
<div className="name">
|
|
|
|
|
<I18n path="modal.edit_playlist.name" />
|
|
|
|
|
</div>
|
2019-09-05 15:36:11 +12:00
|
|
|
<div className="input">
|
|
|
|
|
<input
|
|
|
|
|
type="text"
|
|
|
|
|
onChange={(e) => this.setState({ name: e.target.value })}
|
2020-03-03 16:39:37 +13:00
|
|
|
value={this.state.name}
|
2019-09-05 15:36:11 +12:00
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
<div className="field text">
|
2020-07-19 21:00:00 +12:00
|
|
|
<div className="name">
|
|
|
|
|
<I18n path="modal.edit_playlist.description" />
|
|
|
|
|
</div>
|
2019-09-05 15:36:11 +12:00
|
|
|
<div className="input">
|
|
|
|
|
<input
|
|
|
|
|
type="text"
|
|
|
|
|
onChange={(e) => this.setState({ description: e.target.value })}
|
2020-03-03 16:39:37 +13:00
|
|
|
value={this.state.description}
|
2019-09-05 15:36:11 +12:00
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
<div className="field file">
|
2020-07-19 21:00:00 +12:00
|
|
|
<div className="name">
|
|
|
|
|
<I18n path="modal.edit_playlist.image.label" />
|
|
|
|
|
</div>
|
2019-09-05 15:36:11 +12:00
|
|
|
<div className="input">
|
|
|
|
|
<input
|
|
|
|
|
type="file"
|
|
|
|
|
placeholder="Leave empty to keep existing image"
|
2020-03-03 16:39:37 +13:00
|
|
|
onChange={(e) => this.setImage(e)}
|
2019-09-05 15:36:11 +12:00
|
|
|
/>
|
|
|
|
|
<div className="description">
|
2020-07-19 21:00:00 +12:00
|
|
|
<I18n path="modal.edit_playlist.image.description" />
|
2019-09-05 15:36:11 +12:00
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
<div className="field checkbox white">
|
|
|
|
|
<div className="name">
|
2020-07-19 21:00:00 +12:00
|
|
|
<I18n path="modal.edit_playlist.options.label" />
|
2019-09-05 15:36:11 +12:00
|
|
|
</div>
|
|
|
|
|
<div className="input">
|
|
|
|
|
<label>
|
|
|
|
|
<input
|
|
|
|
|
type="checkbox"
|
|
|
|
|
name="playlist_private"
|
|
|
|
|
checked={this.state.public}
|
2020-03-03 16:39:37 +13:00
|
|
|
onChange={(e) => this.setState({ public: !this.state.public })}
|
2019-09-05 15:36:11 +12:00
|
|
|
/>
|
2020-07-19 21:00:00 +12:00
|
|
|
<span className="label">
|
|
|
|
|
<I18n path="modal.edit_playlist.options.public" />
|
|
|
|
|
</span>
|
2019-09-05 15:36:11 +12:00
|
|
|
</label>
|
|
|
|
|
<label>
|
|
|
|
|
<input
|
|
|
|
|
type="checkbox"
|
|
|
|
|
name="collaborative"
|
|
|
|
|
checked={this.state.collaborative}
|
2020-03-03 16:39:37 +13:00
|
|
|
onChange={(e) => this.setState({ collaborative: !this.state.collaborative })}
|
2019-09-05 15:36:11 +12:00
|
|
|
/>
|
2020-07-19 21:00:00 +12:00
|
|
|
<span className="label">
|
|
|
|
|
<I18n path="modal.edit_playlist.options.collaborative" />
|
|
|
|
|
</span>
|
2019-09-05 15:36:11 +12:00
|
|
|
</label>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
return (
|
|
|
|
|
<div>
|
|
|
|
|
<div className="field text">
|
2020-07-19 21:00:00 +12:00
|
|
|
<div className="name">
|
|
|
|
|
<I18n path="modal.edit_playlist.name" />
|
|
|
|
|
</div>
|
2019-09-05 15:36:11 +12:00
|
|
|
<div className="input">
|
|
|
|
|
<input
|
|
|
|
|
type="text"
|
|
|
|
|
onChange={(e) => this.setState({ name: e.target.value })}
|
2020-03-03 16:39:37 +13:00
|
|
|
value={this.state.name}
|
2019-09-05 15:36:11 +12:00
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
render() {
|
|
|
|
|
return (
|
|
|
|
|
<Modal className="modal--edit-playlist">
|
2020-07-19 21:00:00 +12:00
|
|
|
<h1>
|
|
|
|
|
<I18n path="modal.edit_playlist.title" />
|
|
|
|
|
</h1>
|
2019-09-05 15:36:11 +12:00
|
|
|
{this.state.error ? <h3 className="red-text">{this.state.error}</h3> : null}
|
|
|
|
|
<form onSubmit={(e) => this.savePlaylist(e)}>
|
|
|
|
|
|
|
|
|
|
{this.renderFields()}
|
|
|
|
|
|
|
|
|
|
<div className="actions centered-text">
|
2020-08-16 20:00:10 +12:00
|
|
|
<Button
|
|
|
|
|
type="primary"
|
|
|
|
|
size="large"
|
|
|
|
|
tracking={{ category: 'EditPlaylist', action: 'Submit' }}
|
|
|
|
|
submit
|
|
|
|
|
>
|
2020-07-19 21:00:00 +12:00
|
|
|
<I18n path="actions.save" />
|
2020-08-16 20:00:10 +12:00
|
|
|
</Button>
|
2019-09-05 15:36:11 +12:00
|
|
|
</div>
|
|
|
|
|
</form>
|
|
|
|
|
</Modal>
|
|
|
|
|
);
|
|
|
|
|
}
|
2018-07-08 21:12:18 +12:00
|
|
|
}
|
|
|
|
|
|
2020-03-03 16:39:37 +13:00
|
|
|
const mapStateToProps = (state, ownProps) => {
|
2019-09-05 15:36:11 +12:00
|
|
|
// Decode the URI, and then re-encode selected characters
|
|
|
|
|
// This is needed as Mopidy encodes *some* characters in playlist URIs (but not other characters)
|
|
|
|
|
// We need to retain ":" because this a reserved URI separator
|
|
|
|
|
let uri = decodeURIComponent(ownProps.match.params.uri);
|
|
|
|
|
uri = uri.replace(/\s/g, '%20'); // space
|
|
|
|
|
uri = uri.replace(/\[/g, '%5B'); // [
|
|
|
|
|
uri = uri.replace(/\]/g, '%5D'); // ]
|
|
|
|
|
uri = uri.replace(/\(/g, '%28'); // (
|
|
|
|
|
uri = uri.replace(/\)/g, '%29'); // )
|
|
|
|
|
uri = uri.replace(/\#/g, '%23'); // #
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
uri,
|
|
|
|
|
playlist: (state.core.playlists[uri] !== undefined ? state.core.playlists[uri] : null),
|
|
|
|
|
playlists: state.core.playlists,
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const mapDispatchToProps = (dispatch) => ({
|
|
|
|
|
coreActions: bindActionCreators(coreActions, dispatch),
|
|
|
|
|
uiActions: bindActionCreators(uiActions, dispatch),
|
|
|
|
|
mopidyActions: bindActionCreators(mopidyActions, dispatch),
|
|
|
|
|
spotifyActions: bindActionCreators(spotifyActions, dispatch),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export default connect(mapStateToProps, mapDispatchToProps)(EditPlaylist);
|