Skip to content

Commit

Permalink
Use project completeness (#3536)
Browse files Browse the repository at this point in the history
  • Loading branch information
srallen authored Mar 6, 2017
1 parent 36a68f8 commit 021bd32
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 74 deletions.
35 changes: 6 additions & 29 deletions app/pages/project/finished-banner.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@ export default class FinishedBanner extends React.Component {
this.state = {
hasResultsPage: false,
hide: false,
projectIsComplete: false,
};

this.getCompleteness = this.getCompleteness.bind(this);
this.getResultsPageExistence = this.getResultsPageExistence.bind(this);
this.hide = this.hide.bind(this);
this.refresh = this.refresh.bind(this);
Expand All @@ -31,25 +29,6 @@ export default class FinishedBanner extends React.Component {
}
}

getCompleteness(project) {
if (project.redirect) {
return Promise.resolve(false);
}

return getWorkflowsInOrder(project, { active: true, fields: 'finished_at' })
.then((activeWorkflows) => {
if (activeWorkflows.length === 0) {
// No active workflows? This is probably a custom project.
return false;
} else {
const activeUnfinishedWorkflows = activeWorkflows.filter((workflow) => {
return !workflow.finished_at ? workflow : null;
});
return activeUnfinishedWorkflows.length === 0;
}
});
}

getResultsPageExistence(project) {
return project.get('pages').then((pages) => {
const resultsPages = pages.filter((page) => {
Expand All @@ -71,15 +50,12 @@ export default class FinishedBanner extends React.Component {
this.setState({
hasResultsPage: false,
hide: false,
projectIsComplete: false,
});

Promise.all([
this.getCompleteness(project),
this.getResultsPageExistence(project),
]).then(([projectIsComplete, hasResultsPage]) => {
this.setState({ projectIsComplete, hasResultsPage });
});
this.getResultsPageExistence(project)
.then((hasResultsPage) => {
this.setState({ hasResultsPage });
});
}

renderResultsPage() {
Expand All @@ -103,7 +79,7 @@ export default class FinishedBanner extends React.Component {
const dismissals = finishedProjectDismissals ? JSON.parse(finishedProjectDismissals) : {};
const recentlyDismissed = Date.now() - dismissals[this.props.project.id] < this.props.dismissFor;

if (recentlyDismissed || !this.state.projectIsComplete || this.state.hide) {
if (recentlyDismissed || this.state.hide) {
return null;
}

Expand All @@ -130,4 +106,5 @@ FinishedBanner.propTypes = {
id: React.PropTypes.string,
slug: React.PropTypes.string,
}),
projectIsComplete: React.PropTypes.bool
};
9 changes: 6 additions & 3 deletions app/pages/project/home.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,10 @@ export default class ProjectHomePage extends React.Component {

return (
<div className="project-home-page">
<div className="call-to-action-container">
<FinishedBanner project={this.props.project} />
</div>
{this.props.projectIsComplete &&
(<div className="call-to-action-container">
<FinishedBanner project={this.props.project} />
</div>)}

<div className="project-home-page__description">{this.props.project.description}</div>

Expand Down Expand Up @@ -142,6 +143,7 @@ ProjectHomePage.defaultProps = {
onChangePreferences: () => {},
preferences: {},
project: {},
projectIsComplete: false,
splits: {}
};

Expand All @@ -158,5 +160,6 @@ ProjectHomePage.propTypes = {
introduction: React.PropTypes.string,
researcher_quote: React.PropTypes.string
}).isRequired,
projectIsComplete: React.PropTypes.bool,
splits: React.PropTypes.object
};
2 changes: 1 addition & 1 deletion app/pages/project/index.cjsx
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ ProjectPage = React.createClass
@isWorkflowInactive(project, selectedWorkflowID)

checkIfProjectIsComplete: (project) ->
projectIsComplete = (true for workflow in @state.activeWorkflows when not workflow.finished_at?).length is 0
projectIsComplete = project.completeness > 0.99
@setState { projectIsComplete }

selectRandomWorkflow: (project) ->
Expand Down
13 changes: 2 additions & 11 deletions app/pages/project/metadata.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,7 @@ export default class ProjectMetadata extends React.Component {
}

renderStatus() {
let percentComplete = 0;
this.props.activeWorkflows.map((workflow) => {
percentComplete += workflow.completeness;
return percentComplete;
});

if (this.props.activeWorkflows.length > 0) {
percentComplete /= this.props.activeWorkflows.length;
}
const percentComplete = this.props.project.completeness;

return (
<div className="project-metadata-status-bar">
Expand Down Expand Up @@ -107,9 +99,9 @@ ProjectMetadata.contextTypes = {
};

ProjectMetadata.propTypes = {
activeWorkflows: React.PropTypes.arrayOf(React.PropTypes.object),
project: React.PropTypes.shape({
classifications_count: React.PropTypes.number,
completeness: React.PropTypes.number,
display_name: React.PropTypes.string,
id: React.PropTypes.id,
slug: React.PropTypes.string,
Expand All @@ -118,7 +110,6 @@ ProjectMetadata.propTypes = {
};

ProjectMetadata.defaultProps = {
activeWorkflows: [],
project: {},
showTalkStatus: false,
};
72 changes: 42 additions & 30 deletions app/pages/project/metadata.spec.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,54 @@
import React from 'react'
import assert from 'assert'
import sinon from 'sinon'
import ProjectMetadata from './metadata'
import {shallow, render, mount} from 'enzyme'
import React from 'react';
import assert from 'assert';
import sinon from 'sinon';
import { render, mount } from 'enzyme';

import ProjectMetadata from './metadata';

describe('ProjectMetadata', function(){
var project;
var pusher;
let project;
let pusher;

before(function() {
project = {classifiers_count: 0, classifications_count: 0, subjects_count: 0, retired_subjects_count: 0};
project = { classifiers_count: 0, classifications_count: 0, completeness: 0.56, subjects_count: 0, retired_subjects_count: 0 };

const subscribe = function(channel) { return {bind: function(event, callback) { }}}
pusher = {subscribe: subscribe};
const subscribe = function(channel) { return { bind: function(event, callback) { } }};
pusher = { subscribe };
});

it('renders stats', function(){
const context = {pusher: pusher};
const page = render(<ProjectMetadata project={project} />, {context});
assert(page.find('.project-metadata-stat').length, 4);
});
describe('render', function() {
let wrapper;
before(function() {
const context = { pusher };
wrapper = render(<ProjectMetadata project={project} />, { context });
});

it('subscribes to pusher', function() {
const context = {pusher: pusher};
pusher.subscribe = sinon.spy(pusher.subscribe);
const wrapper = mount(<ProjectMetadata project={project} />, {context});
wrapper.setContext(context);
assert(pusher.subscribe.called, true);
});
it('renders stats', function() {
assert(wrapper.find('.project-metadata-stat').length, 4);
});

it('subscribes to pusher', function() {
const context = {pusher: pusher};
pusher.unsubscribe = sinon.spy();

const wrapper = mount(<ProjectMetadata project={project} />, {context});
wrapper.setContext(context);
wrapper.unmount();
it('renders 56% completeness', function() {
assert(wrapper.find('rect').first().prop('width'), 0.56);
});
});

assert(pusher.unsubscribe.called, true);
describe('pusher', function() {
it('subscribes to pusher', function() {
const context = { pusher };
pusher.subscribe = sinon.spy(pusher.subscribe);
const wrapper = mount(<ProjectMetadata project={project} />, { context });

wrapper.setContext(context);
assert(pusher.subscribe.called, true);
});

it('unsubscribes to pusher', function() {
pusher.unsubscribe = sinon.spy();
const context = { pusher };
const wrapper = mount(<ProjectMetadata project={project} />, { context });
wrapper.setContext(context);
wrapper.unmount();
assert(pusher.unsubscribe.called, true);
});
});
});

0 comments on commit 021bd32

Please sign in to comment.