@@ -4,6 +4,7 @@ import TextField from "@material-ui/core/TextField"
4
4
import {
5
5
ParameterSchema ,
6
6
ProvisionerJobLog ,
7
+ Template ,
7
8
TemplateExample ,
8
9
TemplateVersionVariable ,
9
10
} from "api/typesGenerated"
@@ -106,28 +107,74 @@ const defaultInitialValues: CreateTemplateData = {
106
107
allow_user_cancel_workspace_jobs : false ,
107
108
}
108
109
109
- const getInitialValues = (
110
- canSetMaxTTL : boolean ,
111
- starterTemplate ?: TemplateExample ,
112
- ) => {
110
+ type GetInitialValuesParams = {
111
+ fromExample ?: TemplateExample
112
+ fromCopy ?: Template
113
+ parameters ?: ParameterSchema [ ]
114
+ variables ?: TemplateVersionVariable [ ]
115
+ canSetMaxTTL : boolean
116
+ }
117
+
118
+ const getInitialValues = ( {
119
+ fromExample,
120
+ fromCopy,
121
+ canSetMaxTTL,
122
+ variables,
123
+ parameters,
124
+ } : GetInitialValuesParams ) => {
113
125
let initialValues = defaultInitialValues
126
+
114
127
if ( ! canSetMaxTTL ) {
115
128
initialValues = {
116
129
...initialValues ,
117
130
max_ttl_hours : 0 ,
118
131
}
119
132
}
120
- if ( ! starterTemplate ) {
121
- return initialValues
133
+
134
+ if ( fromExample ) {
135
+ initialValues = {
136
+ ...initialValues ,
137
+ name : fromExample . id ,
138
+ display_name : fromExample . name ,
139
+ icon : fromExample . icon ,
140
+ description : fromExample . description ,
141
+ }
142
+ }
143
+
144
+ if ( fromCopy ) {
145
+ initialValues = {
146
+ ...initialValues ,
147
+ ...fromCopy ,
148
+ name : `${ fromCopy . name } -copy` ,
149
+ display_name : fromCopy . display_name
150
+ ? `Copy of ${ fromCopy . display_name } `
151
+ : "" ,
152
+ }
153
+ }
154
+
155
+ if ( variables ) {
156
+ variables . forEach ( ( variable ) => {
157
+ if ( ! initialValues . user_variable_values ) {
158
+ initialValues . user_variable_values = [ ]
159
+ }
160
+ initialValues . user_variable_values . push ( {
161
+ name : variable . name ,
162
+ value : variable . sensitive ? "" : variable . value ,
163
+ } )
164
+ } )
122
165
}
123
166
124
- return {
125
- ...initialValues ,
126
- name : starterTemplate . id ,
127
- display_name : starterTemplate . name ,
128
- icon : starterTemplate . icon ,
129
- description : starterTemplate . description ,
167
+ if ( parameters ) {
168
+ parameters . forEach ( ( parameter ) => {
169
+ if ( ! initialValues . parameter_values_by_name ) {
170
+ initialValues . parameter_values_by_name = { }
171
+ }
172
+ initialValues . parameter_values_by_name [ parameter . name ] =
173
+ parameter . default_source_value
174
+ } )
130
175
}
176
+
177
+ return initialValues
131
178
}
132
179
133
180
export interface CreateTemplateFormProps {
@@ -142,12 +189,14 @@ export interface CreateTemplateFormProps {
142
189
jobError ?: string
143
190
logs ?: ProvisionerJobLog [ ]
144
191
canSetMaxTTL : boolean
192
+ copiedTemplate ?: Template
145
193
}
146
194
147
195
export const CreateTemplateForm : FC < CreateTemplateFormProps > = ( {
148
196
onCancel,
149
197
onSubmit,
150
198
starterTemplate,
199
+ copiedTemplate,
151
200
parameters,
152
201
variables,
153
202
isSubmitting,
@@ -159,7 +208,13 @@ export const CreateTemplateForm: FC<CreateTemplateFormProps> = ({
159
208
} ) => {
160
209
const styles = useStyles ( )
161
210
const form = useFormik < CreateTemplateData > ( {
162
- initialValues : getInitialValues ( canSetMaxTTL , starterTemplate ) ,
211
+ initialValues : getInitialValues ( {
212
+ canSetMaxTTL,
213
+ fromExample : starterTemplate ,
214
+ fromCopy : copiedTemplate ,
215
+ variables,
216
+ parameters,
217
+ } ) ,
163
218
validationSchema,
164
219
onSubmit,
165
220
} )
@@ -177,6 +232,8 @@ export const CreateTemplateForm: FC<CreateTemplateFormProps> = ({
177
232
< FormFields >
178
233
{ starterTemplate ? (
179
234
< SelectedTemplate template = { starterTemplate } />
235
+ ) : copiedTemplate ? (
236
+ < SelectedTemplate template = { copiedTemplate } />
180
237
) : (
181
238
< TemplateUpload
182
239
{ ...upload }
@@ -329,7 +386,7 @@ export const CreateTemplateForm: FC<CreateTemplateFormProps> = ({
329
386
</ FormSection >
330
387
331
388
{ /* Parameters */ }
332
- { parameters && (
389
+ { parameters && parameters . length > 0 && (
333
390
< FormSection
334
391
title = { t ( "form.parameters.title" ) }
335
392
description = { t ( "form.parameters.description" ) }
@@ -353,21 +410,22 @@ export const CreateTemplateForm: FC<CreateTemplateFormProps> = ({
353
410
) }
354
411
355
412
{ /* Variables */ }
356
- { variables && (
413
+ { variables && variables . length > 0 && (
357
414
< FormSection
358
415
title = "Variables"
359
416
description = "Input variables allow you to customize templates without altering their source code."
360
417
>
361
418
< FormFields >
362
419
{ variables . map ( ( variable , index ) => (
363
420
< VariableInput
421
+ defaultValue = { variable . value }
364
422
variable = { variable }
365
423
disabled = { isSubmitting }
366
424
key = { variable . name }
367
425
onChange = { async ( value ) => {
368
426
await form . setFieldValue ( "user_variable_values." + index , {
369
427
name : variable . name ,
370
- value : value ,
428
+ value,
371
429
} )
372
430
} }
373
431
/>
0 commit comments