1
- import Link from "@material-ui/core/Link"
2
- import MenuItem from "@material-ui/core/MenuItem"
3
1
import { 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"
6
3
import { FormikContextType , useFormik } from "formik"
7
4
import { FC , useState } from "react"
8
- import { Link as RouterLink } from "react-router-dom"
9
5
import * as Yup from "yup"
10
6
import * as TypesGen from "../../api/typesGenerated"
11
- import { CodeExample } from "../../components/CodeExample/CodeExample"
12
- import { EmptyState } from "../../components/EmptyState/EmptyState"
13
7
import { FormFooter } from "../../components/FormFooter/FormFooter"
14
8
import { FullPageForm } from "../../components/FullPageForm/FullPageForm"
15
9
import { Loader } from "../../components/Loader/Loader"
@@ -20,29 +14,18 @@ import { getFormHelpers, nameValidator, onChangeTrimmed } from "../../util/formU
20
14
export const Language = {
21
15
templateLabel : "Template" ,
22
16
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" ,
34
17
}
35
18
36
19
export interface CreateWorkspacePageViewProps {
37
20
loadingTemplates : boolean
38
21
loadingTemplateSchema : boolean
39
22
creatingWorkspace : boolean
23
+ templateName : string
40
24
templates ?: TypesGen . Template [ ]
41
25
selectedTemplate ?: TypesGen . Template
42
26
templateSchema ?: TypesGen . ParameterSchema [ ]
43
27
onCancel : ( ) => void
44
28
onSubmit : ( req : TypesGen . CreateWorkspaceRequest ) => void
45
- onSelectTemplate : ( template : TypesGen . Template ) => void
46
29
}
47
30
48
31
export const validationSchema = Yup . object ( {
@@ -51,7 +34,8 @@ export const validationSchema = Yup.object({
51
34
52
35
export const CreateWorkspacePageView : FC < CreateWorkspacePageViewProps > = ( props ) => {
53
36
const [ parameterValues , setParameterValues ] = useState < Record < string , string > > ( { } )
54
- const styles = useStyles ( )
37
+ useStyles ( )
38
+
55
39
const form : FormikContextType < TypesGen . CreateWorkspaceRequest > = useFormik < TypesGen . CreateWorkspaceRequest > ( {
56
40
initialValues : {
57
41
name : "" ,
@@ -84,75 +68,20 @@ export const CreateWorkspacePageView: FC<CreateWorkspacePageViewProps> = (props)
84
68
} ,
85
69
} )
86
70
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
- }
107
71
108
72
return (
109
73
< FullPageForm title = "Create workspace" onCancel = { props . onCancel } >
110
74
< form onSubmit = { form . handleSubmit } >
111
- { props . loadingTemplates && < Loader /> }
112
-
113
75
< 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 /> }
156
85
{ props . selectedTemplate && props . templateSchema && (
157
86
< >
158
87
< TextField
0 commit comments