Guest User

Untitled

a guest
Aug 21st, 2018
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.20 KB | None | 0 0
  1. import { connect } from 'react-redux';
  2. import { userActions } from '../_actions';
  3.  
  4. class RegisterPage extends React.Component {
  5. constructor(props) {
  6. super(props);
  7.  
  8. this.state = {
  9. user: {
  10. first_name: '',
  11. last_name: '',
  12. properties_id: '',
  13. email: '',
  14. phone_number: '',
  15. password: ''
  16. },
  17. submitted: false,
  18. checked: false,
  19. };
  20.  
  21. this.handleChange = this.handleChange.bind(this);
  22. this.handleSubmit = this.handleSubmit.bind(this);
  23. }
  24.  
  25. componentDidMount() {
  26. this.props.dispatch(userActions.getAll());
  27. }
  28.  
  29. handleChange(event) {
  30. const { name, value } = event.target;
  31. const { user } = this.state;
  32. this.setState({
  33. user: {
  34. ...user,
  35. [name]: value
  36. },
  37. checked: !this.state.checked
  38. });
  39. }
  40.  
  41. handleSubmit(event) {
  42. event.preventDefault();
  43.  
  44. this.setState({ submitted: true });
  45. const { user } = this.state;
  46. const { dispatch } = this.props;
  47.  
  48. if(this.state.checked) {
  49. if (user.first_name && user.last_name && user.properties_id &&
  50. user.email && user.phone_number && user.password) {
  51. dispatch(userActions.register(user));
  52. }
  53. } else {
  54. alert("Please tick the checkbox to agree to Terms and Conditions");
  55. }
  56. }
  57.  
  58. render() {
  59. const { registering, properties } = this.props;
  60. const { user, submitted } = this.state;
  61. return (......)
  62.  
  63. }
  64.  
  65. function mapStateToProps(state) {
  66. const { registering } = state.registration;
  67. const { properties } = state;
  68. return {
  69. properties,
  70. registering
  71. };
  72. }
  73.  
  74. const connectedRegisterPage = connect(mapStateToProps)(RegisterPage);
  75. export { connectedRegisterPage as RegisterPage };
  76.  
  77. export function users(state = {}, action) {
  78. switch (action.type) {
  79. case userConstants.GETALL_REQUEST:
  80. return {
  81. loading: true
  82. };
  83. case userConstants.GETALL_SUCCESS:
  84. return {
  85. items: action.properties
  86. //action.users
  87. };
  88. case userConstants.GETALL_FAILURE:
  89. return {
  90. error: action.error
  91. };
  92. default:
  93. return state
  94. }
  95. }
  96.  
  97. export const userActions = {
  98. login,
  99. logout,
  100. register,
  101. getAll,
  102. delete: _delete
  103. };
  104.  
  105. function getAll() {
  106. return dispatch => {
  107. dispatch(request());
  108.  
  109. userService.getAll()
  110. .then(
  111. properties => dispatch(success(properties)),
  112. error => dispatch(failure(error.toString()))
  113. );
  114. };
  115.  
  116. function request() { return { type: userConstants.GETALL_REQUEST } }
  117. function success(properties) { return { type: userConstants.GETALL_SUCCESS, properties } }
  118. function failure(error) { return { type: userConstants.GETALL_FAILURE, error } }
  119. }
  120.  
  121. // Get All Properties
  122. function getAll() {
  123. const requestOptions = {
  124. method: 'GET'
  125. };
  126.  
  127. return fetch(`${config.apiUrl}/api/properties`, requestOptions).then(handleResponse).then(
  128. properties => {
  129. return properties;
  130. }
  131. );
  132. }
Add Comment
Please, Sign In to add comment