1
0
mirror of https://github.com/ascribe/onion.git synced 2024-06-30 21:52:08 +02:00
onion/js/components/register_piece.js

166 lines
6.1 KiB
JavaScript
Raw Normal View History

2015-06-17 17:48:52 +02:00
'use strict';
import React from 'react';
2015-06-22 10:50:22 +02:00
import Router from 'react-router';
2015-07-07 10:28:39 +02:00
import Col from 'react-bootstrap/lib/Col';
import Row from 'react-bootstrap/lib/Row';
2015-06-30 10:42:58 +02:00
import LicenseActions from '../actions/license_actions';
import LicenseStore from '../stores/license_store';
2015-07-01 16:19:02 +02:00
import PieceListStore from '../stores/piece_list_store';
import PieceListActions from '../actions/piece_list_actions';
2015-07-02 15:40:54 +02:00
import UserStore from '../stores/user_store';
2015-06-22 10:50:22 +02:00
import GlobalNotificationModel from '../models/global_notification_model';
import GlobalNotificationActions from '../actions/global_notification_actions';
import Property from './ascribe_forms/property';
2015-07-09 11:56:54 +02:00
import PropertyCollapsible from './ascribe_forms/property_collapsible';
import RegisterPieceForm from './ascribe_forms/form_register_piece';
//import FormPropertyHeader from './ascribe_forms/form_property_header';
2015-06-22 10:50:22 +02:00
2015-07-02 11:49:17 +02:00
import LoginContainer from './login_container';
import SlidesContainer from './ascribe_slides_container/slides_container';
2015-07-02 11:49:17 +02:00
2015-06-30 10:42:58 +02:00
import { mergeOptions } from '../utils/general_utils';
2015-07-03 19:08:56 +02:00
import { getLangText } from '../utils/lang_utils';
2015-06-29 10:00:26 +02:00
2015-06-17 17:48:52 +02:00
let RegisterPiece = React.createClass( {
2015-06-23 13:55:05 +02:00
mixins: [Router.Navigation],
2015-06-20 16:43:18 +02:00
2015-06-23 13:55:05 +02:00
getInitialState(){
2015-06-30 10:42:58 +02:00
return mergeOptions(
LicenseStore.getState(),
2015-07-02 15:40:54 +02:00
UserStore.getState(),
2015-07-01 16:19:02 +02:00
PieceListStore.getState(),
2015-06-30 10:42:58 +02:00
{
2015-07-02 11:49:17 +02:00
selectedLicense: 0,
2015-07-02 15:40:54 +02:00
isFineUploaderEditable: false
2015-06-30 10:42:58 +02:00
});
},
componentDidMount() {
LicenseActions.fetchLicense();
LicenseStore.listen(this.onChange);
2015-07-01 16:19:02 +02:00
PieceListStore.listen(this.onChange);
2015-07-02 15:40:54 +02:00
UserStore.listen(this.onChange);
2015-06-30 10:42:58 +02:00
},
componentWillUnmount() {
LicenseStore.unlisten(this.onChange);
2015-07-01 16:19:02 +02:00
PieceListStore.unlisten(this.onChange);
2015-07-02 15:40:54 +02:00
UserStore.unlisten(this.onChange);
2015-06-30 10:42:58 +02:00
},
onChange(state) {
this.setState(state);
2015-07-02 15:40:54 +02:00
// once the currentUser object from UserStore is defined (eventually the user was transitioned
// to the login form via the slider and successfully logged in), we can direct him back to the
// register_piece slide
2015-07-02 16:51:22 +02:00
if(state.currentUser && state.currentUser.email || this.state.currentUser && this.state.currentUser.email) {
2015-07-02 15:40:54 +02:00
this.refs.slidesContainer.setSlideNum(0);
// we should also make the fineuploader component editable again
this.setState({
isFineUploaderEditable: true
});
}
2015-06-23 13:55:05 +02:00
},
2015-06-29 11:44:16 +02:00
2015-07-01 11:21:43 +02:00
handleSuccess(response){
let notification = new GlobalNotificationModel(response.notification, 'success', 10000);
2015-06-23 13:55:05 +02:00
GlobalNotificationActions.appendGlobalNotification(notification);
2015-07-01 16:19:02 +02:00
// once the user was able to register a piece successfully, we need to make sure to keep
// the piece list up to date
2015-07-07 15:48:09 +02:00
PieceListActions.fetchPieceList(
this.state.page,
this.state.pageSize,
this.state.searchTerm,
this.state.orderBy,
this.state.orderAsc);
2015-07-10 10:30:17 +02:00
this.transitionTo('piece', {pieceId: response.piece.id});
2015-06-23 13:55:05 +02:00
},
2015-06-30 10:42:58 +02:00
onLicenseChange(event){
//console.log(this.state.licenses[event.target.selectedIndex].url);
2015-06-30 10:42:58 +02:00
this.setState({selectedLicense: event.target.selectedIndex});
},
getLicenses() {
2015-07-07 10:28:39 +02:00
if (this.state.licenses && this.state.licenses.length > 1) {
2015-06-30 10:42:58 +02:00
return (
<Property
name='license'
2015-07-03 19:08:56 +02:00
label={getLangText('Copyright license%s', '...')}
2015-06-30 10:42:58 +02:00
onChange={this.onLicenseChange}
footer={
<a className="pull-right" href={this.state.licenses[this.state.selectedLicense].url} target="_blank">{getLangText('Learn more')}
2015-06-30 10:42:58 +02:00
</a>}>
<select name="license">
{this.state.licenses.map((license, i) => {
return (
<option
name={i}
key={i}
value={ license.code }>
{ license.code.toUpperCase() }: { license.name }
</option>
);
})}
</select>
</Property>);
}
return null;
},
2015-06-23 13:55:05 +02:00
2015-07-02 15:40:54 +02:00
changeSlide() {
// only transition to the login store, if user is not logged in
// ergo the currentUser object is not properly defined
if(!this.state.currentUser.email) {
this.refs.slidesContainer.setSlideNum(1);
}
2015-07-02 13:56:24 +02:00
},
2015-06-23 13:55:05 +02:00
render() {
2015-06-17 17:48:52 +02:00
return (
2015-07-02 13:56:24 +02:00
<SlidesContainer ref="slidesContainer">
2015-07-02 16:51:22 +02:00
<div
onClick={this.changeSlide}
2015-07-02 15:40:54 +02:00
onFocus={this.changeSlide}>
2015-07-07 10:28:39 +02:00
<Row className="no-margin">
2015-07-07 18:07:12 +02:00
<Col xs={12} sm={10} md={8} smOffset={1} mdOffset={2}>
<RegisterPieceForm
isFineUploaderEditable={this.state.isFineUploaderEditable}
handleSuccess={this.handleSuccess}>
2015-07-09 11:56:54 +02:00
<PropertyCollapsible
checkboxLabel={getLangText('Specify editions')}>
<span>{getLangText('Editions')}</span>
<input
type="number"
placeholder="(e.g. 32)"
min={0}/>
</PropertyCollapsible>
2015-07-07 10:28:39 +02:00
{this.getLicenses()}
</RegisterPieceForm>
2015-07-07 10:28:39 +02:00
</Col>
</Row>
</div>
2015-07-02 13:56:24 +02:00
<div>
2015-07-02 15:40:54 +02:00
<LoginContainer
2015-07-03 19:08:56 +02:00
message={getLangText('Please login before ascribing your work%s', '...')}
2015-07-02 15:40:54 +02:00
redirectOnLoggedIn={false}
redirectOnLoginSuccess={false}/>
</div>
</SlidesContainer>
2015-06-17 17:48:52 +02:00
);
}
});
2015-06-22 10:50:22 +02:00
export default RegisterPiece;