You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

WelcomePageLists.js 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // @flow
  2. import React, { Component } from 'react';
  3. import { connect } from 'react-redux';
  4. import { translate } from '../../base/i18n';
  5. import { PagedList } from '../../base/react';
  6. import { CalendarList } from '../../calendar-sync';
  7. import { RecentList } from '../../recent-list';
  8. import { setWelcomePageListsDefaultPage } from '../actions';
  9. /**
  10. * The type of the React {@code Component} props of {@link WelcomePageLists}.
  11. */
  12. type Props = {
  13. /**
  14. * The stored default page index.
  15. */
  16. _defaultPage: number,
  17. /**
  18. * Renders the lists disabled.
  19. */
  20. disabled: boolean,
  21. /**
  22. * The Redux dispatch function.
  23. */
  24. dispatch: Function,
  25. /**
  26. * The i18n translate function.
  27. */
  28. t: Function
  29. };
  30. /**
  31. * Implements the lists displayed on the mobile welcome screen.
  32. */
  33. class WelcomePageLists extends Component<Props> {
  34. /**
  35. * The pages to be rendered.
  36. *
  37. * Note: An element's {@code component} may be {@code undefined} if a
  38. * feature (such as Calendar) is disabled, and that means that the page must
  39. * not be rendered.
  40. */
  41. pages: Array<{
  42. component: ?Object,
  43. icon: string | number,
  44. title: string
  45. }>;
  46. /**
  47. * Initializes a new {@code WelcomePageLists} instance.
  48. *
  49. * @inheritdoc
  50. */
  51. constructor(props) {
  52. super(props);
  53. const { t } = props;
  54. this.pages = [
  55. {
  56. component: RecentList,
  57. icon: 'restore',
  58. title: t('welcomepage.recentList')
  59. },
  60. {
  61. component: CalendarList,
  62. icon: 'event_note',
  63. title: t('welcomepage.calendar')
  64. }
  65. ];
  66. // Bind event handlers so they are only bound once per instance.
  67. this._onSelectPage = this._onSelectPage.bind(this);
  68. }
  69. /**
  70. * Implements React's {@link Component#render}.
  71. *
  72. * @inheritdoc
  73. */
  74. render() {
  75. const { _defaultPage } = this.props;
  76. if (typeof _defaultPage === 'undefined') {
  77. return null;
  78. }
  79. return (
  80. <PagedList
  81. defaultPage = { _defaultPage }
  82. disabled = { this.props.disabled }
  83. onSelectPage = { this._onSelectPage }
  84. pages = { this.pages } />
  85. );
  86. }
  87. _onSelectPage: number => void;
  88. /**
  89. * Callback for the {@code PagedList} page select action.
  90. *
  91. * @private
  92. * @param {number} pageIndex - The index of the selected page.
  93. * @returns {void}
  94. */
  95. _onSelectPage(pageIndex) {
  96. this.props.dispatch(setWelcomePageListsDefaultPage(pageIndex));
  97. }
  98. }
  99. /**
  100. * Maps (parts of) the redux state to the React {@code Component} props of
  101. * {@code WelcomePageLists}.
  102. *
  103. * @param {Object} state - The redux state.
  104. * @protected
  105. * @returns {{
  106. * _defaultPage: number
  107. * }}
  108. */
  109. function _mapStateToProps(state: Object) {
  110. let { defaultPage } = state['features/welcome'];
  111. if (typeof defaultPage === 'undefined') {
  112. const recentList = state['features/recent-list'];
  113. defaultPage = recentList && recentList.length ? 0 : 1;
  114. }
  115. return {
  116. _defaultPage: defaultPage
  117. };
  118. }
  119. export default translate(connect(_mapStateToProps)(WelcomePageLists));