<commit_before>import {
    action,
    computed,
    extendObservable,
    observable }                 from 'mobx';
import { isEmptyObject }         from '_common/utility';
import { localize }              from '_common/localize';
import { WS }                    from 'Services';
import { createChartBarrier }    from './Helpers/chart-barriers';
import { createChartMarkers }    from './Helpers/chart-markers';
import {
    getDetailsExpiry,
    getDetailsInfo }             from './Helpers/details';
import {
    getDigitInfo,
    isDigitContract }            from './Helpers/digits';
import {
    getChartConfig,
    getDisplayStatus,
    getEndSpot,
    getEndSpotTime,
    getFinalPrice,
    getIndicativePrice,
    isEnded,
    isSoldBeforeStart,
    isStarted,
    isUserSold,
    isValidToSell }              from './Helpers/logic';
import BaseStore                 from '../../base-store';

export default class ContractStore extends BaseStore {
    @observable contract_id;

    @observable contract_info = observable.object({});
    @observable digits_info   = observable.object({});
    @observable sell_info     = observable.object({});
    @observable chart_config  = observable.object({});

    @observable has_error         = false;
    @observable error_message     = '';
    @observable is_sell_requested = false;

    // -------------------
    // ----- Actions -----
    // -------------------
    @action.bound
    updateChartType(chart_type) {
        this.chart_config.chart_type = chart_type;
    }

    @action.bound
    updateGranularity(granularity) {
        this.chart_config.granularity = granularity;
    }

    @action.bound
    onMount(contract_id) {
        this.onSwitchAccount(this.accountSwitcherListener.bind(null, contract_id));
        this.has_error     = false;
        this.error_message = '';
        this.contract_id   = contract_id;
        this.smart_chart   = this.root_store.modules.smart_chart;
        this.smart_chart.setContractMode(true);

        if (contract_id) {
            WS.subscribeProposalOpenContract(this.contract_id, this.updateProposal, false);
        }
    }

    @action.bound
    accountSwitcherListener (contract_id) {
        this.has_error     = false;
        this.error_message = '';
        this.contract_id   = contract_id;
        this.smart_chart   = this.root_store.modules.smart_chart;
        this.smart_chart.setContractMode(true);

        if (contract_id) {
            WS.subscribeProposalOpenContract(this.contract_id, this.updateProposal, false);
        }
    }

    @action.bound
    onUnmount(contract_id) {
        this.disposeSwitchAccount();
        if (contract_id) {
            WS.forget('proposal_open_contract', this.updateProposal, { contract_id: +contract_id });
        }
        this.forgetProposalOpenContract();

        this.contract_id       = null;
        this.contract_info     = {};
        this.digits_info       = {};
        this.sell_info         = {};
        this.is_sell_requested = false;

        this.smart_chart.removeBarriers();
        this.smart_chart.removeMarkers();
        this.smart_chart.setContractMode(false);
    }

    @action.bound
    updateProposal(response) {
        if ('error' in response) {
            this.has_error     = true;
            this.error_message = response.error.message;
            this.contract_info = {};
            return;
        }
        if (isEmptyObject(response.proposal_open_contract)) {
            this.has_error     = true;
            this.error_message = localize('Contract does not exist or does not belong to this client.');
            this.contract_info = {};
            return;
        }
        this.contract_info = response.proposal_open_contract;
        if (isEnded(this.contract_info)) {
            this.chart_config = getChartConfig(this.contract_info);
        } else {
            delete this.chart_config.end_epoch;
            delete this.chart_config.start_epoch;
        }
        createChartBarrier(this.smart_chart, this.contract_info);
        createChartMarkers(this.smart_chart, this.contract_info, this);
        this.handleDigits();
    }

    @action.bound
    handleDigits() {
        if (isDigitContract(this.contract_info.contract_type)) {
            extendObservable(this.digits_info, getDigitInfo(this.digits_info, this.contract_info));
        }
    }

    @action.bound
    onClickSell() {
        if (this.contract_id && !this.is_sell_requested && isEmptyObject(this.sell_info)) {
            this.is_sell_requested = true;
            WS.sell(this.contract_id, this.contract_info.bid_price).then(this.handleSell);
        }
    }

    @action.bound
    handleSell(response) {
        if (response.error) {
            this.sell_info = {
                error_message: response.error.message,
            };

            this.is_sell_requested = false;
        } else {
            this.forgetProposalOpenContract();
            WS.proposalOpenContract(this.contract_id).then(action((proposal_response) => {
                this.updateProposal(proposal_response);
                this.sell_info = {
                    sell_price    : response.sell.sold_for,
                    transaction_id: response.sell.transaction_id,
                };
            }));
        }
    }

