Commit 7b0a9395 authored by Рахметова Альбина's avatar Рахметова Альбина

Merge branch 'dev' into '#28'

# Conflicts:
#   frontend/src/store/store.ts
parents 59860c5e 93956466
...@@ -30,7 +30,6 @@ export class CommentController { ...@@ -30,7 +30,6 @@ export class CommentController {
} }
private addComment = async (req: Request, res: Response): Promise<void> => { private addComment = async (req: Request, res: Response): Promise<void> => {
const response = await this.service.addComment(req.body) const response = await this.service.addComment(req.body)
res.send(response) res.send(response)
} }
......
...@@ -4,8 +4,8 @@ import IPost from "./IPost"; ...@@ -4,8 +4,8 @@ import IPost from "./IPost";
export default interface IComment extends Document { export default interface IComment extends Document {
_id: ObjectId _id: ObjectId
user: IUser user: string
post: IPost post: string
comment: string comment: string
datetime: Date datetime: Date
} }
\ No newline at end of file
...@@ -2,4 +2,6 @@ import IComment from "./IComment" ...@@ -2,4 +2,6 @@ import IComment from "./IComment"
export default interface ICommentDto { export default interface ICommentDto {
comment: IComment['comment'] comment: IComment['comment']
post: string
user: string
} }
\ No newline at end of file
...@@ -9,6 +9,7 @@ import IComment from '../interfaces/IComment'; ...@@ -9,6 +9,7 @@ import IComment from '../interfaces/IComment';
import ICommentDto from '../interfaces/ICommentDto'; import ICommentDto from '../interfaces/ICommentDto';
import { Comment } from '../models/Comments'; import { Comment } from '../models/Comments';
import IUser from '../interfaces/IUser'; import IUser from '../interfaces/IUser';
import UserModel from '../models/User';
export class Mongo implements IDataBase { export class Mongo implements IDataBase {
[x: string]: any; [x: string]: any;
...@@ -26,7 +27,7 @@ export class Mongo implements IDataBase { ...@@ -26,7 +27,7 @@ export class Mongo implements IDataBase {
public getPosts = async (): Promise<IResponse<IPost[] | undefined>> => { public getPosts = async (): Promise<IResponse<IPost[] | undefined>> => {
try { try {
const data = await Post.find().sort({ datetime: -1 }) const data = await Post.find()
const response: IResponse<IPost[]> = { const response: IResponse<IPost[]> = {
status: EStatuses.OK, status: EStatuses.OK,
result: data as any, result: data as any,
...@@ -141,7 +142,7 @@ export class Mongo implements IDataBase { ...@@ -141,7 +142,7 @@ export class Mongo implements IDataBase {
} }
public getCommentById = async (id: string): Promise<IResponse<IComment | undefined>> => { public getCommentById = async (id: string): Promise<IResponse<IComment | undefined>> => {
try { try {
const data = await Comment.findById(id).populate('user').populate('post').sort({ datetime: -1 }) const data = await Comment.findById(id).populate('user').populate('post');
const response: IResponse<IComment> = { const response: IResponse<IComment> = {
status: EStatuses.OK, status: EStatuses.OK,
result: data as any, result: data as any,
......
.comment_block {
border: 2px solid black;
padding: 10px 10px;
margin-bottom: 30px;
}
.comment_btn {
margin-left: 500px;
}
\ No newline at end of file
import ICommentProps from "./ICommentProps";
import React, { FunctionComponent, ReactElement } from "react";
import { AppDispatch } from "../../store/store";
import { useDispatch } from "react-redux";
import './Comment.css'
import { deleteCommentById, getCommentsByPost } from "../../store/comments/comments.slice";
const Comment: FunctionComponent<ICommentProps> = (props): ReactElement => {
const dispatch: AppDispatch = useDispatch()
const deleteComment = () => {
try {
dispatch(deleteCommentById(props.comments._id))
} finally {
dispatch(getCommentsByPost(props.params_id))
}
}
return (
<div className="comment_block">
<span>{props.comments.user} wrote: {props.comments.comment}</span>
<button className="comment_btn" onClick={deleteComment}>Delete</button>
</div>
)
}
export default Comment
\ No newline at end of file
import IComment from "../../interfaces/IComment";
export default interface ICommentProps {
comments: IComment
params_id: any
}
\ No newline at end of file
import React, { ChangeEvent, FunctionComponent, ReactElement, useEffect, useState } from "react";
import { useDispatch } from "react-redux";
import { useParams } from "react-router-dom";
import { getPostById } from "../../store/posts/posts.slice";
import { useSelector } from "react-redux";
import { RootState } from "../../store/store";
import { getCommentsByPost } from "../../store/comments/comments.slice";
import Comment from "../../components/Comment/Comment";
const Details: FunctionComponent = (): ReactElement => {
const params: any = useParams();
const dispatch = useDispatch();
const {comments} = useSelector((state: RootState) => state.comments)
useEffect(() => {
dispatch(getPostById(params.id))
}, [params])
useEffect(() => {
//@ts-ignore
dispatch(getCommentsByPost(params.id))
}, [params.id])
return (
<div className="details">
<div className="container">
{comments ? <h1>Comments</h1> : ''}
{comments.map(c => {
return <Comment
params_id={params.id}
comments={c}
/>
})}
</div>
</div>
)
}
export default Details
\ No newline at end of file
...@@ -3,8 +3,8 @@ import IUser from "./IUser" ...@@ -3,8 +3,8 @@ import IUser from "./IUser"
export default interface IComment { export default interface IComment {
_id: string _id: string
user: IUser user: string
post: IPost post: string
comment: string comment: string
datetime: Date datetime: Date
} }
\ No newline at end of file
import { createSlice } from '@reduxjs/toolkit' import { createAsyncThunk, createSlice } from '@reduxjs/toolkit'
import { createAppAsyncThunk } from '../createAppAsyncThunk'
import IResponse from '../../interfaces/IResponse' import IResponse from '../../interfaces/IResponse'
import IComment from '../../interfaces/IComment' import IComment from '../../interfaces/IComment'
import ICommentState from './ICommentState' import ICommentState from './ICommentState'
import { commentsApi } from '../../api/commentApi' import { commentsApi } from '../../api/commentApi'
import ICommentDto from '../../interfaces/ICommentDto' import ICommentDto from '../../interfaces/ICommentDto'
interface CommentState {
comments: IComment[];
loading: boolean;
}
const namespace = 'comments' const initialState: CommentState = {
comments: [] as IComment[],
loading: false
};
export const getCommentsByPost = createAppAsyncThunk( export const getCommentsByPost = createAsyncThunk(
`${namespace}/getCommentsByPost`, 'getCommentsByPost',
async (postId: string): Promise<IResponse<IComment[] | undefined>> => { async (postId: string): Promise<IResponse<IComment[] | undefined>> => {
return commentsApi.getCommentsByPost(postId) return commentsApi.getCommentsByPost(postId)
} }
) )
export const createComment = createAppAsyncThunk( export const createComment = createAsyncThunk(
`${namespace}/createComment`, 'createComment',
async (comment: ICommentDto) => { async (comment: ICommentDto) => {
return commentsApi.createComment(comment) return commentsApi.createComment(comment)
} }
) )
export const deleteCommentById = createAppAsyncThunk( export const deleteCommentById = createAsyncThunk(
`${namespace}/deleteCommentById`, 'deleteCommentById',
async (id: string) => { async (id: string) => {
return commentsApi.deleteCommentById(id) return commentsApi.deleteCommentById(id)
} }
) )
export const commentsSlice = createSlice({ export const commentsSlice = createSlice({
name: namespace, name: 'comment',
initialState: { initialState,
comments: [] as IComment[], reducers: {
loadingComments: false, setInitialComment: (state) => {
messageComments: '' state.comments = [];
} as ICommentState, state.loading = false
reducers: {}, }
},
extraReducers: (builder) => { extraReducers: (builder) => {
builder builder
.addCase(getCommentsByPost.rejected, (state) => { .addCase(getCommentsByPost.rejected, (state) => {
state.loadingComments = false state.loading = false
}) })
.addCase(getCommentsByPost.pending, (state) => { .addCase(getCommentsByPost.pending, (state) => {
state.loadingComments = true state.loading = true
}) })
.addCase(getCommentsByPost.fulfilled, (state, action) => { .addCase(getCommentsByPost.fulfilled, (state, action) => {
state.loadingComments = false state.loading = false
state.comments = action.payload.result as IComment[] state.comments = action.payload.result as IComment[]
state.messageComments = action.payload.message
}) })
.addCase(createComment.rejected, (state) => { .addCase(createComment.rejected, (state) => {
state.loadingComments = false state.loading = false
}) })
.addCase(createComment.pending, (state) => { .addCase(createComment.pending, (state) => {
state.loadingComments = true state.loading = true
}) })
.addCase(createComment.fulfilled, (state, action) => { .addCase(createComment.fulfilled, (state, action) => {
state.loadingComments = false state.loading = false
state.messageComments = action.payload.message
}) })
.addCase(deleteCommentById.rejected, (state) => { .addCase(deleteCommentById.rejected, (state) => {
state.loadingComments = false state.loading = false
}) })
.addCase(deleteCommentById.pending, (state) => { .addCase(deleteCommentById.pending, (state) => {
state.loadingComments = true state.loading = true
}) })
.addCase(deleteCommentById.fulfilled, (state, action) => { .addCase(deleteCommentById.fulfilled, (state, action) => {
state.loadingComments = false state.loading = false
state.messageComments = action.payload.message
}) })
} }
}) })
export const {setInitialComment} = commentsSlice.actions
export default commentsSlice.reducer
\ No newline at end of file
import {configureStore} from '@reduxjs/toolkit'; import {configureStore} from '@reduxjs/toolkit';
import userSlice from './users/users.slice'; import userSlice from './users/users.slice';
<<<<<<< frontend/src/store/store.ts
import { postsSlice } from './posts/posts.slice'; import { postsSlice } from './posts/posts.slice';
=======
import commentsSlice from './comments/comments.slice';
>>>>>>> frontend/src/store/store.ts
export const store = configureStore({ export const store = configureStore({
reducer: { reducer: {
user: userSlice, user: userSlice,
<<<<<<< frontend/src/store/store.ts
posts: postsSlice.reducer, posts: postsSlice.reducer,
=======
comments: commentsSlice
>>>>>>> frontend/src/store/store.ts
}, },
}); });
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment