Files
Iris/src/js/views/modals/EditPlaylist.js

297 lines
8.2 KiB
JavaScript
Raw Normal View History

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';
import { uriSource } from '../../util/helpers';
2020-07-19 21:00:00 +12:00
import { i18n, I18n } from '../../locale';
import Button from '../../components/Button';
2016-11-14 17:32:36 +13:00
class EditPlaylist extends React.Component {
2019-09-05 15:36:11 +12:00
constructor(props) {
super(props);
this.state = {
loaded: false,
2019-09-05 15:36:11 +12:00
error: null,
name: '',
description: '',
image: null,
public: false,
collaborative: false,
};
}
componentDidMount() {
const {
uri,
playlist,
coreActions: {
loadPlaylist,
},
uiActions: {
setWindowTitle,
},
} = this.props;
setWindowTitle(i18n('modal.edit_playlist.title'));
2019-09-05 15:36:11 +12:00
if (playlist) {
2019-09-05 15:36:11 +12:00
this.setState({
loaded: true,
name: playlist.name,
description: playlist.description,
public: (playlist.public === true),
collaborative: (playlist.collaborative === true),
2019-09-05 15:36:11 +12:00
});
} else {
loadPlaylist(uri);
2019-09-05 15:36:11 +12:00
}
}
componentDidUpdate = ({
2020-08-12 08:06:24 +12:00
playlist: prevPlaylist,
}) => {
const {
uri,
playlist,
mopidyActions: {
getPlaylist,
},
} = this.props;
2020-08-12 08:06:24 +12:00
if (playlist !== prevPlaylist) {
getPlaylist(uri);
}
}
2019-09-05 15:36:11 +12: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
}
return null;
2019-09-05 15:36:11 +12:00
}
setImage = (e) => {
2019-09-05 15:36:11 +12:00
const self = this;
// Create a file-reader to import the selected image as a base64 string
const file_reader = new FileReader();
// 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]);
}
savePlaylist = (e) => {
const {
name,
description,
public: isPublic,
collaborative,
image,
} = this.state;
const {
uri,
coreActions: {
savePlaylist,
},
} = this.props;
e.preventDefault();
if (!name || name == '') {
this.setState({ error: i18n('modal.edit_playlist.name_required') });
return false;
}
savePlaylist(
uri,
name,
description,
isPublic,
collaborative,
image,
);
window.history.back();
return false;
}
renderFields = () => {
const {
uri,
} = this.props;
const {
name,
description,
public: isPublic,
collaborative,
image,
} = this.state;
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 })}
value={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 })}
value={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"
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={isPublic}
onChange={() => this.setState({ public: !isPublic })}
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={collaborative}
onChange={() => this.setState({ collaborative: !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>
);
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 })}
value={name}
2019-09-05 15:36:11 +12:00
/>
</div>
</div>
</div>
);
}
}
render = () => {
const {
error,
} = this.state;
2019-09-05 15:36:11 +12:00
return (
<Modal className="modal--edit-playlist">
2020-07-19 21:00:00 +12:00
<h1>
<I18n path="modal.edit_playlist.title" />
</h1>
{error ? <h3 className="red-text">{error}</h3> : null}
2019-09-05 15:36:11 +12:00
<form onSubmit={(e) => this.savePlaylist(e)}>
{this.renderFields()}
<div className="actions centered-text">
<Button
type="primary"
size="large"
tracking={{ category: 'EditPlaylist', action: 'Submit' }}
submit
>
2020-07-19 21:00:00 +12:00
<I18n path="actions.save" />
</Button>
2019-09-05 15:36:11 +12:00
</div>
</form>
</Modal>
);
}
2018-07-08 21:12:18 +12: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.items[uri] !== undefined ? state.core.items[uri] : null),
2019-09-05 15:36:11 +12:00
};
};
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);