Skip to content

Commit

Permalink
redux setup done for blog list
Browse files Browse the repository at this point in the history
  • Loading branch information
JaberHPranto committed Sep 15, 2021
1 parent dbbee51 commit 526ecc3
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 1 deletion.
5 changes: 5 additions & 0 deletions client/src/constants/blogConstant.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const BLOG_FETCH_REQUEST = "BLOG_FETCH_REQUEST"
export const BLOG_FETCH_SUCCESS = "BLOG_FETCH_SUCCESS"
export const BLOG_FETCH_FAIL = "BLOG_FETCH_FAIL"

export const BLOG_CREATE = "BLOG_CREATE"
24 changes: 24 additions & 0 deletions client/src/redux/actions/blogActions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import axios from "axios"
import { BLOG_FETCH_FAIL, BLOG_FETCH_REQUEST, BLOG_FETCH_SUCCESS } from "../../constants/blogConstant"
// @ GET blogs
export const fetchBlogs = () => async (dispatch) => {
try {
dispatch({ type:BLOG_FETCH_REQUEST})

const { data } = await axios.get(`/api/blogs`)

dispatch({
type: BLOG_FETCH_SUCCESS,
payload: data
})

} catch (error) {
console.log(error);
dispatch({
type: BLOG_FETCH_FAIL,
payload: error.response && error.response.data.message ? error.response.data.message : error.message
})
}


}
17 changes: 17 additions & 0 deletions client/src/redux/reducers/blogReducers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { BLOG_FETCH_FAIL, BLOG_FETCH_REQUEST, BLOG_FETCH_SUCCESS } from "../../constants/blogConstant"

export const blogListReducer = ( blogs=[], action) => {
switch (action.type) {
case BLOG_FETCH_REQUEST:
return { loading: true, blogs: [] }

case BLOG_FETCH_SUCCESS:
return { loading: false, blogs: action.payload}

case BLOG_FETCH_FAIL:
return { loading: false, error: action.payload }

default:
return blogs
}
}
6 changes: 5 additions & 1 deletion client/src/store.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { applyMiddleware, combineReducers, createStore } from "redux";
import { composeWithDevTools } from "redux-devtools-extension";
import thunk from "redux-thunk";
import {
blogListReducer
} from "./redux/reducers/blogReducers";
import { cartReducer } from "./redux/reducers/cartReducers";
import {
orderCreateReducer, orderListReducer
Expand Down Expand Up @@ -31,7 +34,8 @@ const reducer = combineReducers({
userDelete: userDeleteReducer,
cart: cartReducer,
orderCreate: orderCreateReducer,
orderList: orderListReducer
orderList: orderListReducer,
blogList:blogListReducer
});

const cartItemsFromStorage = localStorage.getItem("cartItems")
Expand Down

0 comments on commit 526ecc3

Please sign in to comment.