1
0
mirror of https://github.com/aclindsa/moneygo.git synced 2024-11-01 00:10:06 -04:00
moneygo/js/components/AccountsTab.js

250 lines
7.9 KiB
JavaScript
Raw Normal View History

var React = require('react');
var ReactDOM = require('react-dom');
var ReactBootstrap = require('react-bootstrap');
var Grid = ReactBootstrap.Grid;
var Row = ReactBootstrap.Row;
var Col = ReactBootstrap.Col;
var Form = ReactBootstrap.Form;
var FormGroup = ReactBootstrap.FormGroup;
var FormControl = ReactBootstrap.FormControl;
var Checkbox = ReactBootstrap.Checkbox;
var ControlLabel = ReactBootstrap.ControlLabel;
var Button = ReactBootstrap.Button;
var ButtonGroup = ReactBootstrap.ButtonGroup;
var Glyphicon = ReactBootstrap.Glyphicon;
var ListGroup = ReactBootstrap.ListGroup;
var ListGroupItem = ReactBootstrap.ListGroupItem;
2016-02-13 10:46:08 -05:00
var Alert = ReactBootstrap.Alert;
var Modal = ReactBootstrap.Modal;
var Collapse = ReactBootstrap.Collapse;
2017-05-31 21:04:01 -04:00
var Tabs = ReactBootstrap.Tabs;
var Tab = ReactBootstrap.Tab;
var Panel = ReactBootstrap.Panel;
var Combobox = require('react-widgets').Combobox;
2015-07-04 11:42:52 -04:00
var models = require('../models');
var Security = models.Security;
var Account = models.Account;
2017-05-31 21:04:01 -04:00
var AccountType = models.AccountType;
var AccountTypeList = models.AccountTypeList;
var AccountTree = require('./AccountTree');
var AccountCombobox = require('./AccountCombobox');
var AccountRegister = require('./AccountRegister');
var AddEditAccountModal = require('./AddEditAccountModal');
class DeleteAccountModal extends React.Component {
getInitialState(props) {
if (!props)
var accountid = -1;
else if (props.initialAccount != null)
var accountid = props.initialAccount.AccountId;
else if (props.accounts.length > 0)
var accountid = props.accounts[0].AccountId;
2015-07-04 21:11:00 -04:00
else
var accountid = -1;
return {
error: "",
2015-07-04 21:11:00 -04:00
accountid: accountid,
checked: false,
show: false
};
}
constructor() {
super();
this.state = this.getInitialState();
this.onCancel = this.handleCancel.bind(this);
this.onChange = this.handleChange.bind(this);
this.onCheckboxClick = this.handleCheckboxClick.bind(this);
this.onSubmit = this.handleSubmit.bind(this);
}
componentWillReceiveProps(nextProps) {
if (nextProps.show && !this.props.show) {
this.setState(this.getInitialState(nextProps));
}
}
handleCancel() {
2015-07-04 21:11:00 -04:00
if (this.props.onCancel != null)
this.props.onCancel();
}
handleChange(account) {
2015-07-04 21:11:00 -04:00
this.setState({accountid: account.AccountId});
}
handleCheckboxClick() {
2015-07-04 21:11:00 -04:00
this.setState({checked: !this.state.checked});
}
handleSubmit() {
2016-10-05 13:36:47 -04:00
if (this.props.accounts.hasOwnProperty(this.state.accountid)) {
2015-07-04 21:11:00 -04:00
if (this.state.checked) {
if (this.props.onSubmit != null)
2016-10-05 13:36:47 -04:00
this.props.onSubmit(this.props.accounts[this.state.accountid]);
2015-07-04 21:11:00 -04:00
} else {
this.setState({error: "You must confirm you wish to delete this account."});
}
} else {
this.setState({error: "You must select an account."});
}
}
render() {
2015-07-04 21:11:00 -04:00
var checkbox = [];
2016-10-05 13:36:47 -04:00
if (this.props.accounts.hasOwnProperty(this.state.accountid)) {
var parentAccountId = this.props.accounts[this.state.accountid].ParentAccountId;
2015-07-04 21:11:00 -04:00
var parentAccount = "will be deleted and any child accounts will become top-level accounts.";
if (parentAccountId != -1)
2016-10-05 13:36:47 -04:00
parentAccount = "and any child accounts will be re-parented to: " + this.props.accounts[parentAccountId].Name;
2015-07-04 21:11:00 -04:00
var warningString = "I understand that deleting this account cannot be undone and that all transactions " + parentAccount;
checkbox = (
<FormGroup>
<Col xsOffset={2} sm={10}>
<Checkbox
checked={this.state.checked ? "checked" : ""}
onClick={this.onCheckboxClick}>
{warningString}
</Checkbox>
</Col>
</FormGroup>);
2015-07-04 21:11:00 -04:00
}
var warning = [];
if (this.state.error.length != "") {
warning = (
<Alert bsStyle="danger"><strong>Error: </strong>{this.state.error}</Alert>
);
}
return (
<Modal
show={this.props.show}
onHide={this.onCancel}
ref="modal">
2015-07-04 21:11:00 -04:00
<Modal.Header closeButton>
<Modal.Title>Delete Account</Modal.Title>
</Modal.Header>
<Modal.Body>
{warning}
<Form horizontal onSubmit={this.onSubmit}>
<FormGroup>
<Col componentClass={ControlLabel} xs={2}>Delete Account</Col>
<Col xs={10}>
<AccountCombobox
includeRoot={false}
accounts={this.props.accounts}
2016-10-05 13:36:47 -04:00
accountChildren={this.props.accountChildren}
value={this.state.accountid}
onChange={this.onChange}/>
</Col>
</FormGroup>
{checkbox}
</Form>
2015-07-04 21:11:00 -04:00
</Modal.Body>
<Modal.Footer>
<ButtonGroup className="pull-right">
<Button onClick={this.onCancel} bsStyle="warning">Cancel</Button>
<Button onClick={this.onSubmit} bsStyle="success">Delete Account</Button>
2015-07-04 21:11:00 -04:00
</ButtonGroup>
</Modal.Footer>
</Modal>
);
}
}
2015-07-04 21:11:00 -04:00
class AccountsTab extends React.Component {
constructor() {
super();
this.state = {
2015-07-04 21:55:49 -04:00
editingAccount: false,
2015-07-04 21:11:00 -04:00
deletingAccount: false
};
this.onEditAccount = this.handleEditAccount.bind(this);
this.onDeleteAccount = this.handleDeleteAccount.bind(this);
this.onEditingCancel = this.handleEditingCancel.bind(this);
this.onDeletionCancel = this.handleDeletionCancel.bind(this);
this.onUpdateAccount = this.handleUpdateAccount.bind(this);
this.onRemoveAccount = this.handleRemoveAccount.bind(this);
}
handleEditAccount() {
2015-07-04 21:55:49 -04:00
this.setState({editingAccount: true});
}
handleDeleteAccount() {
2015-07-04 21:11:00 -04:00
this.setState({deletingAccount: true});
}
handleEditingCancel() {
2015-07-04 21:55:49 -04:00
this.setState({editingAccount: false});
}
handleDeletionCancel() {
2015-07-04 21:11:00 -04:00
this.setState({deletingAccount: false});
}
handleUpdateAccount(account) {
2015-07-04 21:55:49 -04:00
if (this.props.onUpdateAccount != null)
this.props.onUpdateAccount(account);
this.setState({editingAccount: false});
}
handleRemoveAccount(account) {
2015-07-04 21:11:00 -04:00
if (this.props.onDeleteAccount != null)
this.props.onDeleteAccount(account);
2016-10-05 13:36:47 -04:00
this.setState({deletingAccount: false});
}
render() {
2016-10-05 13:36:47 -04:00
var disabled = (this.props.selectedAccount == -1) ? true : false;
2016-10-05 13:36:47 -04:00
var selectedAccount = null;
if (this.props.accounts.hasOwnProperty(this.props.selectedAccount))
selectedAccount = this.props.accounts[this.props.selectedAccount];
2015-07-04 21:55:49 -04:00
return (
<div>
<AddEditAccountModal
show={this.state.editingAccount}
editAccount={selectedAccount}
accounts={this.props.accounts}
accountChildren={this.props.accountChildren}
onCancel={this.onEditingCancel}
onSubmit={this.onUpdateAccount}
security_list={this.props.security_list}/>
<DeleteAccountModal
show={this.state.deletingAccount}
initialAccount={selectedAccount}
accounts={this.props.accounts}
accountChildren={this.props.accountChildren}
onCancel={this.onDeletionCancel}
onSubmit={this.onRemoveAccount}/>
<ButtonGroup className="account-buttongroup">
<Button onClick={this.onEditAccount}
bsStyle="primary" disabled={disabled}>
<Glyphicon glyph='cog' /> Edit Account</Button>
<Button onClick={this.onDeleteAccount}
bsStyle="danger" disabled={disabled}>
<Glyphicon glyph='trash' /> Delete Account</Button>
</ButtonGroup>
<AccountRegister
pageSize={20}
selectedAccount={this.props.selectedAccount}
accounts={this.props.accounts}
accountChildren={this.props.accountChildren}
securities={this.props.securities}
transactions={this.props.transactions}
transactionPage={this.props.transactionPage}
imports={this.props.imports}
onFetchAllAccounts={this.props.onFetchAllAccounts}
onFetchAllSecurities={this.props.onFetchAllSecurities}
onCreateTransaction={this.props.onCreateTransaction}
onUpdateTransaction={this.props.onUpdateTransaction}
onDeleteTransaction={this.props.onDeleteTransaction}
onSelectTransaction={this.props.onSelectTransaction}
onUnselectTransaction={this.props.onUnselectTransaction}
onFetchTransactionPage={this.props.onFetchTransactionPage}
onOpenImportModal={this.props.onOpenImportModal}
onCloseImportModal={this.props.onCloseImportModal}
onImportOFX={this.props.onImportOFX}
onImportOFXFile={this.props.onImportOFXFile}
onImportGnucash={this.props.onImportGnucash} />
</div>
);
}
}
module.exports = AccountsTab;