mirror of
https://github.com/astuto/astuto.git
synced 2025-12-16 03:37:56 +01:00
Implement basic version of likes
This commit is contained in:
41
app/javascript/actions/submitLike.ts
Normal file
41
app/javascript/actions/submitLike.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
import { Action } from "redux";
|
||||
import { ThunkAction } from "redux-thunk";
|
||||
|
||||
import { State } from "../reducers/rootReducer";
|
||||
|
||||
export const LIKE_SUBMIT_SUCCESS = 'LIKE_SUBMIT_SUCCESS';
|
||||
interface LikeSubmitSuccessAction {
|
||||
type: typeof LIKE_SUBMIT_SUCCESS,
|
||||
postId: number;
|
||||
isLike: boolean;
|
||||
}
|
||||
|
||||
export type LikeActionTypes = LikeSubmitSuccessAction;
|
||||
|
||||
const likeSubmitSuccess = (postId: number, isLike: boolean): LikeSubmitSuccessAction => ({
|
||||
type: LIKE_SUBMIT_SUCCESS,
|
||||
postId,
|
||||
isLike,
|
||||
});
|
||||
|
||||
export const submitLike = (
|
||||
postId: number,
|
||||
isLike: boolean,
|
||||
authenticityToken: string,
|
||||
): ThunkAction<void, State, null, Action<string>> => async (dispatch) => {
|
||||
try {
|
||||
const res = await fetch(`/posts/${postId}/likes`, {
|
||||
method: isLike ? 'POST' : 'DELETE',
|
||||
headers: {
|
||||
Accept: 'application/json',
|
||||
'Content-Type': 'application/json',
|
||||
'X-CSRF-Token': authenticityToken,
|
||||
},
|
||||
});
|
||||
|
||||
if (res.status === 201 || res.status === 202)
|
||||
dispatch(likeSubmitSuccess(postId, isLike));
|
||||
} catch (e) {
|
||||
console.log('An error occurred while liking a post');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user