-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFullViewSnapshotButton.tsx
189 lines (178 loc) · 5.99 KB
/
FullViewSnapshotButton.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import { createSnapshot, getBillingProfiles, snapshotCreateDetails } from 'actions/index';
import { TdrState } from 'reducers';
import {
Button,
Dialog,
DialogActions,
DialogTitle,
Typography,
FormLabel,
TextField,
} from '@mui/material';
import React, { Dispatch } from 'react';
import {
BillingProfileModel,
DatasetModel,
SnapshotRequestContentsModelModeEnum,
} from 'generated/tdr';
import { Action } from 'redux';
import { connect } from 'react-redux';
import { isEmpty, now, uniq } from 'lodash';
import TerraTooltip from './TerraTooltip';
import JadeDropdown from '../dataset/data/JadeDropdown';
import { useOnMount } from '../../libs/utils';
interface FullViewSnapshotButtonProps {
dispatch: Dispatch<Action>;
dataset: DatasetModel;
billingProfiles: Array<BillingProfileModel>;
}
function FullViewSnapshotButton({
dataset,
dispatch,
billingProfiles,
}: Readonly<FullViewSnapshotButtonProps>) {
useOnMount(() => {
dispatch(getBillingProfiles());
});
const defaultBillingProfile = billingProfiles.find(
(billingProfile) => billingProfile.id === dataset.defaultProfileId,
);
const [modalOpen, setModalOpen] = React.useState(false);
const [selectedBillingProfile, setSelectedBillingProfile] = React.useState(defaultBillingProfile);
const [snapshotName, setSnapshotName] = React.useState(
`Full_View_Snapshot_of_${dataset.name}_${now()}`,
);
const [snapshotDescription, setSnapshotDescription] = React.useState(
`Full View Snapshot of Dataset with Dataset name ${dataset.name}, and Dataset id ${dataset.id}.`,
);
// if the default billing profile is undefined or the user does not have permission on it, disable button and show tooltip
const isDisabled = billingProfiles.length === 0;
let tooltipText = '';
if (isDisabled) {
tooltipText = 'You do not have access to any billing profiles to create a snapshot';
}
const handleCreateFullViewSnapshot = () => {
dispatch(
snapshotCreateDetails(
snapshotName,
snapshotDescription,
SnapshotRequestContentsModelModeEnum.ByFullView,
dataset,
),
);
dispatch(createSnapshot(selectedBillingProfile?.id));
};
const onDismiss = () => setModalOpen(false);
const onSelect = () => {
handleCreateFullViewSnapshot();
setModalOpen(false);
};
return (
<>
<TerraTooltip title={isDisabled ? tooltipText : ''}>
<span>
<Button
variant="outlined"
disableElevation
onClick={() => {
setSelectedBillingProfile(defaultBillingProfile || billingProfiles[0]);
setModalOpen(true);
}}
disabled={isDisabled}
>
Create Full View Snapshot
</Button>
</span>
</TerraTooltip>
<Dialog fullWidth maxWidth="sm" onClose={onDismiss} open={modalOpen}>
<DialogTitle id="customized-dialog-title" sx={{ fontSize: '1rem' }}>
Creating snapshot - select a billing project
</DialogTitle>
<div style={{ padding: '0px 24px 16px 24px' }}>
<FormLabel sx={{ fontWeight: 600, color: 'black' }} htmlFor="snapshot-name" required>
Snapshot Name
</FormLabel>
<TextField
fullWidth
margin="normal"
id="snapshot-name"
label="Snapshot Name"
value={snapshotName}
onChange={(e) => setSnapshotName(e.target.value)}
/>
<FormLabel
sx={{ fontWeight: 600, color: 'black' }}
htmlFor="snapshot-description"
required
>
Snapshot Description
</FormLabel>
<TextField
fullWidth
margin="normal"
id="snapshot-description"
label="Snapshot Description"
value={snapshotDescription}
onChange={(e) => setSnapshotDescription(e.target.value)}
/>
<Typography sx={{ color: 'black' }}>
Do you want to use the Google Billing Project associated with this dataset or would you
like to select a different one?
</Typography>
<div style={{ marginTop: 8 }}>
<FormLabel
sx={{ fontWeight: 600, color: 'black' }}
htmlFor="billing-profile-select"
required
>
Google Billing Project
</FormLabel>
</div>
<JadeDropdown
sx={{ height: '2.5rem' }}
disabled={billingProfiles.length <= 1}
options={uniq(
billingProfiles
.filter((billingProfile) => billingProfile.profileName !== undefined)
.map((billingProfile) => billingProfile.profileName) as string[],
)}
name="billing-profile"
onSelectedItem={(event) =>
setSelectedBillingProfile(
billingProfiles.find(
(billingProfile) => billingProfile.profileName === event.target.value,
),
)
}
value={selectedBillingProfile?.profileName || ''}
/>
<Typography>If this is the correct billing project - just click select</Typography>
<DialogActions sx={{ display: 'flex', justifyContent: 'flex-end' }}>
<Button onClick={onDismiss} variant="outlined">
Cancel
</Button>
<Button
onClick={onSelect}
disabled={
selectedBillingProfile?.id === undefined ||
isEmpty(snapshotName) ||
isEmpty(snapshotDescription)
}
variant="contained"
data-cy="select-billing-profile-button"
>
Create
</Button>
</DialogActions>
</div>
</Dialog>
</>
);
}
function mapStateToProps(state: TdrState) {
return {
billingProfiles: state.profiles.profiles,
snapshot: state.snapshots.snapshot,
};
}
export default connect(mapStateToProps)(FullViewSnapshotButton);