<commit_before>import * as React from 'react';
import * as moment from 'moment';
import { TransactionTable, getDirectionColoring } from "common/transactions-view";
import { Transaction, transactionService, walletService, Wallet } from 'walletApi';
import { bind, ListHelpers } from 'walletCommon';
import { Layout } from 'layout';
import { TransactionViewModel, mapTransactionViewModel, mapTransaction } from 'common/models';
import { YearSelector } from 'pages/transactions/yearSelector';
import { MonthSelector } from 'pages/transactions/monthSelector';
import { connect } from 'react-redux';
import { RootState } from 'reducers';

export namespace Transactions {
    export interface Props {
        wallets: Wallet[];
    }

    export interface State {
        changedItems: TransactionViewModel[];
        deletedItems: number[];
        items: TransactionViewModel[];
        selectedYear: number;
        selectedMonth: number;
        maxYear: number;
    }
}

@connect(mapStateToProps)
export class Transactions extends React.Component<Transactions.Props, Transactions.State> {

    constructor(props) {
        super(props);
        const now = new Date();
        this.state = {
            changedItems: [],
            deletedItems: [],
            items: [],
            selectedYear: now.getFullYear(),
            selectedMonth: now.getMonth() + 1,
            maxYear: now.getFullYear()
        };
    }

    componentDidMount() {
        this.dateSelected(this.state.selectedYear, this.state.selectedMonth);
    }

    @bind
    async save() {
        try {
            //this.alertsService.dismissAll();
            if (this.state.changedItems.length === 0 && this.state.deletedItems.length === 0) {
                //this.alertsService.warning("No data has changed.");
                return;
            }
            var transactions = mapTransaction(this.state.changedItems);
            var resultTransactions = await transactionService.batchUpdate(transactions, this.state.deletedItems);
            this.setState({
                changedItems: [],
                deletedItems: []
            });
            //this.alertsService.success("Changes saved successfully.");
        } catch (error) {
            //this.alertsService.error(error);
        }
    }

    @bind
    private deleteItem(item: TransactionViewModel) {
        this.setState((prevState, props) => {
            return {
                items: ListHelpers.remove(prevState.items, item),
                changedItems: ListHelpers.remove(prevState.changedItems, item),
                deletedItems: [...prevState.deletedItems, item.transactionId]
            };
        });
    }

    @bind
    private update(items: TransactionViewModel[], changedItems: TransactionViewModel[]): void {
        this.setState({
            items: items,
            changedItems: changedItems
        });
    }

    @bind
    private yearSelected(year) {
        this.setState({
            selectedYear: year
        }, () => {
            this.dateSelected(this.state.selectedYear, this.state.selectedMonth);
        });
    }

    @bind
    private monthSelected(month: number) {
        this.setState({
            selectedMonth: month
        }, () => {
            this.dateSelected(this.state.selectedYear, this.state.selectedMonth);
        });
    }

    private async dateSelected(year: number, month: number) {
        const start = moment(year + "-" + month + "-01");
        const end = moment(start).endOf("month");
        const result = await transactionService.fetch({
            search: `CreatedAt >= "${start.format("YYYY-MM-DD")}" And CreatedAt <= "${end.format("YYYY-MM-DD")}"`
        });
        this.setState({
            items: mapTransactionViewModel(result, this.props.wallets)
        });
    }

    render() {
        const { maxYear, selectedYear, selectedMonth, items, changedItems } = this.state;
        const { wallets } = this.props;
        return (
            <Layout>
                <form>
                    <div className="form-row">
                        <div className="col">
                            <button type="button" className="btn btn-success" onClick={this.save} name="saveBtn">Save</button>
                        </div>
                        <div className="col">
                            <div className="input-group">
                                <YearSelector from={2009} to={maxYear} year={selectedYear} onChange={this.yearSelected} />
                                <MonthSelector year={selectedYear} month={selectedMonth} onChange={this.monthSelected} />
                            </div>
                        </div>
                    </div>
                </form>
                <TransactionTable changedItems={changedItems} wallets={wallets}
                    items={items} rowColor={getDirectionColoring}
                    deleted={this.deleteItem} update={this.update} />
            </Layout>
        );
    }
}

function mapStateToProps(state: RootState, ownProps: any) {
    return {
        wallets: state.wallets
    };
}<commit_msg>Transaction: wallet through redux store. Save alerts put back.<commit_after>import * as React from 'react';
import * as moment from 'moment';
import { bindActionCreators } from 'redux';

import * as AlertsActions from "actions/alerts"
import { TransactionTable, getDirectionColoring } from "common/transactions-view";
import { Transaction, transactionService, walletService, Wallet } from 'walletApi';
import { bind, ListHelpers } from 'walletCommon';
import { Layout } from 'layout';
import { TransactionViewModel, mapTransactionViewModel, mapTransaction } from 'common/models';
import { YearSelector } from 'pages/transactions/yearSelector';
import { MonthSelector } from 'pages/transactions/monthSelector';
import { connect } from 'react-redux';
import { RootState } from 'reducers';

