Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 60 additions & 41 deletions ui-cra/src/components/Templates/Form/Partials/CostEstimation.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import { createStyles, Grid, makeStyles } from '@material-ui/core';
import { Alert } from '@material-ui/lab';
import { Button, LoadingPage, theme } from '@weaveworks/weave-gitops';
import React, { FC } from 'react';
import { validateFormData } from '../../../../utils/form';

const CostEstimation: FC<{
costEstimate: string;
isCostEstimationLoading: boolean;
costEstimateMessage: string;
handleCostEstimation: () => Promise<void>;
}> = ({ handleCostEstimation, costEstimate, isCostEstimationLoading }) => {
}> = ({
handleCostEstimation,
costEstimate,
isCostEstimationLoading,
costEstimateMessage,
}) => {
const useStyles = makeStyles(() =>
createStyles({
getEstimationButton: {
Expand All @@ -20,54 +27,66 @@ const CostEstimation: FC<{
previewLoading: {
padding: theme.spacing.base,
},
errorMessage: {
marginTop: theme.spacing.xs,
marginRight: theme.spacing.medium,
borderRadius: theme.spacing.xxs,
},
}),
);
const classes = useStyles();
return isCostEstimationLoading ? (
<LoadingPage className={classes.previewLoading} />
) : (
<div>
return (
<>
<h2>Cost Estimation</h2>
<Grid container>
<Grid item xs={6} sm={6} md={6} lg={6}>
<Grid container>
<Grid
item
xs={6}
justifyContent="flex-start"
alignItems="center"
container
>
<div>Monthly Cost:</div>
</Grid>
<Grid
item
xs={6}
justifyContent="flex-end"
alignItems="center"
container
>
<div className={classes.costWrapper}>{costEstimate}</div>
{isCostEstimationLoading ? (
<LoadingPage className={classes.previewLoading} />
) : (
<Grid alignItems="center" container>
<Grid item xs={6} sm={6} md={6} lg={6}>
<Grid container>
<Grid
item
xs={6}
justifyContent="flex-start"
alignItems="center"
container
>
<div>Monthly Cost:</div>
</Grid>
<Grid
item
xs={6}
justifyContent="flex-end"
alignItems="center"
container
>
<div className={classes.costWrapper}>{costEstimate}</div>
</Grid>
</Grid>
</Grid>
</Grid>
<Grid
item
xs={6}
justifyContent="flex-end"
alignItems="center"
container
>
<Button
id="get-estimation"
className={classes.getEstimationButton}
onClick={event => validateFormData(event, handleCostEstimation)}
<Grid
item
xs={6}
justifyContent="flex-end"
alignItems="center"
container
>
GET ESTIMATION
</Button>
<Button
id="get-estimation"
className={classes.getEstimationButton}
onClick={event => validateFormData(event, handleCostEstimation)}
>
GET ESTIMATION
</Button>
</Grid>
{costEstimateMessage && (
<Alert className={classes.errorMessage} severity="warning">
{costEstimateMessage}
</Alert>
)}
</Grid>
</Grid>
</div>
)}
</>
);
};

Expand Down
4 changes: 4 additions & 0 deletions ui-cra/src/components/Templates/Form/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ const ResourceForm: FC<ResourceFormProps> = ({ template, resource }) => {
const [costEstimationLoading, setCostEstimationLoading] =
useState<boolean>(false);
const [costEstimate, setCostEstimate] = useState<string>('00.00 USD');
const [costEstimateMessage, setCostEstimateMessage] = useState<string>('');
const [enableCreatePR, setEnableCreatePR] = useState<boolean>(false);

// get the cost estimate feature flag
Expand Down Expand Up @@ -364,6 +365,7 @@ const ResourceForm: FC<ResourceFormProps> = ({ template, resource }) => {
.then(data => {
const { costEstimate } = data;
setCostEstimate(getFormattedCostEstimate(costEstimate));
setCostEstimateMessage(costEstimate?.message || '');
})
.catch(err =>
setNotifications([
Expand Down Expand Up @@ -531,6 +533,7 @@ const ResourceForm: FC<ResourceFormProps> = ({ template, resource }) => {
handleCostEstimation={handleCostEstimation}
costEstimate={costEstimate}
isCostEstimationLoading={costEstimationLoading}
costEstimateMessage={costEstimateMessage}
/>
) : null}
</Grid>
Expand Down Expand Up @@ -579,6 +582,7 @@ const ResourceForm: FC<ResourceFormProps> = ({ template, resource }) => {
costEstimationLoading,
handleCostEstimation,
costEstimate,
costEstimateMessage,
isCredentialEnabled,
isCostEstimationEnabled,
isKustomizationsEnabled,
Expand Down
10 changes: 7 additions & 3 deletions ui-cra/src/utils/formatters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,13 @@ export const getFormattedCostEstimate = (
});
if (costEstimate) {
const { currency, range } = costEstimate;
const estimate = `${costFormatter.format(
range?.low || 0,
)} - ${costFormatter.format(range?.high || 0)} ${currency}`;
const lowFormated = costFormatter.format(range?.low || 0);
const highFormated = costFormatter.format(range?.high || 0);

const estimate =
(lowFormated === highFormated
? `${lowFormated}`
: `${lowFormated} - ${highFormated}`) + ` ${currency}`;
return estimate;
} else return 'N/A';
};