Skip to content

Commit

Permalink
Refactor to use Promise.all() for requests.
Browse files Browse the repository at this point in the history
  • Loading branch information
jelliotartz committed Oct 17, 2017
1 parent d2f9a3e commit 79a8bee
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 36 deletions.
8 changes: 3 additions & 5 deletions app/components/workflow-default-dialog.jsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import React from 'react';
import Translate from 'react-translate-component';

const WorkflowDefaultDialog = ({ onSuccess }) => {

return (
<div>
<div>
You are about to make the default workflow inactive,
which will remove the 'default' setting from this workflow.
Go to project builder => workflows to set a default workflow.
</div>
<Translate content="workflowDefaultDialog.text" />
<br />
<button type="submit" onSubmit={onSuccess}>ok</button>
</div>
)
Expand Down
3 changes: 3 additions & 0 deletions app/locales/en.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 19 additions & 31 deletions app/pages/admin/project-status.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, { Component } from 'react';
import apiClient from 'panoptes-client/lib/api-client';
import Dialog from 'modal-form/dialog';

import ProjectIcon from '../../components/project-icon';
import getWorkflowsInOrder from '../../lib/get-workflows-in-order';
Expand All @@ -10,9 +11,7 @@ import VersionList from './project-status/version-list';
import ExperimentalFeatures from './project-status/experimental-features';
import Toggle from './project-status/toggle';
import RedirectToggle from './project-status/redirect-toggle';

import WorkflowDefaultDialog from '../../components/workflow-default-dialog'
import Dialog from 'modal-form/dialog';

class ProjectStatus extends Component {
constructor(props) {
Expand All @@ -23,7 +22,6 @@ class ProjectStatus extends Component {
this.renderError = this.renderError.bind(this);
this.renderWorkflows = this.renderWorkflows.bind(this);
this.handleToggle = this.handleToggle.bind(this);
this.handleRemoveDefault = this.handleRemoveDefault.bind(this);

this.state = {
project: null,
Expand Down Expand Up @@ -80,30 +78,29 @@ class ProjectStatus extends Component {

handleToggle(event, workflow) {
this.setState({ error: null });
let checked = event.target.checked;
const checked = event.target.checked;
const defaultWorkflowId = this.state.project.configuration.default_workflow;

const defaultWorkflowId = this.state.project.configuration.default_workflow
const promises = [this.getWorkflows(), this.getProject()];

if (defaultWorkflowId === workflow.id && workflow.active) {
Dialog.alert(
<WorkflowDefaultDialog closeButton={true} required/>
).then(() => {
this.handleRemoveDefault()
})
.catch((error) => {
console.error(error)
<WorkflowDefaultDialog closeButton={true} required={true} />
)
.then(() => {
Promise.all(promises)
.then(() => {
if (!this.state.error) {
workflow.update({ active: checked }).save();
this.state.project.update({ 'configuration.default_workflow': undefined }).save();
}
})
.catch(error => this.setState({ error }));
})
.catch(error => this.setState({ error }));
}

return workflow.update({ 'active': checked }).save()
.then(() => this.getWorkflows())
.catch(error => this.setState({ error }))
}

handleRemoveDefault() {
return this.state.project.update({ 'configuration.default_workflow': undefined }).save()
.then(() => this.getProject())
.catch(error => this.setState({ error }))
workflow.update({ active: checked }).save();
}

renderError() {
Expand All @@ -112,16 +109,6 @@ class ProjectStatus extends Component {
}
}

renderDefaultWorkflowAsterisk(workflow) {
const defaultWorkflowId = this.state.project.configuration.default_workflow

if (defaultWorkflowId === workflow.id) {
return (
' * '
)
}
}

renderWorkflows() {
if (this.state.workflows.length === 0) {
return <div>No workflows found</div>;
Expand All @@ -132,7 +119,8 @@ class ProjectStatus extends Component {
{this.state.workflows.map((workflow) => {
return (
<li key={workflow.id} className="section-list__item">
{this.renderDefaultWorkflowAsterisk(workflow)}
{this.state.project.configuration.default_workflow === workflow.id ?
' * ' : ''}
<WorkflowToggle
workflow={workflow}
name="active"
Expand Down

0 comments on commit 79a8bee

Please sign in to comment.