-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- add an ability to create/update/delete persons
- move persons list into separated component
- Loading branch information
Showing
11 changed files
with
341 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import React from 'react'; | ||
|
||
import Persons from './persons'; | ||
|
||
export default function App() { | ||
return ( | ||
<div className="container"> | ||
<div className="header"> | ||
<h3> | ||
Backendless React Addresses Book | ||
</h3> | ||
</div> | ||
|
||
<Persons/> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import React, { Component } from 'react'; | ||
import { connect } from 'react-redux' | ||
import Modal from 'react-bootstrap/Modal'; | ||
import Button from 'react-bootstrap/Button'; | ||
|
||
import { removePerson } from '../store'; | ||
|
||
class DeletePersonConfirmation extends Component { | ||
|
||
state = { | ||
removing: false, | ||
}; | ||
|
||
close = () => { | ||
this.setState({ | ||
removing: false, | ||
}); | ||
|
||
this.props.onHide() | ||
}; | ||
|
||
confirm = () => { | ||
const { person } = this.props; | ||
|
||
this.setState({ removing: true }); | ||
|
||
this.props.removePerson(person.objectId) | ||
.then(() => this.close()) | ||
}; | ||
|
||
render() { | ||
const { person, show } = this.props; | ||
const { removing } = this.state; | ||
|
||
return ( | ||
<Modal show={show} onHide={this.close}> | ||
<Modal.Header className="bg-danger text-white" closeButton> | ||
<Modal.Title> | ||
Delete Person Confirmation | ||
</Modal.Title> | ||
</Modal.Header> | ||
|
||
<Modal.Body> | ||
<p> | ||
Are you seriously want to delete "<b>{person && person.name}</b>" | ||
</p> | ||
</Modal.Body> | ||
|
||
<Modal.Footer> | ||
<Button variant="secondary" onClick={this.close} disabled={removing}>No</Button> | ||
<Button variant="danger" onClick={this.confirm} disabled={removing}>Yes</Button> | ||
</Modal.Footer> | ||
</Modal> | ||
); | ||
} | ||
} | ||
|
||
export default connect(null, { removePerson })(DeletePersonConfirmation); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
import React, { Component } from 'react'; | ||
import { connect } from 'react-redux' | ||
import Modal from 'react-bootstrap/Modal'; | ||
import Button from 'react-bootstrap/Button'; | ||
import Alert from 'react-bootstrap/Alert'; | ||
|
||
import { createPerson, updatePerson } from '../store'; | ||
|
||
class PersonEditor extends Component { | ||
|
||
constructor(props) { | ||
super(props); | ||
|
||
this.state = { | ||
person : props.person || {}, | ||
saving : false, | ||
serverError: null, | ||
} | ||
} | ||
|
||
componentWillReceiveProps(nextProps) { | ||
const prevPerson = this.props.person || {}; | ||
const nextPerson = nextProps.person || {}; | ||
|
||
if (prevPerson.objectId !== nextPerson.objectId) { | ||
this.setState({ person: nextPerson }) | ||
} | ||
} | ||
|
||
close = () => { | ||
this.setState({ | ||
person : {}, | ||
saving : false, | ||
serverError: null | ||
}); | ||
|
||
this.props.onHide() | ||
}; | ||
|
||
preparePerson() { | ||
const { person } = this.state; | ||
|
||
return { | ||
...person, | ||
name : (person.name || '').trim() || null, | ||
address: (person.address || '').trim() || null, | ||
} | ||
} | ||
|
||
save = () => { | ||
const person = this.preparePerson(); | ||
|
||
const action = this.props.person | ||
? this.props.updatePerson | ||
: this.props.createPerson; | ||
|
||
action(person) | ||
.then(() => this.close()) | ||
.catch(e => this.setState({ serverError: e.message })); | ||
}; | ||
|
||
onNameChange = e => this.setState({ person: { ...this.state.person, name: e.target.value } }); | ||
onAddressChange = e => this.setState({ person: { ...this.state.person, address: e.target.value } }); | ||
|
||
render() { | ||
const { show } = this.props; | ||
const { person, serverError, saving } = this.state; | ||
|
||
const isNew = !this.props.person; | ||
|
||
return ( | ||
<Modal show={show} onHide={this.close}> | ||
<Modal.Header closeButton> | ||
<Modal.Title> | ||
{isNew ? 'Add' : 'Edit'} Person | ||
</Modal.Title> | ||
</Modal.Header> | ||
|
||
<Modal.Body> | ||
<form> | ||
<div className="form-group"> | ||
<label>Name:</label> | ||
<input | ||
className="form-control" | ||
placeholder="Input name" | ||
value={person.name || ''} | ||
onChange={this.onNameChange} | ||
/> | ||
</div> | ||
|
||
<div className="form-group"> | ||
<label>Address:</label> | ||
<input | ||
className="form-control" | ||
placeholder="Input address" | ||
value={person.address || ''} | ||
onChange={this.onAddressChange} | ||
/> | ||
</div> | ||
|
||
{serverError && ( | ||
<Alert variant="danger"> | ||
{serverError} | ||
</Alert> | ||
)} | ||
</form> | ||
</Modal.Body> | ||
|
||
<Modal.Footer> | ||
<Button variant="secondary" onClick={this.close}> | ||
Close | ||
</Button> | ||
<Button variant="primary" onClick={this.save} disabled={saving}> | ||
{saving ? 'Saving...' : 'Save'} | ||
</Button> | ||
</Modal.Footer> | ||
</Modal> | ||
); | ||
} | ||
} | ||
|
||
export default connect(null, { createPerson, updatePerson })(PersonEditor); |
Oops, something went wrong.