Commit 2342f60a by Peter Holmberg

added actions

parent 1656f03a
......@@ -2,7 +2,7 @@ import React, { SFC } from 'react';
export interface Props {
isReadOnly: boolean;
onDelete: (event) => void;
onDelete: () => void;
onSubmit: (event) => void;
}
......@@ -12,7 +12,7 @@ const ButtonRow: SFC<Props> = ({ isReadOnly, onDelete, onSubmit }) => {
<button type="submit" className="btn btn-success" disabled={isReadOnly} onClick={event => onSubmit(event)}>
Save &amp; Test
</button>
<button type="submit" className="btn btn-danger" disabled={isReadOnly} onClick={event => onDelete(event)}>
<button type="submit" className="btn btn-danger" disabled={isReadOnly} onClick={onDelete}>
Delete
</button>
<a className="btn btn-inverse" href="/datasources">
......
......@@ -7,18 +7,21 @@ import PageLoader from '../../../core/components/PageLoader/PageLoader';
import PluginSettings from './PluginSettings';
import BasicSettings from './BasicSettings';
import ButtonRow from './ButtonRow';
import { loadDataSource, setDataSourceName } from '../state/actions';
import { deleteDataSource, loadDataSource, setDataSourceName, updateDataSource } from '../state/actions';
import { getNavModel } from '../../../core/selectors/navModel';
import { getRouteParamsId } from '../../../core/selectors/location';
import { getDataSource, getDataSourceMeta } from '../state/selectors';
import appEvents from '../../../core/app_events';
export interface Props {
navModel: NavModel;
dataSource: DataSource;
dataSourceMeta: Plugin;
pageId: number;
deleteDataSource: typeof deleteDataSource;
loadDataSource: typeof loadDataSource;
setDataSourceName: typeof setDataSourceName;
updateDataSource: typeof updateDataSource;
}
interface State {
name: string;
......@@ -39,11 +42,24 @@ export class DataSourceSettings extends PureComponent<Props, State> {
onSubmit = event => {
event.preventDefault();
console.log(event);
this.props.updateDataSource();
};
onDelete = () => {
appEvents.emit('confirm-modal', {
title: 'Delete',
text: 'Are you sure you want to delete this data source?',
yesText: 'Delete',
icon: 'fa-trash',
onConfirm: () => {
this.confirmDelete();
},
});
};
onDelete = event => {
console.log(event);
confirmDelete = () => {
this.props.deleteDataSource();
};
isReadOnly() {
......@@ -111,7 +127,7 @@ export class DataSourceSettings extends PureComponent<Props, State> {
<ButtonRow
onSubmit={event => this.onSubmit(event)}
isReadOnly={this.isReadOnly()}
onDelete={event => this.onDelete(event)}
onDelete={this.onDelete}
/>
</form>
</div>
......@@ -135,8 +151,10 @@ function mapStateToProps(state) {
}
const mapDispatchToProps = {
deleteDataSource,
loadDataSource,
setDataSourceName,
updateDataSource,
};
export default hot(module)(connect(mapStateToProps, mapDispatchToProps)(DataSourceSettings));
......@@ -157,6 +157,24 @@ export function loadDataSourceTypes(): ThunkResult<void> {
};
}
export function updateDataSource(): ThunkResult<void> {
return async (dispatch, getStore) => {
const dataSource = getStore().dataSources.dataSource;
await getBackendSrv().put(`/api/datasources/${dataSource.id}`, dataSource);
dispatch(loadDataSource(dataSource.id));
};
}
export function deleteDataSource(): ThunkResult<void> {
return async (dispatch, getStore) => {
const dataSource = getStore().dataSources.dataSource;
await getBackendSrv().delete(`/api/datasources/${dataSource.id}`);
dispatch(updateLocation({ path: '/datasources' }));
};
}
export function nameExits(dataSources, name) {
return (
dataSources.filter(dataSource => {
......
......@@ -36,6 +36,9 @@ export const dataSourcesReducer = (state = initialState, action: Action): DataSo
case ActionTypes.LoadDataSourceMeta:
return { ...state, dataSourceMeta: action.payload };
case ActionTypes.SetDataSourceName:
return { ...state, dataSource: { ...state.dataSource, name: action.payload } };
}
return state;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment