Skip to content

refactor(site): Suport template version variables on template creation #6434

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Mar 6, 2023
Prev Previous commit
Next Next commit
Remove unecessary validation
  • Loading branch information
BrunoQuaresma committed Mar 2, 2023
commit 42bff4985c624ad93c8166c7acd3669f129af5ea
36 changes: 16 additions & 20 deletions site/src/pages/CreateTemplatePage/CreateTemplateForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@ import { useFormik } from "formik"
import { SelectedTemplate } from "pages/CreateWorkspacePage/SelectedTemplate"
import { FC } from "react"
import { useTranslation } from "react-i18next"
import { nameValidator, getFormHelpers, onChangeTrimmed } from "util/formUtils"
import {
nameValidator,
getFormHelpers,
onChangeTrimmed,
templateDisplayNameValidator,
} from "util/formUtils"
import { CreateTemplateData } from "xServices/createTemplate/createTemplateXService"
import * as Yup from "yup"
import { WorkspaceBuildLogs } from "components/WorkspaceBuildLogs/WorkspaceBuildLogs"
Expand All @@ -28,15 +33,7 @@ import { VariableInput } from "./VariableInput"

const validationSchema = Yup.object({
name: nameValidator("Name"),
display_name: Yup.string().optional(),
description: Yup.string().optional(),
icon: Yup.string().optional(),
default_ttl_hours: Yup.number(),
allow_user_cancel_workspace_jobs: Yup.boolean(),
parameter_values_by_name: Yup.object().optional(),
user_variable_values: Yup.array()
.of(Yup.object({ name: Yup.string(), value: Yup.string().optional() }))
.optional(),
display_name: templateDisplayNameValidator("Display name"),
})

const defaultInitialValues: CreateTemplateData = {
Expand All @@ -46,8 +43,6 @@ const defaultInitialValues: CreateTemplateData = {
icon: "",
default_ttl_hours: 24,
allow_user_cancel_workspace_jobs: false,
parameter_values_by_name: undefined,
user_variable_values: undefined,
}

const getInitialValues = (starterTemplate?: TemplateExample) => {
Expand All @@ -65,27 +60,27 @@ const getInitialValues = (starterTemplate?: TemplateExample) => {
}

interface CreateTemplateFormProps {
starterTemplate?: TemplateExample
error?: unknown
parameters?: ParameterSchema[]
variables?: TemplateVersionVariable[]
isSubmitting: boolean
onCancel: () => void
onSubmit: (data: CreateTemplateData) => void
isSubmitting: boolean
upload: TemplateUploadProps
starterTemplate?: TemplateExample
parameters?: ParameterSchema[]
variables?: TemplateVersionVariable[]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case, you're adjusting the CreateTemplateForm. It seems that it has a similar logic to "edit template variables", but I'm afraid that it will hard to unify both forms?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes! Because this form creates a template version + the template resource so it has some mixed values. I think this is a bit "nasty" too but Idk how to make it better or clear. 🤔 open to suggestions.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a valid point, those forms are different indeed 👍

error?: unknown
jobError?: string
logs?: ProvisionerJobLog[]
}

export const CreateTemplateForm: FC<CreateTemplateFormProps> = ({
onCancel,
onSubmit,
starterTemplate,
error,
parameters,
variables,
isSubmitting,
onCancel,
onSubmit,
upload,
error,
jobError,
logs,
}) => {
Expand Down Expand Up @@ -126,6 +121,7 @@ export const CreateTemplateForm: FC<CreateTemplateFormProps> = ({
onChange={onChangeTrimmed(form)}
autoFocus
fullWidth
required
label={t("form.fields.name")}
variant="outlined"
/>
Expand Down