@adaptabletools/adaptable
Version:
Powerful AG Grid extension which provides advanced, cutting-edge functionality to meet all DataGrid requirements
151 lines (150 loc) • 8.58 kB
JavaScript
import { Fragment as _Fragment, jsx as _jsx } from "react/jsx-runtime";
import * as React from 'react';
import { useState, useCallback } from 'react';
import { OnePageAdaptableWizard, OnePageWizardSummary } from '../../Wizard/OnePageAdaptableWizard';
import { cloneObject } from '../../../Utilities/Helpers/Helper';
import { AlertScopeWizardSection } from './AlertScopeWizardSection';
import { AlertRulesWizardSection, getRuleStepDescription, renderAlertRulesSummary, } from './AlertRulesWizardSection';
import { isValidAlertRules } from './isValidAlertRules';
import { AlertBehaviourWizardSection, renderAlertBehaviourSummary, } from './AlertBehaviourWizardSection';
import { ALERT_DEFAULT_MESSAGE_TYPE } from '../../../Utilities/Constants/ObjectDefaultConstants';
import { AlertMessageWizardSection, renderAlertMessageSummary } from './AlertMessageWizardSection';
import { isValidAlertMessage } from './isValidAlertMessage';
import { renderScopeSummary } from './BaseAlertScopeWizardSection';
import { isScopeValid } from '../../Components/NewScopeComponent';
import { isRuleBasedAlertDefinition } from '../../../Utilities/Helpers/Scheduling/ScheduledAlertHelper';
import ObjectFactory from '../../../Utilities/ObjectFactory';
import { useDispatch } from 'react-redux';
import * as AlertRedux from '../../../Redux/ActionsReducers/AlertRedux';
import { useAdaptable } from '../../AdaptableContext';
import { ObjectTagsWizardSection, renderObjectTagsSummary, } from '../../Wizard/ObjectTagsWizardSection';
import { AlertTypeWizardSection, renderAlertTypeSummary, isSettingsValid, } from './AlertTypeWizardSection';
import { AlertType, getAlertType } from '../Utilities/getAlertType';
import { mapAlertDefinition } from '../Utilities/mapAlertDefinition';
import { getDefaultAlertDefinition } from '../Utilities/getDefaultAlertDefinition';
import { AlertNotificationWizardSection, renderAlertNotificationSummary, } from './AlertNotificationWizardSection';
import { AlertScheduledWizardSection, isScheduledAlertTimeValid, renderAlertScheduledSummary, } from './AlertScheduledWizardSection';
import { Box } from '../../../components/Flex';
export const AlertWizard = (props) => {
const { api } = useAdaptable();
const dispatch = useDispatch();
const behaviourSpelling = api.internalApi.getCorrectEnglishVariant('Behaviour');
const [alertDefinition, doSetAlertDefinition] = useState(() => {
const alertDefinition = props.data
? cloneObject(props.data)
: ObjectFactory.CreateEmptyAlertDefinition();
const initialType = getAlertType(alertDefinition);
if (initialType !== AlertType.Scheduled &&
'Rule' in alertDefinition &&
!alertDefinition.Rule.BooleanExpression &&
!alertDefinition.Rule.ObservableExpression &&
!alertDefinition.Rule.AggregatedBooleanExpression &&
!alertDefinition.Rule.Predicates) {
alertDefinition.Rule = {
BooleanExpression: '',
};
}
if (props.popupParams?.column &&
props.popupParams?.action === 'New' &&
'Scope' in alertDefinition) {
alertDefinition.Scope = {
ColumnIds: [props.popupParams.column.columnId],
};
}
alertDefinition.MessageType = alertDefinition.MessageType ?? ALERT_DEFAULT_MESSAGE_TYPE;
return alertDefinition;
});
const setAlertDefinition = useCallback((data) => {
doSetAlertDefinition(data);
}, []);
const [alertType, setAlertType] = useState(() => {
return getAlertType(alertDefinition) ?? AlertType.DataChange;
});
const handleFinish = () => {
if (props.data) {
dispatch(AlertRedux.AlertDefinitionEdit(alertDefinition));
}
else {
dispatch(AlertRedux.AlertDefinitionAdd(alertDefinition));
}
props.onCloseWizard();
};
const updateAlertDefinition = useCallback((newAlertDefinition) => {
doSetAlertDefinition(mapAlertDefinition(api, newAlertDefinition));
}, [api]);
const handleAlertTypeChange = (newAlertType) => {
setAlertType(newAlertType);
doSetAlertDefinition(getDefaultAlertDefinition(alertDefinition, newAlertType));
};
const defaultEditSectionName = React.useMemo(() => {
if (props.defaultCurrentSectionName || !props.data) {
return props.defaultCurrentSectionName;
}
const type = getAlertType(props.data);
return type === AlertType.Scheduled ? 'Schedule' : 'Rule';
}, [props.defaultCurrentSectionName, props.data]);
return (_jsx(OnePageAdaptableWizard, { defaultCurrentSectionName: defaultEditSectionName, moduleInfo: props.moduleInfo, modal: props.modal, data: alertDefinition, onHide: props.onCloseWizard, onFinish: handleFinish, sections: [
{
title: 'Name & Type',
isValid: isSettingsValid,
details: (_jsx(_Fragment, { children: "Enter Name and select and select an Alert Type" })),
render: () => (_jsx(Box, { className: "twa:p-2", children: _jsx(AlertTypeWizardSection, { alertType: alertType, onAlertTypeChange: handleAlertTypeChange, onChange: setAlertDefinition }) })),
renderSummary: (data) => renderAlertTypeSummary(alertType, data),
},
{
title: 'Schedule',
isVisible: () => alertType === AlertType.Scheduled,
isValid: (data) => isScheduledAlertTimeValid(data),
render: () => (_jsx(Box, { className: "twa:p-2", children: _jsx(AlertScheduledWizardSection, { onChange: setAlertDefinition }) })),
renderSummary: renderAlertScheduledSummary,
},
{
title: 'Trigger',
isVisible: () => alertType === AlertType.DataChange || alertType == AlertType.Validation,
details: (_jsx(_Fragment, { children: "Specify which columns should trigger the Alert" })),
isValid: (data) => isRuleBasedAlertDefinition(data) ? isScopeValid(data) : true,
render: () => (_jsx(AlertScopeWizardSection, { alertType: alertType, onChange: setAlertDefinition })),
renderSummary: renderScopeSummary,
},
{
title: 'Rule',
isVisible: () => alertType !== AlertType.Scheduled,
details: getRuleStepDescription(alertType),
isValid: isValidAlertRules,
render: () => (_jsx(AlertRulesWizardSection, { alertType: alertType, onChange: updateAlertDefinition, module: "alert" })),
renderSummary: renderAlertRulesSummary,
},
{
title: 'Message',
details: "Select the Alert's Message Type and Text",
isValid: (data) => isValidAlertMessage(data, alertType),
render: () => (_jsx(Box, { className: "twa:p-2", children: _jsx(AlertMessageWizardSection, { alertType: alertType, onChange: setAlertDefinition }) })),
renderSummary: (data) => renderAlertMessageSummary(alertType, data),
},
{
title: 'Notification',
details: 'Create a Notification for the Alert',
render: () => (_jsx(Box, { className: "twa:p-2", children: _jsx(AlertNotificationWizardSection, { alertType: alertType, onChange: setAlertDefinition }) })),
renderSummary: renderAlertNotificationSummary,
},
{
title: behaviourSpelling,
details: 'Configure ' + behaviourSpelling + 's for the Alert',
render: () => (_jsx(Box, { className: "twa:p-2", children: _jsx(AlertBehaviourWizardSection, { alertType: alertType, onChange: setAlertDefinition }) })),
renderSummary: renderAlertBehaviourSummary,
},
{
details: 'Select Alert Tags',
title: 'Tags',
isVisible: () => api.internalApi.shouldDisplayTagSections(),
render: () => (_jsx(Box, { className: "twa:p-2", children: _jsx(ObjectTagsWizardSection, { onChange: setAlertDefinition }) })),
renderSummary: renderObjectTagsSummary,
},
'-',
{
details: 'Review your Alert',
render: () => (_jsx(Box, { className: "twa:p-2", children: _jsx(OnePageWizardSummary, {}) })),
title: 'Summary',
},
] }));
};