|
| 1 | +import { useFormik } from "formik"; |
| 2 | +import { type FC } from "react"; |
| 3 | +import * as Yup from "yup"; |
| 4 | +import { Alert } from "components/Alert/Alert"; |
1 | 5 | import {
|
2 | 6 | FormFields,
|
3 | 7 | FormFooter,
|
4 | 8 | FormSection,
|
5 | 9 | HorizontalForm,
|
6 | 10 | } from "components/Form/Form";
|
7 | 11 | import { RichParameterInput } from "components/RichParameterInput/RichParameterInput";
|
8 |
| -import { useFormik } from "formik"; |
9 |
| -import { FC } from "react"; |
10 | 12 | import {
|
11 | 13 | getInitialRichParameterValues,
|
12 | 14 | useValidationSchemaForRichParameters,
|
13 | 15 | } from "utils/richParameters";
|
14 |
| -import * as Yup from "yup"; |
15 | 16 | import { getFormHelpers } from "utils/formUtils";
|
16 |
| -import { |
| 17 | +import type { |
17 | 18 | TemplateVersionParameter,
|
| 19 | + Workspace, |
18 | 20 | WorkspaceBuildParameter,
|
19 | 21 | } from "api/typesGenerated";
|
20 | 22 |
|
21 | 23 | export type WorkspaceParametersFormValues = {
|
22 | 24 | rich_parameter_values: WorkspaceBuildParameter[];
|
23 | 25 | };
|
24 | 26 |
|
25 |
| -export const WorkspaceParametersForm: FC<{ |
26 |
| - isSubmitting: boolean; |
| 27 | +interface WorkspaceParameterFormProps { |
| 28 | + workspace: Workspace; |
27 | 29 | templateVersionRichParameters: TemplateVersionParameter[];
|
28 | 30 | buildParameters: WorkspaceBuildParameter[];
|
| 31 | + isSubmitting: boolean; |
| 32 | + canChangeVersions: boolean; |
29 | 33 | error: unknown;
|
30 | 34 | onCancel: () => void;
|
31 | 35 | onSubmit: (values: WorkspaceParametersFormValues) => void;
|
32 |
| -}> = ({ |
| 36 | +} |
| 37 | + |
| 38 | +export const WorkspaceParametersForm: FC<WorkspaceParameterFormProps> = ({ |
| 39 | + workspace, |
33 | 40 | onCancel,
|
34 | 41 | onSubmit,
|
35 | 42 | templateVersionRichParameters,
|
36 | 43 | buildParameters,
|
37 | 44 | error,
|
| 45 | + canChangeVersions, |
38 | 46 | isSubmitting,
|
39 | 47 | }) => {
|
40 | 48 | const form = useFormik<WorkspaceParametersFormValues>({
|
@@ -65,97 +73,121 @@ export const WorkspaceParametersForm: FC<{
|
65 | 73 | (parameter) => !parameter.mutable,
|
66 | 74 | );
|
67 | 75 |
|
| 76 | + const disabled = |
| 77 | + workspace.outdated && |
| 78 | + workspace.template_require_active_version && |
| 79 | + !canChangeVersions; |
| 80 | + |
68 | 81 | return (
|
69 |
| - <HorizontalForm onSubmit={form.handleSubmit} data-testid="form"> |
70 |
| - {hasNonEphemeralParameters && ( |
71 |
| - <FormSection |
72 |
| - title="Parameters" |
73 |
| - description="Settings used by your template" |
74 |
| - > |
75 |
| - <FormFields> |
76 |
| - {templateVersionRichParameters.map((parameter, index) => |
77 |
| - // Since we are adding the values to the form based on the index |
78 |
| - // we can't filter them to not loose the right index position |
79 |
| - parameter.mutable && !parameter.ephemeral ? ( |
80 |
| - <RichParameterInput |
81 |
| - {...getFieldHelpers( |
82 |
| - "rich_parameter_values[" + index + "].value", |
83 |
| - )} |
84 |
| - disabled={isSubmitting} |
85 |
| - key={parameter.name} |
86 |
| - onChange={async (value) => { |
87 |
| - await form.setFieldValue("rich_parameter_values." + index, { |
88 |
| - name: parameter.name, |
89 |
| - value: value, |
90 |
| - }); |
91 |
| - }} |
92 |
| - parameter={parameter} |
93 |
| - /> |
94 |
| - ) : null, |
95 |
| - )} |
96 |
| - </FormFields> |
97 |
| - </FormSection> |
| 82 | + <> |
| 83 | + {disabled && ( |
| 84 | + <Alert severity="warning" css={{ marginBottom: 48 }}> |
| 85 | + The template for this workspace requires automatic updates. Update the |
| 86 | + workspace to edit parameters. |
| 87 | + </Alert> |
98 | 88 | )}
|
99 |
| - {hasEphemeralParameters && ( |
100 |
| - <FormSection |
101 |
| - title="Ephemeral Parameters" |
102 |
| - description="These parameters only apply for a single workspace start." |
103 |
| - > |
104 |
| - <FormFields> |
105 |
| - {templateVersionRichParameters.map((parameter, index) => |
106 |
| - // Since we are adding the values to the form based on the index |
107 |
| - // we can't filter them to not loose the right index position |
108 |
| - parameter.mutable && parameter.ephemeral ? ( |
109 |
| - <RichParameterInput |
110 |
| - {...getFieldHelpers( |
111 |
| - "rich_parameter_values[" + index + "].value", |
112 |
| - )} |
113 |
| - disabled={isSubmitting} |
114 |
| - key={parameter.name} |
115 |
| - onChange={async (value) => { |
116 |
| - await form.setFieldValue("rich_parameter_values." + index, { |
117 |
| - name: parameter.name, |
118 |
| - value: value, |
119 |
| - }); |
120 |
| - }} |
121 |
| - parameter={parameter} |
122 |
| - /> |
123 |
| - ) : null, |
124 |
| - )} |
125 |
| - </FormFields> |
126 |
| - </FormSection> |
127 |
| - )} |
128 |
| - {/* They are displayed here only for visibility purposes */} |
129 |
| - {hasImmutableParameters && ( |
130 |
| - <FormSection |
131 |
| - title="Immutable parameters" |
132 |
| - description={ |
133 |
| - <> |
134 |
| - These settings <strong>cannot be changed</strong> after creating |
135 |
| - the workspace. |
136 |
| - </> |
137 |
| - } |
138 |
| - > |
139 |
| - <FormFields> |
140 |
| - {templateVersionRichParameters.map((parameter, index) => |
141 |
| - !parameter.mutable ? ( |
142 |
| - <RichParameterInput |
143 |
| - disabled |
144 |
| - {...getFieldHelpers( |
145 |
| - "rich_parameter_values[" + index + "].value", |
146 |
| - )} |
147 |
| - key={parameter.name} |
148 |
| - parameter={parameter} |
149 |
| - onChange={() => { |
150 |
| - throw new Error("Immutable parameters cannot be changed"); |
151 |
| - }} |
152 |
| - /> |
153 |
| - ) : null, |
154 |
| - )} |
155 |
| - </FormFields> |
156 |
| - </FormSection> |
157 |
| - )} |
158 |
| - <FormFooter onCancel={onCancel} isLoading={isSubmitting} /> |
159 |
| - </HorizontalForm> |
| 89 | + |
| 90 | + <HorizontalForm onSubmit={form.handleSubmit} data-testid="form"> |
| 91 | + {hasNonEphemeralParameters && ( |
| 92 | + <FormSection |
| 93 | + title="Parameters" |
| 94 | + description="Settings used by your template" |
| 95 | + > |
| 96 | + <FormFields> |
| 97 | + {templateVersionRichParameters.map((parameter, index) => |
| 98 | + // Since we are adding the values to the form based on the index |
| 99 | + // we can't filter them to not loose the right index position |
| 100 | + parameter.mutable && !parameter.ephemeral ? ( |
| 101 | + <RichParameterInput |
| 102 | + {...getFieldHelpers( |
| 103 | + "rich_parameter_values[" + index + "].value", |
| 104 | + )} |
| 105 | + disabled={isSubmitting || disabled} |
| 106 | + key={parameter.name} |
| 107 | + onChange={async (value) => { |
| 108 | + await form.setFieldValue( |
| 109 | + "rich_parameter_values." + index, |
| 110 | + { |
| 111 | + name: parameter.name, |
| 112 | + value: value, |
| 113 | + }, |
| 114 | + ); |
| 115 | + }} |
| 116 | + parameter={parameter} |
| 117 | + /> |
| 118 | + ) : null, |
| 119 | + )} |
| 120 | + </FormFields> |
| 121 | + </FormSection> |
| 122 | + )} |
| 123 | + {hasEphemeralParameters && ( |
| 124 | + <FormSection |
| 125 | + title="Ephemeral Parameters" |
| 126 | + description="These parameters only apply for a single workspace start." |
| 127 | + > |
| 128 | + <FormFields> |
| 129 | + {templateVersionRichParameters.map((parameter, index) => |
| 130 | + // Since we are adding the values to the form based on the index |
| 131 | + // we can't filter them to not loose the right index position |
| 132 | + parameter.mutable && parameter.ephemeral ? ( |
| 133 | + <RichParameterInput |
| 134 | + {...getFieldHelpers( |
| 135 | + "rich_parameter_values[" + index + "].value", |
| 136 | + )} |
| 137 | + disabled={isSubmitting || disabled} |
| 138 | + key={parameter.name} |
| 139 | + onChange={async (value) => { |
| 140 | + await form.setFieldValue( |
| 141 | + "rich_parameter_values." + index, |
| 142 | + { |
| 143 | + name: parameter.name, |
| 144 | + value: value, |
| 145 | + }, |
| 146 | + ); |
| 147 | + }} |
| 148 | + parameter={parameter} |
| 149 | + /> |
| 150 | + ) : null, |
| 151 | + )} |
| 152 | + </FormFields> |
| 153 | + </FormSection> |
| 154 | + )} |
| 155 | + {/* They are displayed here only for visibility purposes */} |
| 156 | + {hasImmutableParameters && ( |
| 157 | + <FormSection |
| 158 | + title="Immutable parameters" |
| 159 | + description={ |
| 160 | + <> |
| 161 | + These settings <strong>cannot be changed</strong> after creating |
| 162 | + the workspace. |
| 163 | + </> |
| 164 | + } |
| 165 | + > |
| 166 | + <FormFields> |
| 167 | + {templateVersionRichParameters.map((parameter, index) => |
| 168 | + !parameter.mutable ? ( |
| 169 | + <RichParameterInput |
| 170 | + disabled |
| 171 | + {...getFieldHelpers( |
| 172 | + "rich_parameter_values[" + index + "].value", |
| 173 | + )} |
| 174 | + key={parameter.name} |
| 175 | + parameter={parameter} |
| 176 | + onChange={() => { |
| 177 | + throw new Error("Immutable parameters cannot be changed"); |
| 178 | + }} |
| 179 | + /> |
| 180 | + ) : null, |
| 181 | + )} |
| 182 | + </FormFields> |
| 183 | + </FormSection> |
| 184 | + )} |
| 185 | + <FormFooter |
| 186 | + onCancel={onCancel} |
| 187 | + isLoading={isSubmitting} |
| 188 | + submitDisabled={disabled} |
| 189 | + /> |
| 190 | + </HorizontalForm> |
| 191 | + </> |
160 | 192 | );
|
161 | 193 | };
|
0 commit comments