Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

WelcomePageLists.js 3.2KB

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