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

Merge branch '#29' into 'dev'

displayed comments

See merge request !31
parents f182237d 390476fe
...@@ -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']
user: string
post: string
} }
\ No newline at end of file
.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';
import commentsSlice from './comments/comments.slice';
export const store = configureStore({ export const store = configureStore({
reducer: { reducer: {
user: userSlice, user: userSlice,
comments: commentsSlice
}, },
}); });
......
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