Skip to content

Commit

Permalink
Refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
N00bG1rl committed Sep 25, 2023
1 parent 2dd67be commit 938d278
Showing 1 changed file with 13 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ import { PublicUrlFields } from '../../../components/form/publicUrlFields';
import { RestartMicroserviceDialog } from '../../../components/restartMicroserviceDialog';
import { SetupFields } from '../../../components/form/setupFields';

import { getRuntimeNumberFromString } from '../../../../../utils/helpers';

export type SetupSectionProps = {
application: HttpResponseApplication;
currentMicroservice: MicroserviceStore;
Expand All @@ -43,15 +41,11 @@ export const SetupSection = ({ application, currentMicroservice }: SetupSectionP
const microserviceEnvironment = currentMicroservice.environment;
const microserviceName = currentMicroservice.name;
const microserviceInfo = currentMicroservice.edit?.extra;
const canDelete = canDeleteMicroservice(application.environments, microserviceEnvironment, microserviceId);

const currentRuntimeImageNumber = {
value: microserviceInfo?.runtimeImage,
displayValue: getRuntimeNumberFromString(microserviceInfo?.runtimeImage)
};

const canDelete = canDeleteMicroservice(application.environments, microserviceEnvironment, microserviceId);
const availableEnvironments = application.environments.map(env => env.name);

const currentRuntimeImageNumber = microserviceInfo?.runtimeImage;
const hasPublicUrl = microserviceInfo?.isPublic || false;
const hasM3ConnectorOption = application.environments.find(env => env.name === microserviceEnvironment)?.connections?.m3Connector || false;
// Remove extra slash from ingress path as it is there already with startAdornment.
Expand All @@ -60,7 +54,7 @@ export const SetupSection = ({ application, currentMicroservice }: SetupSectionP
const headArgumentValues = microserviceInfo?.headCommand?.args?.map((arg: string) => ({ value: arg })) || [];

const handleMicroserviceEdit = async ({ microserviceName, headImage, runtimeVersion }: MicroserviceFormParameters) => {
if (microserviceName === currentMicroservice.name && headImage === microserviceInfo?.headImage && runtimeVersion === currentRuntimeImageNumber.value) {
if (microserviceName === currentMicroservice.name && headImage === microserviceInfo?.headImage && runtimeVersion === currentRuntimeImageNumber) {
return;
}

Expand All @@ -83,26 +77,24 @@ export const SetupSection = ({ application, currentMicroservice }: SetupSectionP
};

const handleMicroserviceDelete = async () => {
setIsLoading(true);
setDeleteDialogIsOpen(false);

if (!canDelete) {
enqueueSnackbar('Deleting microservice is disabled.', { variant: 'error' });
return;
}

const success = await deleteMicroservice(applicationId, microserviceEnvironment, microserviceId);
setIsLoading(true);
setDeleteDialogIsOpen(false);

if (!success) {
try {
await deleteMicroservice(applicationId, microserviceEnvironment, microserviceId);
enqueueSnackbar(`Microservice '${microserviceName}' has been deleted.`);
const href = `/microservices/application/${applicationId}/overview`;
navigate(href);
} catch (error) {
enqueueSnackbar(`Failed to delete microservice '${microserviceName}'.`, { variant: 'error' });
} finally {
setIsLoading(false);
return;
}

enqueueSnackbar(`Microservice '${microserviceName}' has been deleted.`);
const href = `/microservices/application/${applicationId}/overview`;
navigate(href);
setIsLoading(false);
};

return (
Expand Down Expand Up @@ -135,7 +127,7 @@ export const SetupSection = ({ application, currentMicroservice }: SetupSectionP
initialValues={{
microserviceName,
developmentEnvironment: microserviceEnvironment,
runtimeVersion: currentRuntimeImageNumber.value,
runtimeVersion: currentRuntimeImageNumber,
headImage: microserviceInfo?.headImage,
headPort: microserviceInfo?.headPort,
entrypoint: '',
Expand Down

0 comments on commit 938d278

Please sign in to comment.