1- import Link from "@material-ui/core/Link"
2- import MenuItem from "@material-ui/core/MenuItem"
31import { makeStyles } from "@material-ui/core/styles"
4- import TextField , { TextFieldProps } from "@material-ui/core/TextField"
5- import OpenInNewIcon from "@material-ui/icons/OpenInNew"
2+ import TextField from "@material-ui/core/TextField"
63import { FormikContextType , useFormik } from "formik"
74import { FC , useState } from "react"
8- import { Link as RouterLink } from "react-router-dom"
95import * as Yup from "yup"
106import * as TypesGen from "../../api/typesGenerated"
11- import { CodeExample } from "../../components/CodeExample/CodeExample"
12- import { EmptyState } from "../../components/EmptyState/EmptyState"
137import { FormFooter } from "../../components/FormFooter/FormFooter"
148import { FullPageForm } from "../../components/FullPageForm/FullPageForm"
159import { Loader } from "../../components/Loader/Loader"
@@ -20,29 +14,18 @@ import { getFormHelpers, nameValidator, onChangeTrimmed } from "../../util/formU
2014export const Language = {
2115 templateLabel : "Template" ,
2216 nameLabel : "Name" ,
23- emptyMessage : "Let's create your first template" ,
24- emptyDescription : (
25- < >
26- To create a workspace you need to have a template. You can{ " " }
27- < Link target = "_blank" href = "https://github.com/coder/coder/blob/main/docs/templates.md" >
28- create one from scratch
29- </ Link > { " " }
30- or use a built-in template by typing the following Coder CLI command:
31- </ >
32- ) ,
33- templateLink : "Read more about this template" ,
3417}
3518
3619export interface CreateWorkspacePageViewProps {
3720 loadingTemplates : boolean
3821 loadingTemplateSchema : boolean
3922 creatingWorkspace : boolean
23+ templateName : string
4024 templates ?: TypesGen . Template [ ]
4125 selectedTemplate ?: TypesGen . Template
4226 templateSchema ?: TypesGen . ParameterSchema [ ]
4327 onCancel : ( ) => void
4428 onSubmit : ( req : TypesGen . CreateWorkspaceRequest ) => void
45- onSelectTemplate : ( template : TypesGen . Template ) => void
4629}
4730
4831export const validationSchema = Yup . object ( {
@@ -51,7 +34,8 @@ export const validationSchema = Yup.object({
5134
5235export const CreateWorkspacePageView : FC < CreateWorkspacePageViewProps > = ( props ) => {
5336 const [ parameterValues , setParameterValues ] = useState < Record < string , string > > ( { } )
54- const styles = useStyles ( )
37+ useStyles ( )
38+
5539 const form : FormikContextType < TypesGen . CreateWorkspaceRequest > = useFormik < TypesGen . CreateWorkspaceRequest > ( {
5640 initialValues : {
5741 name : "" ,
@@ -84,75 +68,20 @@ export const CreateWorkspacePageView: FC<CreateWorkspacePageViewProps> = (props)
8468 } ,
8569 } )
8670 const getFieldHelpers = getFormHelpers < TypesGen . CreateWorkspaceRequest > ( form )
87- const selectedTemplate =
88- props . templates &&
89- form . values . template_id &&
90- props . templates . find ( ( template ) => template . id === form . values . template_id )
91-
92- const handleTemplateChange : TextFieldProps [ "onChange" ] = ( event ) => {
93- if ( ! props . templates ) {
94- throw new Error ( "Templates are not loaded" )
95- }
96-
97- const templateId = event . target . value
98- const selectedTemplate = props . templates . find ( ( template ) => template . id === templateId )
99-
100- if ( ! selectedTemplate ) {
101- throw new Error ( `Template ${ templateId } not found` )
102- }
103-
104- form . setFieldValue ( "template_id" , selectedTemplate . id )
105- props . onSelectTemplate ( selectedTemplate )
106- }
10771
10872 return (
10973 < FullPageForm title = "Create workspace" onCancel = { props . onCancel } >
11074 < form onSubmit = { form . handleSubmit } >
111- { props . loadingTemplates && < Loader /> }
112-
11375 < Stack >
114- { props . templates && props . templates . length === 0 && (
115- < EmptyState
116- className = { styles . emptyState }
117- message = { Language . emptyMessage }
118- description = { Language . emptyDescription }
119- descriptionClassName = { styles . emptyStateDescription }
120- cta = {
121- < CodeExample className = { styles . code } buttonClassName = { styles . codeButton } code = "coder template init" />
122- }
123- />
124- ) }
125- { props . templates && props . templates . length > 0 && (
126- < TextField
127- { ...getFieldHelpers ( "template_id" ) }
128- disabled = { form . isSubmitting }
129- onChange = { handleTemplateChange }
130- autoFocus
131- fullWidth
132- label = { Language . templateLabel }
133- variant = "outlined"
134- select
135- helperText = {
136- selectedTemplate && (
137- < Link
138- className = { styles . readMoreLink }
139- component = { RouterLink }
140- to = { `/templates/${ selectedTemplate . name } ` }
141- target = "_blank"
142- >
143- { Language . templateLink } < OpenInNewIcon />
144- </ Link >
145- )
146- }
147- >
148- { props . templates . map ( ( template ) => (
149- < MenuItem key = { template . id } value = { template . id } >
150- { template . name }
151- </ MenuItem >
152- ) ) }
153- </ TextField >
154- ) }
155-
76+ < TextField
77+ disabled
78+ fullWidth
79+ label = { Language . templateLabel }
80+ value = { props . selectedTemplate ?. name || props . templateName }
81+ variant = "outlined"
82+ />
83+
84+ { props . loadingTemplateSchema && < Loader /> }
15685 { props . selectedTemplate && props . templateSchema && (
15786 < >
15887 < TextField
0 commit comments