Skip to content

Commit 502a034

Browse files
committed
Wire up more to the create form
1 parent 35ddcab commit 502a034

File tree

3 files changed

+59
-20
lines changed

3 files changed

+59
-20
lines changed

site/api.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { mutate } from "swr"
2+
13
interface LoginResponse {
24
session_token: string
35
}
@@ -39,6 +41,32 @@ export interface Project {
3941
active_version_id: string
4042
}
4143

44+
export interface CreateProjectRequest {
45+
name: string
46+
organizationId: string
47+
provisioner: string
48+
}
49+
50+
export namespace Project {
51+
export const create = async (request: CreateProjectRequest): Promise<Project> => {
52+
const response = await fetch(`/api/v2/projects/${request.organizationId}/`, {
53+
method: "POST",
54+
headers: {
55+
"Content-Type": "application/json",
56+
},
57+
body: JSON.stringify(request),
58+
})
59+
60+
const body = await response.json()
61+
await mutate("/api/v2/projects")
62+
if (!response.ok) {
63+
throw new Error(body.message)
64+
}
65+
66+
return body
67+
}
68+
}
69+
4270
export const login = async (email: string, password: string): Promise<LoginResponse> => {
4371
const response = await fetch("/api/v2/login", {
4472
method: "POST",

site/forms/CreateProjectForm.tsx

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,17 @@ import {
1212
DropdownItem,
1313
} from "../components/Form"
1414

15-
import { Organization, Provisioner } from "./../api"
16-
17-
export interface CreateProjectRequest {
18-
provisionerId: string
19-
organizationId: string
20-
name: string
21-
}
15+
import { Organization, Project, Provisioner, CreateProjectRequest } from "./../api"
2216

2317
export interface CreateProjectFormProps {
2418
provisioners: Provisioner[]
2519
organizations: Organization[]
26-
onSubmit: (request: CreateProjectRequest) => Promise<void>
20+
onSubmit: (request: CreateProjectRequest) => Promise<Project>
2721
onCancel: () => void
2822
}
2923

3024
const validationSchema = Yup.object({
31-
provisionerId: Yup.string().required("Email is required."),
25+
provisioner: Yup.string().required("Provisioner is required."),
3226
organizationId: Yup.string().required("Organization is required."),
3327
name: Yup.string().required("Name is required"),
3428
})
@@ -46,18 +40,20 @@ export const CreateProjectForm: React.FC<CreateProjectFormProps> = ({
4640

4741
const form = useFormik<CreateProjectRequest>({
4842
initialValues: {
49-
provisionerId: provisioners[0].id,
50-
organizationId: organizations[0].id,
43+
provisioner: provisioners[0].id,
44+
organizationId: organizations[0].name,
5145
name: "",
5246
},
5347
enableReinitialize: true,
5448
validationSchema: validationSchema,
55-
onSubmit: onSubmit,
49+
onSubmit: (req) => {
50+
return onSubmit(req)
51+
},
5652
})
5753

5854
const organizationDropDownItems: DropdownItem[] = organizations.map((org) => {
5955
return {
60-
value: org.id,
56+
value: org.name,
6157
name: org.name,
6258
}
6359
})
@@ -100,7 +96,7 @@ export const CreateProjectForm: React.FC<CreateProjectFormProps> = ({
10096
<FormSection title="Provider">
10197
<FormDropdownField
10298
form={form}
103-
formFieldName={"provisionerId"}
99+
formFieldName={"provisioner"}
104100
helperText="The backing provisioner for this project."
105101
items={provisionerDropDownItems}
106102
fullWidth
@@ -113,7 +109,16 @@ export const CreateProjectForm: React.FC<CreateProjectFormProps> = ({
113109
<Button className={styles.button} onClick={onCancel} variant="outlined">
114110
Cancel
115111
</Button>
116-
<Button className={styles.button} onClick={form.submitForm} variant="contained" color="primary" type="submit">
112+
<Button
113+
className={styles.button}
114+
onClick={() => {
115+
console.log("submit clicked: " + JSON.stringify(form.values))
116+
form.submitForm()
117+
}}
118+
variant="contained"
119+
color="primary"
120+
type="submit"
121+
>
117122
Submit
118123
</Button>
119124
</div>

site/pages/projects/create.tsx

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ import { makeStyles } from "@material-ui/core/styles"
33
import { useRouter } from "next/router"
44
import useSWR from "swr"
55

6-
import { provisioners } from "../../api"
6+
import * as API from "../../api"
77
import { useUser } from "../../contexts/UserContext"
8+
import { ErrorSummary } from "../../components/ErrorSummary"
89
import { FullScreenLoader } from "../../components/Loader/FullScreenLoader"
910
import { CreateProjectForm } from "../../forms/CreateProjectForm"
1011

@@ -15,8 +16,7 @@ const CreateProjectPage: React.FC = () => {
1516
const { data: organizations, error } = useSWR("/api/v2/users/me/organizations")
1617

1718
if (error) {
18-
// TODO: Merge with error component in other PR
19-
return <div>{"Error"}</div>
19+
return <ErrorSummary error={error} />
2020
}
2121

2222
if (!me || !organizations) {
@@ -27,12 +27,18 @@ const CreateProjectPage: React.FC = () => {
2727
await router.push("/projects")
2828
}
2929

30+
const onSubmit = async (req: API.CreateProjectRequest) => {
31+
const project = await API.Project.create(req)
32+
await router.push("/projects")
33+
return project
34+
}
35+
3036
return (
3137
<div className={styles.root}>
3238
<CreateProjectForm
33-
provisioners={provisioners}
39+
provisioners={API.provisioners}
3440
organizations={organizations}
35-
onSubmit={(request) => alert(JSON.stringify(request))}
41+
onSubmit={onSubmit}
3642
onCancel={onCancel}
3743
/>
3844
</div>

0 commit comments

Comments
 (0)