    forgetProposalOpenContract() {
        WS.forget('proposal_open_contract', this.updateProposal, { contract_id: this.contract_id });
    }
<commit_msg>refactoring contract-store<commit_after>import {
    action,
    computed,
    extendObservable,
    observable }                 from 'mobx';
import { isEmptyObject }         from '_common/utility';
import { localize }              from '_common/localize';
import { WS }                    from 'Services';
import { createChartBarrier }    from './Helpers/chart-barriers';
import { createChartMarkers }    from './Helpers/chart-markers';
import {
    getDetailsExpiry,
    getDetailsInfo }             from './Helpers/details';
import {
    getDigitInfo,
    isDigitContract }            from './Helpers/digits';
import {
    getChartConfig,
    getDisplayStatus,
    getEndSpot,
    getEndSpotTime,
    getFinalPrice,
    getIndicativePrice,
    isEnded,
    isSoldBeforeStart,
    isStarted,
    isUserSold,
    isValidToSell }              from './Helpers/logic';
import BaseStore                 from '../../base-store';

export default class ContractStore extends BaseStore {
    @observable contract_id;

    @observable contract_info = observable.object({});
    @observable digits_info   = observable.object({});
    @observable sell_info     = observable.object({});
    @observable chart_config  = observable.object({});

    @observable has_error         = false;
    @observable error_message     = '';
    @observable is_sell_requested = false;

    // -------------------
    // ----- Actions -----
    // -------------------
    @action.bound
    updateChartType(chart_type) {
        this.chart_config.chart_type = chart_type;
    }

    @action.bound
    updateGranularity(granularity) {
        this.chart_config.granularity = granularity;
    }

    @action.bound
    onMount(contract_id) {
        this.onSwitchAccount(this.accountSwitcherListener.bind(null, contract_id));
        this.has_error     = false;
        this.error_message = '';
        this.contract_id   = contract_id;
        this.smart_chart   = this.root_store.modules.smart_chart;
        this.smart_chart.setContractMode(true);

        if (contract_id) {
            WS.subscribeProposalOpenContract(this.contract_id, this.updateProposal, false);
        }
    }

    @action.bound
    accountSwitcherListener (contract_id) {
        this.has_error     = false;
        this.error_message = '';
        this.contract_id   = contract_id;
        this.smart_chart   = this.root_store.modules.smart_chart;
        this.smart_chart.setContractMode(true);

        if (contract_id) {
            WS.subscribeProposalOpenContract(this.contract_id, this.updateProposal, false);
        }
    }

    @action.bound
    onUnmount(contract_id) {
        this.disposeSwitchAccount();
        this.forgetProposalOpenContract(contract_id);

        this.contract_id       = null;
        this.contract_info     = {};
        this.digits_info       = {};
        this.sell_info         = {};
        this.is_sell_requested = false;

        this.smart_chart.removeBarriers();
        this.smart_chart.removeMarkers();
        this.smart_chart.setContractMode(false);
    }

    @action.bound
    updateProposal(response) {
        if ('error' in response) {
            this.has_error     = true;
            this.error_message = response.error.message;
            this.contract_info = {};
            return;
        }
        if (isEmptyObject(response.proposal_open_contract)) {
            this.has_error     = true;
            this.error_message = localize('Contract does not exist or does not belong to this client.');
            this.contract_info = {};
            return;
        }
        this.contract_info = response.proposal_open_contract;
        if (isEnded(this.contract_info)) {
            this.chart_config = getChartConfig(this.contract_info);
        } else {
            delete this.chart_config.end_epoch;
            delete this.chart_config.start_epoch;
        }
        createChartBarrier(this.smart_chart, this.contract_info);
        createChartMarkers(this.smart_chart, this.contract_info, this);
        this.handleDigits();
    }

    @action.bound
    handleDigits() {
        if (isDigitContract(this.contract_info.contract_type)) {
            extendObservable(this.digits_info, getDigitInfo(this.digits_info, this.contract_info));
        }
    }

    @action.bound
    onClickSell() {
        if (this.contract_id && !this.is_sell_requested && isEmptyObject(this.sell_info)) {
            this.is_sell_requested = true;
            WS.sell(this.contract_id, this.contract_info.bid_price).then(this.handleSell);
        }
    }

    @action.bound
    handleSell(response) {
        if (response.error) {
            this.sell_info = {
                error_message: response.error.message,
            };

            this.is_sell_requested = false;
        } else {
            this.forgetProposalOpenContract();
            WS.proposalOpenContract(this.contract_id).then(action((proposal_response) => {
                this.updateProposal(proposal_response);
                this.sell_info = {
                    sell_price    : response.sell.sold_for,
                    transaction_id: response.sell.transaction_id,
                };
            }));
        }
    }

    forgetProposalOpenContract(contract_id) {
        WS.forget('proposal_open_contract', this.updateProposal, { contract_id: +contract_id || this.contract_id });
    }

{'fraction_non_alphanumeric': 0.10944583217688963, 'fraction_numerical': 0.0, 'mean_word_length': 2.1125827814569536, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '5526330', 'n_tokens_mistral': 3176, 'n_tokens_neox': 3094, 'n_words': 460}