index.tsx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import React, { Component } from 'react';
  2. import type { Dispatch } from 'umi';
  3. import { FormattedMessage, connect } from 'umi';
  4. import { GridContent } from '@ant-design/pro-layout';
  5. import { Menu } from 'antd';
  6. import BaseView from './components/base';
  7. import BindingView from './components/binding';
  8. import type { CurrentUser } from './data.d';
  9. import NotificationView from './components/notification';
  10. import SecurityView from './components/security';
  11. import styles from './style.less';
  12. const { Item } = Menu;
  13. interface SettingsProps {
  14. dispatch: Dispatch;
  15. currentUser: CurrentUser;
  16. }
  17. type SettingsStateKeys = 'base' | 'security' | 'binding' | 'notification';
  18. interface SettingsState {
  19. mode: 'inline' | 'horizontal';
  20. menuMap: Record<string, React.ReactNode>;
  21. selectKey: SettingsStateKeys;
  22. }
  23. class Settings extends Component<SettingsProps, SettingsState> {
  24. main: HTMLDivElement | undefined = undefined;
  25. constructor(props: SettingsProps) {
  26. super(props);
  27. const menuMap = {
  28. base: (
  29. <FormattedMessage id="accountandsettings.menuMap.basic" defaultMessage="Basic Settings" />
  30. ),
  31. security: (
  32. <FormattedMessage
  33. id="accountandsettings.menuMap.security"
  34. defaultMessage="Security Settings"
  35. />
  36. ),
  37. binding: (
  38. <FormattedMessage
  39. id="accountandsettings.menuMap.binding"
  40. defaultMessage="Account Binding"
  41. />
  42. ),
  43. notification: (
  44. <FormattedMessage
  45. id="accountandsettings.menuMap.notification"
  46. defaultMessage="New Message Notification"
  47. />
  48. ),
  49. };
  50. this.state = {
  51. mode: 'inline',
  52. menuMap,
  53. selectKey: 'base',
  54. };
  55. }
  56. componentDidMount() {
  57. const { dispatch } = this.props;
  58. // dispatch({
  59. // type: 'accountAndsettings/fetchCurrent',
  60. // });
  61. window.addEventListener('resize', this.resize);
  62. this.resize();
  63. }
  64. componentWillUnmount() {
  65. window.removeEventListener('resize', this.resize);
  66. }
  67. getMenu = () => {
  68. const { menuMap } = this.state;
  69. return Object.keys(menuMap).map((item) => <Item key={item}>{menuMap[item]}</Item>);
  70. };
  71. getRightTitle = () => {
  72. const { selectKey, menuMap } = this.state;
  73. return menuMap[selectKey];
  74. };
  75. selectKey = (key: SettingsStateKeys) => {
  76. this.setState({
  77. selectKey: key,
  78. });
  79. };
  80. resize = () => {
  81. if (!this.main) {
  82. return;
  83. }
  84. requestAnimationFrame(() => {
  85. if (!this.main) {
  86. return;
  87. }
  88. let mode: 'inline' | 'horizontal' = 'inline';
  89. const { offsetWidth } = this.main;
  90. if (this.main.offsetWidth < 641 && offsetWidth > 400) {
  91. mode = 'horizontal';
  92. }
  93. if (window.innerWidth < 768 && offsetWidth > 400) {
  94. mode = 'horizontal';
  95. }
  96. this.setState({
  97. mode,
  98. });
  99. });
  100. };
  101. renderChildren = () => {
  102. const { selectKey } = this.state;
  103. switch (selectKey) {
  104. case 'base':
  105. return <BaseView />;
  106. case 'security':
  107. return <SecurityView />;
  108. case 'binding':
  109. return <BindingView />;
  110. case 'notification':
  111. return <NotificationView />;
  112. default:
  113. break;
  114. }
  115. return null;
  116. };
  117. render() {
  118. const { currentUser } = this.props;
  119. if (!currentUser.userid) {
  120. return '';
  121. }
  122. const { mode, selectKey } = this.state;
  123. return (
  124. <GridContent>
  125. <div
  126. className={styles.main}
  127. ref={(ref) => {
  128. if (ref) {
  129. this.main = ref;
  130. }
  131. }}
  132. >
  133. <div className={styles.leftMenu}>
  134. <Menu
  135. mode={mode}
  136. selectedKeys={[selectKey]}
  137. onClick={({ key }) => this.selectKey(key as SettingsStateKeys)}
  138. >
  139. {this.getMenu()}
  140. </Menu>
  141. </div>
  142. <div className={styles.right}>
  143. <div className={styles.title}>{this.getRightTitle()}</div>
  144. {this.renderChildren()}
  145. </div>
  146. </div>
  147. </GridContent>
  148. );
  149. }
  150. }
  151. export default connect(
  152. ({ accountAndsettings }: { accountAndsettings: { currentUser: CurrentUser } }) => ({
  153. currentUser: accountAndsettings.currentUser,
  154. }),
  155. )(Settings);