From 45dcdf2a0df5067dce63bc52471b6854e6b25960 Mon Sep 17 00:00:00 2001 From: Sallo Szrajbman Date: Sat, 3 Apr 2021 13:51:08 +0100 Subject: [PATCH] BAEL-4842 - Use React and Spring Boot to Build a Simple CRUD App Updating PR to reduce complexity of react components Fixing react test --- .../frontend/src/App.test.js | 2 +- .../frontend/src/ClientEdit.js | 26 +++++++++---------- .../frontend/src/ClientList.js | 12 +++------ spring-boot-modules/spring-boot-react/pom.xml | 5 ++++ 4 files changed, 22 insertions(+), 23 deletions(-) diff --git a/spring-boot-modules/spring-boot-react/frontend/src/App.test.js b/spring-boot-modules/spring-boot-react/frontend/src/App.test.js index 1f03afeece..ccf118f439 100644 --- a/spring-boot-modules/spring-boot-react/frontend/src/App.test.js +++ b/spring-boot-modules/spring-boot-react/frontend/src/App.test.js @@ -3,6 +3,6 @@ import App from './App'; test('renders learn react link', () => { render(); - const linkElement = screen.getByText(/learn react/i); + const linkElement = screen.getByText(/Clients/i); expect(linkElement).toBeInTheDocument(); }); diff --git a/spring-boot-modules/spring-boot-react/frontend/src/ClientEdit.js b/spring-boot-modules/spring-boot-react/frontend/src/ClientEdit.js index c7a03c0aac..6eafd203ce 100644 --- a/spring-boot-modules/spring-boot-react/frontend/src/ClientEdit.js +++ b/spring-boot-modules/spring-boot-react/frontend/src/ClientEdit.js @@ -35,20 +35,20 @@ class ClientEdit extends Component { this.setState({item}); } - async handleSubmit(event) { - event.preventDefault(); - const {item} = this.state; +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'); - } + 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; diff --git a/spring-boot-modules/spring-boot-react/frontend/src/ClientList.js b/spring-boot-modules/spring-boot-react/frontend/src/ClientList.js index 6cce7cc168..a2bfce6330 100644 --- a/spring-boot-modules/spring-boot-react/frontend/src/ClientList.js +++ b/spring-boot-modules/spring-boot-react/frontend/src/ClientList.js @@ -7,16 +7,14 @@ class ClientList extends Component { constructor(props) { super(props); - this.state = {clients: [], isLoading: true}; + this.state = {clients: []}; 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})); + .then(data => this.setState({clients: data})); } async remove(id) { @@ -33,11 +31,7 @@ class ClientList extends Component { } render() { - const {clients, isLoading} = this.state; - - if (isLoading) { - return

Loading...

; - } + const {clients} = this.state; const clientList = clients.map(client => { return diff --git a/spring-boot-modules/spring-boot-react/pom.xml b/spring-boot-modules/spring-boot-react/pom.xml index b1d33ac66c..b12ad36cb0 100644 --- a/spring-boot-modules/spring-boot-react/pom.xml +++ b/spring-boot-modules/spring-boot-react/pom.xml @@ -25,16 +25,19 @@ org.springframework.boot spring-boot-starter-web + 2.4.3 org.springframework.boot spring-boot-starter-data-jpa + 2.4.3 com.h2database h2 + 1.4.200 runtime @@ -47,6 +50,7 @@ org.springframework.boot spring-boot-starter-test + 2.4.3 test @@ -55,6 +59,7 @@ maven-resources-plugin + 3.1.0 copy-resources