BAEL-4842 - Use React and Spring Boot to Build a Simple CRUD App
Initial source module for the article
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
.App {
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.App-logo {
|
||||
height: 40vmin;
|
||||
pointer-events: none;
|
||||
}
|
||||
|
||||
@media (prefers-reduced-motion: no-preference) {
|
||||
.App-logo {
|
||||
animation: App-logo-spin infinite 20s linear;
|
||||
}
|
||||
}
|
||||
|
||||
.App-header {
|
||||
background-color: #282c34;
|
||||
min-height: 100vh;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
font-size: calc(10px + 2vmin);
|
||||
color: white;
|
||||
}
|
||||
|
||||
.App-link {
|
||||
color: #61dafb;
|
||||
}
|
||||
|
||||
@keyframes App-logo-spin {
|
||||
from {
|
||||
transform: rotate(0deg);
|
||||
}
|
||||
to {
|
||||
transform: rotate(360deg);
|
||||
}
|
||||
}
|
||||
|
||||
.container, .container-fluid {
|
||||
margin-top: 20px;
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
import React, { Component } from 'react';
|
||||
import './App.css';
|
||||
import Home from './Home';
|
||||
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom';
|
||||
import ClientList from './ClientList';
|
||||
import ClientEdit from "./ClientEdit";
|
||||
|
||||
class App extends Component {
|
||||
render() {
|
||||
return (
|
||||
<Router>
|
||||
<Switch>
|
||||
<Route path='/' exact={true} component={Home}/>
|
||||
<Route path='/clients' exact={true} component={ClientList}/>
|
||||
<Route path='/clients/:id' component={ClientEdit}/>
|
||||
</Switch>
|
||||
</Router>
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
export default App;
|
||||
@@ -0,0 +1,8 @@
|
||||
import { render, screen } from '@testing-library/react';
|
||||
import App from './App';
|
||||
|
||||
test('renders learn react link', () => {
|
||||
render(<App />);
|
||||
const linkElement = screen.getByText(/learn react/i);
|
||||
expect(linkElement).toBeInTheDocument();
|
||||
});
|
||||
@@ -0,0 +1,23 @@
|
||||
import React, {Component} from 'react';
|
||||
import {Navbar, NavbarBrand} from 'reactstrap';
|
||||
import {Link} from 'react-router-dom';
|
||||
|
||||
export default class AppNavbar extends Component {
|
||||
constructor(props) {
|
||||
super(props);
|
||||
this.state = {isOpen: false};
|
||||
this.toggle = this.toggle.bind(this);
|
||||
}
|
||||
|
||||
toggle() {
|
||||
this.setState({
|
||||
isOpen: !this.state.isOpen
|
||||
});
|
||||
}
|
||||
|
||||
render() {
|
||||
return <Navbar color="dark" dark expand="md">
|
||||
<NavbarBrand tag={Link} to="/">Home</NavbarBrand>
|
||||
</Navbar>;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
import React, { Component } from 'react';
|
||||
import { Link, withRouter } from 'react-router-dom';
|
||||
import { Button, Container, Form, FormGroup, Input, Label } from 'reactstrap';
|
||||
import AppNavbar from './AppNavbar';
|
||||
|
||||
class ClientEdit extends Component {
|
||||
|
||||
emptyItem = {
|
||||
name: '',
|
||||
email: ''
|
||||
};
|
||||
|
||||
constructor(props) {
|
||||
super(props);
|
||||
this.state = {
|
||||
item: this.emptyItem
|
||||
};
|
||||
this.handleChange = this.handleChange.bind(this);
|
||||
this.handleSubmit = this.handleSubmit.bind(this);
|
||||
}
|
||||
|
||||
async componentDidMount() {
|
||||
if (this.props.match.params.id !== 'new') {
|
||||
const client = await (await fetch(`/clients/${this.props.match.params.id}`)).json();
|
||||
this.setState({item: client});
|
||||
}
|
||||
}
|
||||
|
||||
handleChange(event) {
|
||||
const target = event.target;
|
||||
const value = target.value;
|
||||
const name = target.name;
|
||||
let item = {...this.state.item};
|
||||
item[name] = value;
|
||||
this.setState({item});
|
||||
}
|
||||
|
||||
async handleSubmit(event) {
|
||||
event.preventDefault();
|
||||
const {item} = this.state;
|
||||
|
||||
await fetch('/clients' + (item.id ? '/' + item.id : ''), {
|
||||
method: (item.id) ? 'PUT' : 'POST',
|
||||
headers: {
|
||||
'Accept': 'application/json',
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify(item),
|
||||
});
|
||||
this.props.history.push('/clients');
|
||||
}
|
||||
|
||||
render() {
|
||||
const {item} = this.state;
|
||||
const title = <h2>{item.id ? 'Edit Client' : 'Add Client'}</h2>;
|
||||
|
||||
return <div>
|
||||
<AppNavbar/>
|
||||
<Container>
|
||||
{title}
|
||||
<Form onSubmit={this.handleSubmit}>
|
||||
<FormGroup>
|
||||
<Label for="name">Name</Label>
|
||||
<Input type="text" name="name" id="name" value={item.name || ''}
|
||||
onChange={this.handleChange} autoComplete="name"/>
|
||||
</FormGroup>
|
||||
<FormGroup>
|
||||
<Label for="email">Email</Label>
|
||||
<Input type="text" name="email" id="email" value={item.email || ''}
|
||||
onChange={this.handleChange} autoComplete="email"/>
|
||||
</FormGroup>
|
||||
<FormGroup>
|
||||
<Button color="primary" type="submit">Save</Button>{' '}
|
||||
<Button color="secondary" tag={Link} to="/clients">Cancel</Button>
|
||||
</FormGroup>
|
||||
</Form>
|
||||
</Container>
|
||||
</div>
|
||||
}
|
||||
}
|
||||
|
||||
export default withRouter(ClientEdit);
|
||||
@@ -0,0 +1,81 @@
|
||||
import React, { Component } from 'react';
|
||||
import { Button, ButtonGroup, Container, Table } from 'reactstrap';
|
||||
import AppNavbar from './AppNavbar';
|
||||
import { Link } from 'react-router-dom';
|
||||
|
||||
class ClientList extends Component {
|
||||
|
||||
constructor(props) {
|
||||
super(props);
|
||||
this.state = {clients: [], isLoading: true};
|
||||
this.remove = this.remove.bind(this);
|
||||
}
|
||||
|
||||
componentDidMount() {
|
||||
this.setState({isLoading: true});
|
||||
|
||||
fetch('/clients')
|
||||
.then(response => response.json())
|
||||
.then(data => this.setState({clients: data, isLoading: false}));
|
||||
}
|
||||
|
||||
async remove(id) {
|
||||
await fetch(`/clients/${id}`, {
|
||||
method: 'DELETE',
|
||||
headers: {
|
||||
'Accept': 'application/json',
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
}).then(() => {
|
||||
let updatedClients = [...this.state.clients].filter(i => i.id !== id);
|
||||
this.setState({clients: updatedClients});
|
||||
});
|
||||
}
|
||||
|
||||
render() {
|
||||
const {clients, isLoading} = this.state;
|
||||
|
||||
if (isLoading) {
|
||||
return <p>Loading...</p>;
|
||||
}
|
||||
|
||||
const clientList = clients.map(client => {
|
||||
return <tr key={client.id}>
|
||||
<td style={{whiteSpace: 'nowrap'}}>{client.name}</td>
|
||||
<td>{client.email}</td>
|
||||
<td>
|
||||
<ButtonGroup>
|
||||
<Button size="sm" color="primary" tag={Link} to={"/clients/" + client.id}>Edit</Button>
|
||||
<Button size="sm" color="danger" onClick={() => this.remove(client.id)}>Delete</Button>
|
||||
</ButtonGroup>
|
||||
</td>
|
||||
</tr>
|
||||
});
|
||||
|
||||
return (
|
||||
<div>
|
||||
<AppNavbar/>
|
||||
<Container fluid>
|
||||
<div className="float-right">
|
||||
<Button color="success" tag={Link} to="/clients/new">Add Client</Button>
|
||||
</div>
|
||||
<h3>Clients</h3>
|
||||
<Table className="mt-4">
|
||||
<thead>
|
||||
<tr>
|
||||
<th width="30%">Name</th>
|
||||
<th width="30%">Email</th>
|
||||
<th width="40%">Actions</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{clientList}
|
||||
</tbody>
|
||||
</Table>
|
||||
</Container>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export default ClientList;
|
||||
@@ -0,0 +1,20 @@
|
||||
import React, { Component } from 'react';
|
||||
import './App.css';
|
||||
import AppNavbar from './AppNavbar';
|
||||
import { Link } from 'react-router-dom';
|
||||
import { Button, Container } from 'reactstrap';
|
||||
|
||||
class Home extends Component {
|
||||
render() {
|
||||
return (
|
||||
<div>
|
||||
<AppNavbar/>
|
||||
<Container fluid>
|
||||
<Button color="link"><Link to="/clients">Clients</Link></Button>
|
||||
</Container>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export default Home;
|
||||
@@ -0,0 +1,13 @@
|
||||
body {
|
||||
margin: 0;
|
||||
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
|
||||
'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
|
||||
sans-serif;
|
||||
-webkit-font-smoothing: antialiased;
|
||||
-moz-osx-font-smoothing: grayscale;
|
||||
}
|
||||
|
||||
code {
|
||||
font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New',
|
||||
monospace;
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
import React from 'react';
|
||||
import ReactDOM from 'react-dom';
|
||||
import './index.css';
|
||||
import App from './App';
|
||||
import reportWebVitals from './reportWebVitals';
|
||||
import 'bootstrap/dist/css/bootstrap.min.css';
|
||||
|
||||
ReactDOM.render(
|
||||
<React.StrictMode>
|
||||
<App />
|
||||
</React.StrictMode>,
|
||||
document.getElementById('root')
|
||||
);
|
||||
|
||||
// If you want to start measuring performance in your app, pass a function
|
||||
// to log results (for example: reportWebVitals(console.log))
|
||||
// or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals
|
||||
reportWebVitals();
|
||||
File diff suppressed because one or more lines are too long
|
After Width: | Height: | Size: 2.6 KiB |
@@ -0,0 +1,13 @@
|
||||
const reportWebVitals = onPerfEntry => {
|
||||
if (onPerfEntry && onPerfEntry instanceof Function) {
|
||||
import('web-vitals').then(({ getCLS, getFID, getFCP, getLCP, getTTFB }) => {
|
||||
getCLS(onPerfEntry);
|
||||
getFID(onPerfEntry);
|
||||
getFCP(onPerfEntry);
|
||||
getLCP(onPerfEntry);
|
||||
getTTFB(onPerfEntry);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
export default reportWebVitals;
|
||||
@@ -0,0 +1,5 @@
|
||||
// jest-dom adds custom jest matchers for asserting on DOM nodes.
|
||||
// allows you to do things like:
|
||||
// expect(element).toHaveTextContent(/react/i)
|
||||
// learn more: https://github.com/testing-library/jest-dom
|
||||
import '@testing-library/jest-dom';
|
||||
Reference in New Issue
Block a user