MyDraftList.jsx 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import { withTranslation } from 'react-i18next';
  4. // TODO: GW-333
  5. // import Pagination from 'react-bootstrap/lib/Pagination';
  6. import { createSubscribedElement } from '../UnstatedUtils';
  7. import AppContainer from '../../services/AppContainer';
  8. import PageContainer from '../../services/PageContainer';
  9. import EditorContainer from '../../services/EditorContainer';
  10. import Draft from './Draft';
  11. class MyDraftList extends React.Component {
  12. constructor(props) {
  13. super(props);
  14. this.state = {
  15. drafts: [],
  16. currentDrafts: [],
  17. activePage: 1,
  18. paginationNumbers: {},
  19. };
  20. this.getDraftsFromLocalStorage = this.getDraftsFromLocalStorage.bind(this);
  21. this.getCurrentDrafts = this.getCurrentDrafts.bind(this);
  22. this.clearDraft = this.clearDraft.bind(this);
  23. this.clearAllDrafts = this.clearAllDrafts.bind(this);
  24. this.calculatePagination = this.calculatePagination.bind(this);
  25. }
  26. async componentWillMount() {
  27. await this.getDraftsFromLocalStorage();
  28. this.getCurrentDrafts(1);
  29. }
  30. async getDraftsFromLocalStorage() {
  31. const draftsAsObj = this.props.editorContainer.drafts;
  32. const res = await this.props.appContainer.apiGet('/pages.exist', {
  33. pages: draftsAsObj,
  34. });
  35. // {'/a': '#a', '/b': '#b'} => [{path: '/a', markdown: '#a'}, {path: '/b', markdown: '#b'}]
  36. const drafts = Object.entries(draftsAsObj).map((d) => {
  37. const path = d[0];
  38. return {
  39. path,
  40. markdown: d[1],
  41. isExist: res.pages[path],
  42. };
  43. });
  44. this.setState({ drafts });
  45. }
  46. getCurrentDrafts(selectPageNumber) {
  47. const { appContainer } = this.props;
  48. const limit = appContainer.getConfig().recentCreatedLimit;
  49. const totalCount = this.state.drafts.length;
  50. const activePage = selectPageNumber;
  51. const paginationNumbers = this.calculatePagination(limit, totalCount, activePage);
  52. const currentDrafts = this.state.drafts.slice((activePage - 1) * limit, activePage * limit);
  53. this.setState({
  54. currentDrafts,
  55. activePage,
  56. paginationNumbers,
  57. });
  58. }
  59. /**
  60. * generate Elements of Draft
  61. *
  62. * @param {any} drafts Array of pages Model Obj
  63. *
  64. */
  65. generateDraftList(drafts) {
  66. return drafts.map((draft) => {
  67. return (
  68. <Draft
  69. key={draft.path}
  70. path={draft.path}
  71. markdown={draft.markdown}
  72. isExist={draft.isExist}
  73. clearDraft={this.clearDraft}
  74. />
  75. );
  76. });
  77. }
  78. clearDraft(path) {
  79. this.props.editorContainer.clearDraft(path);
  80. this.setState((prevState) => {
  81. return {
  82. drafts: prevState.drafts.filter((draft) => { return draft.path !== path }),
  83. currentDrafts: prevState.drafts.filter((draft) => { return draft.path !== path }),
  84. };
  85. });
  86. }
  87. clearAllDrafts() {
  88. this.props.editorContainer.clearAllDrafts();
  89. this.setState({
  90. drafts: [],
  91. currentDrafts: [],
  92. activePage: 1,
  93. paginationNumbers: {},
  94. });
  95. }
  96. calculatePagination(limit, totalCount, activePage) {
  97. // calc totalPageNumber
  98. const totalPage = Math.floor(totalCount / limit) + (totalCount % limit === 0 ? 0 : 1);
  99. let paginationStart = activePage - 2;
  100. let maxViewPageNum = activePage + 2;
  101. // pagiNation Number area size = 5 , pageNuber calculate in here
  102. // activePage Position calculate ex. 4 5 [6] 7 8 (Page8 over is Max), 3 4 5 [6] 7 (Page7 is Max)
  103. if (paginationStart < 1) {
  104. const diff = 1 - paginationStart;
  105. paginationStart += diff;
  106. maxViewPageNum = Math.min(totalPage, maxViewPageNum + diff);
  107. }
  108. if (maxViewPageNum > totalPage) {
  109. const diff = maxViewPageNum - totalPage;
  110. maxViewPageNum -= diff;
  111. paginationStart = Math.max(1, paginationStart - diff);
  112. }
  113. return {
  114. totalPage,
  115. paginationStart,
  116. maxViewPageNum,
  117. };
  118. }
  119. /**
  120. * generate Elements of Pagination First Prev
  121. * ex. << < 1 2 3 > >>
  122. * this function set << & <
  123. */
  124. generateFirstPrev(activePage) {
  125. const paginationItems = [];
  126. if (activePage !== 1) {
  127. paginationItems.push(
  128. <Pagination.First key="first" onClick={() => { return this.getCurrentDrafts(1) }} />,
  129. );
  130. paginationItems.push(
  131. <Pagination.Prev key="prev" onClick={() => { return this.getCurrentDrafts(this.state.activePage - 1) }} />,
  132. );
  133. }
  134. else {
  135. paginationItems.push(
  136. <Pagination.First key="first" disabled />,
  137. );
  138. paginationItems.push(
  139. <Pagination.Prev key="prev" disabled />,
  140. );
  141. }
  142. return paginationItems;
  143. }
  144. /**
  145. * generate Elements of Pagination First Prev
  146. * ex. << < 4 5 6 7 8 > >>, << < 1 2 3 4 > >>
  147. * this function set numbers
  148. */
  149. generatePaginations(activePage, paginationStart, maxViewPageNum) {
  150. const paginationItems = [];
  151. for (let number = paginationStart; number <= maxViewPageNum; number++) {
  152. paginationItems.push(
  153. <Pagination.Item key={number} active={number === activePage} onClick={() => { return this.getCurrentDrafts(number) }}>{number}</Pagination.Item>,
  154. );
  155. }
  156. return paginationItems;
  157. }
  158. /**
  159. * generate Elements of Pagination First Prev
  160. * ex. << < 1 2 3 > >>
  161. * this function set > & >>
  162. */
  163. generateNextLast(activePage, totalPage) {
  164. const paginationItems = [];
  165. if (totalPage !== activePage) {
  166. paginationItems.push(
  167. <Pagination.Next key="next" onClick={() => { return this.getCurrentDrafts(this.state.activePage + 1) }} />,
  168. );
  169. paginationItems.push(
  170. <Pagination.Last key="last" onClick={() => { return this.getCurrentDrafts(totalPage) }} />,
  171. );
  172. }
  173. else {
  174. paginationItems.push(
  175. <Pagination.Next key="next" disabled />,
  176. );
  177. paginationItems.push(
  178. <Pagination.Last key="last" disabled />,
  179. );
  180. }
  181. return paginationItems;
  182. }
  183. render() {
  184. const { t } = this.props;
  185. const draftList = this.generateDraftList(this.state.currentDrafts);
  186. const paginationItems = [];
  187. const totalCount = this.state.drafts.length;
  188. const activePage = this.state.activePage;
  189. const totalPage = this.state.paginationNumbers.totalPage;
  190. const paginationStart = this.state.paginationNumbers.paginationStart;
  191. const maxViewPageNum = this.state.paginationNumbers.maxViewPageNum;
  192. const firstPrevItems = this.generateFirstPrev(activePage);
  193. paginationItems.push(firstPrevItems);
  194. const paginations = this.generatePaginations(activePage, paginationStart, maxViewPageNum);
  195. paginationItems.push(paginations);
  196. const nextLastItems = this.generateNextLast(activePage, totalPage);
  197. paginationItems.push(nextLastItems);
  198. return (
  199. <div className="page-list-container-create">
  200. { totalCount === 0
  201. && <span>No drafts yet.</span>
  202. }
  203. { totalCount > 0 && (
  204. <React.Fragment>
  205. <div className="d-flex justify-content-between">
  206. <h4>Total: {totalCount} drafts</h4>
  207. <div className="align-self-center">
  208. <button type="button" className="btn btn-sm btn-default" onClick={this.clearAllDrafts}>
  209. <i className="icon-fw icon-fire text-danger"></i>
  210. {t('Delete All')}
  211. </button>
  212. </div>
  213. </div>
  214. <div className="tab-pane m-t-30 accordion" id="draft-list">
  215. {draftList}
  216. </div>
  217. <Pagination bsSize="small">{paginationItems}</Pagination>
  218. </React.Fragment>
  219. ) }
  220. </div>
  221. );
  222. }
  223. }
  224. /**
  225. * Wrapper component for using unstated
  226. */
  227. const MyDraftListWrapper = (props) => {
  228. return createSubscribedElement(MyDraftList, props, [AppContainer, PageContainer, EditorContainer]);
  229. };
  230. MyDraftList.propTypes = {
  231. t: PropTypes.func.isRequired, // react-i18next
  232. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  233. pageContainer: PropTypes.instanceOf(PageContainer).isRequired,
  234. editorContainer: PropTypes.instanceOf(EditorContainer).isRequired,
  235. };
  236. export default withTranslation()(MyDraftListWrapper);