Skip to content

Commit def3a36

Browse files
committed
Prefill variables
1 parent 10e87f5 commit def3a36

File tree

2 files changed

+42
-20
lines changed

2 files changed

+42
-20
lines changed

site/src/pages/CreateTemplatePage/CreateTemplateForm.tsx

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -107,14 +107,22 @@ const defaultInitialValues: CreateTemplateData = {
107107
allow_user_cancel_workspace_jobs: false,
108108
}
109109

110-
const getInitialValues = (
111-
canSetMaxTTL: boolean,
112-
{
113-
fromExample,
114-
fromCopy,
115-
}: { fromExample?: TemplateExample; fromCopy?: Template },
116-
) => {
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+
}: GetInitialValuesParams) => {
117124
let initialValues = defaultInitialValues
125+
118126
if (!canSetMaxTTL) {
119127
initialValues = {
120128
...initialValues,
@@ -123,7 +131,7 @@ const getInitialValues = (
123131
}
124132

125133
if (fromExample) {
126-
return {
134+
initialValues = {
127135
...initialValues,
128136
name: fromExample.id,
129137
display_name: fromExample.name,
@@ -133,14 +141,29 @@ const getInitialValues = (
133141
}
134142

135143
if (fromCopy) {
136-
return {
144+
initialValues = {
137145
...initialValues,
138146
...fromCopy,
139147
name: `${fromCopy.name}-copy`,
140148
display_name: `${fromCopy.display_name} Copy`,
141149
}
142150
}
143151

152+
if (variables) {
153+
variables.forEach((variable) => {
154+
if (variable.sensitive) {
155+
return
156+
}
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.value,
163+
})
164+
})
165+
}
166+
144167
return initialValues
145168
}
146169

@@ -173,12 +196,13 @@ export const CreateTemplateForm: FC<CreateTemplateFormProps> = ({
173196
logs,
174197
canSetMaxTTL,
175198
}) => {
176-
console.log(copiedTemplate)
177199
const styles = useStyles()
178200
const form = useFormik<CreateTemplateData>({
179-
initialValues: getInitialValues(canSetMaxTTL, {
201+
initialValues: getInitialValues({
202+
canSetMaxTTL,
180203
fromExample: starterTemplate,
181204
fromCopy: copiedTemplate,
205+
variables,
182206
}),
183207
validationSchema,
184208
onSubmit,
@@ -383,7 +407,7 @@ export const CreateTemplateForm: FC<CreateTemplateFormProps> = ({
383407
<FormFields>
384408
{variables.map((variable, index) => (
385409
<VariableInput
386-
defaultValue={variable.value}
410+
defaultValue={form.values.user_variable_values?.[index].value}
387411
variable={variable}
388412
disabled={isSubmitting}
389413
key={variable.name}

site/src/xServices/createTemplate/createTemplateXService.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ export const createTemplateMachine =
114114
data: {
115115
template: Template
116116
version: TemplateVersion
117-
parameters: ParameterSchema[]
117+
118118
variables: TemplateVersionVariable[]
119119
}
120120
}
@@ -155,7 +155,7 @@ export const createTemplateMachine =
155155
{
156156
target: "creating.promptParametersAndVariables",
157157
actions: ["assignCopiedTemplateData"],
158-
cond: "hasParametersOrVariables",
158+
cond: "hasVariables",
159159
},
160160
{
161161
target: "idle",
@@ -340,15 +340,14 @@ export const createTemplateMachine =
340340
organizationId,
341341
templateNameToCopy,
342342
)
343-
const [version, parameters, variables] = await Promise.all([
343+
const [version, variables] = await Promise.all([
344344
getTemplateVersion(template.active_version_id),
345-
getTemplateVersionSchema(template.active_version_id),
345+
346346
getTemplateVersionVariables(template.active_version_id),
347347
])
348348
return {
349349
template,
350350
version,
351-
parameters,
352351
variables,
353352
}
354353
},
@@ -519,7 +518,6 @@ export const createTemplateMachine =
519518
assignCopiedTemplateData: assign({
520519
copiedTemplate: (_, { data }) => data.template,
521520
version: (_, { data }) => data.version,
522-
parameters: (_, { data }) => data.parameters,
523521
variables: (_, { data }) => data.variables,
524522
}),
525523
},
@@ -537,8 +535,8 @@ export const createTemplateMachine =
537535
),
538536
hasNoParametersOrVariables: (_, { data }) =>
539537
data.parameters === undefined && data.variables === undefined,
540-
hasParametersOrVariables: (_, { data }) => {
541-
return data.parameters.length > 0 || data.variables.length > 0
538+
hasVariables: (_, { data }) => {
539+
return data.variables.length > 0
542540
},
543541
},
544542
},

0 commit comments

Comments
 (0)