MyDraftList.jsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. const limit = this.state.pagingLimit;
  56. const totalDrafts = this.state.drafts.length;
  57. const activePage = selectPageNumber;
  58. const currentDrafts = this.state.drafts.slice((activePage - 1) * limit, activePage * limit);
  59. this.setState({
  60. currentDrafts,
  61. activePage,
  62. totalDrafts,
  63. pagingLimit: limit,
  64. });
  65. }
  66. /**
  67. * generate Elements of Draft
  68. *
  69. * @param {any} drafts Array of pages Model Obj
  70. *
  71. */
  72. generateDraftList(drafts) {
  73. return drafts.map((draft) => {
  74. return (
  75. <Draft
  76. key={draft.path}
  77. path={draft.path}
  78. markdown={draft.markdown}
  79. isExist={draft.isExist}
  80. clearDraft={this.clearDraft}
  81. />
  82. );
  83. });
  84. }
  85. clearDraft(path) {
  86. this.props.editorContainer.clearDraft(path);
  87. this.setState((prevState) => {
  88. return {
  89. drafts: prevState.drafts.filter((draft) => { return draft.path !== path }),
  90. currentDrafts: prevState.drafts.filter((draft) => { return draft.path !== path }),
  91. };
  92. });
  93. }
  94. clearAllDrafts() {
  95. this.props.editorContainer.clearAllDrafts();
  96. this.setState({
  97. drafts: [],
  98. currentDrafts: [],
  99. activePage: 1,
  100. totalDrafts: 0,
  101. pagingLimit: Infinity,
  102. });
  103. }
  104. render() {
  105. const { t } = this.props;
  106. const draftList = this.generateDraftList(this.state.currentDrafts);
  107. const totalCount = this.state.totalDrafts;
  108. return (
  109. <div className="page-list-container-create">
  110. { totalCount === 0
  111. && <span>No drafts yet.</span>
  112. }
  113. { totalCount > 0 && (
  114. <React.Fragment>
  115. <div className="d-flex justify-content-between">
  116. <h4>Total: {totalCount} drafts</h4>
  117. <div className="align-self-center">
  118. <button type="button" className="btn btn-sm btn-outline-danger" onClick={this.clearAllDrafts}>
  119. <i className="icon-fw icon-fire"></i>
  120. {t('delete_all')}
  121. </button>
  122. </div>
  123. </div>
  124. <div className="tab-pane mt-5 accordion" id="draft-list">
  125. {draftList}
  126. </div>
  127. <PaginationWrapper
  128. activePage={this.state.activePage}
  129. changePage={this.handlePage}
  130. totalItemsCount={this.state.totalDrafts}
  131. pagingLimit={this.state.pagingLimit}
  132. />
  133. </React.Fragment>
  134. ) }
  135. </div>
  136. );
  137. }
  138. }
  139. /**
  140. * Wrapper component for using unstated
  141. */
  142. const MyDraftListWrapper = withUnstatedContainers(MyDraftList, [AppContainer, PageContainer, EditorContainer]);
  143. MyDraftList.propTypes = {
  144. t: PropTypes.func.isRequired, // react-i18next
  145. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  146. pageContainer: PropTypes.instanceOf(PageContainer).isRequired,
  147. editorContainer: PropTypes.instanceOf(EditorContainer).isRequired,
  148. };
  149. export default withTranslation()(MyDraftListWrapper);