Skip to content

Commit 9642e6d

Browse files
authored
add cost estimate message if available set constEstimate to single value if low and high are the same (#1845)
1 parent ed7b1e0 commit 9642e6d

File tree

3 files changed

+71
-44
lines changed

3 files changed

+71
-44
lines changed

ui-cra/src/components/Templates/Form/Partials/CostEstimation.tsx

Lines changed: 60 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
import { createStyles, Grid, makeStyles } from '@material-ui/core';
2+
import { Alert } from '@material-ui/lab';
23
import { Button, LoadingPage, theme } from '@weaveworks/weave-gitops';
34
import React, { FC } from 'react';
45
import { validateFormData } from '../../../../utils/form';
56

67
const CostEstimation: FC<{
78
costEstimate: string;
89
isCostEstimationLoading: boolean;
10+
costEstimateMessage: string;
911
handleCostEstimation: () => Promise<void>;
10-
}> = ({ handleCostEstimation, costEstimate, isCostEstimationLoading }) => {
12+
}> = ({
13+
handleCostEstimation,
14+
costEstimate,
15+
isCostEstimationLoading,
16+
costEstimateMessage,
17+
}) => {
1118
const useStyles = makeStyles(() =>
1219
createStyles({
1320
getEstimationButton: {
@@ -20,54 +27,66 @@ const CostEstimation: FC<{
2027
previewLoading: {
2128
padding: theme.spacing.base,
2229
},
30+
errorMessage: {
31+
marginTop: theme.spacing.xs,
32+
marginRight: theme.spacing.medium,
33+
borderRadius: theme.spacing.xxs,
34+
},
2335
}),
2436
);
2537
const classes = useStyles();
26-
return isCostEstimationLoading ? (
27-
<LoadingPage className={classes.previewLoading} />
28-
) : (
29-
<div>
38+
return (
39+
<>
3040
<h2>Cost Estimation</h2>
31-
<Grid container>
32-
<Grid item xs={6} sm={6} md={6} lg={6}>
33-
<Grid container>
34-
<Grid
35-
item
36-
xs={6}
37-
justifyContent="flex-start"
38-
alignItems="center"
39-
container
40-
>
41-
<div>Monthly Cost:</div>
42-
</Grid>
43-
<Grid
44-
item
45-
xs={6}
46-
justifyContent="flex-end"
47-
alignItems="center"
48-
container
49-
>
50-
<div className={classes.costWrapper}>{costEstimate}</div>
41+
{isCostEstimationLoading ? (
42+
<LoadingPage className={classes.previewLoading} />
43+
) : (
44+
<Grid alignItems="center" container>
45+
<Grid item xs={6} sm={6} md={6} lg={6}>
46+
<Grid container>
47+
<Grid
48+
item
49+
xs={6}
50+
justifyContent="flex-start"
51+
alignItems="center"
52+
container
53+
>
54+
<div>Monthly Cost:</div>
55+
</Grid>
56+
<Grid
57+
item
58+
xs={6}
59+
justifyContent="flex-end"
60+
alignItems="center"
61+
container
62+
>
63+
<div className={classes.costWrapper}>{costEstimate}</div>
64+
</Grid>
5165
</Grid>
5266
</Grid>
53-
</Grid>
54-
<Grid
55-
item
56-
xs={6}
57-
justifyContent="flex-end"
58-
alignItems="center"
59-
container
60-
>
61-
<Button
62-
id="get-estimation"
63-
className={classes.getEstimationButton}
64-
onClick={event => validateFormData(event, handleCostEstimation)}
67+
<Grid
68+
item
69+
xs={6}
70+
justifyContent="flex-end"
71+
alignItems="center"
72+
container
6573
>
66-
GET ESTIMATION
67-
</Button>
74+
<Button
75+
id="get-estimation"
76+
className={classes.getEstimationButton}
77+
onClick={event => validateFormData(event, handleCostEstimation)}
78+
>
79+
GET ESTIMATION
80+
</Button>
81+
</Grid>
82+
{costEstimateMessage && (
83+
<Alert className={classes.errorMessage} severity="warning">
84+
{costEstimateMessage}
85+
</Alert>
86+
)}
6887
</Grid>
69-
</Grid>
70-
</div>
88+
)}
89+
</>
7190
);
7291
};
7392

ui-cra/src/components/Templates/Form/index.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@ const ResourceForm: FC<ResourceFormProps> = ({ template, resource }) => {
300300
const [costEstimationLoading, setCostEstimationLoading] =
301301
useState<boolean>(false);
302302
const [costEstimate, setCostEstimate] = useState<string>('00.00 USD');
303+
const [costEstimateMessage, setCostEstimateMessage] = useState<string>('');
303304
const [enableCreatePR, setEnableCreatePR] = useState<boolean>(false);
304305

305306
// get the cost estimate feature flag
@@ -364,6 +365,7 @@ const ResourceForm: FC<ResourceFormProps> = ({ template, resource }) => {
364365
.then(data => {
365366
const { costEstimate } = data;
366367
setCostEstimate(getFormattedCostEstimate(costEstimate));
368+
setCostEstimateMessage(costEstimate?.message || '');
367369
})
368370
.catch(err =>
369371
setNotifications([
@@ -531,6 +533,7 @@ const ResourceForm: FC<ResourceFormProps> = ({ template, resource }) => {
531533
handleCostEstimation={handleCostEstimation}
532534
costEstimate={costEstimate}
533535
isCostEstimationLoading={costEstimationLoading}
536+
costEstimateMessage={costEstimateMessage}
534537
/>
535538
) : null}
536539
</Grid>
@@ -579,6 +582,7 @@ const ResourceForm: FC<ResourceFormProps> = ({ template, resource }) => {
579582
costEstimationLoading,
580583
handleCostEstimation,
581584
costEstimate,
585+
costEstimateMessage,
582586
isCredentialEnabled,
583587
isCostEstimationEnabled,
584588
isKustomizationsEnabled,

ui-cra/src/utils/formatters.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,13 @@ export const getFormattedCostEstimate = (
4242
});
4343
if (costEstimate) {
4444
const { currency, range } = costEstimate;
45-
const estimate = `${costFormatter.format(
46-
range?.low || 0,
47-
)} - ${costFormatter.format(range?.high || 0)} ${currency}`;
45+
const lowFormated = costFormatter.format(range?.low || 0);
46+
const highFormated = costFormatter.format(range?.high || 0);
47+
48+
const estimate =
49+
(lowFormated === highFormated
50+
? `${lowFormated}`
51+
: `${lowFormated} - ${highFormated}`) + ` ${currency}`;
4852
return estimate;
4953
} else return 'N/A';
5054
};

0 commit comments

Comments
 (0)