Skip to content

Commit a783e50

Browse files
committed
react
1 parent 84f875f commit a783e50

4 files changed

Lines changed: 75 additions & 17 deletions

File tree

src/main/java/com/github/throyer/common/springboot/controllers/app/RoleController.java renamed to src/main/java/com/github/throyer/common/springboot/controllers/app/JSONController.java

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,36 @@
88
import org.springframework.http.ResponseEntity;
99
import org.springframework.security.access.prepost.PreAuthorize;
1010
import org.springframework.web.bind.annotation.GetMapping;
11+
import org.springframework.web.bind.annotation.PathVariable;
1112
import org.springframework.web.bind.annotation.RequestMapping;
1213
import org.springframework.web.bind.annotation.RestController;
1314

1415
import com.github.throyer.common.springboot.domain.role.entity.Role;
1516
import com.github.throyer.common.springboot.domain.role.repository.RoleRepository;
17+
import com.github.throyer.common.springboot.domain.user.model.UserDetails;
18+
import com.github.throyer.common.springboot.domain.user.repository.UserRepository;
1619

1720
@RestController
18-
@RequestMapping("/app/roles")
21+
@RequestMapping("/app/json")
1922
@PreAuthorize("hasAnyAuthority('ADM')")
20-
public class RoleController {
23+
public class JSONController {
2124

2225
@Autowired
23-
private RoleRepository repository;
26+
private RoleRepository roles;
2427

25-
@GetMapping
26-
public ResponseEntity<List<Role>> index() {
27-
return ok(repository.findAll());
28+
@Autowired
29+
private UserRepository users;
30+
31+
@GetMapping("roles")
32+
public ResponseEntity<List<Role>> roles() {
33+
return ok(roles.findAll());
34+
}
35+
36+
@GetMapping("user/{id}")
37+
public ResponseEntity<UserDetails> user(@PathVariable Long id) {
38+
var user = users.findByIdFetchRoles(id)
39+
.orElseThrow();
40+
41+
return ok(new UserDetails(user));
2842
}
2943
}

src/main/java/com/github/throyer/common/springboot/controllers/app/UserController.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,14 @@ public String index(
4848
}
4949

5050
@GetMapping("/form")
51-
public String form(Model model) {
52-
model.addAttribute("user", new CreateOrUpdateUserByAppForm());
51+
public String createForm(Model model) {
52+
model.addAttribute("create", true);
53+
return "app/users/form";
54+
}
55+
56+
@GetMapping("/form/{id}")
57+
public String editForm(Model model) {
58+
model.addAttribute("create", false);
5359
return "app/users/form";
5460
}
5561

src/main/resources/static/js/forms/users-form.js

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,51 @@
11
const App = () => {
22

3+
const [waiting, setWaiting] = React.useState(true)
4+
35
const [roles, setRoles] = React.useState([])
6+
const [user, setUser] = React.useState()
47

58
const findAllRoles = React.useCallback(async () => {
69
const { data: roles } = await axios
7-
.get('app/roles');
10+
.get('app/json/roles');
811

912
setRoles(roles);
1013
}, []);
1114

15+
const findUser = React.useCallback(async () => {
16+
const id = Number(window.location.pathname.split('/').pop())
17+
18+
if (Number.isSafeInteger(Number(id))) {
19+
const { data: user } = await axios
20+
.get(`app/json/user/${id}`);
21+
22+
setUser(user);
23+
return;
24+
}
25+
26+
setUser({
27+
id: null,
28+
name: "",
29+
email: "",
30+
roles: []
31+
})
32+
}, []);
33+
34+
const fetch = React.useCallback(async () => {
35+
await Promise.all([findUser(), findAllRoles()]);
36+
setWaiting(false);
37+
}, []);
38+
1239

1340
React.useEffect(() => {
14-
findAllRoles();
41+
fetch();
1542
}, [])
1643

17-
18-
return (
44+
return !waiting && (
1945
<div className="col-md-4 shadow-sm p-3 mb-5 bg-body rounded">
46+
<pre>
47+
{JSON.stringify(user, undefined, 4)}
48+
</pre>
2049
<div className="row">
2150
<div className="col-md-12">
2251
<div className="form-group">
@@ -32,7 +61,12 @@ const App = () => {
3261
required
3362
name="name"
3463
type="text"
64+
value={user.name}
3565
className="form-control form-control-sm"
66+
onChange={({ target: { value } }) => setUser({
67+
...user,
68+
name: value
69+
})}
3670
/>
3771
</div>
3872
</div>
@@ -47,10 +81,15 @@ const App = () => {
4781
</small>
4882
</label>
4983
<input
84+
required
5085
name="email"
5186
type="email"
87+
value={user.email}
5288
className="form-control form-control-sm"
53-
required
89+
onChange={({ target: { value } }) => setUser({
90+
...user,
91+
email: value
92+
})}
5493
/>
5594

5695
<div className="invalid-feedback">
@@ -73,8 +112,8 @@ const App = () => {
73112
className="form-select form-select-sm"
74113
>
75114
<option value="">Selecione</option>
76-
{roles.map(({ id, name }) => (
77-
<option value={id}>{name}</option>
115+
{roles.map(({ id, name, initials }) => (
116+
<option selected={user.roles[0] == initials} value={id}>{name}</option>
78117
))}
79118
</select>
80119

src/main/resources/templates/app/users/form.html

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<div class="col-md-12">
77
<h3 class="font-weight-light">
88
<i class="fas fa-user"></i>
9-
<span th:text="${user.id == null ? 'Create' : 'Edit'}"></span>
9+
<span th:text="${create ? 'Create' : 'Edit'}"></span>
1010
User
1111
</h3>
1212
<hr>
@@ -17,7 +17,6 @@ <h3 class="font-weight-light">
1717
novalidate
1818
method="POST"
1919
th:action="@{/app/users}"
20-
th:data-user="${user}"
2120
class="row d-flex justify-content-center needs-validation">
2221
</form>
2322
</section>

0 commit comments

Comments
 (0)