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 {
}
private addComment = async (req: Request, res: Response): Promise<void> => {
const response = await this.service.addComment(req.body)
res.send(response)
}
......
......@@ -4,8 +4,8 @@ import IPost from "./IPost";
export default interface IComment extends Document {
_id: ObjectId
user: IUser
post: IPost
user: string
post: string
comment: string
datetime: Date
}
\ No newline at end of file
......@@ -2,4 +2,6 @@ import IComment from "./IComment"
export default interface ICommentDto {
comment: IComment['comment']
post: string
user: string
}
\ No newline at end of file
......@@ -9,6 +9,7 @@ import IComment from '../interfaces/IComment';
import ICommentDto from '../interfaces/ICommentDto';
import { Comment } from '../models/Comments';
import IUser from '../interfaces/IUser';
import UserModel from '../models/User';
export class Mongo implements IDataBase {
[x: string]: any;
......@@ -26,7 +27,7 @@ export class Mongo implements IDataBase {
public getPosts = async (): Promise<IResponse<IPost[] | undefined>> => {
try {
const data = await Post.find().sort({ datetime: -1 })
const data = await Post.find()
const response: IResponse<IPost[]> = {
status: EStatuses.OK,
result: data as any,
......@@ -141,7 +142,7 @@ export class Mongo implements IDataBase {
}
public getCommentById = async (id: string): Promise<IResponse<IComment | undefined>> => {
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> = {
status: EStatuses.OK,
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"
export default interface IComment {
_id: string
user: IUser
post: IPost
user: string
post: string
comment: string
datetime: Date
}
\ No newline at end of file
import { createSlice } from '@reduxjs/toolkit'
import { createAppAsyncThunk } from '../createAppAsyncThunk'
import { createAsyncThunk, createSlice } from '@reduxjs/toolkit'
import IResponse from '../../interfaces/IResponse'
import IComment from '../../interfaces/IComment'
import ICommentState from './ICommentState'
import { commentsApi } from '../../api/commentApi'
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(
`${namespace}/getCommentsByPost`,
export const getCommentsByPost = createAsyncThunk(
'getCommentsByPost',
async (postId: string): Promise<IResponse<IComment[] | undefined>> => {
return commentsApi.getCommentsByPost(postId)
}
)
export const createComment = createAppAsyncThunk(
`${namespace}/createComment`,
export const createComment = createAsyncThunk(
'createComment',
async (comment: ICommentDto) => {
return commentsApi.createComment(comment)
}
)
export const deleteCommentById = createAppAsyncThunk(
`${namespace}/deleteCommentById`,
export const deleteCommentById = createAsyncThunk(
'deleteCommentById',
async (id: string) => {
return commentsApi.deleteCommentById(id)
}
)
export const commentsSlice = createSlice({
name: namespace,
initialState: {
comments: [] as IComment[],
loadingComments: false,
messageComments: ''
} as ICommentState,
reducers: {},
name: 'comment',
initialState,
reducers: {
setInitialComment: (state) => {
state.comments = [];
state.loading = false
}
},
extraReducers: (builder) => {
builder
.addCase(getCommentsByPost.rejected, (state) => {
state.loadingComments = false
state.loading = false
})
.addCase(getCommentsByPost.pending, (state) => {
state.loadingComments = true
state.loading = true
})
.addCase(getCommentsByPost.fulfilled, (state, action) => {
state.loadingComments = false
state.loading = false
state.comments = action.payload.result as IComment[]
state.messageComments = action.payload.message
})
.addCase(createComment.rejected, (state) => {
state.loadingComments = false
state.loading = false
})
.addCase(createComment.pending, (state) => {
state.loadingComments = true
state.loading = true
})
.addCase(createComment.fulfilled, (state, action) => {
state.loadingComments = false
state.messageComments = action.payload.message
state.loading = false
})
.addCase(deleteCommentById.rejected, (state) => {
state.loadingComments = false
state.loading = false
})
.addCase(deleteCommentById.pending, (state) => {
state.loadingComments = true
state.loading = true
})
.addCase(deleteCommentById.fulfilled, (state, action) => {
state.loadingComments = false
state.messageComments = action.payload.message
state.loading = false
})
}
})
\ No newline at end of file
})
export const {setInitialComment} = commentsSlice.actions
export default commentsSlice.reducer
\ No newline at end of file
import {configureStore} from '@reduxjs/toolkit';
import userSlice from './users/users.slice';
<<<<<<< frontend/src/store/store.ts
import { postsSlice } from './posts/posts.slice';
=======
import commentsSlice from './comments/comments.slice';
>>>>>>> frontend/src/store/store.ts
export const store = configureStore({
reducer: {
user: userSlice,
<<<<<<< frontend/src/store/store.ts
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