Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
ed77ef7
XService: GetTemplateParameters
mtojek Jan 24, 2023
5d8330c
Rich parameter input shows up
mtojek Jan 24, 2023
25140eb
Render option icons
mtojek Jan 25, 2023
622eb4b
Icons
mtojek Jan 25, 2023
6a9fce9
WIP
mtojek Jan 25, 2023
d5f1afc
For testing purposes: template
mtojek Jan 25, 2023
2cbc5b0
Fix: useState
mtojek Jan 25, 2023
82d7710
WIP: dynamic validation
mtojek Jan 26, 2023
79dbb5d
Yup validation
mtojek Jan 26, 2023
17dece6
Translations
mtojek Jan 26, 2023
a886301
Remove temporary template
mtojek Jan 26, 2023
dc79db5
make fmt
mtojek Jan 26, 2023
e5aa5a9
WIP
mtojek Jan 26, 2023
a009555
Fix: tests
mtojek Jan 27, 2023
1311493
Fix: fmt
mtojek Jan 27, 2023
feeb10c
URL param
mtojek Jan 27, 2023
64d3016
Refactor
mtojek Jan 27, 2023
ac61aae
Test: rich param value
mtojek Jan 27, 2023
a1f22e9
Storybook
mtojek Jan 27, 2023
dfec8ca
Fix
mtojek Jan 27, 2023
2e321df
Refactor for testing purposes
mtojek Jan 27, 2023
f41de12
Typo
mtojek Jan 27, 2023
40fa739
test: string validation
mtojek Jan 27, 2023
2017a3a
Merge branch 'main' into 5574-site-1
mtojek Jan 30, 2023
d9f03b6
Button: build parameters
mtojek Jan 30, 2023
17bee41
Full screen page
mtojek Jan 30, 2023
b9da6e0
Fix: navigate
mtojek Jan 30, 2023
f372027
XState done
mtojek Jan 31, 2023
b7c0916
refactor: postWorkspaceBuild
mtojek Jan 31, 2023
d8f7cc3
RichParameterInput rendered
mtojek Jan 31, 2023
0dac060
Fix: bad initial value
mtojek Jan 31, 2023
4036caa
Validation works
mtojek Jan 31, 2023
608d713
Maybe
mtojek Jan 31, 2023
2edca1e
Fix
mtojek Jan 31, 2023
c771199
Go back button
mtojek Jan 31, 2023
35cf603
GoBack button
mtojek Jan 31, 2023
640ae42
Form
mtojek Jan 31, 2023
f31a7a0
Fix
mtojek Jan 31, 2023
5228e9c
Storybook
mtojek Feb 1, 2023
3b842f2
Fix: CreateWorkspacePage
mtojek Feb 1, 2023
c62d5a4
fmt
mtojek Feb 1, 2023
6cab87f
Test
mtojek Feb 1, 2023
491107a
Merge branch 'main' into 5574-site-1
mtojek Feb 1, 2023
097be54
ns
mtojek Feb 1, 2023
1210435
fmt
mtojek Feb 1, 2023
26a5e5d
All tests
mtojek Feb 1, 2023
8e2576e
feat: WorkspaceActions depend on template parameters
mtojek Feb 1, 2023
1873645
Fix
mtojek Feb 1, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Validation works
  • Loading branch information
mtojek committed Jan 31, 2023
commit 4036caacfb67391add7a19b58b834e9e350903b3
1 change: 0 additions & 1 deletion site/src/api/api.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import axios, { AxiosRequestHeaders } from "axios"
import dayjs from "dayjs"
import * as Types from "./types"
import { WorkspaceBuildTransition } from "./types"
import * as TypesGen from "./typesGenerated"

