I'm trying to get a single order by the ID, i'm using dispatch inside useEffect hook ,and i'm getting @@INIT action after reloading the page. This is my reducer :
export const orderDetailsReducer = (state = {orderItems: [], shippingAddress: {}}, action) => {
switch (action.type){
case ORDER_DETAILS_REQUEST:
return {
...state,
loading : true
}
case ORDER_DETAILS_SUCCESS:
return{
loading : false,
order : action.payload
}
case ORDER_DETAILS_FAIL:
return{
loading : false,
error : action.payload
}
default:
return state
}
}
And this is the action :
export const getOrderDetails = (id) => async (dispatch, getState) => {
try{
dispatch({
type: ORDER_DETAILS_REQUEST,
})
const {
userLogin: { userInfo }
} = getState()
const config = {
headers: {
'Content-type': 'application/json',
Authorization: `Bearer ${userInfo.token}`
}
}
const {data} = await axios.get(
`/api/order/${id}`,
config
)
dispatch({
type : ORDER_DETAILS_SUCCESS,
payload : data
})
}
catch(error){
dispatch({
type: ORDER_DETAILS_FAIL,
payload: error.response && error.response.data.detail
? error.response.data.detail
: error.detail
})
}
}
And here is a part of my jsx file :
const dispatch = useDispatch()
const {id} = useParams()
const orderDetails = useSelector(state => state.orderDetails)
const {error, loading, order} = orderDetails
useEffect(() => {
dispatch(getOrderDetails(id))
}, [dispatch, id]);
When i try to fetch data in the UI it shows but when i reload the page it shows me a white page and the action turns to @@INIT.