Advertisement
Guest User

Code cleanup

a guest
Oct 29th, 2016
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const Api = {
  2.  
  3.   signUp(user) {
  4.     console.log(Constant.default.apiUrl);
  5.     return fetch(Constant.default.apiUrl + '/signup', {
  6.       method: 'POST',
  7.       headers: {
  8.         'Accept': 'application/json',
  9.         'Content-Type': 'application/json',
  10.       },
  11.       body: JSON.stringify(user)
  12.     })
  13.     .then(response => response.json())
  14.     .then(responseJson => {
  15.       if (responseJson.success) {
  16.         console.log(responseJson.userData);
  17.         return responseJson.userData;
  18.       } else {
  19.         throw responseJson;
  20.       }
  21.     })
  22.     .catch(err => {
  23.         throw {
  24.             // this just makes the error source obvious
  25.             message: 'Error in sign up',
  26.             error: err
  27.         };
  28.     });
  29.   },
  30.  
  31.   login(user) {
  32.     return fetch(Constant.default.apiUrl + '/login', {
  33.       method: 'POST',
  34.       headers: {
  35.         'Accept': 'application/json',
  36.         'Content-Type': 'application/json',
  37.       },
  38.       body: JSON.stringify(user)
  39.     })
  40.     .then(response => response.json())
  41.     .then(responseJson => {
  42.       console.log("responsejson " + responseJson);
  43.       console.log("laaaa");
  44.       if (responseJson.success) {
  45.         return responseJson.userData;
  46.       } else {
  47.         throw responseJson;
  48.       }
  49.     })
  50.     .catch(err => {
  51.         throw {
  52.             // this just makes the error source obvious
  53.             message: 'Error in login',
  54.             error: err
  55.         };
  56.     });
  57.   }
  58. };
  59.  
  60. export default Api;
  61.  
  62. function register() {
  63.  
  64.   if (this.state.password.length > 3 && this.state.password === this.state.confirmPassword) {
  65.     var user = {
  66.       firstName: this.props.firstName,
  67.       lastName: this.props.lastName,
  68.       emailAddress: this.state.emailAddress,
  69.       password: this.state.password,
  70.     };
  71.  
  72.     Api.signUp(user)
  73.     .then((result) => {
  74.         console.log("sign up result: " + JSON.stringify(result));
  75.         return Api.login(user);
  76.     })
  77.     .then(result => {
  78.       console.log("login result: " + JSON.stringify(result));
  79.     })
  80.     .catch(resultErr => console.log(resultErr));
  81.   } else {
  82.     this.setState({
  83.       error: true,
  84.       errorMessage: 'Veuillez remplir les champs correctement'
  85.     });
  86.   }
  87. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement