1
0
mirror of https://github.com/ascribe/onion.git synced 2024-06-29 00:58:03 +02:00
onion/js/components/register_piece.js

179 lines
5.9 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';
import WhitelabelActions from '../actions/whitelabel_actions';
import WhitelabelStore from '../stores/whitelabel_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';
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-07-13 22:58:53 +02:00
2015-06-17 17:48:52 +02:00
let RegisterPiece = React.createClass( {
2015-07-13 22:58:53 +02:00
propTypes: {
headerMessage: React.PropTypes.string,
submitMessage: React.PropTypes.string,
children: React.PropTypes.oneOfType([
React.PropTypes.arrayOf(React.PropTypes.element),
React.PropTypes.element,
React.PropTypes.string
])
2015-07-13 22:58:53 +02:00
},
2015-06-23 13:55:05 +02:00
mixins: [Router.Navigation],
2015-06-20 16:43:18 +02:00
2015-07-13 22:58:53 +02:00
getDefaultProps() {
return {
canSpecifyEditions: true
};
},
2015-06-23 13:55:05 +02:00
getInitialState(){
2015-06-30 10:42:58 +02:00
return mergeOptions(
2015-07-02 15:40:54 +02:00
UserStore.getState(),
WhitelabelStore.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,
isFineUploaderActive: false
2015-06-30 10:42:58 +02:00
});
},
componentDidMount() {
WhitelabelActions.fetchWhitelabel();
2015-07-01 16:19:02 +02:00
PieceListStore.listen(this.onChange);
2015-07-02 15:40:54 +02:00
UserStore.listen(this.onChange);
WhitelabelStore.listen(this.onChange);
2015-06-30 10:42:58 +02:00
},
componentWillUnmount() {
2015-07-01 16:19:02 +02:00
PieceListStore.unlisten(this.onChange);
2015-07-02 15:40:54 +02:00
UserStore.unlisten(this.onChange);
WhitelabelStore.unlisten(this.onChange);
2015-06-30 10:42:58 +02:00
},
onChange(state) {
this.setState(state);
2015-07-21 15:30:00 +02:00
if(this.state.currentUser && this.state.currentUser.email) {
// we should also make the fineuploader component editable again
this.setState({
isFineUploaderActive: 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,
this.state.filterBy
);
2015-07-07 15:48:09 +02:00
2015-07-10 10:30:17 +02:00
this.transitionTo('piece', {pieceId: response.piece.id});
2015-06-23 13:55:05 +02:00
},
2015-07-13 22:58:53 +02:00
getSpecifyEditions() {
2015-08-05 16:02:48 +02:00
if(this.state.whitelabel && this.state.whitelabel.acl_create_editions || Object.keys(this.state.whitelabel).length === 0) {
2015-07-13 22:58:53 +02:00
return (
<PropertyCollapsible
2015-07-14 00:12:33 +02:00
name="num_editions"
2015-07-13 22:58:53 +02:00
checkboxLabel={getLangText('Specify editions')}>
<span>{getLangText('Editions')}</span>
<input
type="number"
placeholder="(e.g. 32)"
min={0}/>
</PropertyCollapsible>
);
}
},
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 && !this.state.currentUser.email) {
2015-07-02 15:40:54 +02:00
this.refs.slidesContainer.setSlideNum(1);
}
2015-07-02 13:56:24 +02:00
},
// basically redirects to the second slide (index: 1), when the user is not logged in
onLoggedOut() {
this.refs.slidesContainer.setSlideNum(1);
},
onLogin() {
// 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
if(this.state.currentUser && this.state.currentUser.email) {
window.history.back();
}
},
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
2015-07-13 22:58:53 +02:00
{...this.props}
isFineUploaderActive={this.state.isFineUploaderActive}
handleSuccess={this.handleSuccess}
onLoggedOut={this.onLoggedOut}>
2015-07-13 22:58:53 +02:00
{this.props.children}
2015-07-15 17:33:38 +02:00
{this.getSpecifyEditions()}
</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}
onLogin={this.onLogin}/>
</div>
</SlidesContainer>
2015-06-17 17:48:52 +02:00
);
}
});
2015-06-22 10:50:22 +02:00
export default RegisterPiece;