Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recent imports #198

Closed
2 changes: 2 additions & 0 deletions scripts/build-client-js.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ cross-env BABEL_ENV="browser" browserify -t [babelify] \
revision.js \
search.js \
statistics.js \
recent-imports.js \
-p [ factor-bundle \
-o ../../../static/js/entity-editor.js \
-o ../../../static/js/editor/edit.js \
Expand All @@ -25,5 +26,6 @@ cross-env BABEL_ENV="browser" browserify -t [babelify] \
-o ../../../static/js/revision.js \
-o ../../../static/js/search.js \
-o ../../../static/js/statistics.js \
-o ../../../static/js/recent-imports.js \
] > ../../../static/js/bundle.js
popd
2 changes: 2 additions & 0 deletions scripts/watch-client-js.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ cross-env BABEL_ENV="browser" watchify -t [babelify] \
revision.js \
search.js \
statistics.js \
recent-imports.js \
-p [ factor-bundle \
-o ../../../static/js/entity-editor.js \
-o ../../../static/js/editor/edit.js \
Expand All @@ -25,5 +26,6 @@ cross-env BABEL_ENV="browser" watchify -t [babelify] \
-o ../../../static/js/revision.js \
-o ../../../static/js/search.js \
-o ../../../static/js/statistics.js \
-o ../../../static/js/recent-imports.js \
] -o ../../../static/js/bundle.js -dv
popd
87 changes: 87 additions & 0 deletions src/client/components/pages/parts/recent-import-results.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* Copyright (C) 2018 Shivam Tripathi
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

import * as bootstrap from 'react-bootstrap';
import * as utilsHelper from '../../../helpers/utils';
import PropTypes from 'prop-types';
import React from 'react';


const {formatDate, getImportUrl} = utilsHelper;
const {Table} = bootstrap;

/**
* Renders the document and displays the recentImports table.
* @returns {ReactElement} a HTML document which displays the recentImports
*/

function RecentImportsTable(props) {
const {offset, recentImports} = props;
return (
<div>
<div> <h2 className="text-center">Click to review them!</h2> </div>
<Table
bordered
condensed
striped
>
<thead>
<tr>
<th >#</th>
<th>Name</th>
<th>Type</th>
<th>Date Added</th>
<th>Source</th>
</tr>
</thead>
<tbody>
{
recentImports.map((imports, i) => {
const type = imports.type.toLowerCase();
const {import_id: id, importedAt} = imports;
return (
<tr key={id}>
<td>{i + 1 + offset}</td>
<td>
<a href={getImportUrl(type, id)}>
{imports.defaultAlias.name}
</a>
</td>
<td>{imports.type}</td>
<td>
{formatDate(new Date(importedAt))}
</td>
<td>
{imports.source}
</td>
</tr>
);
})
}
</tbody>
</Table>
</div>
);
}

RecentImportsTable.propTypes = {
offset: PropTypes.number.isRequired,
recentImports: PropTypes.array.isRequired
};

export default RecentImportsTable;
106 changes: 106 additions & 0 deletions src/client/components/pages/recent-imports.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import * as bootstrap from 'react-bootstrap';
import PaginationProps from '../../helpers/pagination-props';
import PropTypes from 'prop-types';
import React from 'react';
import RecentImportsTable from './parts/recent-import-results';
import request from 'superagent-bluebird-promise';


const {PageHeader, Pagination} = bootstrap;

class RecentImports extends React.Component {
constructor(props) {
super(props);

this.paginationPropsGenerator = PaginationProps({
displayedPagesRange: 10,
itemsPerPage: props.limit
});

this.state = {
currentPage: props.currentPage,
offset: 0,
paginationProps: {
hasBeginningPage: false,
hasEndPage: false,
hasNextPage: false,
hasPreviousPage: false,
totalPages: 0
},
recentImports: []
};

this.handleClick = this.handleClick.bind(this);
this.handleCb = this.handleCb.bind(this);
}

componentDidMount() {
this.handleClick(this.state.currentPage);
}

componentDidUpdate() {
window.history.replaceState(
null, null, `?page=${this.state.currentPage}`
);
}

async handleClick(pageNumber) {
const {currentPage, limit, offset, totalResults, recentImports} =
await request.get(`/imports/recent/raw?page=${pageNumber}`)
.then((res) => JSON.parse(res.text));

const paginationProps = this.paginationPropsGenerator(
totalResults, currentPage
);

this.setState({
currentPage, limit, offset, paginationProps, recentImports,
totalResults
});
}

handleCb() {
this.handleClick(this.state.currentPage + 1);
}

render() {
const {currentPage, limit, totalResults, paginationProps} = this.state;
return (
<div>
<PageHeader>Recent Imports</PageHeader>
<h4> The following data has been imported recently. </h4>
<RecentImportsTable
offset={this.state.offset}
recentImports={this.state.recentImports}
/>
<p> {`Displaying ${limit} of ${totalResults} results`} </p>
<Pagination
boundaryLinks
ellipsis
activePage={currentPage}
className={paginationProps.totalPages === 0 ?
'hidden' : 'shown'}
first={paginationProps.hasBeginningPage}
items={paginationProps.totalPages}
last={paginationProps.hasEndPage}
next={paginationProps.hasNextPage}
prev={paginationProps.hasPreviousPage}
onSelect={this.handleClick}
/>
</div>
);
}
}

RecentImports.displayName = 'RecentImports';
RecentImports.propTypes = {
currentPage: PropTypes.number,
limit: PropTypes.number
};
RecentImports.defaultProps = {
currentPage: 1,
limit: 10
};

export default RecentImports;

6 changes: 6 additions & 0 deletions src/client/containers/layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,12 @@ class Layout extends React.Component {
{' Statistics '}
</NavItem>
</Nav>
<Nav pullRight>
<NavItem href="/imports/recent">
<FontAwesome name="user-check"/>
{' Review recent imports '}
</NavItem>
</Nav>
{!(homepage || hideSearch) &&
<form
action="/search"
Expand Down
36 changes: 36 additions & 0 deletions src/client/controllers/recent-imports.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (C) 2018 Shivam Tripathi
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

import Layout from '../containers/layout';
import React from 'react';
import ReactDOM from 'react-dom';
import RecentImports from '../components/pages/recent-imports';
import {extractLayoutProps} from '../helpers/props';


const propsTarget = document.getElementById('props');
const props = propsTarget ? JSON.parse(propsTarget.innerHTML) : {};
const markup = (
<Layout {...extractLayoutProps(props)}>
<RecentImports
{...props}
/>
</Layout>
);

ReactDOM.hydrate(markup, document.getElementById('target'));
Loading