|
1 | 1 | import PropTypes from 'prop-types'; |
2 | 2 | import React from 'react'; |
3 | | -import { connect, useSelector } from 'react-redux'; |
4 | | -import { bindActionCreators } from 'redux'; |
| 3 | +import { useDispatch, useSelector } from 'react-redux'; |
5 | 4 | import { Tab, Tabs, TabList, TabPanel } from 'react-tabs'; |
6 | 5 | import { Helmet } from 'react-helmet'; |
7 | | -import { useTranslation, withTranslation } from 'react-i18next'; |
| 6 | +import { useTranslation } from 'react-i18next'; |
8 | 7 | import { withRouter, browserHistory } from 'react-router'; |
9 | 8 | import { parse } from 'query-string'; |
10 | 9 | import { createApiKey, removeApiKey } from '../actions'; |
@@ -45,107 +44,85 @@ function SocialLoginPanel() { |
45 | 44 | ); |
46 | 45 | } |
47 | 46 |
|
48 | | -class AccountView extends React.Component { |
49 | | - componentDidMount() { |
50 | | - document.body.className = this.props.theme; |
51 | | - } |
| 47 | +function AccountView({ location }) { |
| 48 | + const { t } = useTranslation(); |
52 | 49 |
|
53 | | - render() { |
54 | | - const queryParams = parse(this.props.location.search); |
55 | | - const showError = !!queryParams.error; |
56 | | - const errorType = queryParams.error; |
57 | | - const accessTokensUIEnabled = window.process.env.UI_ACCESS_TOKEN_ENABLED; |
| 50 | + const queryParams = parse(location.search); |
| 51 | + const showError = !!queryParams.error; |
| 52 | + const errorType = queryParams.error; |
| 53 | + const accessTokensUIEnabled = window.process.env.UI_ACCESS_TOKEN_ENABLED; |
58 | 54 |
|
59 | | - return ( |
60 | | - <div className="account-settings__container"> |
61 | | - <Helmet> |
62 | | - <title>{this.props.t('AccountView.Title')}</title> |
63 | | - </Helmet> |
64 | | - <Toast /> |
| 55 | + const apiKeys = useSelector((state) => state.user.apiKeys); |
| 56 | + const dispatch = useDispatch(); |
65 | 57 |
|
66 | | - <Nav layout="dashboard" /> |
| 58 | + return ( |
| 59 | + <div className="account-settings__container"> |
| 60 | + <Helmet> |
| 61 | + <title>{t('AccountView.Title')}</title> |
| 62 | + </Helmet> |
| 63 | + <Toast /> |
67 | 64 |
|
68 | | - {showError && ( |
69 | | - <Overlay |
70 | | - title={this.props.t('ErrorModal.LinkTitle')} |
71 | | - ariaLabel={this.props.t('ErrorModal.LinkTitle')} |
72 | | - closeOverlay={() => { |
73 | | - browserHistory.push(this.props.location.pathname); |
74 | | - }} |
75 | | - > |
76 | | - <ErrorModal type="oauthError" service={errorType} /> |
77 | | - </Overlay> |
78 | | - )} |
| 65 | + <Nav layout="dashboard" /> |
79 | 66 |
|
80 | | - <main className="account-settings"> |
81 | | - <header className="account-settings__header"> |
82 | | - <h1 className="account-settings__title"> |
83 | | - {this.props.t('AccountView.Settings')} |
84 | | - </h1> |
85 | | - </header> |
86 | | - {accessTokensUIEnabled && ( |
87 | | - <Tabs className="account__tabs"> |
88 | | - <TabList> |
89 | | - <div className="tabs__titles"> |
| 67 | + {showError && ( |
| 68 | + <Overlay |
| 69 | + title={t('ErrorModal.LinkTitle')} |
| 70 | + ariaLabel={t('ErrorModal.LinkTitle')} |
| 71 | + closeOverlay={() => { |
| 72 | + browserHistory.push(location.pathname); |
| 73 | + }} |
| 74 | + > |
| 75 | + <ErrorModal type="oauthError" service={errorType} /> |
| 76 | + </Overlay> |
| 77 | + )} |
| 78 | + |
| 79 | + <main className="account-settings"> |
| 80 | + <header className="account-settings__header"> |
| 81 | + <h1 className="account-settings__title"> |
| 82 | + {t('AccountView.Settings')} |
| 83 | + </h1> |
| 84 | + </header> |
| 85 | + {accessTokensUIEnabled && ( |
| 86 | + <Tabs className="account__tabs"> |
| 87 | + <TabList> |
| 88 | + <div className="tabs__titles"> |
| 89 | + <Tab> |
| 90 | + <h4 className="tabs__title">{t('AccountView.AccountTab')}</h4> |
| 91 | + </Tab> |
| 92 | + {accessTokensUIEnabled && ( |
90 | 93 | <Tab> |
91 | 94 | <h4 className="tabs__title"> |
92 | | - {this.props.t('AccountView.AccountTab')} |
| 95 | + {t('AccountView.AccessTokensTab')} |
93 | 96 | </h4> |
94 | 97 | </Tab> |
95 | | - {accessTokensUIEnabled && ( |
96 | | - <Tab> |
97 | | - <h4 className="tabs__title"> |
98 | | - {this.props.t('AccountView.AccessTokensTab')} |
99 | | - </h4> |
100 | | - </Tab> |
101 | | - )} |
102 | | - </div> |
103 | | - </TabList> |
104 | | - <TabPanel> |
105 | | - <SocialLoginPanel /> |
106 | | - </TabPanel> |
107 | | - <TabPanel> |
108 | | - <APIKeyForm {...this.props} /> |
109 | | - </TabPanel> |
110 | | - </Tabs> |
111 | | - )} |
112 | | - {!accessTokensUIEnabled && <SocialLoginPanel />} |
113 | | - </main> |
114 | | - </div> |
115 | | - ); |
116 | | - } |
117 | | -} |
118 | | - |
119 | | -function mapStateToProps(state) { |
120 | | - return { |
121 | | - initialValues: state.user, // <- initialValues for reduxForm |
122 | | - previousPath: state.ide.previousPath, |
123 | | - user: state.user, |
124 | | - apiKeys: state.user.apiKeys, |
125 | | - theme: state.preferences.theme |
126 | | - }; |
127 | | -} |
128 | | - |
129 | | -function mapDispatchToProps(dispatch) { |
130 | | - return bindActionCreators( |
131 | | - { |
132 | | - createApiKey, |
133 | | - removeApiKey |
134 | | - }, |
135 | | - dispatch |
| 98 | + )} |
| 99 | + </div> |
| 100 | + </TabList> |
| 101 | + <TabPanel> |
| 102 | + <SocialLoginPanel /> |
| 103 | + </TabPanel> |
| 104 | + <TabPanel> |
| 105 | + <APIKeyForm |
| 106 | + // TODO: it makes more sense to connect the APIKeyForm component directly -Linda |
| 107 | + apiKeys={apiKeys} |
| 108 | + createApiKey={() => dispatch(createApiKey)} |
| 109 | + removeApiKey={() => dispatch(removeApiKey)} |
| 110 | + t={t} |
| 111 | + /> |
| 112 | + </TabPanel> |
| 113 | + </Tabs> |
| 114 | + )} |
| 115 | + {!accessTokensUIEnabled && <SocialLoginPanel />} |
| 116 | + </main> |
| 117 | + </div> |
136 | 118 | ); |
137 | 119 | } |
138 | 120 |
|
139 | 121 | AccountView.propTypes = { |
140 | | - previousPath: PropTypes.string.isRequired, |
141 | | - theme: PropTypes.string.isRequired, |
142 | | - t: PropTypes.func.isRequired, |
143 | 122 | location: PropTypes.shape({ |
144 | 123 | search: PropTypes.string.isRequired, |
145 | 124 | pathname: PropTypes.string.isRequired |
146 | 125 | }).isRequired |
147 | 126 | }; |
148 | 127 |
|
149 | | -export default withTranslation()( |
150 | | - withRouter(connect(mapStateToProps, mapDispatchToProps)(AccountView)) |
151 | | -); |
| 128 | +export default withRouter(AccountView); |
0 commit comments