PageTimeline.jsx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import { withTranslation } from 'react-i18next';
  4. import AppContainer from '../services/AppContainer';
  5. import PageContainer from '../services/PageContainer';
  6. import PaginationWrapper from './PaginationWrapper';
  7. import { withUnstatedContainers } from './UnstatedUtils';
  8. import RevisionLoader from './Page/RevisionLoader';
  9. class PageTimeline extends React.Component {
  10. constructor(props) {
  11. super(props);
  12. const { appContainer } = this.props;
  13. this.showPages = this.showPages.bind(this);
  14. this.handlePage = this.handlePage.bind(this);
  15. this.state = {
  16. activePage: 1,
  17. totalPages: 0,
  18. limit: appContainer.getConfig().recentCreatedLimit,
  19. // TODO: remove after when timeline is implemented with React and inject data with props
  20. pages: this.props.pages,
  21. };
  22. }
  23. async handlePage(selectedPage) {
  24. await this.showPages(selectedPage);
  25. }
  26. async showPages(selectedPage) {
  27. const { appContainer, pageContainer } = this.props;
  28. const { path } = pageContainer.state;
  29. const limit = this.state.limit;
  30. const offset = (selectedPage - 1) * limit;
  31. const res = await appContainer.apiv3Get('/pages/list', { path, limit, offset });
  32. const activePage = selectedPage;
  33. const totalPages = res.data.totalCount;
  34. const pages = res.data.pages;
  35. this.setState({
  36. activePage,
  37. totalPages,
  38. pages,
  39. });
  40. }
  41. componentWillMount() {
  42. const { appContainer } = this.props;
  43. // initialize GrowiRenderer
  44. this.growiRenderer = appContainer.getRenderer('timeline');
  45. this.showPages(1);
  46. }
  47. render() {
  48. const { pages } = this.state;
  49. if (pages == null) {
  50. return <React.Fragment></React.Fragment>;
  51. }
  52. return (
  53. <div>
  54. { pages.map((page) => {
  55. return (
  56. <div className="timeline-body" key={`key-${page.id}`}>
  57. <div className="card card-timeline">
  58. <div className="card-header"><a href={page.path}>{page.path}</a></div>
  59. <div className="card-body">
  60. <RevisionLoader
  61. lazy
  62. growiRenderer={this.growiRenderer}
  63. pageId={page.id}
  64. revisionId={page.revision}
  65. />
  66. </div>
  67. </div>
  68. </div>
  69. );
  70. }) }
  71. <PaginationWrapper
  72. activePage={this.state.activePage}
  73. changePage={this.handlePage}
  74. totalItemsCount={this.state.totalPages}
  75. pagingLimit={this.state.limit}
  76. />
  77. </div>
  78. );
  79. }
  80. }
  81. PageTimeline.propTypes = {
  82. t: PropTypes.func.isRequired, // i18next
  83. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  84. pageContainer: PropTypes.instanceOf(PageContainer).isRequired,
  85. pages: PropTypes.arrayOf(PropTypes.object),
  86. };
  87. /**
  88. * Wrapper component for using unstated
  89. */
  90. const PageTimelineWrapper = withUnstatedContainers(PageTimeline, [AppContainer, PageContainer]);
  91. export default withTranslation()(PageTimelineWrapper);