RecentCreated.jsx 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import Pagination from 'react-bootstrap/lib/Pagination';
  4. import { createSubscribedElement } from '../UnstatedUtils';
  5. import AppContainer from '../../services/AppContainer';
  6. import PageContainer from '../../services/PageContainer';
  7. import Page from '../PageList/Page';
  8. class RecentCreated extends React.Component {
  9. constructor(props) {
  10. super(props);
  11. this.state = {
  12. pages: [],
  13. activePage: 1,
  14. paginationNumbers: {},
  15. };
  16. this.calculatePagination = this.calculatePagination.bind(this);
  17. }
  18. componentWillMount() {
  19. this.getRecentCreatedList(1);
  20. }
  21. getRecentCreatedList(selectPageNumber) {
  22. const { appContainer, pageContainer } = this.props;
  23. const { pageId } = pageContainer.state;
  24. const userId = appContainer.me;
  25. const limit = appContainer.getConfig().recentCreatedLimit;
  26. const offset = (selectPageNumber - 1) * limit;
  27. // pagesList get and pagination calculate
  28. this.props.appContainer.apiGet('/pages.recentCreated', {
  29. page_id: pageId, user: userId, limit, offset,
  30. })
  31. .then((res) => {
  32. const totalCount = res.totalCount;
  33. const pages = res.pages;
  34. const activePage = selectPageNumber;
  35. // pagiNation calculate function call
  36. const paginationNumbers = this.calculatePagination(limit, totalCount, activePage);
  37. this.setState({
  38. pages,
  39. activePage,
  40. paginationNumbers,
  41. });
  42. });
  43. }
  44. calculatePagination(limit, totalCount, activePage) {
  45. // calc totalPageNumber
  46. const totalPage = Math.floor(totalCount / limit) + (totalCount % limit === 0 ? 0 : 1);
  47. let paginationStart = activePage - 2;
  48. let maxViewPageNum = activePage + 2;
  49. // pagiNation Number area size = 5 , pageNuber calculate in here
  50. // activePage Position calculate ex. 4 5 [6] 7 8 (Page8 over is Max), 3 4 5 [6] 7 (Page7 is Max)
  51. if (paginationStart < 1) {
  52. const diff = 1 - paginationStart;
  53. paginationStart += diff;
  54. maxViewPageNum = Math.min(totalPage, maxViewPageNum + diff);
  55. }
  56. if (maxViewPageNum > totalPage) {
  57. const diff = maxViewPageNum - totalPage;
  58. maxViewPageNum -= diff;
  59. paginationStart = Math.max(1, paginationStart - diff);
  60. }
  61. return {
  62. totalPage,
  63. paginationStart,
  64. maxViewPageNum,
  65. };
  66. }
  67. /**
  68. * generate Elements of Page
  69. *
  70. * @param {any} pages Array of pages Model Obj
  71. *
  72. */
  73. generatePageList(pages) {
  74. return pages.map((page) => {
  75. return <Page page={page} key={`recent-created:list-view:${page._id}`} />;
  76. });
  77. }
  78. /**
  79. * generate Elements of Pagination First Prev
  80. * ex. << < 1 2 3 > >>
  81. * this function set << & <
  82. */
  83. generateFirstPrev(activePage) {
  84. const paginationItems = [];
  85. if (activePage !== 1) {
  86. paginationItems.push(
  87. <Pagination.First key="first" onClick={() => { return this.getRecentCreatedList(1) }} />,
  88. );
  89. paginationItems.push(
  90. <Pagination.Prev key="prev" onClick={() => { return this.getRecentCreatedList(this.state.activePage - 1) }} />,
  91. );
  92. }
  93. else {
  94. paginationItems.push(
  95. <Pagination.First key="first" disabled />,
  96. );
  97. paginationItems.push(
  98. <Pagination.Prev key="prev" disabled />,
  99. );
  100. }
  101. return paginationItems;
  102. }
  103. /**
  104. * generate Elements of Pagination First Prev
  105. * ex. << < 4 5 6 7 8 > >>, << < 1 2 3 4 > >>
  106. * this function set numbers
  107. */
  108. generatePaginations(activePage, paginationStart, maxViewPageNum) {
  109. const paginationItems = [];
  110. for (let number = paginationStart; number <= maxViewPageNum; number++) {
  111. paginationItems.push(
  112. <Pagination.Item key={number} active={number === activePage} onClick={() => { return this.getRecentCreatedList(number) }}>{number}</Pagination.Item>,
  113. );
  114. }
  115. return paginationItems;
  116. }
  117. /**
  118. * generate Elements of Pagination First Prev
  119. * ex. << < 1 2 3 > >>
  120. * this function set > & >>
  121. */
  122. generateNextLast(activePage, totalPage) {
  123. const paginationItems = [];
  124. if (totalPage !== activePage) {
  125. paginationItems.push(
  126. <Pagination.Next key="next" onClick={() => { return this.getRecentCreatedList(this.state.activePage + 1) }} />,
  127. );
  128. paginationItems.push(
  129. <Pagination.Last key="last" onClick={() => { return this.getRecentCreatedList(totalPage) }} />,
  130. );
  131. }
  132. else {
  133. paginationItems.push(
  134. <Pagination.Next key="next" disabled />,
  135. );
  136. paginationItems.push(
  137. <Pagination.Last key="last" disabled />,
  138. );
  139. }
  140. return paginationItems;
  141. }
  142. render() {
  143. const pageList = this.generatePageList(this.state.pages);
  144. const paginationItems = [];
  145. const activePage = this.state.activePage;
  146. const totalPage = this.state.paginationNumbers.totalPage;
  147. const paginationStart = this.state.paginationNumbers.paginationStart;
  148. const maxViewPageNum = this.state.paginationNumbers.maxViewPageNum;
  149. const firstPrevItems = this.generateFirstPrev(activePage);
  150. paginationItems.push(firstPrevItems);
  151. const paginations = this.generatePaginations(activePage, paginationStart, maxViewPageNum);
  152. paginationItems.push(paginations);
  153. const nextLastItems = this.generateNextLast(activePage, totalPage);
  154. paginationItems.push(nextLastItems);
  155. return (
  156. <div className="page-list-container-create">
  157. <ul className="page-list-ul page-list-ul-flat">
  158. {pageList}
  159. </ul>
  160. <Pagination bsSize="small">{paginationItems}</Pagination>
  161. </div>
  162. );
  163. }
  164. }
  165. /**
  166. * Wrapper component for using unstated
  167. */
  168. const RecentCreatedWrapper = (props) => {
  169. return createSubscribedElement(RecentCreated, props, [AppContainer, PageContainer]);
  170. };
  171. RecentCreated.propTypes = {
  172. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  173. pageContainer: PropTypes.instanceOf(PageContainer).isRequired,
  174. };
  175. export default RecentCreatedWrapper;