export const hardCodedCSRFCookie = (): string => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const renderComponent = async (props: Partial<WorkspaceActionsProps> = {}) => {
handleUpdate={jest.fn()}
handleCancel={jest.fn()}
handleChangeVersion={jest.fn()}
handleBuildParameters={jest.fn()}
isUpdating={false}
/>,
)
Expand All @@ -37,6 +38,7 @@ const renderAndClick = async (props: Partial<WorkspaceActionsProps> = {}) => {
handleUpdate={jest.fn()}
handleCancel={jest.fn()}
handleChangeVersion={jest.fn()}
handleBuildParameters={jest.fn()}
isUpdating={false}
/>,
)
Expand Down
116 changes: 57 additions & 59 deletions site/src/pages/CreateWorkspacePage/CreateWorkspacePageView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,13 @@ export const CreateWorkspacePageView: FC<
template_id: props.selectedTemplate ? props.selectedTemplate.id : "",
rich_parameter_values: initialRichParameterValues,
},
validationSchema: ValidationSchemaForRichParameters(
"createWorkspacePage",
props.templateParameters,
),
validationSchema: Yup.object({
name: nameValidator(t("nameLabel", { ns: "createWorkspacePage" })),
rich_parameter_values: ValidationSchemaForRichParameters(
"createWorkspacePage",
props.templateParameters,
),
}),
enableReinitialize: true,
initialTouched: props.initialTouched,
onSubmit: (request) => {
Expand Down Expand Up @@ -438,68 +441,63 @@ export const ValidationSchemaForRichParameters = (
return Yup.object()
}

return Yup.object({
name: nameValidator(t("nameLabel", { ns })),
rich_parameter_values: Yup.array()
.of(
Yup.object().shape({
name: Yup.string().required(),
value: Yup.string()
.required(t("validationRequiredParameter"))
.test("verify with template", (val, ctx) => {
const name = ctx.parent.name
const templateParameter = templateParameters.find(
(parameter) => parameter.name === name,
)
if (templateParameter) {
switch (templateParameter.type) {
case "number":
if (
templateParameter.validation_min === 0 &&
templateParameter.validation_max === 0
) {
return Yup.array()
.of(
Yup.object().shape({
name: Yup.string().required(),
value: Yup.string()
.required(t("validationRequiredParameter"))
.test("verify with template", (val, ctx) => {
const name = ctx.parent.name
const templateParameter = templateParameters.find(
(parameter) => parameter.name === name,
)
if (templateParameter) {
switch (templateParameter.type) {
case "number":
if (
templateParameter.validation_min === 0 &&
templateParameter.validation_max === 0
) {
return true
}

if (
Number(val) < templateParameter.validation_min ||
templateParameter.validation_max < Number(val)
) {
return ctx.createError({
path: ctx.path,
message: t("validationNumberNotInRange", {
min: templateParameter.validation_min,
max: templateParameter.validation_max,
}),
})
}
break
case "string":
{
if (templateParameter.validation_regex.length === 0) {
return true
}

if (
Number(val) < templateParameter.validation_min ||
templateParameter.validation_max < Number(val)
) {
const regex = new RegExp(templateParameter.validation_regex)
if (val && !regex.test(val)) {
return ctx.createError({
path: ctx.path,
message: t("validationNumberNotInRange", {
min: templateParameter.validation_min,
max: templateParameter.validation_max,
message: t("validationPatternNotMatched", {
error: templateParameter.validation_error,
pattern: templateParameter.validation_regex,
}),
})
}
break
case "string":
{
if (templateParameter.validation_regex.length === 0) {
return true
}

const regex = new RegExp(
templateParameter.validation_regex,
)
if (val && !regex.test(val)) {
return ctx.createError({
path: ctx.path,
message: t("validationPatternNotMatched", {
error: templateParameter.validation_error,
pattern: templateParameter.validation_regex,
}),
})
}
}
break
}
}
break
}
return true
}),
}),
)
.required(),
})
}
return true
}),
}),
)
.required()
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
UpdateWorkspaceErrors,
WorkspaceBuildParametersPageView,
} from "./WorkspaceBuildParametersPageView"
import { getWorkspaceBuildParameters } from "api/api"

export const WorkspaceBuildParametersPage: FC = () => {
const { t } = useTranslation("workspaceBuildParametersPage")
Expand Down Expand Up @@ -51,6 +50,7 @@ export const WorkspaceBuildParametersPage: FC = () => {
workspace={selectedWorkspace}
templateParameters={templateParameters}
workspaceBuildParameters={workspaceBuildParameters}
updatingWorkspace={state.matches("updatingWorkspace")}
hasErrors={state.matches("error")}
updateWorkspaceErrors={{
[UpdateWorkspaceErrors.GET_WORKSPACE_ERROR]: getWorkspaceError,
Expand All @@ -60,6 +60,17 @@ export const WorkspaceBuildParametersPage: FC = () => {
getWorkspaceBuildParametersError,
[UpdateWorkspaceErrors.UPDATE_WORKSPACE_ERROR]: updateWorkspaceError,
}}
onCancel={() => {
// Go back
navigate(-1)
}}
onSubmit={(request) => {
console.log("onSubmit 1")
send({
type: "UPDATE_WORKSPACE",
request,
})
}}
/>
</>
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import {
ValidationSchemaForRichParameters,
workspaceBuildParameterValue,
} from "pages/CreateWorkspacePage/CreateWorkspacePageView"
import { FormFooter } from "components/FormFooter/FormFooter"
import * as Yup from "yup"

export enum UpdateWorkspaceErrors {
GET_WORKSPACE_ERROR = "getWorkspaceError",
Expand All @@ -26,6 +28,9 @@ export interface WorkspaceBuildParametersPageViewProps {
workspaceBuildParameters?: TypesGen.WorkspaceBuildParameter[]

initialTouched?: FormikTouched<TypesGen.CreateWorkspaceRequest>
updatingWorkspace: boolean
onCancel: () => void
onSubmit: (req: TypesGen.CreateWorkspaceBuildRequest) => void

hasErrors: boolean
updateWorkspaceErrors: Partial<Record<UpdateWorkspaceErrors, Error | unknown>>
Expand All @@ -36,6 +41,7 @@ export const WorkspaceBuildParametersPageView: FC<
> = (props) => {
const { t } = useTranslation("workspaceBuildParametersPage")
const styles = useStyles()
const formFooterStyles = useFormFooterStyles()

const initialRichParameterValues = selectInitialRichParametersValues(
props.templateParameters,
Expand All @@ -51,13 +57,17 @@ export const WorkspaceBuildParametersPageView: FC<
transition: "start",
rich_parameter_values: initialRichParameterValues,
},
validationSchema: ValidationSchemaForRichParameters(
"workspaceBuildParametersPage",
props.templateParameters,
),
validationSchema: Yup.object({
rich_parameter_values: ValidationSchemaForRichParameters(
"workspaceBuildParametersPage",
props.templateParameters,
),
}),
enableReinitialize: true,
initialTouched: props.initialTouched,
onSubmit: () => {
onSubmit: (request) => {
console.info("onSubmit 2")
props.onSubmit(request)
form.setSubmitting(false)
},
})
Expand Down Expand Up @@ -138,33 +148,41 @@ export const WorkspaceBuildParametersPageView: FC<
>
{props.templateParameters && props.workspaceBuildParameters && (
<div className={styles.formSection}>
<Stack
direction="column"
spacing={4} // Spacing here is diff because the fields here don't have the MUI floating label spacing
className={styles.formSectionFields}
>
{props.templateParameters.map((parameter, index) => (
<RichParameterInput
{...getFieldHelpers(
"rich_parameter_values[" + index + "].value",
)}
disabled={form.isSubmitting}
index={index}
key={parameter.name}
onChange={(value) => {
form.setFieldValue("rich_parameter_values." + index, {
name: parameter.name,
value: value,
})
}}
parameter={parameter}
initialValue={workspaceBuildParameterValue(
initialRichParameterValues,
parameter,
)}
<form onSubmit={form.handleSubmit}>
<Stack
direction="column"
spacing={4} // Spacing here is diff because the fields here don't have the MUI floating label spacing
className={styles.formSectionFields}
>
{props.templateParameters.map((parameter, index) => (
<RichParameterInput
{...getFieldHelpers(
"rich_parameter_values[" + index + "].value",
)}
disabled={form.isSubmitting}
index={index}
key={parameter.name}
onChange={(value) => {
form.setFieldValue("rich_parameter_values." + index, {
name: parameter.name,
value: value,
})
}}
parameter={parameter}
initialValue={workspaceBuildParameterValue(
initialRichParameterValues,
parameter,
)}
/>
))}
<FormFooter
styles={formFooterStyles}
onCancel={props.onCancel}
isLoading={props.updatingWorkspace}
submitLabel={t("updateWorkspace")}
/>
))}
</Stack>
</Stack>
</form>
</div>
)}
</FullPageForm>
Expand Down