Skip to content
Permalink
Browse files

- enable RT for Persons

  • Loading branch information...
Vladmir Upirov
Vladmir Upirov committed Mar 22, 2019
1 parent a9f2548 commit 100989023d59e0a7df8ff9a98b68933802fd6f4d
Showing with 67 additions and 17 deletions.
  1. +16 −3 src/persons/index.js
  2. +4 −0 src/store/action-types.js
  3. +15 −0 src/store/actions/persons.js
  4. +32 −14 src/store/reducers/persons.js
@@ -2,8 +2,9 @@ import React, { Component } from 'react';
import { connect } from 'react-redux'
import Button from 'react-bootstrap/Button';
import ButtonGroup from 'react-bootstrap/ButtonGroup';
import Backendless from 'backendless';

import { loadPersons, getPersons } from '../store';
import { loadPersons, getPersons, onPersonCreate, onPersonUpdate, onPersonRemove } from '../store';

import Editor from './editor';
import DeleteConfirmation from './delete-confirmation';
@@ -35,8 +36,20 @@ class Persons extends Component {
showDeleteConfirmation = person => this.setState({ showDeleteConfirmation : true, deleteConfirmationProps: { person } });
hideDeleteConfirmation = () => this.setState({ showDeleteConfirmation: false, deleteConfirmationProps: null });

componentWillMount() {
componentWillMount(){
this.props.loadPersons();

this.personRT = Backendless.Data.of('Person').rt();

this.personRT.addCreateListener(this.props.onPersonCreate);
this.personRT.addUpdateListener(this.props.onPersonUpdate);
this.personRT.addDeleteListener(this.props.onPersonRemove);
}

componentWillUnmount(){
this.personRT.removeCreateListener(this.props.onPersonCreate);
this.personRT.removeUpdateListener(this.props.onPersonUpdate);
this.personRT.removeDeleteListener(this.props.onPersonRemove);
}

onAddClick = () => this.showEditor(null);
@@ -100,4 +113,4 @@ class Persons extends Component {
}
}

export default connect(mapStateToProps, { loadPersons })(Persons);
export default connect(mapStateToProps, { loadPersons, onPersonCreate, onPersonUpdate, onPersonRemove })(Persons);
@@ -6,6 +6,10 @@ export default mirrorKeys({
CREATE_PERSON_SUCCESS: null,
UPDATE_PERSON_SUCCESS: null,
REMOVE_PERSON_SUCCESS: null,

ON_PERSON_CREATE: null,
ON_PERSON_UPDATE: null,
ON_PERSON_REMOVE: null,
});

function mirrorKeys(obj) {
@@ -22,3 +22,18 @@ export const removePerson = personId => ({
types : [null, t.REMOVE_PERSON_SUCCESS, null],
apiCall: () => Backendless.Data.of('Person').remove(personId),
});

export const onPersonCreate = person => ({
person,
type: t.ON_PERSON_CREATE,
});

export const onPersonUpdate = person => ({
person,
type: t.ON_PERSON_UPDATE,
});

export const onPersonRemove = person => ({
person,
type: t.ON_PERSON_REMOVE,
});
@@ -12,21 +12,39 @@ const personsReducer = reduceReducers(initialState,
})),

reducersMap({
[t.CREATE_PERSON_SUCCESS]: (state, action) => ({
...state,
list: state.list.concat(action.result)
}),

[t.UPDATE_PERSON_SUCCESS]: (state, action) => ({
...state,
list: state.list.map(oldPerson => oldPerson.objectId === action.result.objectId ? action.result : oldPerson)
}),

[t.REMOVE_PERSON_SUCCESS]: (state, action) => ({
...state,
list: state.list.filter(person => person.objectId !== action.personId)
})
[t.CREATE_PERSON_SUCCESS]: (state, action) => addPerson(state, action.result),
[t.UPDATE_PERSON_SUCCESS]: (state, action) => updatePerson(state, action.result),
[t.REMOVE_PERSON_SUCCESS]: (state, action) => deletePerson(state, action.personId),

[t.ON_PERSON_CREATE]: (state, action) => addPerson(state, action.person),
[t.ON_PERSON_UPDATE]: (state, action) => updatePerson(state, action.person),
[t.ON_PERSON_REMOVE]: (state, action) => deletePerson(state, action.person.objectId)
})
);

function addPerson(state, person) {
if (state.list.find(p => p.objectId === person.objectId)) {
return state
}

return {
...state,
list: state.list.concat(person)
}
}

function updatePerson(state, person) {
return {
...state,
list: state.list.map(p => p.objectId === person.objectId ? person : p)
}
}

function deletePerson(state, personId) {
return {
...state,
list: state.list.filter(person => person.objectId !== personId)
}
}

export default personsReducer

0 comments on commit 1009890

Please sign in to comment.
You can’t perform that action at this time.