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 '~/client/services/AppContainer';
  6. import PageContainer from '~/client/services/PageContainer';
  7. import EditorContainer from '~/client/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. if (draftsAsObj == null) {
  37. return;
  38. }
  39. const res = await this.props.appContainer.apiGet('/pages.exist', {
  40. pagePaths: JSON.stringify(Object.keys(draftsAsObj)),
  41. });
  42. // {'/a': '#a', '/b': '#b'} => [{path: '/a', markdown: '#a'}, {path: '/b', markdown: '#b'}]
  43. const drafts = Object.entries(draftsAsObj).map((d) => {
  44. const path = d[0];
  45. return {
  46. path,
  47. markdown: d[1],
  48. isExist: res.pages[path],
  49. };
  50. });
  51. this.setState({ drafts, totalDrafts: drafts.length });
  52. }
  53. getCurrentDrafts(selectPageNumber) {
  54. const limit = 50; // implement only this component.(this default value is 50 (pageLimitationL))
  55. const totalDrafts = this.state.drafts.length;
  56. const activePage = selectPageNumber;
  57. const currentDrafts = this.state.drafts.slice((activePage - 1) * limit, activePage * limit);
  58. this.setState({
  59. currentDrafts,
  60. activePage,
  61. totalDrafts,
  62. pagingLimit: limit,
  63. });
  64. }
  65. /**
  66. * generate Elements of Draft
  67. *
  68. * @param {any} drafts Array of pages Model Obj
  69. *
  70. */
  71. generateDraftList(drafts) {
  72. return drafts.map((draft, index) => {
  73. return (
  74. <Draft
  75. index={index}
  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 className="mt-2">No drafts yet.</span>
  112. }
  113. { totalCount > 0 && (
  114. <React.Fragment>
  115. <div className="d-flex justify-content-between mt-2">
  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-2 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. align="center"
  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);