mirror of
https://github.com/astuto/astuto.git
synced 2025-12-15 19:27:52 +01:00
Implement basic version of likes
This commit is contained in:
@@ -14,10 +14,12 @@ class LikesController < ApplicationController
|
|||||||
end
|
end
|
||||||
|
|
||||||
def destroy
|
def destroy
|
||||||
like = Like.where(like_params)
|
like = Like.find_by(like_params)
|
||||||
|
|
||||||
|
return if like.nil?
|
||||||
|
|
||||||
if like.destroy
|
if like.destroy
|
||||||
render json: {}, status: :no_content
|
render json: {}, status: :accepted
|
||||||
else
|
else
|
||||||
render json: {
|
render json: {
|
||||||
error: I18n.t('errors.likes.destroy', message: like.errors.full_messages)
|
error: I18n.t('errors.likes.destroy', message: like.errors.full_messages)
|
||||||
@@ -28,6 +30,9 @@ class LikesController < ApplicationController
|
|||||||
private
|
private
|
||||||
|
|
||||||
def like_params
|
def like_params
|
||||||
params.permit(:post_id).merge(user_id: current_user.id)
|
{
|
||||||
|
post_id: params[:post_id],
|
||||||
|
user_id: current_user.id,
|
||||||
|
}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
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');
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -97,16 +97,19 @@ class BoardP extends React.Component<Props> {
|
|||||||
<PostList
|
<PostList
|
||||||
posts={posts.items}
|
posts={posts.items}
|
||||||
postStatuses={postStatuses.items}
|
postStatuses={postStatuses.items}
|
||||||
hasMore={posts.haveMore}
|
|
||||||
areLoading={posts.areLoading}
|
areLoading={posts.areLoading}
|
||||||
error={posts.error}
|
error={posts.error}
|
||||||
|
|
||||||
|
hasMore={posts.haveMore}
|
||||||
handleLoadMore={() =>
|
handleLoadMore={() =>
|
||||||
posts.areLoading ?
|
posts.areLoading ?
|
||||||
null
|
null
|
||||||
:
|
:
|
||||||
requestPosts(board.id, posts.page + 1, filters.searchQuery, filters.postStatusId)
|
requestPosts(board.id, posts.page + 1, filters.searchQuery, filters.postStatusId)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
isLoggedIn={isLoggedIn}
|
||||||
|
authenticityToken={authenticityToken}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -18,8 +18,11 @@ interface Props {
|
|||||||
areLoading: boolean;
|
areLoading: boolean;
|
||||||
error: string;
|
error: string;
|
||||||
|
|
||||||
handleLoadMore(): void;
|
|
||||||
hasMore: boolean;
|
hasMore: boolean;
|
||||||
|
handleLoadMore(): void;
|
||||||
|
|
||||||
|
isLoggedIn: boolean;
|
||||||
|
authenticityToken: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
const PostList = ({
|
const PostList = ({
|
||||||
@@ -27,8 +30,10 @@ const PostList = ({
|
|||||||
postStatuses,
|
postStatuses,
|
||||||
areLoading,
|
areLoading,
|
||||||
error,
|
error,
|
||||||
|
hasMore,
|
||||||
handleLoadMore,
|
handleLoadMore,
|
||||||
hasMore
|
isLoggedIn,
|
||||||
|
authenticityToken,
|
||||||
}: Props) => (
|
}: Props) => (
|
||||||
<div className="postList">
|
<div className="postList">
|
||||||
{ error ? <DangerText>{error}</DangerText> : null }
|
{ error ? <DangerText>{error}</DangerText> : null }
|
||||||
@@ -48,8 +53,13 @@ const PostList = ({
|
|||||||
title={post.title}
|
title={post.title}
|
||||||
description={post.description}
|
description={post.description}
|
||||||
postStatus={postStatuses.find(postStatus => postStatus.id === post.postStatusId)}
|
postStatus={postStatuses.find(postStatus => postStatus.id === post.postStatusId)}
|
||||||
|
likesCount={post.likesCount}
|
||||||
|
liked={post.liked}
|
||||||
commentsCount={post.commentsCount}
|
commentsCount={post.commentsCount}
|
||||||
|
|
||||||
|
isLoggedIn={isLoggedIn}
|
||||||
|
authenticityToken={authenticityToken}
|
||||||
|
|
||||||
key={i}
|
key={i}
|
||||||
/>
|
/>
|
||||||
))
|
))
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import * as React from 'react';
|
import * as React from 'react';
|
||||||
|
|
||||||
|
import LikeButton from '../../containers/LikeButton';
|
||||||
import CommentsNumber from '../shared/CommentsNumber';
|
import CommentsNumber from '../shared/CommentsNumber';
|
||||||
import PostStatusLabel from '../shared/PostStatusLabel';
|
import PostStatusLabel from '../shared/PostStatusLabel';
|
||||||
import { DescriptionText } from '../shared/CustomTexts';
|
import { DescriptionText } from '../shared/CustomTexts';
|
||||||
@@ -11,21 +12,46 @@ interface Props {
|
|||||||
title: string;
|
title: string;
|
||||||
description?: string;
|
description?: string;
|
||||||
postStatus: IPostStatus;
|
postStatus: IPostStatus;
|
||||||
|
likesCount: number;
|
||||||
|
liked: number;
|
||||||
commentsCount: number;
|
commentsCount: number;
|
||||||
|
|
||||||
|
isLoggedIn: boolean;
|
||||||
|
authenticityToken: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
const PostListItem = ({ id, title, description, postStatus, commentsCount }: Props) => (
|
const PostListItem = ({
|
||||||
<a href={`/posts/${id}`} className="postLink">
|
id,
|
||||||
<div className="postListItem">
|
title,
|
||||||
<span className="postTitle">{title}</span>
|
description,
|
||||||
<DescriptionText limit={120}>{description}</DescriptionText>
|
postStatus,
|
||||||
|
likesCount,
|
||||||
|
liked,
|
||||||
|
commentsCount,
|
||||||
|
|
||||||
<div className="postDetails">
|
isLoggedIn,
|
||||||
<CommentsNumber number={commentsCount} />
|
authenticityToken,
|
||||||
{ postStatus ? <PostStatusLabel {...postStatus} /> : null }
|
}: Props) => (
|
||||||
|
<React.Fragment>
|
||||||
|
<LikeButton
|
||||||
|
postId={id}
|
||||||
|
likesCount={likesCount}
|
||||||
|
liked={liked}
|
||||||
|
isLoggedIn={isLoggedIn}
|
||||||
|
authenticityToken={authenticityToken}
|
||||||
|
/>
|
||||||
|
<a href={`/posts/${id}`} className="postLink">
|
||||||
|
<div className="postListItem">
|
||||||
|
<span className="postTitle">{title}</span>
|
||||||
|
<DescriptionText limit={120}>{description}</DescriptionText>
|
||||||
|
|
||||||
|
<div className="postDetails">
|
||||||
|
<CommentsNumber number={commentsCount} />
|
||||||
|
{ postStatus ? <PostStatusLabel {...postStatus} /> : null }
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</a>
|
||||||
</a>
|
</React.Fragment>
|
||||||
);
|
);
|
||||||
|
|
||||||
export default PostListItem;
|
export default PostListItem;
|
||||||
37
app/javascript/components/LikeButton/LikeButtonP.tsx
Normal file
37
app/javascript/components/LikeButton/LikeButtonP.tsx
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
import * as React from 'react';
|
||||||
|
|
||||||
|
interface Props {
|
||||||
|
postId: number;
|
||||||
|
likesCount: number;
|
||||||
|
liked: number;
|
||||||
|
handleLikeSubmit(
|
||||||
|
postId: number,
|
||||||
|
isLike: boolean,
|
||||||
|
authenticityToken: string,
|
||||||
|
): void;
|
||||||
|
authenticityToken: string;
|
||||||
|
isLoggedIn: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
const LikeButtonP = ({
|
||||||
|
postId,
|
||||||
|
likesCount,
|
||||||
|
liked,
|
||||||
|
handleLikeSubmit,
|
||||||
|
authenticityToken,
|
||||||
|
isLoggedIn,
|
||||||
|
}: Props) => (
|
||||||
|
<div className="likeButtonContainer">
|
||||||
|
<button onClick={() =>
|
||||||
|
isLoggedIn ?
|
||||||
|
handleLikeSubmit(postId, !liked, authenticityToken)
|
||||||
|
:
|
||||||
|
window.location.href = `/users/sign_in`
|
||||||
|
}>
|
||||||
|
{ liked ? 'down' : 'up' }
|
||||||
|
</button>
|
||||||
|
<span className="likesCountLabel">{likesCount}</span>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
|
||||||
|
export default LikeButtonP;
|
||||||
16
app/javascript/containers/LikeButton.tsx
Normal file
16
app/javascript/containers/LikeButton.tsx
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
import { connect } from 'react-redux';
|
||||||
|
|
||||||
|
import LikeButtonP from '../components/LikeButton/LikeButtonP';
|
||||||
|
|
||||||
|
import { submitLike } from '../actions/submitLike';
|
||||||
|
|
||||||
|
const mapDispatchToProps = dispatch => ({
|
||||||
|
handleLikeSubmit(postId: number, isLike: boolean, authenticityToken: string) {
|
||||||
|
dispatch(submitLike(postId, isLike, authenticityToken));
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
export default connect(
|
||||||
|
null,
|
||||||
|
mapDispatchToProps,
|
||||||
|
)(LikeButtonP);
|
||||||
@@ -4,7 +4,10 @@ interface IPost {
|
|||||||
description?: string;
|
description?: string;
|
||||||
boardId: number;
|
boardId: number;
|
||||||
postStatusId?: number;
|
postStatusId?: number;
|
||||||
|
likesCount: number;
|
||||||
|
liked: number;
|
||||||
commentsCount: number;
|
commentsCount: number;
|
||||||
|
hotness: number;
|
||||||
userId: number;
|
userId: number;
|
||||||
createdAt: string;
|
createdAt: string;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,10 @@ interface IPostJSON {
|
|||||||
description?: string;
|
description?: string;
|
||||||
board_id: number;
|
board_id: number;
|
||||||
post_status_id?: number;
|
post_status_id?: number;
|
||||||
|
likes_count: number;
|
||||||
|
liked: number;
|
||||||
comments_count: number;
|
comments_count: number;
|
||||||
|
hotness: number;
|
||||||
user_id: number;
|
user_id: number;
|
||||||
created_at: string;
|
created_at: string;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,7 +21,10 @@ const initialState: IPost = {
|
|||||||
description: null,
|
description: null,
|
||||||
boardId: 0,
|
boardId: 0,
|
||||||
postStatusId: null,
|
postStatusId: null,
|
||||||
|
likesCount: 0,
|
||||||
|
liked: 0,
|
||||||
commentsCount: 0,
|
commentsCount: 0,
|
||||||
|
hotness: 0,
|
||||||
userId: 0,
|
userId: 0,
|
||||||
createdAt: '',
|
createdAt: '',
|
||||||
};
|
};
|
||||||
@@ -41,7 +44,10 @@ const postReducer = (
|
|||||||
description: action.post.description,
|
description: action.post.description,
|
||||||
boardId: action.post.board_id,
|
boardId: action.post.board_id,
|
||||||
postStatusId: action.post.post_status_id,
|
postStatusId: action.post.post_status_id,
|
||||||
|
likesCount: action.post.likes_count,
|
||||||
|
liked: action.post.liked,
|
||||||
commentsCount: action.post.comments_count,
|
commentsCount: action.post.comments_count,
|
||||||
|
hotness: action.post.hotness,
|
||||||
userId: action.post.user_id,
|
userId: action.post.user_id,
|
||||||
createdAt: action.post.created_at,
|
createdAt: action.post.created_at,
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -22,6 +22,11 @@ import {
|
|||||||
SET_POST_STATUS_FILTER,
|
SET_POST_STATUS_FILTER,
|
||||||
} from '../actions/changeFilters';
|
} from '../actions/changeFilters';
|
||||||
|
|
||||||
|
import {
|
||||||
|
LikeActionTypes,
|
||||||
|
LIKE_SUBMIT_SUCCESS,
|
||||||
|
} from '../actions/submitLike';
|
||||||
|
|
||||||
export interface PostsState {
|
export interface PostsState {
|
||||||
items: Array<IPost>;
|
items: Array<IPost>;
|
||||||
page: number;
|
page: number;
|
||||||
@@ -42,7 +47,10 @@ const initialState: PostsState = {
|
|||||||
|
|
||||||
const postsReducer = (
|
const postsReducer = (
|
||||||
state = initialState,
|
state = initialState,
|
||||||
action: PostsRequestActionTypes | ChangeFiltersActionTypes,
|
action:
|
||||||
|
PostsRequestActionTypes |
|
||||||
|
ChangeFiltersActionTypes |
|
||||||
|
LikeActionTypes,
|
||||||
): PostsState => {
|
): PostsState => {
|
||||||
switch (action.type) {
|
switch (action.type) {
|
||||||
case POSTS_REQUEST_START:
|
case POSTS_REQUEST_START:
|
||||||
@@ -78,6 +86,19 @@ const postsReducer = (
|
|||||||
filters: filtersReducer(state.filters, action),
|
filters: filtersReducer(state.filters, action),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
case LIKE_SUBMIT_SUCCESS:
|
||||||
|
return {
|
||||||
|
...state,
|
||||||
|
items: state.items.map(post => {
|
||||||
|
if (action.postId === post.id) {
|
||||||
|
return action.isLike ?
|
||||||
|
{ ...post, likesCount: post.likesCount + 1, liked: 1 }
|
||||||
|
:
|
||||||
|
{ ...post, likesCount: post.likesCount - 1, liked: 0 }
|
||||||
|
} else return post;
|
||||||
|
}),
|
||||||
|
};
|
||||||
|
|
||||||
default:
|
default:
|
||||||
return state;
|
return state;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user