Guest User

Untitled

a guest
Dec 6th, 2017
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. import React from 'react';
  2. import { View, Text , TouchableOpacity , TextInput } from 'react-native';
  3. import { connect } from "react-redux";
  4. import { bindActionCreators } from "redux";
  5. import * as signUpActions from "../actions/SignUpActions";
  6.  
  7. class SignUp extends React.Component {
  8.  
  9. constructor(){
  10. super();
  11. this.state = {
  12. name : '',
  13. password : '',
  14. };
  15. }
  16.  
  17. saveUser(){
  18. let user = {};
  19. user.name = this.state.name;
  20. user.password = this.state.password;
  21. this.props.registerUser(user);
  22. }
  23.  
  24. static navigationOptions = {
  25. title : 'Sign Up',
  26. };
  27.  
  28. render(){
  29. return (
  30. <View>
  31. <TextInput
  32. placeholder="Username"
  33. onChangeText={(text) => this.setState({name : text})}
  34. />
  35. <TextInput
  36. placeholder="Password"
  37. onChangeText={(text) => this.setState({password : text})}
  38. />
  39. <TouchableOpacity onPress = {() => this.saveUser()} >
  40. <Text>DONE</Text>
  41. </TouchableOpacity>
  42. </View>
  43. );
  44. }
  45. }
  46.  
  47. export default connect(
  48. state => ({
  49. user : state.user
  50. }),
  51. dispatch => bindActionCreators(signUpActions, dispatch)
  52. )(SignUp);
  53.  
  54. function storeUser(user) {
  55. return {
  56. type : 'REGISTER_USER',
  57. payload : user,
  58. };
  59. };
  60.  
  61. export function registerUser(user) {
  62. return function (dispatch, getState) {
  63. fetch(<the-url>)
  64. .then((response) => {return response.json()})
  65. .then((responseData) => dispatch(storeUser(responseData)))
  66. .catch((err) => console.log(err));
  67. };
  68. };
  69.  
  70. const initialState = {
  71. data : {},
  72. };
  73.  
  74. export default function signUpReducer(state = initialState, action) {
  75. console.log(action.payload)
  76. switch (action.type) {
  77. case 'REGISTER_USER' :
  78. return {
  79. ...state ,
  80. user : action.payload
  81. }
  82. default :
  83. return state;
  84. }
  85. }
Add Comment
Please, Sign In to add comment