1
0
mirror of https://github.com/ascribe/onion.git synced 2024-06-28 08:37:59 +02:00
onion/js/components/whitelabel/wallet/components/cyland/cyland_register_piece.js

224 lines
8.3 KiB
JavaScript
Raw Normal View History

2015-07-13 22:58:53 +02:00
'use strict';
import React from 'react';
import { History } from 'react-router';
2015-08-12 09:52:51 +02:00
2015-08-12 13:53:17 +02:00
import Moment from 'moment';
2015-08-12 09:52:51 +02:00
import Col from 'react-bootstrap/lib/Col';
import Row from 'react-bootstrap/lib/Row';
import LinkContainer from 'react-router-bootstrap/lib/LinkContainer';
2015-08-12 09:52:51 +02:00
import PieceListStore from '../../../../../stores/piece_list_store';
import PieceListActions from '../../../../../actions/piece_list_actions';
2015-08-12 13:34:41 +02:00
import PieceStore from '../../../../../stores/piece_store';
import PieceActions from '../../../../../actions/piece_actions';
2015-08-12 09:52:51 +02:00
import GlobalNotificationModel from '../../../../../models/global_notification_model';
import GlobalNotificationActions from '../../../../../actions/global_notification_actions';
2015-07-13 22:58:53 +02:00
import CylandAdditionalDataForm from './cyland_forms/cyland_additional_data_form';
2015-08-12 13:34:41 +02:00
import LoanForm from '../../../../ascribe_forms/form_loan';
import RegisterPieceForm from '../../../../ascribe_forms/form_register_piece';
2015-08-12 13:34:41 +02:00
2015-08-12 09:52:51 +02:00
import SlidesContainer from '../../../../ascribe_slides_container/slides_container';
2015-08-12 13:34:41 +02:00
import ApiUrls from '../../../../../constants/api_urls';
2015-08-12 09:52:51 +02:00
import { getLangText } from '../../../../../utils/lang_utils';
2015-10-13 17:29:53 +02:00
import { setDocumentTitle } from '../../../../../utils/dom_utils';
2015-08-12 13:53:17 +02:00
import { mergeOptions } from '../../../../../utils/general_utils';
2015-08-12 13:34:41 +02:00
import { getAclFormMessage } from '../../../../../utils/form_utils';
2015-07-13 22:58:53 +02:00
let CylandRegisterPiece = React.createClass({
propTypes: {
// Provided from PrizeApp
currentUser: React.PropTypes.object.isRequired,
whitelabel: React.PropTypes.object.isRequired,
// Provided from router
location: React.PropTypes.object
},
2015-08-12 09:52:51 +02:00
mixins: [History],
2015-08-12 13:34:41 +02:00
2015-08-12 09:52:51 +02:00
getInitialState(){
return mergeOptions(
PieceListStore.getState(),
2016-02-05 10:38:59 +01:00
PieceStore.getInitialState(),
2015-08-12 09:52:51 +02:00
{
2015-08-19 17:39:25 +02:00
step: 0
2015-08-12 09:52:51 +02:00
});
},
componentDidMount() {
PieceListStore.listen(this.onChange);
2015-08-12 13:34:41 +02:00
PieceStore.listen(this.onChange);
const queryParams = this.props.location.query;
2015-08-24 11:22:44 +02:00
// Since every step of this register process is atomic,
// we may need to enter the process at step 1 or 2.
// If this is the case, we'll need the piece number to complete submission.
// It is encoded in the URL as a queryParam and we're checking for it here.
//
// We're using 'in' here as we want to know if 'piece_id' is present in the url,
// we don't care about the value.
2016-02-05 10:38:59 +01:00
if ('piece_id' in queryParams) {
PieceActions.fetchPiece(queryParams.piece_id);
}
2015-08-12 09:52:51 +02:00
},
componentWillUnmount() {
PieceListStore.unlisten(this.onChange);
2015-08-12 13:34:41 +02:00
PieceStore.unlisten(this.onChange);
2015-08-12 09:52:51 +02:00
},
onChange(state) {
this.setState(state);
},
2016-02-05 10:38:59 +01:00
handleRegisterSuccess(response) {
2015-08-19 16:02:10 +02:00
this.refreshPieceList();
2016-02-05 10:38:59 +01:00
// Also load the newly registered piece for the next step
if (response && response.piece) {
2015-08-12 13:34:41 +02:00
PieceActions.updatePiece(response.piece);
}
2016-02-05 10:38:59 +01:00
this.nextSlide({ piece_id: response.piece.id });
2015-08-12 13:34:41 +02:00
},
handleAdditionalDataSuccess() {
// We need to refetch the piece again after submitting the additional data
2016-02-05 10:38:59 +01:00
// since we want its otherData to be displayed when the user choses to click
// on the browsers back button.
2016-02-05 10:38:59 +01:00
PieceActions.fetchPiece(this.state.piece.id);
2015-08-19 16:02:10 +02:00
this.refreshPieceList();
2015-08-19 17:39:25 +02:00
2016-02-05 10:38:59 +01:00
this.nextSlide();
2015-08-12 09:52:51 +02:00
},
2015-08-17 12:18:40 +02:00
handleLoanSuccess(response) {
const notification = new GlobalNotificationModel(response.notification, 'success', 10000);
2015-08-17 12:18:40 +02:00
GlobalNotificationActions.appendGlobalNotification(notification);
2015-08-18 10:53:50 +02:00
2015-08-19 16:02:10 +02:00
this.refreshPieceList();
2016-01-11 17:52:32 +01:00
this.history.push(`/pieces/${this.state.piece.id}`);
2015-08-19 16:02:10 +02:00
},
2016-02-05 10:38:59 +01:00
nextSlide(queryParams) {
// We need to increase the step to lock the forms that are already filled out
2015-08-19 17:39:25 +02:00
this.setState({
2016-02-05 10:38:59 +01:00
step: this.state.step + 1
2015-08-19 17:39:25 +02:00
});
2016-02-05 10:38:59 +01:00
this.refs.slidesContainer.nextSlide(queryParams);
2015-08-19 17:39:25 +02:00
},
2015-08-19 16:02:10 +02:00
refreshPieceList() {
2016-02-05 10:38:59 +01:00
const { filterBy, orderAsc, orderBy, page, pageSize, search } = this.state;
PieceListActions.fetchPieceList({ page, pageSize, search, orderBy, orderAsc, filterBy });
2015-08-17 12:18:40 +02:00
},
2015-07-13 22:58:53 +02:00
render() {
const { currentUser, location, whitelabel } = this.props;
const { piece, step } = this.state;
2015-08-12 13:34:41 +02:00
const today = new Moment();
2016-02-05 10:38:59 +01:00
const datetimeWhenWeAllWillBeFlyingCoolHoverboardsAndDinosaursWillLiveAgain = new Moment().add(1000, 'years');
const loanHeading = getLangText('Loan to Cyland archive');
const loanButtons = (
<div>
2015-12-16 15:33:00 +01:00
<div className='col-xs-6 ascribe-form-btn-container-left'>
<button className='btn btn-default btn-wide'>
{getLangText('Loan to archive')}
</button>
</div>
2015-12-16 15:33:00 +01:00
<div className='col-xs-6 ascribe-form-btn-container-right'>
<LinkContainer to='/collection'>
<button
type='button'
className='btn btn-secondary btn-wide'>
{getLangText('Loan later')}
</button>
</LinkContainer>
</div>
</div>
);
2015-10-13 17:29:53 +02:00
setDocumentTitle(getLangText('Register a new piece'));
2015-07-13 22:58:53 +02:00
return (
<SlidesContainer
ref="slidesContainer"
forwardProcess={true}
glyphiconClassNames={{
pending: 'glyphicon glyphicon-chevron-right',
completed: 'glyphicon glyphicon-lock'
}}
location={location}>
2015-08-24 11:22:44 +02:00
<div data-slide-title={getLangText('Register work')}>
2015-08-12 09:52:51 +02:00
<Row className="no-margin">
<Col xs={12} sm={10} md={8} smOffset={1} mdOffset={2}>
<RegisterPieceForm
2016-01-18 17:31:56 +01:00
{...this.props}
disabled={step > 0}
2015-08-12 09:52:51 +02:00
enableLocalHashing={false}
2016-01-18 17:31:56 +01:00
handleSuccess={this.handleRegisterSuccess}
2015-08-12 09:52:51 +02:00
headerMessage={getLangText('Submit to Cyland Archive')}
isFineUploaderActive={true}
2016-01-18 17:31:56 +01:00
submitMessage={getLangText('Submit')} />
2015-08-12 09:52:51 +02:00
</Col>
</Row>
</div>
2015-08-24 11:22:44 +02:00
<div data-slide-title={getLangText('Additional details')}>
2015-08-12 13:34:41 +02:00
<Row className="no-margin">
<Col xs={12} sm={10} md={8} smOffset={1} mdOffset={2}>
<CylandAdditionalDataForm
disabled={step > 1}
2015-08-12 13:34:41 +02:00
handleSuccess={this.handleAdditionalDataSuccess}
piece={piece} />
2015-08-12 13:34:41 +02:00
</Col>
</Row>
</div>
2015-08-24 11:22:44 +02:00
<div data-slide-title={getLangText('Loan')}>
2015-08-12 13:34:41 +02:00
<Row className="no-margin">
<Col xs={12} sm={10} md={8} smOffset={1} mdOffset={2}>
<LoanForm
2015-08-24 11:22:44 +02:00
loanHeading={getLangText('Loan to Cyland archive')}
buttons={loanButtons}
message={getAclFormMessage({
aclName: 'acl_loan',
entities: piece,
isPiece: true,
senderName: currentUser.username
})}
id={{piece_id: piece.id}}
2015-08-12 13:34:41 +02:00
url={ApiUrls.ownership_loans_pieces}
email={whitelabel.user}
2015-08-12 13:34:41 +02:00
gallery="Cyland Archive"
startDate={today}
endDate={datetimeWhenWeAllWillBeFlyingCoolHoverboardsAndDinosaursWillLiveAgain}
2015-08-28 14:27:02 +02:00
showStartDate={false}
showEndDate={false}
2015-08-17 12:18:40 +02:00
showPersonalMessage={false}
2015-09-15 09:12:30 +02:00
handleSuccess={this.handleLoanSuccess} />
2015-08-12 13:34:41 +02:00
</Col>
</Row>
2015-08-12 09:52:51 +02:00
</div>
</SlidesContainer>
2015-07-13 22:58:53 +02:00
);
}
});
export default CylandRegisterPiece;