user Actions
Here is I am calling user login API but its direct called the success not request nor reject
this is my user actions file code
LOGIN_USER_REJECT,
LOGIN_USER_SUCCESS,
LOGIN_USER_REQUEST
} from "../constants/Userconstants"
import axios from "axios"
export const LoginUser = (email, password) => {
return async(dispatch) => {
try {
dispatch({ type: LOGIN_USER_REQUEST })
const config = {headers:{'Content-Type':'application/json'}}
const { data } = await axios.post('/api/v1/login', { email, password }, config)
dispatch({ type: LOGIN_USER_SUCCESS, payload: data.user })
} catch (error) {
dispatch({ type: LOGIN_USER_REJECT, payload : error.response.data.messag })
}
}
}
user Reducer
this is my reducer file I use constant which same in the user actions and also in the user Reducer
LOGIN_USER_REJECT,
LOGIN_USER_SUCCESS,
LOGIN_USER_REQUEST
} from "../constants/Userconstants"
export const Login_user = (state = {user:{}} , action) =>{
switch (action.type) {
case LOGIN_USER_REQUEST:
return {
loading: true,
user: {},
isAuthenticated:false
}
case LOGIN_USER_SUCCESS:
return {
loading: false,
user: action.payload,
isAuthenticated:false,
...state
}
case LOGIN_USER_REJECT:
return {
loading: false,
isAuthenticated:false,
error: action.payload,
user:null
}
default:
return {
...state
}
}
}```
my login page
This is my Login page where I called the LoginUser actions with the help of button
const Login = () => {
const dispatch = useDispatch()
const [email, setEmail] = useState("")
const [password, setPassword] = useState("")
const handleSubmit = async(e) => {
e.preventDefault()
dispatch(LoginUser(email , password))
setEmail("")
setPassword("")
} ```
I am fetching this API using redux but not called these two actions pending and reject I want to also called these two actions“`