Skip to content

Commit

Permalink
Add Edit functionality for subscription policy
Browse files Browse the repository at this point in the history
  • Loading branch information
tharikaGitHub committed Jun 9, 2020
1 parent 60a7aa7 commit 8404ab6
Show file tree
Hide file tree
Showing 7 changed files with 174 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,7 @@
"Throttling.Subscription.Policy.policy.delete.error": "Subscription Rate Limiting Policy could not be deleted.",
"Throttling.Subscription.Policy.policy.delete.success": "Subscription Rate Limiting Policy successfully deleted.",
"Throttling.Subscription.Policy.policy.dialog.delete.error": "Subscription Rate Limiting Policy will be deleted.",
"Throttling.Subscription.Policy.policy.edit.success": "Subscription Rate Limiting Policy updated successfully.",
"Throttling.Subscription.Policy.search.default": "Subscription Rate Limiting Policies",
"Throttling.Subscription.billing.cycle": "Billing Cycle",
"Throttling.Subscription.currency": "Currency",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,7 @@
"Throttling.Subscription.Policy.policy.delete.error": "",
"Throttling.Subscription.Policy.policy.delete.success": "",
"Throttling.Subscription.Policy.policy.dialog.delete.error": "",
"Throttling.Subscription.Policy.policy.edit.success": "",
"Throttling.Subscription.Policy.search.default": "",
"Throttling.Subscription.billing.cycle": "",
"Throttling.Subscription.currency": "",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ const RouteMenuMapping = (intl) => [
defaultMessage: 'Subscription Policies',
}),
path: '/throttling/subscription',
component: DemoTable,
component: () => <SubscriptionThrottlingPolicies />,
icon: <PhonelinkSetupIcon />,
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,7 @@ import FormControlLabel from '@material-ui/core/FormControlLabel';
import FormControl from '@material-ui/core/FormControl';
import MenuItem from '@material-ui/core/MenuItem';
import Select from '@material-ui/core/Select';
// import DeleteForeverIcon from '@material-ui/icons/DeleteForever';
import Switch from '@material-ui/core/Switch';
// import Table from '@material-ui/core/Table';
// import TableBody from '@material-ui/core/TableBody';
// import TableCell from '@material-ui/core/TableCell';
// import TableRow from '@material-ui/core/TableRow';
// import AddCircle from '@material-ui/icons/AddCircle';
import HelpOutline from '@material-ui/icons/HelpOutline';
import CircularProgress from '@material-ui/core/CircularProgress';
import Tooltip from '@material-ui/core/Tooltip';
Expand Down Expand Up @@ -127,6 +121,8 @@ function reducer(state, newValue) {
...state,
graphQL: { ...state.graphQL, [field]: value },
};
case 'editDetails':
return value;
default:
return newValue;
}
Expand All @@ -140,7 +136,10 @@ function AddEdit(props) {
const classes = useStyles();
const intl = useIntl();
const [saving, setSaving] = useState(false);
const { history } = props;
const { history, match: { params } } = props;
const restApi = new API();
const isEdit = (params.id !== null) && (params.id !== undefined);

const [initialState, setInitialState] = useState({
policyName: '',
description: '',
Expand Down Expand Up @@ -174,10 +173,67 @@ function AddEdit(props) {
},
});

const [state, dispatch] = useReducer(reducer, initialState);

useEffect(() => {
if (isEdit) {
restApi.subscriptionThrottlingPolicyGet(params.id).then((result) => {
let requestCountEdit = '';
let dataAmountEdit = '';
let timeUnitEdit = 'min';
let unitTimeEdit = '';
let typeEdit = 'REQUESTCOUNTLIMIT';
let dataUnitEdit = 'KB';
if (result.body.defaultLimit.requestCount !== null) {
requestCountEdit = result.body.defaultLimit.requestCount.requestCount;
timeUnitEdit = result.body.defaultLimit.requestCount.timeUnit;
unitTimeEdit = result.body.defaultLimit.requestCount.unitTime;
typeEdit = result.body.defaultLimit.requestCount.type;
} else if (result.body.defaultLimit.bandwidth != null) {
dataAmountEdit = result.body.defaultLimit.bandwidth.dataAmount;
dataUnitEdit = result.body.defaultLimit.bandwidth.dataUnit;
timeUnitEdit = result.body.defaultLimit.bandwidth.timeUnit;
unitTimeEdit = result.body.defaultLimit.bandwidth.unitTime;
typeEdit = result.body.defaultLimit.bandwidth.type;
}
const editState = {
policyName: result.body.policyName,
description: result.body.description,
defaultLimit: {
requestCount: requestCountEdit,
timeUnit: timeUnitEdit,
unitTime: unitTimeEdit,
type: typeEdit,
dataAmount: dataAmountEdit,
dataUnit: dataUnitEdit,
},
rateLimitCount: (result.body.rateLimitCount === 0) ? '' : result.body.rateLimitCount,
rateLimitTimeUnit: (result.body.rateLimitCount === 0) ? 'sec' : result.body.rateLimitTimeUnit,
billingPlan: result.body.billingPlan,
monetization: {
monetizationPlan: 'FIXEDRATE',
fixedPrice: '',
pricePerRequest: '',
currencyType: '',
billingCycle: 'week',
},
customAttributes: result.body.customAttributes,
stopOnQuotaReach: result.body.stopOnQuotaReach,
permissions: {
roles: ['Internal/everyone'],
permissionStatus: 'allow',
},
graphQL: {
maxComplexity: result.body.graphQLMaxComplexity,
maxDepth: result.body.graphQLMaxDepth,
},
};
dispatch({ field: 'editDetails', value: editState });
});
}
setInitialState({
policyName: 'name',
description: 'desciption',
policyName: '',
description: '',
defaultLimit: {
requestCount: '',
timeUnit: 'min',
Expand Down Expand Up @@ -242,7 +298,6 @@ function AddEdit(props) {
return error;
};

const [state, dispatch] = useReducer(reducer, initialState);
const {
policyName,
description,
Expand Down Expand Up @@ -274,7 +329,6 @@ function AddEdit(props) {
maxDepth,
},
} = state;
const restApi = new API();

const onChange = (e) => {
dispatch({ field: e.target.name, value: e.target.value });
Expand Down Expand Up @@ -306,6 +360,8 @@ function AddEdit(props) {
return (false);
}
let subscriptionThrottlingPolicy;
let promisedAddSubscriptionPolicy;

if (type === 'REQUESTCOUNTLIMIT') {
subscriptionThrottlingPolicy = {
policyName: state.policyName,
Expand Down Expand Up @@ -364,30 +420,58 @@ function AddEdit(props) {
},
};
}
const promisedAddSubscriptionPolicy = restApi.addSubscriptionThrottlingPolicy(
subscriptionThrottlingPolicy,
);
if (promisedAddSubscriptionPolicy) {
setSaving(true);

if (isEdit) {
const policyId = params.id;
promisedAddSubscriptionPolicy = restApi.updateSubscriptionThrottlingPolicy(policyId,
subscriptionThrottlingPolicy);
if (promisedAddSubscriptionPolicy) {
setSaving(true);
}
return promisedAddSubscriptionPolicy
.then(() => {
Alert.success(intl.formatMessage({
id: 'Throttling.Subscription.Policy.policy.edit.success',
defaultMessage: 'Subscription Rate Limiting Policy updated successfully.',
}));
history.push('/throttling/subscription');
})
.catch((error) => {
const { response } = error;
if (response.body) {
Alert.error(response.body.description);
}
return null;
})
.finally(() => {
setSaving(false);
});
} else {
promisedAddSubscriptionPolicy = restApi.addSubscriptionThrottlingPolicy(
subscriptionThrottlingPolicy,
);
if (promisedAddSubscriptionPolicy) {
setSaving(true);
}
return promisedAddSubscriptionPolicy
.then(() => {
Alert.success(intl.formatMessage({
id: 'Throttling.Subscription.Policy.policy.add.success',
defaultMessage: 'Subscription Rate Limiting Policy added successfully.',
}));
history.push('/throttling/subscription');
})
.catch((error) => {
const { response } = error;
if (response.body) {
Alert.error(response.body.description);
}
return null;
})
.finally(() => {
setSaving(false);
});
}
return promisedAddSubscriptionPolicy
.then(() => {
Alert.success(intl.formatMessage({
id: 'Throttling.Subscription.Policy.policy.add.success',
defaultMessage: 'Subscription Rate Limiting Policy added successfully.',
}));
history.push('/throttling/subscription');
})
.catch((error) => {
const { response } = error;
if (response.body) {
Alert.error(response.body.description);
}
return null;
})
.finally(() => {
setSaving(false);
});
};

return (
Expand Down Expand Up @@ -421,6 +505,7 @@ function AddEdit(props) {
margin='dense'
name='policyName'
value={policyName}
disabled={isEdit}
onChange={onChange}
InputProps={{
id: 'policyName',
Expand Down Expand Up @@ -1036,9 +1121,22 @@ function AddEdit(props) {
);
}

AddEdit.defaultProps = {
match: {
params: {
id: null,
},
},
};

AddEdit.propTypes = {
classes: PropTypes.shape({}).isRequired,
history: PropTypes.shape({ push: PropTypes.func }).isRequired,
match: PropTypes.shape({
params: PropTypes.shape({
id: PropTypes.string,
}),
}),
};

export default AddEdit;
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import ListBase from 'AppComponents/AdminPages/Addons/ListBase';
import DescriptionIcon from '@material-ui/icons/Description';
import Link from '@material-ui/core/Link';
import Configurations from 'Config';
import AddEdit from 'AppComponents/Throttling/Application/AddEdit';
import Delete from 'AppComponents/Throttling/Subscription/Delete';
import API from 'AppData/api';
import EditIcon from '@material-ui/icons/Edit';
Expand Down Expand Up @@ -191,6 +190,12 @@ export default function ListSubscriptionThrottlingPolicies() {
sort: false,
},
},
{ // Id column has to be always the last.
name: 'policyId',
options: {
display: false,
},
},
];

const emptyBoxProps = {
Expand Down Expand Up @@ -283,9 +288,9 @@ export default function ListSubscriptionThrottlingPolicies() {
editComponentProps={{
icon: <EditIcon />,
title: 'Edit Subscription Policy',
routeTo: '/throttling/subscription/',
}}
DeleteComponent={Delete}
EditComponent={AddEdit}
/>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import React from 'react';
import { withRouter, Switch, Route } from 'react-router-dom';
import SubscriptionThrottlingPolicies from 'AppComponents/Throttling/Subscription/List';
import AddEdit from 'AppComponents/Throttling/Subscription/AddEdit';
import ResourceNotFound from 'AppComponents/Base/Errors/ResourceNotFound';

/**
* Render a list
Expand All @@ -30,6 +31,8 @@ function Tasks() {
<Switch>
<Route exact path='/throttling/subscription' component={SubscriptionThrottlingPolicies} />
<Route exact path='/throttling/subscription/add' component={AddEdit} />
<Route path='/throttling/subscription/:id' component={AddEdit} />
<Route component={ResourceNotFound} />
</Switch>
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,35 @@ class API extends Resource {
});
}

/**
* Update a Subscription Throttling Policy
*/
updateSubscriptionThrottlingPolicy(policyId, body) {
return this.client.then((client) => {
const payload = {
policyId: policyId,
body,
'Content-Type': 'application/json',
};
return client.apis['Subscription Policy (Individual)'].put_throttling_policies_subscription__policyId_(
payload,
this._requestMetaData(),
);
});
}

/**
* Get details of a Subscription Throttling Policy
*/
subscriptionThrottlingPolicyGet(policyId) {
return this.client.then((client) => {
return client.apis['Subscription Policy (Individual)'].get_throttling_policies_subscription__policyId_(
{ policyId: policyId },
this._requestMetaData(),
);
});
}

/**
* Add an Application Throttling Policy
*/
Expand Down

0 comments on commit 8404ab6

Please sign in to comment.