class NewPerson extends React.Component {
addPerson() {
var name = this.refs.name.value;
var surname = this.refs.surname.value;
this.refs.name.value = "";
this.refs.surname.value = "";
var person = {'name': name, 'surname': surname};
$.ajax({
url: 'rest/people/',
type: 'POST',
data: person,
success: (response) => {
console.log('Person added!', response);
}
});
}
render() {
return (
)
}
}
class PeopleList extends React.Component{
render() {
var people = this.props.people.map(person =>
);
return (
)
}
}
class Person extends React.Component{
constructor(props) {
super(props);
this.state = {editable: false, isModalOpen: false};
}
handleDelete(id) {
if (confirm('Está a punto de eliminar a una persona. ¿Está seguro de que desea continuar?')) {
$.ajax({
url: 'rest/people/' + id,
type: 'DELETE',
success(response) {
console.log('Person successfully removed');
}
});
}
}
handleEdit(){
if (this.state.editable) {
var id = this.refs.id.value;
var name = this.refs.name.value;
var surname = this.refs.surname.value;
var person = {'id': id, 'name': name, 'surname': surname};
}
$.ajax({
url: 'rest/people/' + id,
type: 'PUT',
data: person,
success: (response) => {
console.log('Person updated!', response);
}
});
this.setState({editable: !this.state.editable});
}
openModal() {
this.setState({isModalOpen: true});
}
closeModal() {
this.setState({isModalOpen: false });
}
render() {
var id = this.state.editable ? : {this.props.person.id};
var name = this.state.editable ? : {this.props.person.name};
var surname = this.state.editable ? : {this.props.person.surname};
return (
{id} |
{name} |
{surname} |
this.openModal()}>
this.closeModal()}>
|
{this.state.editable ? 'Submit' : 'Edit' }
|
|
)
}
}
class App extends React.Component {
constructor(props) {
super(props);
this.state = {people: []};
}
componentDidMount() {
$.ajax({
url: 'rest/people',
type: 'GET',
success: (response) => {
this.setState({people: response})
}
});
}
componentWillUpdate() {
$.ajax({
url: 'rest/people',
type: 'GET',
success: (response) => {
this.setState({people: response})
}
});
}
render() {
return (
)
}
}
class NewPet extends React.Component {
constructor(props) {
super(props);
}
addPet() {
var name = this.refs.name.value;
var kind = this.refs.kind.value;
var breed = this.refs.breed.value;
var owner = this.props.owner;
this.refs.name.value = "";
this.refs.kind.value = "";
this.refs.breed.value = "";
var pet = {'name': name, 'kind': kind, 'breed': breed, 'owner': owner};
$.ajax({
url: 'rest/pets/',
type: 'POST',
data: pet,
success: (response) => {
console.log('Pet added!', response);
}
});
}
render() {
return (
)
}
}
class PetsList extends React.Component{
constructor(props) {
super(props);
}
render() {
var pets = this.props.pets.map(pet =>
);
return (
Pets de {this.props.owner_name}
# |
Name |
Kind |
breed |
owner |
|
|
{pets}
)
}
}
class Pet extends React.Component{
constructor(props) {
super(props);
this.state = {editable: false};
}
handleDelete(id) {
if (confirm('Está a punto de eliminar a una mascota. ¿Está seguro de que desea continuar?')) {
$.ajax({
url: 'rest/pets/' + id,
type: 'DELETE',
success(response) {
console.log('Pet successfully removed');
}
});
}
}
handleEdit(id){
if (this.state.editable) {
var id = id;
var name = this.refs.name.value;
var kind = this.refs.kind.value;
var breed = this.refs.breed.value;
var owner = this.refs.owner.value;
var pet = {'id': id, 'name': name, 'kind': kind, 'breed': breed, 'owner': owner};
}
$.ajax({
url: 'rest/pets/' + id,
type: 'PUT',
data: pet,
success: (response) => {
console.log('Pet updated!', response);
}
});
this.setState({editable: !this.state.editable});
}
render() {
var id = this.state.editable ? : {this.props.pet.id};
var name = this.state.editable ? : {this.props.pet.name};
var kind = this.state.editable ? : {this.props.pet.kind};
var breed = this.state.editable ? : {this.props.pet.breed};
var owner = this.state.editable ? : {this.props.pet.owner};
return (
{id} |
{name} |
{kind} |
{breed} |
{owner} |
{this.state.editable ? 'Submit' : 'Edit' }
|
|
)
}
}
class PetsApp extends React.Component {
constructor(props) {
super(props);
this.state = {pets: []};
}
componentDidMount() {
$.ajax({
url: 'rest/people/' + this.props.id + '/pets' ,
type: 'GET',
success: (response) => {
this.setState({pets: response})
}
});
}
componentWillUnmount() {
}
componentWillUpdate() {
$.ajax({
url: 'rest/people/' + this.props.id + '/pets' ,
type: 'GET',
success: (response) => {
this.setState({pets: response})
}
});
}
render() {
return (
)
}
}
ReactDOM.render(
,
document.getElementById('people-container')
)
class Modal extends React.Component {
render() {
if (this.props.isOpen === false)
return null
let modalStyle = {
position: 'absolute',
top: '40%',
left: '50%',
transform: 'translate(-50%, -50%)',
zIndex: '9999',
background: '#fff'
}
let backdropStyle = {
position: 'absolute',
width: '100%',
height: '100%',
top: '0px',
left: '0px',
zIndex: '9998',
background: 'rgba(0, 0, 0, 0.3)'
}
return (
{this.props.children}
this.close(e)}/>}
)
}
close(e) {
e.preventDefault()
if (this.props.onClose) {
this.props.onClose()
}
}
}