From bc858ac13895dbef92e11fea5254a67deec28bcb Mon Sep 17 00:00:00 2001 From: BLasan Date: Mon, 25 Mar 2024 17:49:45 +0530 Subject: [PATCH 1/2] Add: Validation for Policy Upload --- .../PolicyForm/UploadPolicyDropzone.tsx | 22 +++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/portals/publisher/src/main/webapp/source/src/app/components/Apis/Details/Policies/PolicyForm/UploadPolicyDropzone.tsx b/portals/publisher/src/main/webapp/source/src/app/components/Apis/Details/Policies/PolicyForm/UploadPolicyDropzone.tsx index efdad40c26a..322e4f223a9 100644 --- a/portals/publisher/src/main/webapp/source/src/app/components/Apis/Details/Policies/PolicyForm/UploadPolicyDropzone.tsx +++ b/portals/publisher/src/main/webapp/source/src/app/components/Apis/Details/Policies/PolicyForm/UploadPolicyDropzone.tsx @@ -21,7 +21,7 @@ import { styled } from '@mui/material/styles'; import { List, IconButton , Theme } from '@mui/material'; import Typography from '@mui/material/Typography'; import Box from '@mui/material/Box'; -import { FormattedMessage } from 'react-intl'; +import { FormattedMessage, useIntl } from 'react-intl'; import ListItem from '@mui/material/ListItem'; import ListItemAvatar from '@mui/material/ListItemAvatar'; import ListItemSecondaryAction from '@mui/material/ListItemSecondaryAction'; @@ -35,10 +35,11 @@ import clsx from 'clsx'; import Icon from '@mui/material/Icon'; import { HelpOutline } from '@mui/icons-material'; import { green, red } from '@mui/material/colors'; - +import APIMAlert from 'AppComponents/Shared/Alert'; const PREFIX = 'UploadPolicyDropzone'; + const classes = { dropZoneWrapper: `${PREFIX}-dropZoneWrapper`, acceptDrop: `${PREFIX}-acceptDrop`, @@ -102,10 +103,23 @@ const UploadPolicyDropzone: FC = ({ policyDefinitionFile, setPolicyDefinitionFile, }) => { - + const intl = useIntl(); const handleDrop = (policyDefinition: any) => { - setPolicyDefinitionFile(policyDefinition); + if(policyDefinition===null || policyDefinition===undefined) { + APIMAlert.error(intl.formatMessage({ + id: 'Uploading.Policies.Error', + defaultMessage: 'Incompatible file type', + })); + } + if (policyDefinition[0].name.endsWith('.j2') || policyDefinition[0].name.endsWith('.xml')) { + setPolicyDefinitionFile(policyDefinition); + } else { + APIMAlert.error(intl.formatMessage({ + id: 'Uploading.Policies.Error', + defaultMessage: 'Incompatible file type', + })); + } }; const renderPolicyFileDropzone = () => { From 60ef32d5f2045f440955fd9c6045b6a84e81677a Mon Sep 17 00:00:00 2001 From: BLasan Date: Tue, 26 Mar 2024 00:56:12 +0530 Subject: [PATCH 2/2] Fix: Indentation Issue Simplified the logic --- .../PolicyForm/UploadPolicyDropzone.tsx | 20 ++++++++----------- 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/portals/publisher/src/main/webapp/source/src/app/components/Apis/Details/Policies/PolicyForm/UploadPolicyDropzone.tsx b/portals/publisher/src/main/webapp/source/src/app/components/Apis/Details/Policies/PolicyForm/UploadPolicyDropzone.tsx index 322e4f223a9..bb378a36ac0 100644 --- a/portals/publisher/src/main/webapp/source/src/app/components/Apis/Details/Policies/PolicyForm/UploadPolicyDropzone.tsx +++ b/portals/publisher/src/main/webapp/source/src/app/components/Apis/Details/Policies/PolicyForm/UploadPolicyDropzone.tsx @@ -106,20 +106,16 @@ const UploadPolicyDropzone: FC = ({ const intl = useIntl(); const handleDrop = (policyDefinition: any) => { - if(policyDefinition===null || policyDefinition===undefined) { - APIMAlert.error(intl.formatMessage({ - id: 'Uploading.Policies.Error', - defaultMessage: 'Incompatible file type', - })); - } - if (policyDefinition[0].name.endsWith('.j2') || policyDefinition[0].name.endsWith('.xml')) { + if (policyDefinition && policyDefinition[0] && + (policyDefinition[0].name.endsWith('.j2') || + policyDefinition[0].name.endsWith('.xml'))) { setPolicyDefinitionFile(policyDefinition); - } else { - APIMAlert.error(intl.formatMessage({ - id: 'Uploading.Policies.Error', - defaultMessage: 'Incompatible file type', - })); + return; } + APIMAlert.error(intl.formatMessage({ + id: 'Uploading.Policies.Error', + defaultMessage: 'Incompatible file type', + })); }; const renderPolicyFileDropzone = () => {