MyDraftList.jsx 4.8 KB

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