<commit_before>import React, {Component} from "react";
import assetUtils from "common/asset_utils";
import AssetActions from "actions/AssetActions";
import MarketsActions from "actions/MarketsActions";
import counterpart from "counterpart";
import PredictionMarketsOverviewTable from "./PredictionMarketsOverviewTable";
import PredictionMarketDetailsTable from "./PredictionMarketDetailsTable";
import SearchInput from "../Utility/SearchInput";
import HelpContent from "../Utility/HelpContent";
import AddOpinionModal from "./AddOpinionModal";
import CreateMarketModal from "./CreateMarketModal";
import ResolveModal from "./ResolveModal";
import {ChainStore} from "bitsharesjs";
import {Button} from "bitshares-ui-style-guide";
import {Asset, Price} from "../../lib/common/MarketClasses";

export default class PredictionMarkets extends Component {
    constructor(props) {
        super(props);
        this.state = {
            assets: [],
            lastAssetSymbol: null,
            predictionMarkets: [],
            currentAccountId: null,
            searchTerm: "",
            detailsSearchTerm: "",
            selectedMarket: null,
            opinions: [],
            preselectedOpinion: "no",
            preselectedAmount: 0,
            isCreateMarketModalOpen: false,
            isAddOpinionModalOpen: false,
            isResolveModalOpen: false,
            symbols: []
        };

        this.onCreatePredictionMarketModalOpen = this.onCreatePredictionMarketModalOpen.bind(
            this
        );
        this.onCreatePredictionMarketModalClose = this.onCreatePredictionMarketModalClose.bind(
            this
        );
        this.onAddOpinionModalOpen = this.onAddOpinionModalOpen.bind(this);
        this.onAddOpinionModalClose = this.onAddOpinionModalClose.bind(this);
        this.onSearch = this.onSearch.bind(this);
        this.onSearchDetails = this.onSearchDetails.bind(this);
        this.onMarketAction = this.onMarketAction.bind(this);
        this.onResolveModalOpen = this.onResolveModalOpen.bind(this);
        this.onResolveModalClose = this.onResolveModalClose.bind(this);
    }

    componentWillMount() {
        this._checkAssets(this.props.assets);
    }

    componentWillReceiveProps(np) {
        if (np.currentAccount !== this.props.currentAccount) {
            this.setState({
                currentAccountId: ChainStore.getAccount(np.currentAccount).get(
                    "id"
                )
            });
        }

        if (np.assets !== this.props.assets) {
            this._checkAssets(np.assets);
            this._updateSymbolsList();
        }

        if (np.marketLimitOrders !== this.props.marketLimitOrders) {
            this._updateOpinionsList(np.marketLimitOrders);
        }
    }

    _checkAssets(fetchedAssets) {
        let searchAsset = this.state.lastAssetSymbol
            ? this.state.lastAssetSymbol
            : "A";
        if (fetchedAssets) {
            const lastAsset = fetchedAssets
                .sort((a, b) => {
                    if (a.symbol > b.symbol) {
                        return 1;
                    } else if (a.symbol < b.symbol) {
                        return -1;
                    } else {
                        return 0;
                    }
                })
                .last();
            searchAsset = lastAsset ? lastAsset.symbol : "A";
            const assets = fetchedAssets.filter(
                a => a.bitasset_data && a.bitasset_data.is_prediction_market
            );
            this.setState({
                assets: [...assets]
            });
            this._updatePredictionMarketsList();
        }
        if (
            !this.state.lastAssetSymbol ||
            this.state.lastAssetSymbol !== searchAsset
        ) {
            AssetActions.getAssetList.defer(searchAsset, 100);
            this.setState({
                lastAssetSymbol: searchAsset
            });
        }
    }

