BAEL-4842 - Use React and Spring Boot to Build a Simple CRUD App

Updating PR to reduce complexity of react components
Fixing react test
This commit is contained in:
Sallo Szrajbman
2021-04-03 13:51:08 +01:00
parent 6a82efdd39
commit 86d75ea394
4 changed files with 22 additions and 23 deletions

View File

@@ -3,6 +3,6 @@ import App from './App';
test('renders learn react link', () => {
render(<App />);
const linkElement = screen.getByText(/learn react/i);
const linkElement = screen.getByText(/Clients/i);
expect(linkElement).toBeInTheDocument();
});

View File

@@ -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;

View File

@@ -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 <p>Loading...</p>;
}
const {clients} = this.state;
const clientList = clients.map(client => {
return <tr key={client.id}>