aboutsummaryrefslogtreecommitdiffstats
path: root/ui/app/pages/settings/contact-list-tab/contact-list-tab.component.js
blob: f7a01d672192b97e7fb883a5922a0d81fa70ae99 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import React, { Component } from 'react'
import PropTypes from 'prop-types'
import ContactList from '../../../components/app/contact-list'
import EditContact from './edit-contact'
import AddContact from './add-contact'
import ViewContact from './view-contact'
import MyAccounts from './my-accounts'
import {
  CONTACT_ADD_ROUTE,
  CONTACT_VIEW_ROUTE,
  CONTACT_MY_ACCOUNTS_ROUTE,
} from '../../../helpers/constants/routes'

export default class ContactListTab extends Component {
  static contextTypes = {
    t: PropTypes.func,
  }

  static propTypes = {
    addressBook: PropTypes.array,
    history: PropTypes.object,
    selectedAddress: PropTypes.string,
    viewingContact: PropTypes.bool,
    editingContact: PropTypes.bool,
    addingContact: PropTypes.bool,
    showContactContent: PropTypes.bool,
    hideAddressBook: PropTypes.bool,
    showingMyAccounts: PropTypes.bool,
  }

  renderAddresses () {
    const { addressBook, history, selectedAddress } = this.props
    const contacts = addressBook.filter(({ name }) => !!name)
    const nonContacts = addressBook.filter(({ name }) => !name)

    return (
      <div>
        <ContactList
          searchForContacts={() => contacts}
          searchForRecents={() => nonContacts}
          selectRecipient={(address) => {
            history.push(`${CONTACT_VIEW_ROUTE}/${address}`)
          }}
          selectedAddress={selectedAddress}
        />
      </div>
    )
  }

  renderAddButton () {
    const { history } = this.props
    return <div
      className="address-book-add-button__button"
      onClick={() => {
        history.push(CONTACT_ADD_ROUTE)
      }}>
      <img
        className="account-menu__item-icon"
        src="images/plus-btn-white.svg"
      />
    </div>
  }

  renderMyAccountsButton () {
    const { history } = this.props
    const { t } = this.context
    return (
      <div
        className="address-book__my-accounts-button"
        onClick={() => {
          history.push(CONTACT_MY_ACCOUNTS_ROUTE)
        }}
      >
        <div className="address-book__my-accounts-button__header">{t('myWalletAccounts')}</div>
        <div className="address-book__my-accounts-button__content">
          <div className="address-book__my-accounts-button__text">
            { t('myWalletAccountsDescription') }
          </div>
          <div className="address-book__my-accounts-button__caret" />
        </div>
      </div>
    )
  }

  renderContactContent () {
    const { viewingContact, editingContact, addingContact, showContactContent } = this.props

    if (!showContactContent) {
      return null
    }

    let ContactContentComponent = null
    if (viewingContact) {
      ContactContentComponent = ViewContact
    } else if (editingContact) {
      ContactContentComponent = EditContact
    } else if (addingContact) {
      ContactContentComponent = AddContact
    }

    return (ContactContentComponent && <div className="address-book-contact-content">
      <ContactContentComponent />
    </div>)
  }

  renderAddressBookContent () {
    const { hideAddressBook, showingMyAccounts } = this.props

    if (!hideAddressBook && !showingMyAccounts) {
      return (<div className="address-book">
        { this.renderMyAccountsButton() }
        { this.renderAddresses() }
      </div>)
    } else if (!hideAddressBook && showingMyAccounts) {
      return (<MyAccounts />)
    }
  }

  render () {
    const { addingContact } = this.props

    return (
      <div className="address-book-wrapper">
        { this.renderAddressBookContent() }
        { this.renderContactContent() }
        {!addingContact && <div className="address-book-add-button">
          { this.renderAddButton() }
        </div>}
      </div>
    )
  }
}