1 | import React, {Component, PureComponent} from 'react';
|
2 | import PropTypes from 'prop-types';
|
3 | import classNames from 'classnames';
|
4 |
|
5 | import Avatar, {Size} from '../avatar/avatar';
|
6 | import Button from '../button/button';
|
7 | import Dropdown from '../dropdown/dropdown';
|
8 | import PopupMenu from '../popup-menu/popup-menu';
|
9 |
|
10 | import styles from './header.css';
|
11 |
|
12 | const rgItemType = PopupMenu.ListProps.Type.LINK;
|
13 |
|
14 | export default class Profile extends PureComponent {
|
15 | static Size = Size;
|
16 |
|
17 | static propTypes = {
|
18 | className: PropTypes.string,
|
19 | closeOnSelect: PropTypes.bool,
|
20 | hasUpdates: PropTypes.bool,
|
21 | loading: PropTypes.bool,
|
22 | onLogin: PropTypes.func,
|
23 | onLogout: PropTypes.func,
|
24 | onSwitchUser: PropTypes.func,
|
25 | profileUrl: PropTypes.string,
|
26 | renderPopupItems: PropTypes.func,
|
27 | LinkComponent: PropTypes.oneOfType([
|
28 | PropTypes.instanceOf(Component),
|
29 | PropTypes.func,
|
30 | PropTypes.string
|
31 | ]),
|
32 | translations: PropTypes.shape({
|
33 | profile: PropTypes.string,
|
34 | login: PropTypes.string,
|
35 | logout: PropTypes.string,
|
36 | applyChangedUser: PropTypes.string,
|
37 | switchUser: PropTypes.string
|
38 | }),
|
39 | user: PropTypes.shape({
|
40 | guest: PropTypes.bool,
|
41 | profile: PropTypes.object
|
42 | }),
|
43 | size: PropTypes.number,
|
44 | round: PropTypes.bool,
|
45 | showLogIn: PropTypes.bool,
|
46 | showLogOut: PropTypes.bool,
|
47 | showSwitchUser: PropTypes.bool,
|
48 | showApplyChangedUser: PropTypes.bool,
|
49 | onRevertPostponement: PropTypes.func,
|
50 | renderGuest: PropTypes.func
|
51 | };
|
52 |
|
53 | static defaultProps = {
|
54 | closeOnSelect: true,
|
55 | renderPopupItems: items => items,
|
56 | translations: {},
|
57 | size: Size.Size32,
|
58 | renderGuest: ({loading, onLogin, className, translations}) => (
|
59 | <div
|
60 | className={classNames(styles.profileEmpty, className)}
|
61 | >
|
62 | <Button
|
63 | theme={Button.Theme.DARK}
|
64 | primary
|
65 | data-test="ring-header-login-button"
|
66 | disabled={loading}
|
67 | loader={loading}
|
68 | onClick={onLogin}
|
69 | >
|
70 | {translations.login || 'Log in...'}
|
71 | </Button>
|
72 | </div>
|
73 | )
|
74 | };
|
75 |
|
76 | render() {
|
77 | const {
|
78 | className,
|
79 | closeOnSelect,
|
80 | hasUpdates,
|
81 | onLogout,
|
82 | user,
|
83 | profileUrl,
|
84 | LinkComponent,
|
85 | onSwitchUser,
|
86 | renderPopupItems,
|
87 | onRevertPostponement,
|
88 | showApplyChangedUser,
|
89 | showLogIn,
|
90 | showLogOut,
|
91 | showSwitchUser,
|
92 | renderGuest,
|
93 | translations,
|
94 | size,
|
95 | round,
|
96 |
|
97 | loading, onLogin,
|
98 | ...props
|
99 | } = this.props;
|
100 |
|
101 | if (!user) {
|
102 | return (
|
103 | <div
|
104 | {...props}
|
105 | className={classNames(styles.profileEmpty, className)}
|
106 | >
|
107 | <Avatar size={size} round={round}/>
|
108 | </div>
|
109 | );
|
110 | }
|
111 |
|
112 | if (user.guest) {
|
113 | return renderGuest(this.props);
|
114 | }
|
115 |
|
116 | const anchorClassName = classNames(styles.avatarWrapper, {
|
117 | [styles.hasUpdates]: hasUpdates
|
118 | });
|
119 |
|
120 | const anchor = (
|
121 | <div className={anchorClassName}>
|
122 | <Avatar
|
123 | url={user.profile && user.profile.avatar && user.profile.avatar.url}
|
124 | size={size}
|
125 | round={round}
|
126 | />
|
127 | </div>
|
128 | );
|
129 |
|
130 | const items = [
|
131 | showApplyChangedUser && {
|
132 | rgItemType,
|
133 | label: translations.applyChangedUser || 'Apply changeduser',
|
134 | className: styles.profileMenuItem,
|
135 | onClick: onRevertPostponement
|
136 | },
|
137 | showLogIn && {
|
138 | rgItemType,
|
139 | label: translations.login || 'Log in',
|
140 | className: styles.profileMenuItem,
|
141 | onClick: onRevertPostponement
|
142 | },
|
143 | {
|
144 | rgItemType: PopupMenu.ListProps.Type.LINK,
|
145 | label: translations.profile || 'Profile',
|
146 |
|
147 | target: '_self',
|
148 | href: profileUrl,
|
149 | LinkComponent
|
150 | },
|
151 | showSwitchUser && {
|
152 | rgItemType,
|
153 | label: translations.switchUser || 'Switch user',
|
154 | className: styles.profileMenuItem,
|
155 | onClick: onSwitchUser
|
156 | },
|
157 | showLogOut && {
|
158 | rgItemType,
|
159 | label: translations.logout || 'Log out',
|
160 |
|
161 | onClick: onLogout
|
162 | }
|
163 | ].filter(it => !!it);
|
164 |
|
165 | return (
|
166 | <Dropdown
|
167 | {...props}
|
168 | title={user.name}
|
169 | anchor={anchor}
|
170 | data-test="ring-profile"
|
171 | className={classNames(styles.profile, className)}
|
172 | >
|
173 | <PopupMenu
|
174 | closeOnSelect={closeOnSelect}
|
175 | data={renderPopupItems(items)}
|
176 | left={-2}
|
177 | top={-8}
|
178 | sidePadding={32}
|
179 | />
|
180 | </Dropdown>
|
181 | );
|
182 | }
|
183 | }
|