Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.29 KB | None | 0 0
  1. import React from 'react';
  2. import { connect } from "react-redux"
  3. import { createPerson } from '../../actions/peopleActions';
  4.  
  5. @connect(
  6. (store) => {
  7. return {
  8. people: store.people.people,
  9. };
  10. },
  11. (dispatch) => {
  12. return {
  13. createPerson: () => {
  14. dispatch(createPerson());
  15. }
  16. }
  17. }
  18. )
  19. export default class PeopleSearch extends React.Component {
  20.  
  21. createPerson = () => {
  22. this.props.createPerson();
  23. /*****************
  24. This is where I want to be able to execute only once the new person
  25. is created and get the ID of the newly created record. But currently,
  26. this next line outputs the people array as it exists before the new
  27. record is created.
  28. *****************/
  29. console.log("After createPerson, with: ", this.props.people);
  30. };
  31.  
  32. render = () => {
  33. return (
  34. <div>
  35. <button onClick={this.createPerson}>
  36. Create Person
  37. </button>
  38. </div>);
  39. }
  40. }
  41.  
  42. import axios from "axios";
  43. import cookie from "react-cookie";
  44.  
  45. import config from "../config.js";
  46.  
  47. export function createPerson(fName, mName, lName, sexAtBirth, notes) {
  48.  
  49. const body = {
  50. object: {
  51. fName,
  52. mName,
  53. lName,
  54. sexAtBirth,
  55. notes
  56. }
  57. };
  58. return (dispatch) => {
  59. dispatch({type: "CREATE_PERSON"});
  60. axios.post(config.api_url + "/api/v2/person/create", body, axiosConfig)
  61. .then((response) => {
  62. dispatch({type: "CREATE_PERSON_FULFILLED", payload: response.data})
  63. })
  64. .catch((err) => {
  65. dispatch({type: "CREATE_PERSON_REJECTED", payload: err})
  66. })
  67. }
  68. }
  69.  
  70. export default function reducer(
  71. state={
  72. people: [],
  73. fetching: false,
  74. error: null,
  75. },
  76. action = ""
  77. ) {
  78. case "CREATE_PERSON": {
  79. return {
  80. ...state,
  81. fetching: true
  82. };
  83. }
  84. case "CREATE_PERSON_FULFILLED": {
  85. return {
  86. ...state,
  87. fetching: false,
  88. people: [
  89. ...state.people,
  90. action.payload
  91. ]
  92. };
  93. }
  94. case "CREATE_PERSON_REJECTED": {
  95. return {
  96. ...state,
  97. fetching: false,
  98. error: action.payload
  99. };
  100. }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement