2015-10-26 19:17:06 +01:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
import React from 'react';
|
|
|
|
import classNames from 'classnames';
|
|
|
|
|
2015-12-04 18:30:45 +01:00
|
|
|
import MarketConsignError from '../market_consign_error';
|
|
|
|
|
2015-11-24 12:04:08 +01:00
|
|
|
import MarketAdditionalDataForm from '../market_forms/market_additional_data_form';
|
2015-11-05 11:58:32 +01:00
|
|
|
|
|
|
|
import AclFormFactory from '../../../../../ascribe_forms/acl_form_factory';
|
|
|
|
import ConsignForm from '../../../../../ascribe_forms/form_consign';
|
|
|
|
|
|
|
|
import ModalWrapper from '../../../../../ascribe_modal/modal_wrapper';
|
|
|
|
|
2015-11-06 14:00:58 +01:00
|
|
|
import AclProxy from '../../../../../acl_proxy';
|
|
|
|
|
2015-12-04 18:30:45 +01:00
|
|
|
import EditionListActions from '../../../../../../actions/edition_list_actions';
|
2015-11-05 11:58:32 +01:00
|
|
|
import PieceActions from '../../../../../../actions/piece_actions';
|
2015-10-26 19:17:06 +01:00
|
|
|
import WhitelabelActions from '../../../../../../actions/whitelabel_actions';
|
|
|
|
import WhitelabelStore from '../../../../../../stores/whitelabel_store';
|
|
|
|
|
2015-11-05 11:58:32 +01:00
|
|
|
import ApiUrls from '../../../../../../constants/api_urls';
|
|
|
|
|
2015-11-10 18:24:46 +01:00
|
|
|
import { getAclFormMessage, getAclFormDataId } from '../../../../../../utils/form_utils';
|
2015-10-26 19:17:06 +01:00
|
|
|
import { getLangText } from '../../../../../../utils/lang_utils';
|
|
|
|
|
2015-11-24 12:04:08 +01:00
|
|
|
let MarketSubmitButton = React.createClass({
|
2015-10-26 19:17:06 +01:00
|
|
|
propTypes: {
|
2015-10-27 17:01:50 +01:00
|
|
|
availableAcls: React.PropTypes.object.isRequired,
|
2015-11-10 18:24:46 +01:00
|
|
|
currentUser: React.PropTypes.object,
|
2015-11-05 11:58:32 +01:00
|
|
|
editions: React.PropTypes.array.isRequired,
|
|
|
|
handleSuccess: React.PropTypes.func.isRequired,
|
2015-10-27 17:01:50 +01:00
|
|
|
className: React.PropTypes.string,
|
2015-10-26 19:17:06 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
getInitialState() {
|
2015-10-27 17:01:50 +01:00
|
|
|
return WhitelabelStore.getState();
|
2015-10-26 19:17:06 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
WhitelabelStore.listen(this.onChange);
|
2015-11-05 11:58:32 +01:00
|
|
|
|
2015-10-26 19:17:06 +01:00
|
|
|
WhitelabelActions.fetchWhitelabel();
|
|
|
|
},
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
WhitelabelStore.unlisten(this.onChange);
|
|
|
|
},
|
|
|
|
|
|
|
|
onChange(state) {
|
|
|
|
this.setState(state);
|
|
|
|
},
|
|
|
|
|
2015-11-06 14:00:58 +01:00
|
|
|
canEditionBeSubmitted(edition) {
|
|
|
|
if (edition && edition.extra_data && edition.other_data) {
|
|
|
|
const { extra_data, other_data } = edition;
|
|
|
|
|
|
|
|
if (extra_data.artist_bio && extra_data.work_description &&
|
2015-12-02 12:25:11 +01:00
|
|
|
extra_data.technology_details && extra_data.display_instructions &&
|
|
|
|
other_data.length > 0) {
|
2015-11-06 14:00:58 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
2015-11-05 11:58:32 +01:00
|
|
|
getFormDataId() {
|
|
|
|
return getAclFormDataId(false, this.props.editions);
|
|
|
|
},
|
|
|
|
|
|
|
|
getAggregateEditionDetails() {
|
|
|
|
const { editions } = this.props;
|
|
|
|
|
|
|
|
// Currently, we only care if all the given editions are from the same parent piece
|
|
|
|
// and if they can be submitted
|
|
|
|
return editions.reduce((details, curEdition) => {
|
|
|
|
return {
|
|
|
|
solePieceId: details.solePieceId === curEdition.parent ? details.solePieceId : null,
|
2015-11-06 14:00:58 +01:00
|
|
|
canSubmit: details.canSubmit && this.canEditionBeSubmitted(curEdition)
|
2015-11-05 11:58:32 +01:00
|
|
|
};
|
|
|
|
}, {
|
|
|
|
solePieceId: editions.length > 0 ? editions[0].parent : null,
|
2015-11-06 14:00:58 +01:00
|
|
|
canSubmit: this.canEditionBeSubmitted(editions[0])
|
2015-11-05 11:58:32 +01:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
handleAdditionalDataSuccess(pieceId) {
|
|
|
|
// Fetch newly updated piece to update the views
|
|
|
|
PieceActions.fetchOne(pieceId);
|
|
|
|
|
|
|
|
this.refs.consignModal.show();
|
|
|
|
},
|
|
|
|
|
2015-12-04 18:30:45 +01:00
|
|
|
handleConsignError() {
|
|
|
|
// Unselect failed editions
|
|
|
|
EditionListActions.clearAllEditionSelections();
|
|
|
|
},
|
|
|
|
|
2015-10-26 19:17:06 +01:00
|
|
|
render() {
|
2015-11-05 11:58:32 +01:00
|
|
|
const { availableAcls, currentUser, className, editions, handleSuccess } = this.props;
|
2015-12-02 14:26:15 +01:00
|
|
|
const { whitelabel: { name: whitelabelName = 'Market', user: whitelabelAdminEmail } } = this.state;
|
2015-11-05 11:58:32 +01:00
|
|
|
const { solePieceId, canSubmit } = this.getAggregateEditionDetails();
|
2015-11-10 18:24:46 +01:00
|
|
|
const message = getAclFormMessage({
|
|
|
|
aclName: 'acl_consign',
|
|
|
|
entities: editions,
|
|
|
|
isPiece: false,
|
|
|
|
additionalMessage: getLangText('Suggested price:'),
|
|
|
|
senderName: currentUser.username
|
|
|
|
});
|
|
|
|
|
|
|
|
const triggerButton = (
|
|
|
|
<button className={classNames('btn', 'btn-default', 'btn-sm', className)}>
|
2015-12-02 14:26:15 +01:00
|
|
|
{getLangText('CONSIGN TO %s', whitelabelName.toUpperCase())}
|
2015-11-10 18:24:46 +01:00
|
|
|
</button>
|
|
|
|
);
|
|
|
|
const consignForm = (
|
|
|
|
<AclFormFactory
|
|
|
|
action='acl_consign'
|
|
|
|
autoFocusProperty='message'
|
2015-12-02 14:26:15 +01:00
|
|
|
email={whitelabelAdminEmail}
|
2015-11-10 18:24:46 +01:00
|
|
|
message={message}
|
|
|
|
labels={{
|
|
|
|
'message': getLangText('Message (also suggest a sales price if necessary)')
|
|
|
|
}}
|
|
|
|
pieceOrEditions={editions}
|
|
|
|
showNotification />
|
|
|
|
);
|
2015-11-05 11:58:32 +01:00
|
|
|
|
|
|
|
if (solePieceId && !canSubmit) {
|
2015-12-04 18:30:45 +01:00
|
|
|
if (availableAcls.acl_edit) {
|
|
|
|
return (
|
|
|
|
<AclProxy
|
|
|
|
aclObject={availableAcls}
|
|
|
|
aclName='acl_consign'>
|
|
|
|
<ModalWrapper
|
|
|
|
trigger={triggerButton}
|
|
|
|
handleSuccess={this.handleAdditionalDataSuccess.bind(this, solePieceId)}
|
|
|
|
title={getLangText('Add additional information')}>
|
|
|
|
<MarketAdditionalDataForm
|
|
|
|
pieceId={solePieceId}
|
|
|
|
submitLabel={getLangText('Continue to consignment')} />
|
|
|
|
</ModalWrapper>
|
|
|
|
|
|
|
|
<ModalWrapper
|
|
|
|
ref="consignModal"
|
|
|
|
handleSuccess={handleSuccess}
|
|
|
|
title={getLangText('Consign artwork')}>
|
|
|
|
{consignForm}
|
|
|
|
</ModalWrapper>
|
|
|
|
</AclProxy>
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
// Oops, well this is a difficult situation...
|
|
|
|
// The user's likely already transferred another edition from the piece so
|
|
|
|
// they can't update the missing fields that are necessary for consignment
|
|
|
|
// to marketplaces.
|
|
|
|
// Let's show an error in response to explain the problem and let them
|
|
|
|
// contact the whitelabel themselves.
|
|
|
|
return (
|
2015-11-05 11:58:32 +01:00
|
|
|
<ModalWrapper
|
2015-11-10 18:24:46 +01:00
|
|
|
trigger={triggerButton}
|
2015-12-04 18:30:45 +01:00
|
|
|
handleSuccess={this.handleConsignError}
|
|
|
|
title={getLangText("Oops, we can't consign this piece to %s", whitelabelName)}>
|
|
|
|
<MarketConsignError
|
|
|
|
whitelabelName={whitelabelName} />
|
2015-11-05 11:58:32 +01:00
|
|
|
</ModalWrapper>
|
2015-12-04 18:30:45 +01:00
|
|
|
);
|
|
|
|
}
|
2015-11-05 11:58:32 +01:00
|
|
|
} else {
|
|
|
|
return (
|
2015-11-10 18:24:46 +01:00
|
|
|
<AclProxy
|
2015-12-04 16:34:25 +01:00
|
|
|
show={availableAcls.acl_consign && canSubmit}>
|
2015-11-10 18:24:46 +01:00
|
|
|
<ModalWrapper
|
|
|
|
trigger={triggerButton}
|
|
|
|
handleSuccess={handleSuccess}
|
2015-12-02 14:26:15 +01:00
|
|
|
title={getLangText('Consign artwork to %s', whitelabelName)}>
|
2015-11-10 18:24:46 +01:00
|
|
|
{consignForm}
|
|
|
|
</ModalWrapper>
|
|
|
|
</AclProxy>
|
2015-11-05 11:58:32 +01:00
|
|
|
);
|
|
|
|
}
|
2015-10-26 19:17:06 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-11-24 12:04:08 +01:00
|
|
|
export default MarketSubmitButton;
|