homeflowjs 1.0.83 → 1.0.85

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "homeflowjs",
3
- "version": "1.0.83",
3
+ "version": "1.0.85",
4
4
  "sideEffects": [
5
5
  "modal/**/*",
6
6
  "user/default-profile/**/*",
@@ -1,35 +1,78 @@
1
- import React from 'react';
1
+ import React, { useRef, useState, useEffect } from 'react';
2
+ import PropTypes from 'prop-types';
2
3
  import { connect } from 'react-redux';
3
4
 
4
5
  import notify from '../../app/notify';
5
- import { signInUser } from '../../actions/user.actions'
6
+ import { signInUser } from '../../actions/user.actions';
6
7
  import { setLoading } from '../../actions/app.actions';
8
+ import { useRecaptcha } from '../../hooks';
9
+
10
+ const UserSignInForm = ({
11
+ children,
12
+ signInUser,
13
+ userCredentials,
14
+ setLoading,
15
+ withRecaptcha,
16
+ ...otherProps
17
+ }) => {
18
+ const formRef = useRef(null);
19
+ const [isRequestCompleted, setIsRequestCompleted] = useState(null);
20
+
21
+ const onRecaptchaSubmit = (recaptchaData) => {
22
+ signInUser(userCredentials).then(({ success }) => {
23
+ if (success) {
24
+ notify('You have successfully signed in', 'success');
25
+ } else {
26
+ notify('Sign in failed, please try again', 'error');
27
+ }
28
+ })
29
+ .catch(() => notify('Sign in failed, please try again', 'error'))
30
+ .finally(() => {
31
+ setLoading({ userSignIn: false });
32
+ setIsRequestCompleted((prevState) => !prevState)
33
+ });
34
+ };
35
+
36
+ const { onRecaptchaRender, onResetRecaptcha } = useRecaptcha({
37
+ formID: 'login-form',
38
+ formRef,
39
+ onRecaptchaSubmit,
40
+ });
41
+
42
+ useEffect(() => {
43
+ if (isRequestCompleted) {
44
+ onResetRecaptcha();
45
+ }
46
+ }, [isRequestCompleted]);
7
47
 
8
- const UserSignInForm = ({ children, signInUser, userCredentials, setLoading, ...otherProps }) => {
9
48
  const handleSubmit = (e) => {
10
49
  e.preventDefault();
11
50
  setLoading({ userSignIn: true });
12
-
13
- signInUser(userCredentials)
14
- .then(({ success }) => {
15
- if (success) {
16
- notify('You have successfully signed in', 'success');
17
- } else {
18
- notify('Sign in failed, please try again', 'error');
19
- }
20
- })
21
- .catch(() => notify('Sign in failed, please try again', 'error'))
22
- .finally(() => setLoading({ userSignIn: false }));
51
+ setIsRequestCompleted(false);
52
+ if (withRecaptcha) {
53
+ onRecaptchaRender();
54
+ } else {
55
+ onRecaptchaSubmit();
56
+ }
23
57
  };
24
58
 
25
59
  return (
26
- <form onSubmit={handleSubmit} {...otherProps}>
60
+ <form onSubmit={handleSubmit} {...otherProps} ref={formRef}>
27
61
  {children}
28
62
  </form>
29
63
  );
30
64
  };
31
65
 
32
- const mapStateToProps = state => ({
66
+ UserSignInForm.propTypes = {
67
+ userCredentials: PropTypes.object.isRequired,
68
+ withRecaptcha: PropTypes.bool,
69
+ };
70
+
71
+ UserSignInForm.defaultProps = {
72
+ withRecaptcha: false,
73
+ };
74
+
75
+ const mapStateToProps = (state) => ({
33
76
  userCredentials: state.user.userCredentials,
34
77
  });
35
78
 
@@ -38,7 +81,4 @@ const mapDispatchToProps = {
38
81
  setLoading,
39
82
  };
40
83
 
41
- export default connect(
42
- mapStateToProps,
43
- mapDispatchToProps,
44
- )(UserSignInForm)
84
+ export default connect(mapStateToProps, mapDispatchToProps)(UserSignInForm);