2
0

RecentCreated.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import Pagination from 'react-bootstrap/lib/Pagination';
  4. export default class RecentCreated extends React.Component {
  5. constructor(props) {
  6. super(props);
  7. this.state = {
  8. RecentCreatedData : '',
  9. pages : [] ,
  10. limit : 10, // TODO GC-941で対応予定
  11. active : 1,
  12. totalPage: 0,
  13. };
  14. this.getRecentCreatedList = this.getRecentCreatedList.bind(this);
  15. }
  16. componentWillMount() {
  17. this.getRecentCreatedList(1);
  18. console.log(this.state);
  19. }
  20. getRecentCreatedList(selectPageNumber) {
  21. const pageId = this.props.pageId;
  22. const userId = this.props.crowi.me;
  23. const limit = this.state.limit;
  24. const offset = (selectPageNumber - 1) * limit;
  25. this.props.crowi.apiGet('/pages.recentCreated', {page_id: pageId , user: userId , limit: limit , offset: offset , })
  26. .then(res => {
  27. console.log("res.pages=", res.pages);
  28. const totalCount = res.pages[0].totalCount;
  29. const totalPage = totalCount % limit == 0 ? totalCount / limit : Math.floor(totalCount / limit) + 1;
  30. const pages = res.pages[1];
  31. let inUse = {};
  32. const active = selectPageNumber;
  33. this.setState({
  34. pages: pages,
  35. inUse: inUse,
  36. active: active,
  37. totalPage: totalPage,
  38. });
  39. });
  40. }
  41. render() {
  42. let totalPage = this.state.totalPage;
  43. let active = this.state.active;
  44. let items = [];
  45. let paginationStart = active;
  46. console.log(this.state);
  47. if (1 === active) {
  48. items.push(
  49. <Pagination.First disabled />
  50. );
  51. items.push(
  52. <Pagination.Prev disabled />
  53. );
  54. }
  55. else {
  56. items.push(
  57. <Pagination.First onClick={() => this.getRecentCreatedList(1)} />
  58. );
  59. items.push(
  60. <Pagination.Prev onClick={() => this.getRecentCreatedList(this.state.active - 1)} />
  61. );
  62. }
  63. if (0 < (active - 2) && active < (totalPage - 2 )) {
  64. paginationStart = active - 2;
  65. }else if( active < 3){
  66. paginationStart = 1;
  67. }else if( active >= (totalPage -2) ){
  68. paginationStart = (totalPage > 5)? totalPage -4 : totalPage = 4 ? totalPage - 3 : totalPage -2 ;
  69. }
  70. let MaxViewNum = (paginationStart ) + 4;
  71. if(totalPage < paginationStart + 4){
  72. MaxViewNum = totalPage;
  73. }
  74. for (let number = paginationStart; number <= MaxViewNum; number++) { // TODO GC-992で対応予定
  75. items.push(
  76. <Pagination.Item key={number} active={number === active} onClick={ () => this.getRecentCreatedList(number)}>{number}</Pagination.Item>
  77. );
  78. }
  79. if (totalPage === active) {
  80. items.push(
  81. <Pagination.Next disabled />
  82. );
  83. items.push(
  84. <Pagination.Last disabled />
  85. );
  86. }
  87. else {
  88. items.push(
  89. <Pagination.Next onClick={() => this.getRecentCreatedList(this.state.active + 1)} />
  90. );
  91. items.push(
  92. <Pagination.Last onClick={() => this.getRecentCreatedList(totalPage)} />
  93. );
  94. }
  95. // TODO same key Warning
  96. return (
  97. <div className="page-list-container-create">
  98. <ul className="page-list-ul page-list-ul-flat">
  99. {!this.state.pages.lenth != 0 &&
  100. this.state.pages.map((page, i) => {
  101. return <li key="{page.id}">
  102. <img src="/images/icons/user.svg" className="picture img-circle" ></img>
  103. <a href="{page.path}" className="page-list-link" data-path="{page.path}" data-short-path="{page.revision.body}">{decodeURI(page.path)}</a>
  104. </li> ;
  105. })
  106. }
  107. </ul>
  108. <Pagination bsSize="small">{items}</Pagination>
  109. </div>
  110. );
  111. }
  112. }
  113. RecentCreated.propTypes = {
  114. pageId: PropTypes.string.isRequired,
  115. };
  116. RecentCreated.defaultProps = {
  117. };