    _updatePredictionMarketsList() {
        const predictionMarkets = this.state.assets.map(item => ({
            asset_id: item[1].id,
            issuer: item[1].issuer,
            description: assetUtils.parseDescription(
                item[1].options.description
            ).main,
            symbol: item[1].symbol,
            condition: assetUtils.parseDescription(item[1].options.description)
                .condition,
            options: item[1].options
        }));
        this.setState({
            predictionMarkets
        });
    }

    _updateOpinionsList(fetchedOpinions) {
        let orders = [];
        fetchedOpinions.forEach((order, order_id) => {
            const opinion =
                order.market_base === order.sell_price.base.asset_id
                    ? "yes"
                    : "no";
            orders.push({
                order_id,
                opinionator: order.seller,
                opinion,
                amount: order.for_sale,
                fee: order.fee
            });
        });
        this.setState({opinions: [...orders]});
    }

    _updateSymbolsList() {
        let assets = this.props.assets.toJS();
        let symbols = [];
        for (let item in assets) {
            symbols.push(assets[item].symbol);
        }
        this.setState({symbols});
    }

    async getMarketOpinions(market) {
        console.log("Fetching opinions");
        if (this.state.subscribedMarket) {
            console.log("Unsubscribe");
            await MarketsActions.unSubscribeMarket(
                this.state.subscribedMarket.base,
                this.state.subscribedMarket.quote
            );
        }
        const base = ChainStore.getAsset(
            market.options.core_exchange_rate.base.asset_id
        );
        const quote = ChainStore.getAsset(
            market.options.core_exchange_rate.quote.asset_id
        );
        console.log("Subscribe");
        await MarketsActions.subscribeMarket(
            base,
            quote,
            this.props.bucketSize,
            this.props.currentGroupOrderLimit
        );
        this.setState({
            subscribedMarket: {
                base,
                quote
            }
        });
    }

