Newer
Older
Tom Robinson
committed
"use strict";
import ActionButton from "metabase/components/ActionButton.react";
import Header from "metabase/components/Header.react";
import Icon from "metabase/components/Icon.react";
import PopoverWithTrigger from "metabase/components/PopoverWithTrigger.react";
import AddToDashSelectQuestionModal from "./AddToDashSelectQuestionModal.react";
import DeleteDashboardModal from "./DeleteDashboardModal.react";
Tom Robinson
committed
import {
setEditingDashboard,
fetchDashboard,
setDashboardAttributes,
saveDashboard,
deleteDashboard
Tom Robinson
committed
} from '../actions';
export default class DashboardHeader extends React.Component {
onEditDashboard() {
this.props.dispatch(setEditingDashboard(true))
}
onDashboardAttributeChange(attribute, value) {
this.props.dispatch(setDashboardAttributes({
id: this.props.dashboard.id,
attributes: { [attribute]: value }
}));
}
onRevertDashboard() {
this.props.dispatch(fetchDashboard(this.props.dashboard.id));
}
async onSaveDashboard() {
await this.props.dispatch(saveDashboard(this.props.dashboard.id));
this.props.dispatch(setEditingDashboard(false));
}
async onDeleteDashboard() {
await this.props.dispatch(deleteDashboard(this.props.dashboard.id));
this.props.onDashboardDeleted(this.props.dashboard.id)
this.props.onChangeLocation("/")
}
Tom Robinson
committed
getEditingButtons() {
var editingButtons = [];
if (this.props.isDirty) {
// if (this.state.recentlySaved === "updated" || (this.props.cardIsDirtyFn() && this.props.card.is_creator)) {
editingButtons.push(
<ActionButton
actionFn={() => this.onSaveDashboard()}
className='Button Button--small Button--primary text-uppercase'
normalText="Update"
activeText="Updating…"
failedText="Update failed"
successText="Updated"
/>
);
// }
// if (this.props.cardIsDirtyFn()) {
editingButtons.push(
<a className="Button Button--small text-uppercase" href="#" onClick={() => this.onRevertDashboard()}>Discard Changes</a>
);
// }
}
editingButtons.push(
<PopoverWithTrigger
Tom Robinson
committed
tether={false}
triggerClasses="Button Button--small text-uppercase"
triggerElement="Delete"
>
<DeleteDashboardModal
dispatch={this.props.dispatch}
dashboard={this.props.dashboard}
onClose={() => this.refs.deleteDashboardModal.toggleModal()}
onDelete={() => this.onDeleteDashboard()}
/>
Tom Robinson
committed
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
</PopoverWithTrigger>
);
return editingButtons;
}
getHeaderButtons() {
var buttonSections = [];
var { dashboard, dashcards } = this.props;
if (dashboard && dashboard.can_write && !this.props.isEditing) {
buttonSections.push([
<a title="Edit Dashboard Layout" className="text-brand-hover" onClick={() => this.onEditDashboard()}>
<Icon name="pencil" width="16px" height="16px" />
</a>
]);
}
// buttonSections.push([
// <a title="Add Question to Dashboard" className="text-brand-hover" onClick={() => this.addQuestion()}>
// <Icon name="add" width="16px" height="16px" />
// </a>
// ]);
buttonSections.push([
<PopoverWithTrigger
ref="addQuestionModal"
tether={false}
triggerElement={<Icon name="add" width="16px" height="16px" />}
>
<AddToDashSelectQuestionModal
dispatch={this.props.dispatch}
dashboard={dashboard}
cards={this.props.cards}
onClose={() => this.refs.addQuestionModal.toggleModal()}
Tom Robinson
committed
/>
</PopoverWithTrigger>
Tom Robinson
committed
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
return buttonSections;
}
render() {
var { dashboard, dashcards } = this.props;
return (
<Header
objectType="dashboard"
item={dashboard}
isEditing={this.props.isEditing}
isEditingInfo={this.props.isEditing}
headerButtons={this.getHeaderButtons()}
editingTitle="You are editing a dashboard"
editingButtons={this.getEditingButtons()}
setItemAttributeFn={this.onDashboardAttributeChange.bind(this)}
>
</Header>
);
}
}
DashboardHeader.propTypes = {
dispatch: React.PropTypes.func.isRequired,
isEditing: React.PropTypes.bool.isRequired,
dashboard: React.PropTypes.object.isRequired
};