MyDraftList.jsx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import { withTranslation } from 'react-i18next';
  4. import { withUnstatedContainers } from '../UnstatedUtils';
  5. import AppContainer from '../../services/AppContainer';
  6. import PageContainer from '../../services/PageContainer';
  7. import EditorContainer from '../../services/EditorContainer';
  8. import PaginationWrapper from '../PaginationWrapper';
  9. import Draft from './Draft';
  10. class MyDraftList extends React.Component {
  11. constructor(props) {
  12. super(props);
  13. this.state = {
  14. drafts: [],
  15. currentDrafts: [],
  16. activePage: 1,
  17. totalDrafts: 0,
  18. // [TODO: rename pageLimitationM to pageLimitationL]
  19. pagingLimit: Infinity,
  20. };
  21. this.handlePage = this.handlePage.bind(this);
  22. this.getDraftsFromLocalStorage = this.getDraftsFromLocalStorage.bind(this);
  23. this.getCurrentDrafts = this.getCurrentDrafts.bind(this);
  24. this.clearDraft = this.clearDraft.bind(this);
  25. this.clearAllDrafts = this.clearAllDrafts.bind(this);
  26. }
  27. async componentWillMount() {
  28. await this.getDraftsFromLocalStorage();
  29. this.getCurrentDrafts(1);
  30. }
  31. async handlePage(selectedPage) {
  32. await this.getDraftsFromLocalStorage();
  33. await this.getCurrentDrafts(selectedPage);
  34. }
  35. async getDraftsFromLocalStorage() {
  36. const draftsAsObj = this.props.editorContainer.drafts;
  37. if (draftsAsObj == null) {
  38. return;
  39. }
  40. const res = await this.props.appContainer.apiGet('/pages.exist', {
  41. pagePaths: JSON.stringify(Object.keys(draftsAsObj)),
  42. });
  43. // {'/a': '#a', '/b': '#b'} => [{path: '/a', markdown: '#a'}, {path: '/b', markdown: '#b'}]
  44. const drafts = Object.entries(draftsAsObj).map((d) => {
  45. const path = d[0];
  46. return {
  47. path,
  48. markdown: d[1],
  49. isExist: res.pages[path],
  50. };
  51. });
  52. this.setState({ drafts, totalDrafts: drafts.length });
  53. }
  54. getCurrentDrafts(selectPageNumber) {
  55. // TODO implement temporarily paging number only this component (this paging size is pageLimitationL).
  56. const limit = this.state.pagingLimit;
  57. const totalDrafts = this.state.drafts.length;
  58. const activePage = selectPageNumber;
  59. const currentDrafts = this.state.drafts.slice((activePage - 1) * limit, activePage * limit);
  60. this.setState({
  61. currentDrafts,
  62. activePage,
  63. totalDrafts,
  64. pagingLimit: limit,
  65. });
  66. }
  67. /**
  68. * generate Elements of Draft
  69. *
  70. * @param {any} drafts Array of pages Model Obj
  71. *
  72. */
  73. generateDraftList(drafts) {
  74. return drafts.map((draft) => {
  75. return (
  76. <Draft
  77. key={draft.path}
  78. path={draft.path}
  79. markdown={draft.markdown}
  80. isExist={draft.isExist}
  81. clearDraft={this.clearDraft}
  82. />
  83. );
  84. });
  85. }
  86. clearDraft(path) {
  87. this.props.editorContainer.clearDraft(path);
  88. this.setState((prevState) => {
  89. return {
  90. drafts: prevState.drafts.filter((draft) => { return draft.path !== path }),
  91. currentDrafts: prevState.drafts.filter((draft) => { return draft.path !== path }),
  92. };
  93. });
  94. }
  95. clearAllDrafts() {
  96. this.props.editorContainer.clearAllDrafts();
  97. this.setState({
  98. drafts: [],
  99. currentDrafts: [],
  100. activePage: 1,
  101. totalDrafts: 0,
  102. pagingLimit: Infinity,
  103. });
  104. }
  105. render() {
  106. const { t } = this.props;
  107. const draftList = this.generateDraftList(this.state.currentDrafts);
  108. const totalCount = this.state.totalDrafts;
  109. return (
  110. <div>
  111. { totalCount === 0
  112. && <span>No drafts yet.</span>
  113. }
  114. { totalCount > 0 && (
  115. <React.Fragment>
  116. <div className="d-flex justify-content-between">
  117. <h4>Total: {totalCount} drafts</h4>
  118. <div className="align-self-center">
  119. <button type="button" className="btn btn-sm btn-outline-danger" onClick={this.clearAllDrafts}>
  120. <i className="icon-fw icon-fire"></i>
  121. {t('delete_all')}
  122. </button>
  123. </div>
  124. </div>
  125. <div className="tab-pane mt-5 accordion" id="draft-list">
  126. {draftList}
  127. </div>
  128. <PaginationWrapper
  129. activePage={this.state.activePage}
  130. changePage={this.handlePage}
  131. totalItemsCount={this.state.totalDrafts}
  132. pagingLimit={this.state.pagingLimit}
  133. size="sm"
  134. />
  135. </React.Fragment>
  136. ) }
  137. </div>
  138. );
  139. }
  140. }
  141. /**
  142. * Wrapper component for using unstated
  143. */
  144. const MyDraftListWrapper = withUnstatedContainers(MyDraftList, [AppContainer, PageContainer, EditorContainer]);
  145. MyDraftList.propTypes = {
  146. t: PropTypes.func.isRequired, // react-i18next
  147. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  148. pageContainer: PropTypes.instanceOf(PageContainer).isRequired,
  149. editorContainer: PropTypes.instanceOf(EditorContainer).isRequired,
  150. };
  151. export default withTranslation()(MyDraftListWrapper);