lodash#toPairs JavaScript Examples

The following examples show how to use lodash#toPairs. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SelectStablecoin.js    From bonded-stablecoin-ui with MIT License 4 votes vote down vote up
SelectStablecoin = () => {
  const { data, loading, loaded } = useSelector((state) => state.list);
  const activeAddress = useSelector((state) => state.active.address);
  const { recentList } = useSelector((state) => state.settings);
  const dispatch = useDispatch();
  const { t } = useTranslation();
  const optionList = [];

  const dataList = toPairs(data).sort(([_, a_info], [__, b_info]) => (b_info.bonded_state?.reserve || 0) - (a_info.bonded_state?.reserve || 0)).sort(([_, a_info], [__, b_info]) => {
    if ((a_info.fund && b_info.fund) || (!a_info.fund && !b_info.fund)) {
      return 0
    } else if (a_info.fund && (!b_info.fund)) {
      return -1
    } else if (!(a_info.fund) && b_info.fund) {
      return 1
    }
    return 0
  })

  dataList.forEach(([aa, info]) => {
    if (aa === 'PU5YFREC4OBEYADLOHMBEEA4CI2Z5AKA') // broken AA
      return;
    const { asset_2, symbol, params, bonded_state, fund } = info;
    const targetCurrency = getTargetCurrency(params, bonded_state);
    const interest_rate_percent = bonded_state ? Decimal.mul(bonded_state.interest_rate, 100).toNumber() : null;
    if (!recentList.includes(aa)) {
      optionList.push(
        <Select.Option value={aa} key={aa}>
          <CoinIcon width="1em" style={{ marginRight: 10 }} height="1em" type={2} pegged={targetCurrency} />
          {fund ? "v2" : "v1"} - {targetCurrency}{interest_rate_percent ? ` ${interest_rate_percent}% interest` : ''} : {symbol || asset_2} (
          {aa})
        </Select.Option>
      );
    }
  })

  const optionListRecent = loaded && recentList.filter(aa => data[aa]).map((aa) => {
    const { asset_2, symbol, params, bonded_state, fund } = data[aa];
    const targetCurrency = getTargetCurrency(params, bonded_state);
    const interest_rate_percent = bonded_state ? Decimal.mul(bonded_state.interest_rate, 100).toNumber() : null;
    return (
      <Select.Option value={aa} key={aa}>
        <CoinIcon width="1em" height="1em" style={{ marginRight: 10 }} type={2} pegged={targetCurrency} />
        {fund ? "v2" : "v1"} - {targetCurrency}{interest_rate_percent ? ` ${interest_rate_percent}% interest` : ''} : {symbol || asset_2} (
        {aa})
      </Select.Option>
    );
  });

  return (
    <div
      style={{
        background: "#fff",
        padding: 10,
        marginBottom: 20,
      }}
    >
      <Row>
        <Select
          size="large"
          optionFilterProp="children"
          placeholder={t("select_stablecoin.placeholder", "Please select a stablecoin")}
          style={{ width: "100%" }}
          showSearch={true}
          value={activeAddress || undefined}
          loading={loading}
          onChange={(address) => {
            dispatch(changeActive(address));
          }}
        >
          {optionListRecent.length > 0 && (
            <OptGroup label={t("select_stablecoin.recent", "Recent")}>{optionListRecent}</OptGroup>
          )}
          {optionList.length > 0 && (
            <OptGroup label={optionListRecent.length > 0 ? t("select_stablecoin.other", "Other") : t("select_stablecoin.all", "All")}>
              {optionList}
            </OptGroup>
          )}
        </Select>
      </Row>
    </div>
  );
}