Commit faddca58 authored by Rasoul Madani's avatar Rasoul Madani
Browse files

شماره 31

parent fb57dd85
......@@ -4,6 +4,8 @@ import {
USER_REGISTER_SUCCESS,
USER_REGISTER_FAIL,
USER_SIGNIN_REQUEST,
USER_SIGNIN_SUCCESS,
USER_SIGNIN_FAIL
} from "../constants/userConstants";
export const register = (name, email, password) => async (dispatch) => {
dispatch({
......@@ -40,5 +42,18 @@ export const signin = (email, password) => async (dispatch) => {
email,
password,
});
} catch (error) {}
dispatch({
type: USER_SIGNIN_SUCCESS,
payload: data,
});
localStorage.setItem("userInfo", JSON.stringify(data));
} catch (error) {
dispatch({
type: USER_SIGNIN_FAIL,
payload:
error.response && error.response.data.message
? error.response.data.message
: error.message,
});
}
};
export const USER_REGISTER_REQUEST = "USER_REGISTER_REQUEST";
export const USER_REGISTER_SUCCESS = "USER_REGISTER_SUCCESS";
export const USER_REGISTER_FAIL = "USER_REGISTER_FAIL";
export const USER_SIGNIN_REQUEST = 'USER_SIGNIN_REQUEST'
export const USER_SIGNIN_REQUEST = "USER_SIGNIN_REQUEST";
export const USER_SIGNIN_SUCCESS = "USER_SIGNIN_SUCCESS";
export const USER_SIGNIN_FAIL = "USER_SIGNIN_FAIL";
......@@ -2,7 +2,9 @@ import {
USER_REGISTER_REQUEST,
USER_REGISTER_SUCCESS,
USER_REGISTER_FAIL,
USER_SIGNIN_REQUEST
USER_SIGNIN_REQUEST,
USER_SIGNIN_SUCCESS,
USER_SIGNIN_FAIL,
} from "../constants/userConstants";
export const userRegisterReducer = (state = {}, action) => {
switch (action.type) {
......@@ -17,11 +19,15 @@ export const userRegisterReducer = (state = {}, action) => {
}
};
export const userSigninReducer = (state={},action)=>{
switch(action.type){
export const userSigninReducer = (state = {}, action) => {
switch (action.type) {
case USER_SIGNIN_REQUEST:
return {loading:true}
default :
return state
return { loading: true };
case USER_SIGNIN_SUCCESS:
return { loading: false, userInfo: action.payload };
case USER_SIGNIN_FAIL:
return { loading: false, error: action.payload };
default:
return state;
}
}
};
import { useState } from "react";
import { useState, useEffect } from "react";
import { Link } from "react-router-dom";
import { useDispatch } from "react-redux";
import { useDispatch, useSelector } from "react-redux";
import { signin } from "../actions/userActions";
import LoadingBox from "../components/LoadingBox";
import MessageBox from "../components/MessageBox";
export default function SigninScreen(props) {
const dispatch = useDispatch();
const [email, setEmail] = useState("");
......@@ -9,16 +11,26 @@ export default function SigninScreen(props) {
const redirect = props.location.search
? props.location.search.split("=")[1]
: "/";
const userSignin = useSelector((state) => state.userSignin);
const { userInfo, loading, error } = userSignin;
const submitHandler = (e) => {
e.preventDefault();
dispatch(signin(email, password));
};
useEffect(() => {
if (userInfo) {
props.history.push(redirect);
}
}, [props.history, redirect, userInfo]);
return (
<div>
<form className="form" onSubmit={submitHandler}>
<div>
<h1>Sign In</h1>
</div>
{loading && <LoadingBox></LoadingBox>}
{error && <MessageBox variant="danger">{error}</MessageBox>}
<div>
<label htmlFor="email">Email</label>
<input
......
......@@ -17,6 +17,11 @@ const initialState = {
? JSON.parse(localStorage.getItem("userInfo"))
: null,
},
userSignin: {
userInfo: localStorage.getItem("userInfo")
? JSON.parse(localStorage.getItem("userInfo"))
: null,
},
cart: {
cartItems: localStorage.getItem("cartItems")
? JSON.parse(localStorage.getItem("cartItems"))
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment