瀏覽代碼

fix: isValid prop for auth form

Entony 6 年之前
父節點
當前提交
dfc2f31f21
共有 1 個文件被更改,包括 8 次插入14 次删除
  1. 8 14
      src/components/signIn/index.js

+ 8 - 14
src/components/signIn/index.js

@@ -6,12 +6,10 @@ import InputComponent from "../common";
 
 class SignIn extends Component {
 	componentDidMount() {
-		const { history } = this.props;
 		const token = localStorage.getItem("token");
 
 		if (token) {
-			history.push("/");
-			// this.setState({ token });
+			this.setState({ token });
 		}
 	}
 
@@ -45,13 +43,12 @@ class SignIn extends Component {
 				}
 			}
 		},
-		// isValid: false
+		isValid: false,
 		token: null
 	};
 
 	submit = e => {
 		e.preventDefault();
-		const { history } = this.props;
 		const { singIn } = this.state;
 		// https://test-app-a-level.herokuapp.com
 		const values = Object.keys(singIn).reduce((prev, elem) => ({ ...prev, [elem]: singIn[elem].value }), {});
@@ -66,8 +63,7 @@ class SignIn extends Component {
 			data: values
 		})
 			.then(res => {
-				history.push("/");
-				// this.setState({ token: res.data.token });
+				this.setState({ token: res.data.token });
 				localStorage.setItem("token", res.data.token);
 			})
 			.catch(err => console.log(err.response));
@@ -81,7 +77,7 @@ class SignIn extends Component {
 		}
 
 		if (rules.minLength) {
-			isValid = value.length > rules.minLength && isValid;
+			isValid = value.length >= rules.minLength && isValid;
 		}
 
 		return isValid;
@@ -93,9 +89,7 @@ class SignIn extends Component {
 		this.setState(prevState => {
 			const valid = this.validator(value, prevState.singIn[name].validation);
 
-			const otherValid = Object.keys(prevState.singIn).reduce((prev, elem) => {
-				return prevState.singIn[elem].valid && prev;
-			}, true);
+			const otherValid = Object.keys(prevState.singIn).some(el => !prevState.singIn[el].valid);
 
 			return {
 				singIn: {
@@ -119,8 +113,6 @@ class SignIn extends Component {
 			return <Redirect to="/" />;
 		}
 
-		console.log("this.", this.props);
-
 		return (
 			<form className="auth-box__sign-in-form" onSubmit={this.submit}>
 				{Object.keys(singIn).map(el => (
@@ -131,7 +123,9 @@ class SignIn extends Component {
 						config={{ ...singIn[el].config, value: singIn[el].value, onChange: this.change }}
 					/>
 				))}
-				<button type="submit">Sign in</button>
+				<button disabled={!isValid} type="submit">
+					Sign in
+				</button>
 			</form>
 		);
 	}