1
- import { useMachine } from "@xstate/react" ;
2
- import { isApiValidationError } from "api/errors" ;
3
1
import { useDashboard } from "components/Dashboard/DashboardProvider" ;
4
2
import { FullPageHorizontalForm } from "components/FullPageForm/FullPageHorizontalForm" ;
5
3
import { Loader } from "components/Loader/Loader" ;
6
- import { Stack } from "components/Stack/Stack" ;
7
4
import { useOrganizationId } from "hooks/useOrganizationId" ;
8
5
import { FC } from "react" ;
9
6
import { Helmet } from "react-helmet-async" ;
10
7
import { useNavigate , useSearchParams } from "react-router-dom" ;
11
8
import { pageTitle } from "utils/page" ;
12
- import { createTemplateMachine } from "xServices/createTemplate/createTemplateXService" ;
9
+ import { CreateTemplateData } from "xServices/createTemplate/createTemplateXService" ;
13
10
import { CreateTemplateForm } from "./CreateTemplateForm" ;
14
11
import { ErrorAlert } from "components/Alert/ErrorAlert" ;
12
+ import { useMutation , useQuery } from "@tanstack/react-query" ;
13
+ import {
14
+ createTemplate ,
15
+ templateByName ,
16
+ templateVersion ,
17
+ templateVersionVariables ,
18
+ } from "api/queries/templates" ;
19
+ import { ProvisionerType } from "api/typesGenerated" ;
20
+ import { calculateAutostopRequirementDaysValue } from "utils/schedule" ;
21
+
22
+ const provisioner : ProvisionerType =
23
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any -- Playwright needs to use a different provisioner type!
24
+ typeof ( window as any ) . playwright !== "undefined" ? "echo" : "terraform" ;
15
25
16
26
const CreateTemplatePage : FC = ( ) => {
17
27
const navigate = useNavigate ( ) ;
18
- const organizationId = useOrganizationId ( ) ;
19
28
const [ searchParams ] = useSearchParams ( ) ;
20
- const [ state , send ] = useMachine ( createTemplateMachine , {
21
- context : {
22
- organizationId,
23
- exampleId : searchParams . get ( "exampleId" ) ,
24
- templateNameToCopy : searchParams . get ( "fromTemplate" ) ,
25
- } ,
26
- actions : {
27
- onCreate : ( _ , { data } ) => {
28
- navigate ( `/templates/${ data . name } ` ) ;
29
- } ,
30
- } ,
31
- } ) ;
29
+ // const organizationId = useOrganizationId();
30
+ // const [state, send] = useMachine(createTemplateMachine, {
31
+ // context: {
32
+ // organizationId,
33
+ // exampleId: searchParams.get("exampleId"),
34
+ // templateNameToCopy: searchParams.get("fromTemplate"),
35
+ // },
36
+ // actions: {
37
+ // onCreate: (_, { data }) => {
38
+ // navigate(`/templates/${data.name}`);
39
+ // },
40
+ // },
41
+ // });
32
42
33
- const { starterTemplate, error, file, jobError, jobLogs, variables } =
34
- state . context ;
35
- const shouldDisplayForm = ! state . hasTag ( "loading" ) ;
36
- const { entitlements } = useDashboard ( ) ;
37
- const allowAdvancedScheduling =
38
- entitlements . features [ "advanced_template_scheduling" ] . enabled ;
39
- // Requires the template RBAC feature, otherwise disabling everyone access
40
- // means no one can access.
41
- const allowDisableEveryoneAccess =
42
- entitlements . features [ "template_rbac" ] . enabled ;
43
- const allowAutostopRequirement =
44
- entitlements . features [ "template_autostop_requirement" ] . enabled ;
43
+ // const { starterTemplate, error, file, jobError, jobLogs, variables } =
44
+ // state.context;
45
+ // const shouldDisplayForm = !state.hasTag("loading");
46
+ // const { entitlements } = useDashboard();
47
+ // const allowAdvancedScheduling =
48
+ // entitlements.features["advanced_template_scheduling"].enabled;
49
+ // // Requires the template RBAC feature, otherwise disabling everyone access
50
+ // // means no one can access.
51
+ // const allowDisableEveryoneAccess =
52
+ // entitlements.features["template_rbac"].enabled;
53
+ // const allowAutostopRequirement =
54
+ // entitlements.features["template_autostop_requirement"].enabled;
45
55
46
56
const onCancel = ( ) => {
47
57
navigate ( - 1 ) ;
@@ -54,7 +64,14 @@ const CreateTemplatePage: FC = () => {
54
64
</ Helmet >
55
65
56
66
< FullPageHorizontalForm title = "Create Template" onCancel = { onCancel } >
57
- { state . hasTag ( "loading" ) && < Loader /> }
67
+ { searchParams . has ( "fromTemplate" ) ? (
68
+ < DuplicateTemplateView />
69
+ ) : searchParams . has ( "exampleId" ) ? (
70
+ < ImportStaterTemplateView />
71
+ ) : (
72
+ < UploadTemplateView />
73
+ ) }
74
+ { /* {state.hasTag("loading") && <Loader />}
58
75
59
76
<Stack spacing={6}>
60
77
{Boolean(error) && !isApiValidationError(error) && (
@@ -92,10 +109,129 @@ const CreateTemplatePage: FC = () => {
92
109
logs={jobLogs}
93
110
/>
94
111
)}
95
- </ Stack >
112
+ </Stack> */ }
96
113
</ FullPageHorizontalForm >
97
114
</ >
98
115
) ;
99
116
} ;
100
117
118
+ const DuplicateTemplateView = ( ) => {
119
+ const navigate = useNavigate ( ) ;
120
+ const organizationId = useOrganizationId ( ) ;
121
+ const [ searchParams ] = useSearchParams ( ) ;
122
+ const templateByNameQuery = useQuery (
123
+ templateByName ( organizationId , searchParams . get ( "fromTemplate" ) ! ) ,
124
+ ) ;
125
+ const templateVersionQuery = useQuery ( {
126
+ ...templateVersion (
127
+ templateByNameQuery . data ?. template . active_version_id ?? "" ,
128
+ ) ,
129
+ enabled : templateByNameQuery . data !== undefined ,
130
+ } ) ;
131
+ const templateVersionVariablesQuery = useQuery ( {
132
+ ...templateVersionVariables (
133
+ templateByNameQuery . data ?. template . active_version_id ?? "" ,
134
+ ) ,
135
+ enabled : templateByNameQuery . data !== undefined ,
136
+ } ) ;
137
+ const isLoading =
138
+ templateByNameQuery . isLoading ||
139
+ templateVersionQuery . isLoading ||
140
+ templateVersionVariablesQuery . isLoading ;
141
+ const loadingError =
142
+ templateByNameQuery . error ||
143
+ templateVersionQuery . error ||
144
+ templateVersionVariablesQuery . error ;
145
+
146
+ const formEntitlements = useFormEntitlements ( ) ;
147
+
148
+ const createTemplateMutation = useMutation ( createTemplate ( ) ) ;
149
+
150
+ if ( isLoading ) {
151
+ return < Loader /> ;
152
+ }
153
+
154
+ if ( loadingError ) {
155
+ return < ErrorAlert error = { loadingError } /> ;
156
+ }
157
+
158
+ return (
159
+ < CreateTemplateForm
160
+ { ...formEntitlements }
161
+ copiedTemplate = { templateByNameQuery . data ! . template }
162
+ error = { createTemplateMutation . error }
163
+ isSubmitting = { createTemplateMutation . isLoading }
164
+ variables = { templateVersionVariablesQuery . data }
165
+ onCancel = { ( ) => navigate ( - 1 ) }
166
+ onSubmit = { async ( formData ) => {
167
+ const template = await createTemplateMutation . mutateAsync ( {
168
+ organizationId,
169
+ version : {
170
+ storage_method : "file" ,
171
+ file_id : templateVersionQuery . data ! . job . file_id ,
172
+ provisioner : provisioner ,
173
+ tags : { } ,
174
+ } ,
175
+ data : prepareData ( formData ) ,
176
+ } ) ;
177
+ navigate ( `/templates/${ template . name } ` ) ;
178
+ } }
179
+
180
+ // jobError={jobError}
181
+ // logs={jobLogs}
182
+ />
183
+ ) ;
184
+ } ;
185
+
186
+ const useFormEntitlements = ( ) => {
187
+ const { entitlements } = useDashboard ( ) ;
188
+ const allowAdvancedScheduling =
189
+ entitlements . features [ "advanced_template_scheduling" ] . enabled ;
190
+ // Requires the template RBAC feature, otherwise disabling everyone access
191
+ // means no one can access.
192
+ const allowDisableEveryoneAccess =
193
+ entitlements . features [ "template_rbac" ] . enabled ;
194
+ const allowAutostopRequirement =
195
+ entitlements . features [ "template_autostop_requirement" ] . enabled ;
196
+
197
+ return {
198
+ allowAdvancedScheduling,
199
+ allowDisableEveryoneAccess,
200
+ allowAutostopRequirement,
201
+ } ;
202
+ } ;
203
+
204
+ const ImportStaterTemplateView = ( ) => {
205
+ return < div > Import</ div > ;
206
+ } ;
207
+
208
+ const UploadTemplateView = ( ) => {
209
+ return < div > Upload</ div > ;
210
+ } ;
211
+
212
+ const prepareData = ( formData : CreateTemplateData ) => {
213
+ const {
214
+ default_ttl_hours,
215
+ max_ttl_hours,
216
+ parameter_values_by_name,
217
+ allow_everyone_group_access,
218
+ autostop_requirement_days_of_week,
219
+ autostop_requirement_weeks,
220
+ ...safeTemplateData
221
+ } = formData ;
222
+
223
+ return {
224
+ ...safeTemplateData ,
225
+ disable_everyone_group_access : ! formData . allow_everyone_group_access ,
226
+ default_ttl_ms : formData . default_ttl_hours * 60 * 60 * 1000 , // Convert hours to ms
227
+ max_ttl_ms : formData . max_ttl_hours * 60 * 60 * 1000 , // Convert hours to ms
228
+ autostop_requirement : {
229
+ days_of_week : calculateAutostopRequirementDaysValue (
230
+ formData . autostop_requirement_days_of_week ,
231
+ ) ,
232
+ weeks : formData . autostop_requirement_weeks ,
233
+ } ,
234
+ } ;
235
+ } ;
236
+
101
237
export default CreateTemplatePage ;
0 commit comments