liking and quoting work

This commit is contained in:
Aditya Sankaran
2019-10-30 18:31:55 -04:00
parent 19780a1395
commit 84ad61b954
14 changed files with 575 additions and 6 deletions

View File

@@ -0,0 +1,55 @@
import {LIKE_POST, UNLIKE_POST, SET_POST, SET_POSTS} from '../types';
import axios from 'axios';
export const getPosts = () => (dispatch) => {
axios
.get('/posts')
.then((res) => {
dispatch({
type: SET_POSTS,
payload: res.data
});
})
.catch((err) => {
dispatch({
type: SET_POSTS,
payload: []
});
});
};
export const likePost = (postId) => (dispatch) => {
axios
.get(`/posts/${postId}/like`)
.then((res) => {
dispatch({
type: LIKE_POST,
payload: res.data
});
})
.catch((err) => console.log(err));
};
export const unlikePost = (postId) => (dispatch) => {
axios
.get(`/posts/${postId}/unlike`)
.then((res) => {
dispatch({
type: UNLIKE_POST,
payload: res.data
});
})
.catch((err) => console.log(err));
};
export const getPost = (postId) => (dispatch) => {
axios
.get(`/posts/${postId}`)
.then((res) => {
dispatch({
type: SET_POST,
payload: res.data
});
})
.catch((err) => console.log(err));
};

View File

@@ -0,0 +1,31 @@
import { LIKE_POST, UNLIKE_POST} from '../types';
const initialState = {
likes: [],
credentials: {}
}
export default function(state = initialState, action) {
switch (action.type) {
case LIKE_POST:
return {
...state,
likes: [
...state.likes,
{
userHandle: state.credentials.handle,
postId: action.payload.postId
}
]
}
case UNLIKE_POST:
return {
...state,
likes: state.likes.filter(
(like) => like.postId === action.payload.postId
)
};
default:
return state;
}
}

View File

@@ -15,4 +15,14 @@ const reducers = combineReducers({
UI: uiReducer
});
//const store = createStore(reducers, )
const store = createStore(
reducers,
initialState,
compose(
applyMiddleware(...middleware),
window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()
)
)
export default store;

View File

@@ -0,0 +1,3 @@
export const SET_POST = 'SET_POST';
export const LIKE_POST = 'LIKE_POST';
export const UNLIKE_POST = 'UNLIKE_POST';