Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tweaks for add funds to org form #520

Merged
merged 4 commits into from
Jun 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 22 additions & 10 deletions src/components/AddFundsForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ class AddFundsForm extends React.Component {
super(props);
const { intl } = props;

/* If the component doesn't receive a host property it means that
the form will load the hosts from the list of collectives the
user is an admin of. See issue #1080 */
this.isAddFundsToOrg = !this.props.host;

this.state = {
form: { totalAmount: 0, hostFeePercent: get(props, 'collective.hostFeePercent') },
result: {}
Expand All @@ -40,6 +45,7 @@ class AddFundsForm extends React.Component {
'totalAmount.label': { id: 'addfunds.amount.label', defaultMessage: 'amount' },
'description.label': { id: 'addfunds.description.label', defaultMessage: 'description' },
'FromCollectiveId.label': { id: 'addfunds.FromCollectiveId.label', defaultMessage: 'source' },
'FromCollectiveId.addfundstoorg.label': { id: 'addfundstoorg.FromCollectiveId.label', defaultMessage: 'host' },
'hostFeePercent.label': { id: 'addfunds.hostFeePercent.label', defaultMessage: 'Host fee' },
'platformFeePercent.label': { id: 'addfunds.platformFeePercent.label', defaultMessage: 'Platform fee' },
'name.label': { id: 'user.name.label', defaultMessage: 'name' },
Expand All @@ -61,7 +67,7 @@ class AddFundsForm extends React.Component {
{
name: "FromCollectiveId",
type: "component",
when: () => this.props.host,
when: () => !this.isAddFundsToOrg,
component: AddFundsSourcePicker,
options: {
collective: this.props.collective,
Expand All @@ -71,8 +77,9 @@ class AddFundsForm extends React.Component {
{
name: "FromCollectiveId",
type: "component",
when: () => !this.props.host,
when: () => this.isAddFundsToOrg,
component: AddFundsSourcePickerForUserWithData,
label: 'FromCollectiveId.addfundstoorg.label',
options: {
LoggedInUser: this.props.LoggedInUser,
}
Expand All @@ -95,6 +102,7 @@ class AddFundsForm extends React.Component {
},
{
name: "hostFeePercent",
when: () => !this.isAddFundsToOrg,
type: 'number',
className: 'right',
post: '%'
Expand All @@ -103,19 +111,21 @@ class AddFundsForm extends React.Component {
name: "platformFeePercent",
type: 'number',
post: '%',
when: () => this.props.LoggedInUser && this.props.LoggedInUser.isRoot()
when: () => ((this.props.LoggedInUser && this.props.LoggedInUser.isRoot())
&& !this.isAddFundsToOrg),
}
]
];

this.fields = this.fields.map(field => {
if (this.messages[`${field.name}.label`]) {
field.label = intl.formatMessage(this.messages[`${field.name}.label`]);
const label = this.messages[field.label || `${field.name}.label`];
if (label) {
field.label = intl.formatMessage(label);
}
if (this.messages[`${field.name}.description`]) {
field.description = intl.formatMessage(this.messages[`${field.name}.description`]);
}
return field;
})
});
}

handleChange(obj, attr, value) {
Expand Down Expand Up @@ -241,11 +251,13 @@ class AddFundsForm extends React.Component {
<td><FormattedMessage id="addfunds.totalAmount" defaultMessage="Funding amount" /></td>
<td className="amount">{formatCurrency(this.state.form.totalAmount, this.props.collective.currency, { precision: 2 })}</td>
</tr>
{ !this.isAddFundsToOrg &&
<tr>
<td><FormattedMessage id="addfunds.hostFees" defaultMessage="Host fees ({hostFees})" values={{ hostFees: `${hostFeePercent}%` }} /></td>
<td className="amount">{formatCurrency(hostFeePercent/100 * this.state.form.totalAmount, this.props.collective.currency, { precision: 2 })}</td>
</tr>
{ platformFeePercent > 0 &&
}
{ platformFeePercent > 0 && !this.isAddFundsToOrg &&
<tr>
<td><FormattedMessage id="addfunds.platformFees" defaultMessage="Platform fees ({platformFees})" values={{ platformFees: `${platformFeePercent}%` }} /></td>
<td className="amount">{formatCurrency(platformFeePercent/100 * this.state.form.totalAmount, this.props.collective.currency, { precision: 2 })}</td>
Expand Down Expand Up @@ -278,8 +290,8 @@ class AddFundsForm extends React.Component {
</Col>
</Row>
<Row>
<Col sm={3}></Col>
<Col sm={9} className="actions">
<Col xsHidden md={2}></Col>
<Col xs={12} md={10} className="actions">
<Button bsStyle="primary" onClick={() => this.handleSubmit()} disabled={loading}>
{ loading && <FormattedMessage id="form.processing" defaultMessage="processing" /> }
{ !loading && <FormattedMessage id="addfunds.submit" defaultMessage="Add Funds" /> }
Expand Down
47 changes: 38 additions & 9 deletions src/components/MenuBar.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import Sticky from 'react-stickynode';
import gql from 'graphql-tag';
import { get, throttle, uniqBy, pick } from 'lodash';
import { graphql } from 'react-apollo';
import { Modal } from 'react-bootstrap';
import { Modal, Row, Col } from 'react-bootstrap';

import { FormattedMessage, defineMessages } from 'react-intl';
import { animateScroll } from 'react-scrollchor/lib/helpers';

Expand Down Expand Up @@ -56,6 +57,7 @@ class MenuBar extends React.Component {
sticky: false,
logoLink: `/${props.collective.slug}`,
showAddFunds: false,
fundsAdded: false,
};

this.messages = defineMessages({
Expand Down Expand Up @@ -187,7 +189,8 @@ class MenuBar extends React.Component {
this.setState({ loading: true });
try {
await this.props.addFundsToOrg(params);
this.setState({ showAddFunds: false, loading: false });
this.setState({ fundsAdded: true, loading: false, error: null });
return null;
} catch (e) {
console.error(e);
return err(e.message && e.message.replace(/GraphQL error:/, ""));
Expand All @@ -206,13 +209,39 @@ class MenuBar extends React.Component {
style={{ zIndex: 999999 }}
>
<Modal.Body>
<AddFundsForm
LoggedInUser={LoggedInUser}
collective={collective}
onSubmit={this.addFundsToOrg}
onCancel={this.hideAddFunds}
/>
{ this.state.error && <div>{this.state.error}</div> }
<Row>
<Col sm={12}>
{ this.state.fundsAdded &&
<div>
<h1>Funds added to organization successfully</h1>
<center>
<Button
className="blue"
onClick={() => this.setState({ fundsAdded: false, showAddFunds: false })}
>
Ok
</Button>
</center>
</div> }

{ !this.state.fundsAdded &&
<AddFundsForm
LoggedInUser={LoggedInUser}
collective={collective}
onSubmit={this.addFundsToOrg}
onCancel={this.hideAddFunds}
/> }
</Col>
</Row>
<Row>
<Col sm={2}></Col>
<Col sm={10}>
{ this.state.error &&
<div style={{ color: 'red', fontWeight: 'bold' }}>
{this.state.error}
</div> }
</Col>
</Row>
</Modal.Body>
</Modal>
);
Expand Down