mirror of
https://github.com/ClaytonWWilson/CS307-Team24.git
synced 2026-03-10 21:25:04 +00:00
Liking posts with Redux and Material UI
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import {SET_USER, SET_ERRORS, CLEAR_ERRORS, LOADING_UI, SET_AUTHENTICATED, SET_UNAUTHENTICATED} from '../types';
|
||||
import {SET_USER, SET_ERRORS, CLEAR_ERRORS, LOADING_UI, SET_AUTHENTICATED, SET_UNAUTHENTICATED, LIKE_POST, UNLIKE_POST, SET_LIKES} from '../types';
|
||||
import axios from 'axios';
|
||||
|
||||
|
||||
@@ -81,6 +81,55 @@ export const deleteUser = () => (dispatch) => {
|
||||
dispatch({ type: SET_UNAUTHENTICATED });
|
||||
}
|
||||
|
||||
export const getLikes = () => (dispatch) => {
|
||||
axios.get('/likes')
|
||||
.then((res) => {
|
||||
dispatch({
|
||||
type: SET_LIKES,
|
||||
payload: res.data
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
export const likePost = (postId, postArray) => (dispatch) => {
|
||||
postArray.push(postId);
|
||||
dispatch({
|
||||
type: LIKE_POST,
|
||||
payload: {
|
||||
likes: postArray
|
||||
}
|
||||
})
|
||||
axios.get(`/like/${postId}`)
|
||||
.then((res) => {
|
||||
getLikes();
|
||||
})
|
||||
|
||||
|
||||
}
|
||||
|
||||
export const unlikePost = (postId, postArray) => (dispatch) => {
|
||||
let i;
|
||||
for (i = 0; i < postArray.length; i++) {
|
||||
if (postArray[i] === postId) {
|
||||
postArray.splice(i, 1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
dispatch({
|
||||
type: UNLIKE_POST,
|
||||
payload: {
|
||||
likes: postArray
|
||||
}
|
||||
})
|
||||
|
||||
axios.get(`/unlike/${postId}`)
|
||||
.then((res) => {
|
||||
getLikes();
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
const setAuthorizationHeader = (token) => {
|
||||
const FBIdToken = `Bearer ${token}`;
|
||||
localStorage.setItem('FBIdToken', FBIdToken);
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import {SET_USER, SET_ERRORS, CLEAR_ERRORS, LOADING_UI, SET_AUTHENTICATED, SET_UNAUTHENTICATED} from '../types';
|
||||
import {SET_USER, SET_ERRORS, CLEAR_ERRORS, LOADING_UI, SET_AUTHENTICATED, SET_UNAUTHENTICATED, LIKE_POST, UNLIKE_POST, SET_LIKES} from '../types';
|
||||
|
||||
const initialState = {
|
||||
authenticated: false,
|
||||
@@ -19,9 +19,25 @@ export default function(state = initialState, action) {
|
||||
return initialState;
|
||||
case SET_USER:
|
||||
return {
|
||||
...state,
|
||||
authenticated: true,
|
||||
...action.payload,
|
||||
};
|
||||
case LIKE_POST:
|
||||
return {
|
||||
...state,
|
||||
...action.payload
|
||||
}
|
||||
case UNLIKE_POST:
|
||||
return {
|
||||
...state,
|
||||
...action.payload
|
||||
}
|
||||
case SET_LIKES:
|
||||
return {
|
||||
...state,
|
||||
...action.payload
|
||||
}
|
||||
default:
|
||||
return state;
|
||||
}
|
||||
|
||||
@@ -3,6 +3,9 @@ export const SET_AUTHENTICATED = 'SET_AUTHENTICATED';
|
||||
export const SET_UNAUTHENTICATED = 'SET_UNAUTHENTICATED';
|
||||
export const SET_USER = 'SET_USER';
|
||||
export const LOADING_USER = 'LOADING_USER';
|
||||
export const LIKE_POST = 'LIKE_POST';
|
||||
export const UNLIKE_POST = 'UNLIKE_POST';
|
||||
export const SET_LIKES = 'SET_LIKES';
|
||||
|
||||
// UI reducer types
|
||||
export const SET_ERRORS = 'SET_ERRORS';
|
||||
|
||||
Reference in New Issue
Block a user