Skip to content

Commit fd8cdef

Browse files
committed
Add user creation form
1 parent cd3f141 commit fd8cdef

File tree

4 files changed

+119
-12
lines changed

4 files changed

+119
-12
lines changed

src/App.css

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
.App {
22
max-width: 500px;
33
margin: 0 auto;
4+
display: flex;
5+
}
6+
7+
.App > * {
8+
flex-grow: 1;
9+
flex-basis: 0;
10+
padding: 0 10px;
411
}

src/App.tsx

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React from "react";
22
import "./App.css";
33
import { gql } from "@apollo/client/core";
44
import { useUsersQuery } from "./generated/graphql";
5+
import { CreateUserForm } from "./CreateUserForm";
56

67
const USER_FRAGMENT_GQL = gql`
78
fragment UserInfo on User {
@@ -59,19 +60,25 @@ function App() {
5960

6061
return (
6162
<div className="App">
62-
{loading ? (
63-
<p>Loading...</p>
64-
) : data?.users.length ? (
65-
<ul>
66-
{data.users.map((user) => (
67-
<li key={user.id}>{user.name}</li>
68-
))}
69-
</ul>
70-
) : (
71-
<p>There is no data available</p>
72-
)}
63+
<CreateUserForm />
7364

74-
{!!error && <p>{error}</p>}
65+
<section>
66+
<h1>Users</h1>
67+
68+
{loading ? (
69+
<p>Loading...</p>
70+
) : data?.users.length ? (
71+
<ul>
72+
{data.users.map((user) => (
73+
<li key={user.id}>{user.name}</li>
74+
))}
75+
</ul>
76+
) : (
77+
<p>There is no data available</p>
78+
)}
79+
80+
{!!error && <p>{error}</p>}
81+
</section>
7582
</div>
7683
);
7784
}

src/CreateUserForm.tsx

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import React, { ChangeEvent, FormEvent } from "react";
2+
import { CreateUserInput, useCreateUserMutation } from "./generated/graphql";
3+
4+
export const CreateUserForm = () => {
5+
const [createUserMutation, { loading, error }] = useCreateUserMutation();
6+
7+
const initialFormState = {
8+
username: "",
9+
name: "",
10+
email: "",
11+
password: "",
12+
};
13+
const [formData, setFormData] = React.useState(initialFormState);
14+
15+
const onChange = (name: keyof CreateUserInput) => (
16+
e: ChangeEvent<HTMLInputElement>
17+
) => {
18+
const value = e.target.value;
19+
setFormData({
20+
...formData,
21+
[name]: value,
22+
});
23+
};
24+
const getInputProps = (name: keyof CreateUserInput) => ({
25+
value: formData[name],
26+
onChange: onChange(name),
27+
});
28+
29+
const onSubmit = async (e: FormEvent<HTMLFormElement>) => {
30+
e.preventDefault();
31+
32+
await createUserMutation({
33+
variables: {
34+
createUserInput: formData,
35+
},
36+
});
37+
38+
setFormData(initialFormState);
39+
};
40+
41+
return (
42+
<section>
43+
<h1>Create User</h1>
44+
45+
<form onSubmit={onSubmit}>
46+
<label>
47+
Username
48+
<input name={"new-username"} {...getInputProps("username")} />
49+
</label>
50+
51+
<label>
52+
Name
53+
<input name={"new-name"} {...getInputProps("name")} />
54+
</label>
55+
56+
<label>
57+
Email
58+
<input
59+
type={"email"}
60+
name={"new-email"}
61+
{...getInputProps("email")}
62+
/>
63+
</label>
64+
65+
<label>
66+
Password
67+
<input
68+
type={"password"}
69+
name={"new-password"}
70+
{...getInputProps("password")}
71+
/>
72+
</label>
73+
74+
<button disabled={loading}>Create</button>
75+
76+
{!!error && <p>{error}</p>}
77+
</form>
78+
</section>
79+
);
80+
};

src/index.css

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,16 @@ code {
1111
font-family: source-code-pro, Menlo, Monaco, Consolas, "Courier New",
1212
monospace;
1313
}
14+
15+
form > * + * {
16+
margin-top: 10px;
17+
}
18+
19+
label {
20+
display: block;
21+
}
22+
23+
input {
24+
display: block;
25+
margin-top: 5px;
26+
}

0 commit comments

Comments
 (0)