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

refactor databases components and fix bug with selecting databases #670

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
209 changes: 126 additions & 83 deletions public/js/databases.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,11 @@ export class Databases extends Component {
super(props);
this.state = { type: '' };
this.preSelectedDbs = this.props.preSelectedDbs;
this.databases = this.databases.bind(this);
this.nselected = this.nselected.bind(this);
this.categories = this.categories.bind(this);
this.handleClick = this.handleClick.bind(this);
this.handleToggle = this.handleToggle.bind(this);
this.renderDatabases = this.renderDatabases.bind(this);
this.renderDatabase = this.renderDatabase.bind(this);
this.updateDatabaseType = this.updateDatabaseType.bind(this);
this.getCategories = this.getCategories.bind(this);
}
componentDidUpdate() {
if (this.databases() && this.databases().length === 1) {
$('.databases').find('input').prop('checked', true);
this.handleClick(this.databases()[0]);
}

componentDidUpdate() {
if (this.preSelectedDbs) {
var selectors = this.preSelectedDbs.map((db) => `input[value=${db.id}]`);
$(selectors.join(',')).prop('checked', true);
Expand All @@ -28,8 +19,58 @@ export class Databases extends Component {
}
this.props.onDatabaseTypeChanged(this.state.type);
}
databases(category) {
var databases = this.props.databases;

getCategories() {
return _.uniq(_.map(this.props.databases, _.iteratee('type'))).sort();
}

updateDatabaseType (type) {
if (type != this.state.type) this.setState({ type });

}

render() {
return (
<div className="form-group databases-container">
{_.map(this.getCategories(), (category) =>(
<DatabaseCategory
key={category}
type={this.state.type}
databases={this.props.databases}
updateDatabaseType={this.updateDatabaseType}
category={category}
getCategories={this.getCategories}
/>
))}
</div>
);
}
}

export class DatabaseCategory extends Component {
constructor(props) {
super(props);
this.state = { allSelected: false, selectedDbs: [] };
this.databases = this.props.databases;
this.category = this.props.category;
this.getCategories = this.props.getCategories;
this.getDatabases = this.getDatabases.bind(this);
this.nselected = this.nselected.bind(this);
this.handleClick = this.handleClick.bind(this);
this.handleToggle = this.handleToggle.bind(this);
this.renderPanelHeading = this.renderPanelHeading.bind(this);
this.renderDatabase = this.renderDatabase.bind(this);

}
componentDidUpdate() {
// SELECT DATABASE by default if only one is present
if (this.getDatabases() && this.getDatabases().length === 1) {
$('.databases').find('input').prop('checked', true);
this.handleClick(this.getDatabases()[0]);
}
}
getDatabases(category) {
var databases = this.databases;
if (category) {
databases = _.select(databases, (database) => database.type === category);
}
Expand All @@ -41,67 +82,96 @@ export class Databases extends Component {
return $('input[name="databases[]"]:checked').length;
}

categories() {
return _.uniq(_.map(this.props.databases, _.iteratee('type'))).sort();
}
handleClick(checked, database, category) {
const getSelectedDbs = (currentState) =>
checked
? currentState.selectedDbs.concat([database.id])
: currentState.selectedDbs.filter((db) => db !== database.id);

handleClick(database) {
this.setState(
(prevState) => ({
selectedDbs: getSelectedDbs(prevState),
allSelected:
getSelectedDbs(prevState).length === this.getDatabases(category).length,
})
);
var type = this.nselected() ? database.type : '';
if (type != this.state.type) this.setState({ type: type });
this.props.updateDatabaseType(type);
}

handleToggle(toggleState, type) {
switch (toggleState) {
case '[Select all]':
$(`.${type} .database input:not(:checked)`).click();
break;
case '[Deselect all]':
$(`.${type} .database input:checked`).click();
break;
}
this.forceUpdate();
handleToggle(type) {
this.state.allSelected
? $(`.${type} .database input:checked`).click()
: $(`.${type} .database input:not(:checked)`).click();
}
renderDatabases(category) {
// Panel name and column width.
var panelTitle = category[0].toUpperCase() + category.substring(1).toLowerCase() + ' databases';
var columnClass = this.categories().length === 1 ? 'col-md-12' : 'col-md-6';

renderDatabase(database){
var disabled = this.props.type && this.props.type !== database.type;

return (
<label className={(disabled && 'disabled database') || 'database'}>
<input
type="checkbox"
name="databases[]"
value={database.id}
data-type={database.type}
disabled={disabled}
checked={this.state.selectedDbs.includes(database.id)}
onChange={_.bind(function (evt) {
this.handleClick(evt.target.checked, database, this.category);
}, this)}
/>
{' ' + (database.title || database.name)}
</label>
);
}

renderPanelHeading(){
// Panel name and column width.
var panelTitle =
this.category[0].toUpperCase() +
this.category.substring(1).toLowerCase() +
' databases';
// Toggle button.
var toggleState = '[Select all]';
var toggleState =
this.state.allSelected ? '[Deselect all]' : '[Select all]';
var toggleClass = 'btn-link';
var toggleShown = this.databases(category).length > 1;
var toggleDisabled = this.state.type && this.state.type !== category;
var toggleShown = this.getDatabases(this.category).length > 1;
var toggleDisabled = this.props.type && this.props.type !== this.category;
if (toggleShown && toggleDisabled) toggleClass += ' disabled';
if (!toggleShown) toggleClass += ' hidden';
if (this.nselected() === this.databases(category).length) {
toggleState = '[Deselect all]';
}
return (
<div className="panel-heading">
<h4 style={{ display: 'inline' }}>{panelTitle}</h4> &nbsp;&nbsp;
<button
type="button"
className={toggleClass}
disabled={toggleDisabled}
onClick={function () {
this.handleToggle(this.category);
}.bind(this)}
>
{toggleState}
</button>
</div>);
}

render() {
var columnClass = this.getCategories().length === 1 ? 'col-md-12' : 'col-md-6';

// JSX.
return (
<div className={columnClass} key={'DB_' + category}>
<div className={columnClass} key={'DB_' + this.category}>
<div className="panel panel-default">
<div className="panel-heading">
<h4 style={{ display: 'inline' }}>{panelTitle}</h4> &nbsp;&nbsp;
<button
type="button"
className={toggleClass}
disabled={toggleDisabled}
onClick={function () {
this.handleToggle(toggleState, category);
}.bind(this)}
>
{toggleState}
</button>
</div>
<ul className={'list-group databases ' + category}>
{this.renderPanelHeading()}
<ul className={'list-group databases ' + this.category}>
{_.map(
this.databases(category),
this.getDatabases(this.category),
_.bind(function (database, index) {
return (
<li
className="list-group-item"
key={'DB_' + category + index}
key={'DB_' + this.category + index}
>
{this.renderDatabase(database)}
</li>
Expand All @@ -114,31 +184,4 @@ export class Databases extends Component {
);
}

renderDatabase(database) {
var disabled = this.state.type && this.state.type !== database.type;

return (
<label className={(disabled && 'disabled database') || 'database'}>
<input
type="checkbox"
name="databases[]"
value={database.id}
data-type={database.type}
disabled={disabled}
onChange={_.bind(function () {
this.handleClick(database);
}, this)}
/>
{' ' + (database.title || database.name)}
</label>
);
}

render() {
return (
<div className="form-group databases-container">
{_.map(this.categories(), this.renderDatabases)}
</div>
);
}
}
Loading