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

Skip to content

Commit ce82a85

Browse files
committed
Pre-fill info from example data
1 parent 439ec15 commit ce82a85

File tree

4 files changed

+34
-14
lines changed

4 files changed

+34
-14
lines changed

site/src/pages/CreateTemplatePage/CreateTemplateForm.tsx

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,24 +39,36 @@ const defaultInitialValues: FormValues = {
3939
}
4040

4141
interface CreateTemplateFormProps {
42-
initialValues?: typeof defaultInitialValues
4342
starterTemplate?: TemplateExample
4443
errors?: unknown
4544
isSubmitting: boolean
4645
onCancel: () => void
4746
}
4847

48+
const getInitialValues = (starterTemplate?: TemplateExample) => {
49+
if (!starterTemplate) {
50+
return defaultInitialValues
51+
}
52+
53+
return {
54+
...defaultInitialValues,
55+
name: starterTemplate.id,
56+
display_name: starterTemplate.name,
57+
icon: starterTemplate.icon,
58+
description: starterTemplate.description,
59+
}
60+
}
61+
4962
export const CreateTemplateForm: FC<CreateTemplateFormProps> = ({
50-
initialValues = defaultInitialValues,
5163
starterTemplate,
5264
errors,
5365
isSubmitting,
5466
onCancel,
5567
}) => {
5668
const styles = useStyles()
5769
const formFooterStyles = useFormFooterStyles()
58-
const form = useFormik({
59-
initialValues,
70+
const form = useFormik<FormValues>({
71+
initialValues: getInitialValues(starterTemplate),
6072
validationSchema,
6173
onSubmit: () => {
6274
console.log("Submit")
@@ -79,11 +91,7 @@ export const CreateTemplateForm: FC<CreateTemplateFormProps> = ({
7991
</p>
8092
</div>
8193

82-
<Stack
83-
direction="column"
84-
spacing={1}
85-
className={styles.formSectionFields}
86-
>
94+
<Stack direction="column" className={styles.formSectionFields}>
8795
{starterTemplate && <SelectedTemplate template={starterTemplate} />}
8896

8997
<TextField

site/src/pages/CreateTemplatePage/CreateTemplatePage.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const CreateTemplatePage: FC = () => {
2626
},
2727
},
2828
})
29+
const { starterTemplate } = state.context
2930

3031
const onCancel = () => {
3132
navigate(-1)
@@ -40,7 +41,11 @@ const CreateTemplatePage: FC = () => {
4041
<FullPageHorizontalForm title={t("title")} onCancel={onCancel}>
4142
{state.hasTag("loading") && <Loader />}
4243
{state.matches("idle") && (
43-
<CreateTemplateForm isSubmitting={false} onCancel={onCancel} />
44+
<CreateTemplateForm
45+
starterTemplate={starterTemplate}
46+
isSubmitting={false}
47+
onCancel={onCancel}
48+
/>
4449
)}
4550
</FullPageHorizontalForm>
4651
</>

site/src/pages/StarterTemplatePage/StarterTemplatePageView.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import PlusIcon from "@material-ui/icons/AddOutlined"
1515
import { AlertBanner } from "components/AlertBanner/AlertBanner"
1616
import { useTranslation } from "react-i18next"
1717
import { Stack } from "components/Stack/Stack"
18+
import { Link } from "react-router-dom"
1819

1920
export interface StarterTemplatePageViewProps {
2021
context: StarterTemplateContext
@@ -53,7 +54,13 @@ export const StarterTemplatePageView: FC<StarterTemplatePageViewProps> = ({
5354
>
5455
{t("actions.viewSourceCode")}
5556
</Button>
56-
<Button startIcon={<PlusIcon />}>{t("actions.useTemplate")}</Button>
57+
<Button
58+
component={Link}
59+
to={`/templates/new?exampleId=${starterTemplate.id}`}
60+
startIcon={<PlusIcon />}
61+
>
62+
{t("actions.useTemplate")}
63+
</Button>
5764
</>
5865
}
5966
>

site/src/xServices/createTemplate/createTemplateXService.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export const createTemplateMachine = createMachine(
1919
context: {} as CreateTemplateContext,
2020
events: {} as { type: "CREATE"; data: any },
2121
services: {} as {
22-
loadingStarterTemplate: {
22+
loadStarterTemplate: {
2323
data: TemplateExample
2424
}
2525
createTemplate: {
@@ -95,8 +95,8 @@ export const createTemplateMachine = createMachine(
9595
},
9696
},
9797
actions: {
98-
assignError: (_, { data }) => assign({ error: data }),
99-
assignStarterTemplate: (_, { data }) => assign({ starterTemplate: data }),
98+
assignError: assign({ error: (_, { data }) => data }),
99+
assignStarterTemplate: assign({ starterTemplate: (_, { data }) => data }),
100100
},
101101
guards: {
102102
isExampleProvided: ({ exampleId }) => Boolean(exampleId),

0 commit comments

Comments
 (0)