<commit_before>import Webiny from 'Webiny';
const Ui = Webiny.Ui.Components;

class Data extends Webiny.Ui.Component {

    constructor(props) {
        super(props);
        this.autoRefreshInterval = null; // Ony when 'autoRefresh' prop is used

        this.state = {
            data: null
        };

        this.bindMethods('setData,filter');
        Webiny.Mixins.ApiComponent.extend(this);
    }

    componentWillMount() {
        super.componentWillMount();
        if (!_.isFunction(this.props.children)) {
            console.warn('Warning: Data component only accepts a function as its child element!');
        }
        this.setState({loading: true});
    }

    componentDidMount() {
        super.componentDidMount();
        this.request = this.api.execute().then(response => {
            this.setData(response);
            return response.getData();
        });

        if (_.isNumber(this.props.autoRefresh)) {
            this.autoRefreshInterval = setInterval(() => {
                this.request = this.api.execute().then(response => {
                    this.setData(response);
                    return response.getData();
                });
            }, this.props.autoRefresh * 1000)
        }
    }

    componentWillUnmount() {
        super.componentWillUnmount();
        if (this.request) {
            this.request.abort();
        }

        if (this.autoRefreshInterval) {
            clearInterval(this.autoRefreshInterval);
        }
    }

    setData(response) {
        if (response.isAborted() || !this.isMounted()) {
            return;
        }

        if (response.isError()) {
            this.setState({loading: false});
            Webiny.Growl.info(response.getError(), 'Could not fetch data', true);
            return;
        }
        this.setState({data: response.getData(), loading: false});
    }

    filter(filters = {}) {
        this.setState({loading: true});
        this.request = this.api.setQuery(filters).execute().then(this.setData);
        return this.request;
    }
}

Data.defaultProps = {
    waitForData: true,
    autoRefresh: null,
    renderer() {
        if (this.props.waitForData && !this.state.data) {
            return null;
        }

        const loader = this.state.loading ? <Ui.Loader/> : null;

        return (
            <webiny-data>
                {_.isFunction(this.props.children) ? this.props.children.call(this, this.state.data, this.filter, loader, this) : null}
            </webiny-data>
        );
    }
};

export default Data;<commit_msg>- onLoad and onInitialLoad callbacks on Data component<commit_after>import Webiny from 'Webiny';
const Ui = Webiny.Ui.Components;

class Data extends Webiny.Ui.Component {

    constructor(props) {
        super(props);
        this.autoRefreshInterval = null; // Ony when 'autoRefresh' prop is used

        this.state = {
            data: null
        };

        this.bindMethods('setData,filter');
        Webiny.Mixins.ApiComponent.extend(this);
    }

    componentWillMount() {
        super.componentWillMount();
        if (!_.isFunction(this.props.children)) {
            console.warn('Warning: Data component only accepts a function as its child element!');
        }
        this.setState({loading: true});
    }

    componentDidMount() {
        super.componentDidMount();
        this.request = this.api.execute().then(apiResponse => {
            this.setData(apiResponse);
            this.props.onInitialLoad(apiResponse);
            return apiResponse.getData();
        });

        if (_.isNumber(this.props.autoRefresh)) {
            this.autoRefreshInterval = setInterval(() => {
                this.request = this.api.execute().then(response => {
                    this.setData(response);
                    return response.getData();
                });
            }, this.props.autoRefresh * 1000)
        }
    }

    componentWillUnmount() {
        super.componentWillUnmount();
        if (this.request) {
            this.request.abort();
        }

        if (this.autoRefreshInterval) {
            clearInterval(this.autoRefreshInterval);
        }
    }

    setData(response) {
        if (response.isAborted() || !this.isMounted()) {
            return;
        }

        if (response.isError()) {
            this.setState({loading: false});
            Webiny.Growl.info(response.getError(), 'Could not fetch data', true);
            return;
        }
        this.setState({data: response.getData(), loading: false});
    }

    filter(filters = {}) {
        this.setState({loading: true});
        this.request = this.api.setQuery(filters).execute().then(apiResponse => {
            this.setData(apiResponse);
            this.props.onLoad(apiResponse);
        });
        return this.request;
    }
}

Data.defaultProps = {
    waitForData: true,
    autoRefresh: null,
    onLoad: _.noop,
    onInitialLoad: _.noop,
    renderer() {
        if (this.props.waitForData && !this.state.data) {
            return null;
        }

        const loader = this.state.loading ? <Ui.Loader/> : null;

        return (
            <webiny-data>
                {_.isFunction(this.props.children) ? this.props.children.call(this, this.state.data, this.filter, loader, this) : null}
            </webiny-data>
        );
    }
};

export default Data;
{'fraction_non_alphanumeric': 0.13006914595402727, 'fraction_numerical': 0.0014950476546439918, 'mean_word_length': 1.9102773246329527, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 16, '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': '1858282', 'n_tokens_mistral': 1501, 'n_tokens_neox': 1486, 'n_words': 263}