mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Big refactoring for dashboard init redux actions
This commit is contained in:
41
public/app/core/components/AlertBox/AlertBox.tsx
Normal file
41
public/app/core/components/AlertBox/AlertBox.tsx
Normal file
@@ -0,0 +1,41 @@
|
||||
import React, { FunctionComponent } from 'react';
|
||||
import { AppNotificationSeverity } from 'app/types';
|
||||
|
||||
interface Props {
|
||||
title: string;
|
||||
icon?: string;
|
||||
text?: string;
|
||||
severity: AppNotificationSeverity;
|
||||
onClose?: () => void;
|
||||
}
|
||||
|
||||
function getIconFromSeverity(severity: AppNotificationSeverity): string {
|
||||
switch (severity) {
|
||||
case AppNotificationSeverity.Error: {
|
||||
return 'fa fa-exclamation-triangle';
|
||||
}
|
||||
case AppNotificationSeverity.Success: {
|
||||
return 'fa fa-check';
|
||||
}
|
||||
default: return null;
|
||||
}
|
||||
}
|
||||
|
||||
export const AlertBox: FunctionComponent<Props> = ({ title, icon, text, severity, onClose }) => {
|
||||
return (
|
||||
<div className={`alert alert-${severity}`}>
|
||||
<div className="alert-icon">
|
||||
<i className={icon || getIconFromSeverity(severity)} />
|
||||
</div>
|
||||
<div className="alert-body">
|
||||
<div className="alert-title">{title}</div>
|
||||
{text && <div className="alert-text">{text}</div>}
|
||||
</div>
|
||||
{onClose && (
|
||||
<button type="button" className="alert-close" onClick={onClose}>
|
||||
<i className="fa fa fa-remove" />
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
};
|
||||
@@ -1,5 +1,6 @@
|
||||
import React, { Component } from 'react';
|
||||
import { AppNotification } from 'app/types';
|
||||
import { AlertBox } from '../AlertBox/AlertBox';
|
||||
|
||||
interface Props {
|
||||
appNotification: AppNotification;
|
||||
@@ -22,18 +23,13 @@ export default class AppNotificationItem extends Component<Props> {
|
||||
const { appNotification, onClearNotification } = this.props;
|
||||
|
||||
return (
|
||||
<div className={`alert-${appNotification.severity} alert`}>
|
||||
<div className="alert-icon">
|
||||
<i className={appNotification.icon} />
|
||||
</div>
|
||||
<div className="alert-body">
|
||||
<div className="alert-title">{appNotification.title}</div>
|
||||
<div className="alert-text">{appNotification.text}</div>
|
||||
</div>
|
||||
<button type="button" className="alert-close" onClick={() => onClearNotification(appNotification.id)}>
|
||||
<i className="fa fa fa-remove" />
|
||||
</button>
|
||||
</div>
|
||||
<AlertBox
|
||||
severity={appNotification.severity}
|
||||
title={appNotification.title}
|
||||
text={appNotification.text}
|
||||
icon={appNotification.icon}
|
||||
onClose={() => onClearNotification(appNotification.id)}
|
||||
/>
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import _ from 'lodash';
|
||||
import { AppNotification, AppNotificationSeverity, AppNotificationTimeout } from 'app/types';
|
||||
import { getMessageFromError } from 'app/core/utils/errors';
|
||||
|
||||
const defaultSuccessNotification: AppNotification = {
|
||||
title: '',
|
||||
@@ -33,21 +33,10 @@ export const createSuccessNotification = (title: string, text?: string): AppNoti
|
||||
});
|
||||
|
||||
export const createErrorNotification = (title: string, text?: any): AppNotification => {
|
||||
// Handling if text is an error object
|
||||
if (text && !_.isString(text)) {
|
||||
if (text.message) {
|
||||
text = text.message;
|
||||
} else if (text.data && text.data.message) {
|
||||
text = text.data.message;
|
||||
} else {
|
||||
text = text.toString();
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
...defaultErrorNotification,
|
||||
title: title,
|
||||
text: text,
|
||||
text: getMessageFromError(text),
|
||||
id: Date.now(),
|
||||
};
|
||||
};
|
||||
|
||||
15
public/app/core/utils/errors.ts
Normal file
15
public/app/core/utils/errors.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import _ from 'lodash';
|
||||
|
||||
export function getMessageFromError(err: any): string | null {
|
||||
if (err && !_.isString(err)) {
|
||||
if (err.message) {
|
||||
return err.message;
|
||||
} else if (err.data && err.data.message) {
|
||||
return err.data.message;
|
||||
} else {
|
||||
return JSON.stringify(err);
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
Reference in New Issue
Block a user