    onMarketAction({market, action}) {
        switch (action) {
            case "resolve": {
                this.setState({
                    selectedPredictionMarket: market,
                    preselectedAmount: 0
                });<commit_msg>Fixed the bug with opinions fetching<commit_after>import React, {Component} from "react";
import assetUtils from "common/asset_utils";
import AssetActions from "actions/AssetActions";
import MarketsActions from "actions/MarketsActions";
import counterpart from "counterpart";
import PredictionMarketsOverviewTable from "./PredictionMarketsOverviewTable";
import PredictionMarketDetailsTable from "./PredictionMarketDetailsTable";
import SearchInput from "../Utility/SearchInput";
import HelpContent from "../Utility/HelpContent";
import AddOpinionModal from "./AddOpinionModal";
import CreateMarketModal from "./CreateMarketModal";
import ResolveModal from "./ResolveModal";
import {ChainStore} from "bitsharesjs";
import {Button} from "bitshares-ui-style-guide";
import {Asset, Price} from "../../lib/common/MarketClasses";

export default class PredictionMarkets extends Component {
    constructor(props) {
        super(props);
        this.state = {
            assets: [],
            lastAssetSymbol: null,
            predictionMarkets: [],
            currentAccountId: null,
            searchTerm: "",
            detailsSearchTerm: "",
            selectedMarket: null,
            opinions: [],
            preselectedOpinion: "no",
            preselectedAmount: 0,
            isCreateMarketModalOpen: false,
            isAddOpinionModalOpen: false,
            isResolveModalOpen: false,
            symbols: []
        };

        this.onCreatePredictionMarketModalOpen = this.onCreatePredictionMarketModalOpen.bind(
            this
        );
        this.onCreatePredictionMarketModalClose = this.onCreatePredictionMarketModalClose.bind(
            this
        );
        this.onAddOpinionModalOpen = this.onAddOpinionModalOpen.bind(this);
        this.onAddOpinionModalClose = this.onAddOpinionModalClose.bind(this);
        this.onSearch = this.onSearch.bind(this);
        this.onSearchDetails = this.onSearchDetails.bind(this);
        this.onMarketAction = this.onMarketAction.bind(this);
        this.onResolveModalOpen = this.onResolveModalOpen.bind(this);
        this.onResolveModalClose = this.onResolveModalClose.bind(this);
    }

    componentWillMount() {
        this._checkAssets(this.props.assets);
    }

    componentWillReceiveProps(np) {
        if (np.currentAccount !== this.props.currentAccount) {
            this.setState({
                currentAccountId: ChainStore.getAccount(np.currentAccount).get(
                    "id"
                )
            });
        }

        if (np.assets !== this.props.assets) {
            this._checkAssets(np.assets);
            this._updateSymbolsList();
        }

        if (np.marketLimitOrders !== this.props.marketLimitOrders) {
            this._updateOpinionsList(np.marketLimitOrders);
        }
    }

    _checkAssets(fetchedAssets) {
        let searchAsset = this.state.lastAssetSymbol
            ? this.state.lastAssetSymbol
            : "A";
        if (fetchedAssets) {
            const lastAsset = fetchedAssets
                .sort((a, b) => {
                    if (a.symbol > b.symbol) {
                        return 1;
                    } else if (a.symbol < b.symbol) {
                        return -1;
                    } else {
                        return 0;
                    }
                })
                .last();
            searchAsset = lastAsset ? lastAsset.symbol : "A";
            const assets = fetchedAssets.filter(
                a => a.bitasset_data && a.bitasset_data.is_prediction_market
            );
            this.setState({
                assets: [...assets]
            });
            this._updatePredictionMarketsList();
        }
        if (
            !this.state.lastAssetSymbol ||
            this.state.lastAssetSymbol !== searchAsset
        ) {
            AssetActions.getAssetList.defer(searchAsset, 100);
            this.setState({
                lastAssetSymbol: searchAsset
            });
        }
    }

    _updatePredictionMarketsList() {
        const predictionMarkets = this.state.assets.map(item => ({
            asset_id: item[1].id,
            issuer: item[1].issuer,
            description: assetUtils.parseDescription(
                item[1].options.description
            ).main,
            symbol: item[1].symbol,
            condition: assetUtils.parseDescription(item[1].options.description)
                .condition,
            options: item[1].options
        }));
        this.setState({
            predictionMarkets
        });
    }

    _updateOpinionsList(fetchedOpinions) {
        let orders = [];
        fetchedOpinions.forEach((order, order_id) => {
            const opinion =
                order.market_base === order.sell_price.base.asset_id
                    ? "yes"
                    : "no";
            orders.push({
                order_id,
                opinionator: order.seller,
                opinion,
                amount: order.for_sale,
                fee: order.fee
            });
        });
        this.setState({opinions: [...orders]});
    }

    _updateSymbolsList() {
        let assets = this.props.assets.toJS();
        let symbols = [];
        for (let item in assets) {
            symbols.push(assets[item].symbol);
        }
        this.setState({symbols});
    }

    async getMarketOpinions(market) {
        if (this.state.subscribedMarket) {
            await MarketsActions.unSubscribeMarket(
                this.state.subscribedMarket.quote.get("id"),
                this.state.subscribedMarket.base.get("id")
            );
        }
        const base = ChainStore.getAsset(
            market.options.core_exchange_rate.base.asset_id
        );
        const quote = ChainStore.getAsset(
            market.options.core_exchange_rate.quote.asset_id
        );
        await MarketsActions.subscribeMarket(
            base,
            quote,
            this.props.bucketSize,
            this.props.currentGroupOrderLimit
        );
        this.setState({
            subscribedMarket: {
                base,
                quote
            }
        });
    }

    onMarketAction({market, action}) {
        switch (action) {
            case "resolve": {
                this.setState({
                    selectedPredictionMarket: market,
                    preselectedAmount: 0
                });
{'fraction_non_alphanumeric': 0.10080112001244458, 'fraction_numerical': 0.0021778019755775064, 'mean_word_length': 1.7132306393753955, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 13, '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': '1884863', 'n_tokens_mistral': 3426, 'n_tokens_neox': 3278, 'n_words': 561}