Skip to content

Commit

Permalink
Add condition when add custom email subject with only space
Browse files Browse the repository at this point in the history
  • Loading branch information
Puridach Wutthihathaithamrong committed May 12, 2024
1 parent 41ce35c commit cd5f364
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 8 deletions.
13 changes: 13 additions & 0 deletions superset-frontend/src/features/alerts/AlertReportModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,7 @@ export const TRANSLATIONS = {
CRONTAB_ERROR_TEXT: t('crontab'),
WORKING_TIMEOUT_ERROR_TEXT: t('working timeout'),
RECIPIENTS_ERROR_TEXT: t('recipients'),
EMAIL_SUBJECT_ERROR_TEXT: t('email subject'),
ERROR_TOOLTIP_MESSAGE: t(
'Not all required fields are complete. Please provide the following:',
),
Expand Down Expand Up @@ -492,6 +493,7 @@ const AlertReportModal: FunctionComponent<AlertReportModalProps> = ({
NotificationSetting[]
>([]);
const [emailSubject, setEmailSubject] = useState<string>('');
const [emailError, setEmailError] = useState(false);

const onNotificationAdd = () => {
setNotificationSettings([
Expand Down Expand Up @@ -1062,6 +1064,11 @@ const AlertReportModal: FunctionComponent<AlertReportModalProps> = ({
const validateNotificationSection = () => {
const hasErrors = !checkNotificationSettings();
const errors = hasErrors ? [TRANSLATIONS.RECIPIENTS_ERROR_TEXT] : [];

if (emailError){
errors.push(TRANSLATIONS.EMAIL_SUBJECT_ERROR_TEXT);
}

updateValidationStatus(Sections.Notification, errors);
};

Expand Down Expand Up @@ -1217,6 +1224,7 @@ const AlertReportModal: FunctionComponent<AlertReportModalProps> = ({
contentType,
notificationSettings,
conditionNotNull,
emailError,
]);
useEffect(() => {
enforceValidation();
Expand Down Expand Up @@ -1270,6 +1278,10 @@ const AlertReportModal: FunctionComponent<AlertReportModalProps> = ({
}
};

const handleErrorUpdate = (hasError: boolean) => {
setEmailError(hasError);
};

return (
<StyledModal
className="no-content-padding"
Expand Down Expand Up @@ -1720,6 +1732,7 @@ const AlertReportModal: FunctionComponent<AlertReportModalProps> = ({
onInputChange={onInputChange}
email_subject={currentAlert?.email_subject || ''}
defaultSubject={emailSubject || ''}
setErrorSubject={handleErrorUpdate}
/>
</StyledNotificationMethodWrapper>
))}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ const StyledNotificationMethod = styled.div`
textarea {
height: auto;
}
&.error {
input {
border-color: ${({ theme }) => theme.colors.error.base};
}
}
}
.inline-container {
Expand All @@ -56,10 +62,14 @@ interface NotificationMethodProps {
) => void;
email_subject: string;
defaultSubject: string;
setErrorSubject: (hasError: boolean) => void;
}

const TRANSLATIONS = {
EMAIL_SUBJECT_NAME: t('Email subject name (optional)'),
EMAIL_SUBJECT_ERROR_TEXT: t(
'Please enter valid text. Spaces alone are not permitted.',
),
};

export const NotificationMethod: FunctionComponent<NotificationMethodProps> = ({
Expand All @@ -70,11 +80,13 @@ export const NotificationMethod: FunctionComponent<NotificationMethodProps> = ({
onInputChange,
email_subject,
defaultSubject,
setErrorSubject,
}) => {
const { method, recipients, options } = setting || {};
const [recipientValue, setRecipientValue] = useState<string>(
recipients || '',
);
const [error, setError] = useState(false);
const theme = useTheme();

if (!setting) {
Expand Down Expand Up @@ -115,9 +127,17 @@ export const NotificationMethod: FunctionComponent<NotificationMethodProps> = ({
const onSubjectChange = (
event: React.ChangeEvent<HTMLTextAreaElement | HTMLInputElement>,
) => {
const value = event.target.value;

if (onInputChange) {
onInputChange(event);
}

const hasError = value.length > 0 && value.trim().length === 0;
setError(hasError);
if (setErrorSubject) {
setErrorSubject(hasError);
}
};

// Set recipients
Expand Down Expand Up @@ -166,15 +186,25 @@ export const NotificationMethod: FunctionComponent<NotificationMethodProps> = ({
<div className="control-label">
{TRANSLATIONS.EMAIL_SUBJECT_NAME}
</div>
<div className="input-container">
<div className={`input-container ${error ? 'error' : ''}`}>
<input
type="text"
name="email_subject"
value={email_subject}
placeholder={defaultSubject}
onChange={onSubjectChange}
/>
</div>{' '}
</div>
{error && (
<div
style={{
color: theme.colors.error.base,
fontSize: theme.gridUnit * 3,
}}
>
{TRANSLATIONS.EMAIL_SUBJECT_ERROR_TEXT}
</div>
)}
</>
) : null}
</StyledInputContainer>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@
# under the License.
"""add subject column to report schedule
Revision ID: 100e3853bb0c
Revises: 5f57af97bc3f
Create Date: 2024-05-05 17:19:37.789891
Revision ID: 9621c6d56ffb
Revises: 4081be5b6b74
Create Date: 2024-05-10 11:09:12.046862
"""

import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision = "100e3853bb0c"
down_revision = "5f57af97bc3f"
revision = '9621c6d56ffb'
down_revision = '4081be5b6b74'


def upgrade():
Expand All @@ -38,4 +38,4 @@ def upgrade():


def downgrade():
op.drop_column("report_schedule", "email_subject")
op.drop_column("report_schedule", "email_subject")

0 comments on commit cd5f364

Please sign in to comment.