fix(alerts/reports): ensure correct type is sent to api (#12189)

* fix(alerts/reports): ensure correct type is sent to api

* fix: chane actions labels to generic Edit/Delete

* fix: ts-ignore

* fix: add more dependencies to callback functions

* fix: remove duplicate api calls when toggling between types

* lint
This commit is contained in:
ʈᵃᵢ
2020-12-23 04:38:11 -08:00
committed by GitHub
parent e5e9a5f05c
commit 3fb4e54690
5 changed files with 120 additions and 115 deletions

View File

@@ -51,6 +51,7 @@ export {
Switch,
Tabs,
Tooltip,
Input as AntdInput,
} from 'antd';
export { TreeProps } from 'antd/lib/tree';
export { FormInstance } from 'antd/lib/form';

View File

@@ -17,7 +17,7 @@
* under the License.
*/
import React, { useState, useMemo, useEffect } from 'react';
import React, { useState, useMemo } from 'react';
import { useHistory } from 'react-router-dom';
import { t, SupersetClient, makeApi, styled } from '@superset-ui/core';
import moment from 'moment';
@@ -183,10 +183,6 @@ function AlertList({
}
};
useEffect(() => {
refreshData();
}, [isReportEnabled]);
const columns = useMemo(
() => [
{
@@ -303,7 +299,7 @@ function AlertList({
canEdit
? {
label: 'edit-action',
tooltip: t('Edit Alert'),
tooltip: t('Edit'),
placement: 'bottom',
icon: 'edit' as IconName,
onClick: handleEdit,
@@ -312,7 +308,7 @@ function AlertList({
canDelete
? {
label: 'delete-action',
tooltip: t('Delete Alert'),
tooltip: t('Delete'),
placement: 'bottom',
icon: 'trash' as IconName,
onClick: handleDelete,

View File

@@ -554,6 +554,7 @@ const AlertReportModal: FunctionComponent<AlertReportModalProps> = ({
const data: any = {
...currentAlert,
type: isReport ? 'Report' : 'Alert',
validator_type: conditionNotNull ? 'not null' : 'operator',
validator_config_json: conditionNotNull
? {}
@@ -973,7 +974,6 @@ const AlertReportModal: FunctionComponent<AlertReportModalProps> = ({
owners: [],
recipients: [],
sql: '',
type: isReport ? 'Report' : 'Alert',
validator_config_json: {},
validator_type: '',
});

View File

@@ -19,7 +19,7 @@
import React, { useState, useCallback, useRef, FunctionComponent } from 'react';
import { t, useTheme } from '@superset-ui/core';
import { Input } from 'src/common/components';
import { Input, AntdInput } from 'src/common/components';
import { Radio } from 'src/common/components/Radio';
import { CronPicker, CronError } from 'src/common/components/CronPicker';
import { StyledInputContainer } from '../AlertReportModal';
@@ -34,8 +34,7 @@ export const AlertReportCronScheduler: FunctionComponent<AlertReportCronSchedule
onChange,
}) => {
const theme = useTheme();
// @ts-ignore
const inputRef = useRef<Input>(null);
const inputRef = useRef<AntdInput>(null);
const [scheduleFormat, setScheduleFormat] = useState<'picker' | 'input'>(
'picker',
);

View File

@@ -206,119 +206,128 @@ export function useSingleViewResource<D extends object = any>(
setState(currentState => ({ ...currentState, ...update }));
}
const fetchResource = useCallback((resourceID: number) => {
// Set loading state
updateState({
loading: true,
});
return SupersetClient.get({
endpoint: `/api/v1/${resourceName}/${resourceID}`,
})
.then(
({ json = {} }) => {
updateState({
resource: json.result,
error: null,
});
return json.result;
},
createErrorHandler(errMsg => {
handleErrorMsg(
t(
'An error occurred while fetching %ss: %s',
resourceLabel,
JSON.stringify(errMsg),
),
);
updateState({
error: errMsg,
});
}),
)
.finally(() => {
updateState({ loading: false });
const fetchResource = useCallback(
(resourceID: number) => {
// Set loading state
updateState({
loading: true,
});
}, []);
const createResource = useCallback((resource: D) => {
// Set loading state
updateState({
loading: true,
});
return SupersetClient.get({
endpoint: `/api/v1/${resourceName}/${resourceID}`,
})
.then(
({ json = {} }) => {
updateState({
resource: json.result,
error: null,
});
return json.result;
},
createErrorHandler(errMsg => {
handleErrorMsg(
t(
'An error occurred while fetching %ss: %s',
resourceLabel,
JSON.stringify(errMsg),
),
);
return SupersetClient.post({
endpoint: `/api/v1/${resourceName}/`,
body: JSON.stringify(resource),
headers: { 'Content-Type': 'application/json' },
})
.then(
({ json = {} }) => {
updateState({
resource: json.result,
error: null,
});
return json.id;
},
createErrorHandler(errMsg => {
handleErrorMsg(
t(
'An error occurred while creating %ss: %s',
resourceLabel,
JSON.stringify(errMsg),
),
);
updateState({
error: errMsg,
});
}),
)
.finally(() => {
updateState({ loading: false });
});
},
[handleErrorMsg, resourceName, resourceLabel],
);
updateState({
error: errMsg,
});
}),
)
.finally(() => {
updateState({ loading: false });
const createResource = useCallback(
(resource: D) => {
// Set loading state
updateState({
loading: true,
});
}, []);
const updateResource = useCallback((resourceID: number, resource: D) => {
// Set loading state
updateState({
loading: true,
});
return SupersetClient.post({
endpoint: `/api/v1/${resourceName}/`,
body: JSON.stringify(resource),
headers: { 'Content-Type': 'application/json' },
})
.then(
({ json = {} }) => {
updateState({
resource: json.result,
error: null,
});
return json.id;
},
createErrorHandler(errMsg => {
handleErrorMsg(
t(
'An error occurred while creating %ss: %s',
resourceLabel,
JSON.stringify(errMsg),
),
);
return SupersetClient.put({
endpoint: `/api/v1/${resourceName}/${resourceID}`,
body: JSON.stringify(resource),
headers: { 'Content-Type': 'application/json' },
})
.then(
({ json = {} }) => {
updateState({
resource: json.result,
error: null,
});
return json.result;
},
createErrorHandler(errMsg => {
handleErrorMsg(
t(
'An error occurred while fetching %ss: %s',
resourceLabel,
JSON.stringify(errMsg),
),
);
updateState({
error: errMsg,
});
}),
)
.finally(() => {
updateState({ loading: false });
});
},
[handleErrorMsg, resourceName, resourceLabel],
);
updateState({
error: errMsg,
});
return errMsg;
}),
)
.finally(() => {
updateState({ loading: false });
const updateResource = useCallback(
(resourceID: number, resource: D) => {
// Set loading state
updateState({
loading: true,
});
}, []);
return SupersetClient.put({
endpoint: `/api/v1/${resourceName}/${resourceID}`,
body: JSON.stringify(resource),
headers: { 'Content-Type': 'application/json' },
})
.then(
({ json = {} }) => {
updateState({
resource: json.result,
error: null,
});
return json.result;
},
createErrorHandler(errMsg => {
handleErrorMsg(
t(
'An error occurred while fetching %ss: %s',
resourceLabel,
JSON.stringify(errMsg),
),
);
updateState({
error: errMsg,
});
return errMsg;
}),
)
.finally(() => {
updateState({ loading: false });
});
},
[handleErrorMsg, resourceName, resourceLabel],
);
return {
state,