import React from 'react';
import PropTypes from 'prop-types';
import { withTranslation } from 'react-i18next';
import { withUnstatedContainers } from '../UnstatedUtils';
import AppContainer from '../../services/AppContainer';
import PageContainer from '../../services/PageContainer';
import EditorContainer from '../../services/EditorContainer';
import PaginationWrapper from '../PaginationWrapper';
import Draft from './Draft';
class MyDraftList extends React.Component {
constructor(props) {
super(props);
this.state = {
drafts: [],
currentDrafts: [],
activePage: 1,
totalDrafts: 0,
// [TODO: rename pageLimitationM to pageLimitationL]
pagingLimit: Infinity,
};
this.handlePage = this.handlePage.bind(this);
this.getDraftsFromLocalStorage = this.getDraftsFromLocalStorage.bind(this);
this.getCurrentDrafts = this.getCurrentDrafts.bind(this);
this.clearDraft = this.clearDraft.bind(this);
this.clearAllDrafts = this.clearAllDrafts.bind(this);
}
async componentWillMount() {
await this.getDraftsFromLocalStorage();
this.getCurrentDrafts(1);
}
async handlePage(selectedPage) {
await this.getDraftsFromLocalStorage();
await this.getCurrentDrafts(selectedPage);
}
async getDraftsFromLocalStorage() {
const draftsAsObj = this.props.editorContainer.drafts;
if (draftsAsObj == null) {
return;
}
const res = await this.props.appContainer.apiGet('/pages.exist', {
pagePaths: JSON.stringify(Object.keys(draftsAsObj)),
});
// {'/a': '#a', '/b': '#b'} => [{path: '/a', markdown: '#a'}, {path: '/b', markdown: '#b'}]
const drafts = Object.entries(draftsAsObj).map((d) => {
const path = d[0];
return {
path,
markdown: d[1],
isExist: res.pages[path],
};
});
this.setState({ drafts, totalDrafts: drafts.length });
}
getCurrentDrafts(selectPageNumber) {
const limit = this.state.pagingLimit;
const totalDrafts = this.state.drafts.length;
const activePage = selectPageNumber;
const currentDrafts = this.state.drafts.slice((activePage - 1) * limit, activePage * limit);
this.setState({
currentDrafts,
activePage,
totalDrafts,
pagingLimit: limit,
});
}
/**
* generate Elements of Draft
*
* @param {any} drafts Array of pages Model Obj
*
*/
generateDraftList(drafts) {
return drafts.map((draft) => {
return (