export namespace Transactions {
    export interface Props {
        wallets: Wallet[];
        actions?: typeof AlertsActions,
    }

    export interface State {
        changedItems: TransactionViewModel[];
        deletedItems: number[];
        items: TransactionViewModel[];
        selectedYear: number;
        selectedMonth: number;
        maxYear: number;
    }
}

@connect(mapStateToProps, mapDispatchToProps)
export class Transactions extends React.Component<Transactions.Props, Transactions.State> {

    constructor(props) {
        super(props);
        const now = new Date();
        this.state = {
            changedItems: [],
            deletedItems: [],
            items: [],
            selectedYear: now.getFullYear(),
            selectedMonth: now.getMonth() + 1,
            maxYear: now.getFullYear()
        };
    }

    get alertsService() {
        return this.props.actions;
    }

    componentDidMount() {
        this.dateSelected(this.state.selectedYear, this.state.selectedMonth);
    }

    componentWillReceiveProps() {
        this.dateSelected(this.state.selectedYear, this.state.selectedMonth);
    }

    @bind
    async save() {
        try {
            this.alertsService.dismissAllAlert();
            if (this.state.changedItems.length === 0 && this.state.deletedItems.length === 0) {
                this.alertsService.showAlert({ type: "warning", message: "No data has changed." });
                return;
            }
            var transactions = mapTransaction(this.state.changedItems);
            var resultTransactions = await transactionService.batchUpdate(transactions, this.state.deletedItems);
            this.setState({
                changedItems: [],
                deletedItems: []
            });
            this.alertsService.showAlert({ type: "success", message: "Changes saved successfully." });
        } catch (error) {
            this.alertsService.showAlert({ type: "danger", message: error });
        }
    }

    @bind
    private deleteItem(item: TransactionViewModel) {
        this.setState((prevState, props) => {
            return {
                items: ListHelpers.remove(prevState.items, item),
                changedItems: ListHelpers.remove(prevState.changedItems, item),
                deletedItems: [...prevState.deletedItems, item.transactionId]
            };
        });
    }

    @bind
    private update(items: TransactionViewModel[], changedItems: TransactionViewModel[]): void {
        this.setState({
            items: items,
            changedItems: changedItems
        });
    }

    @bind
    private yearSelected(year) {
        this.setState({
            selectedYear: year
        }, () => {
            this.dateSelected(this.state.selectedYear, this.state.selectedMonth);
        });
    }

    @bind
    private monthSelected(month: number) {
        this.setState({
            selectedMonth: month
        }, () => {
            this.dateSelected(this.state.selectedYear, this.state.selectedMonth);
        });
    }

    private async dateSelected(year: number, month: number) {
        const start = moment(year + "-" + month + "-01");
        const end = moment(start).endOf("month");
        const result = await transactionService.fetch({
            search: `CreatedAt >= "${start.format("YYYY-MM-DD")}" And CreatedAt <= "${end.format("YYYY-MM-DD")}"`
        });
        this.setState({
            items: mapTransactionViewModel(result, this.props.wallets)
        });
    }

    render() {
        const { maxYear, selectedYear, selectedMonth, items, changedItems } = this.state;
        const { wallets } = this.props;
        return (
            <Layout>
                <form>
                    <div className="form-row">
                        <div className="col">
                            <button type="button" className="btn btn-success" onClick={this.save} name="saveBtn">Save</button>
                        </div>
                        <div className="col">
                            <div className="input-group">
                                <YearSelector from={2009} to={maxYear} year={selectedYear} onChange={this.yearSelected} />
                                <MonthSelector year={selectedYear} month={selectedMonth} onChange={this.monthSelected} />
                            </div>
                        </div>
                    </div>
                </form>
                <TransactionTable changedItems={changedItems} wallets={wallets}
                    items={items} rowColor={getDirectionColoring}
                    deleted={this.deleteItem} update={this.update} />
            </Layout>
        );
    }
}

function mapStateToProps(state: RootState, ownProps: any) {
    return {
        wallets: state.wallets
    };
}

function mapDispatchToProps(dispatch, ownProps: any) {
    return {
        actions: bindActionCreators(AlertsActions as any, dispatch) as typeof AlertsActions
    };
}
{'fraction_non_alphanumeric': 0.11719253058596266, 'fraction_numerical': 0.0016557814368503357, 'mean_word_length': 2.0677200902934536, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7490954', 'n_tokens_mistral': 2777, 'n_tokens_neox': 2729, 'n_words': 585}