1
0
mirror of https://github.com/ascribe/onion.git synced 2024-12-23 09:50:31 +01:00
onion/js/components/ascribe_forms/form_copyright_association.js
Brett Sun b5eda1cdd2 Inject isLoggedIn through withCurrentUser
Makes checking for logged in status less error prone than always using
`currentUser.email` or `currentUser.username`.
2016-06-07 14:56:40 +02:00

89 lines
3.1 KiB
JavaScript

'use strict';
import React from 'react';
import GlobalNotificationModel from '../../models/global_notification_model';
import GlobalNotificationActions from '../../actions/global_notification_actions';
import Form from './form';
import Property from './property';
import { currentUserShape } from '../prop_types';
import ApiUrls from '../../constants/api_urls';
import AppConstants from '../../constants/application_constants';
import { getLangText } from '../../utils/lang_utils';
import { withCurrentUser } from '../../utils/react_utils';
const { bool } = React.PropTypes;
const CopyrightAssociationForm = React.createClass({
propTypes: {
// Injected through HOCs
currentUser: currentUserShape.isRequired,
isLoggedIn: bool.isRequired
},
handleSubmitSuccess() {
const notification = new GlobalNotificationModel(getLangText('Copyright association updated'), 'success', 10000);
GlobalNotificationActions.appendGlobalNotification(notification);
},
getProfileFormData() {
return { email: this.props.currentUser.email };
},
render() {
const { currentUser, isLoggedIn } = this.props;
const selectDefaultValue = ' -- ' + getLangText('select an association') + ' -- ';
let selectedState = selectDefaultValue;
if (currentUser.profile && currentUser.profile.copyright_association) {
if (AppConstants.copyrightAssociations.indexOf(currentUser.profile.copyright_association) !== -1) {
selectedState = AppConstants.copyrightAssociations[selectedState];
}
}
if (isLoggedIn) {
return (
<Form
ref='form'
url={ApiUrls.users_profile}
getFormData={this.getProfileFormData}
handleSuccess={this.handleSubmitSuccess}>
<Property
name="copyright_association"
className="ascribe-property-collapsible-toggle"
label={getLangText('Copyright Association')}>
<select defaultValue={selectedState} name="contract">
<option
name={0}
key={0}
value={selectDefaultValue}>
{selectDefaultValue}
</option>
{AppConstants.copyrightAssociations.map((association, i) => {
return (
<option
name={i + 1}
key={i + 1}
value={association}>
{ association }
</option>
);
})}
</select>
</Property>
<hr />
</Form>
);
} else {
return null;
}
}
});
export default withCurrentUser(CopyrightAssociationForm);