master

parent 1dd2db46
import { FunctionComponent, ReactElement, useState, ChangeEvent, FormEvent } from 'react'; import {
import { AppDispatch, AppState } from '../../store/store'; FunctionComponent,
import { shallowEqual, useDispatch } from 'react-redux'; ReactElement,
useState,
ChangeEvent,
FormEvent,
} from 'react';
import {AppDispatch, AppState} from '../../store/store';
import {shallowEqual, useDispatch} from 'react-redux';
import IPostDto from '../../interfaces/IPostDto'; import IPostDto from '../../interfaces/IPostDto';
import { createPost } from '../../store/posts/posts.slice'; import {createPost} from '../../store/posts/posts.slice';
import { useSelector } from 'react-redux'; import {useSelector} from 'react-redux';
const CreatePost: FunctionComponent = (): ReactElement => { const CreatePost: FunctionComponent = (): ReactElement => {
const { user } = useSelector((state: AppState) => state.user,shallowEqual); const {user} = useSelector((state: AppState) => state.user, shallowEqual);
const dispatch: AppDispatch = useDispatch() const dispatch: AppDispatch = useDispatch();
const [post, setPost] = useState<IPostDto>({ const [post, setPost] = useState<IPostDto>({
title: '', title: '',
description: '', description: '',
image: undefined image: undefined,
}) });
const [fileName, setFileName] = useState<string>('') const [fileName, setFileName] = useState<string>('');
const inputHandler = (e: ChangeEvent<HTMLInputElement>): void => { const inputHandler = (e: ChangeEvent<HTMLInputElement>): void => {
setPost(prevState => { setPost((prevState) => {
return { ...prevState, [e.target.name]: e.target.value } return {...prevState, [e.target.name]: e.target.value};
}) });
} };
const inputFileHandler = (e: ChangeEvent<HTMLInputElement>): void => { const inputFileHandler = (e: ChangeEvent<HTMLInputElement>): void => {
setPost(prevState => { setPost((prevState) => {
return { return {
...prevState, ...prevState,
image: e.target.files ? e.target.files[0] : undefined image: e.target.files ? e.target.files[0] : undefined,
} };
}) });
setFileName(e.target.files && e.target.files[0] ? e.target.files[0].name : '') setFileName(
} e.target.files && e.target.files[0] ? e.target.files[0].name : ''
);
};
const submitHandler = (e: FormEvent) => { const submitHandler = (e: FormEvent) => {
e.preventDefault() e.preventDefault();
const formData = new FormData(); const formData = new FormData();
Object.keys(post).forEach((key: any) => { formData.append('title', post.title);
//@ts-ignore formData.append('description', post.description);
formData.append(key, post[key]) formData.append('user', user._id);
}) dispatch(createPost(formData));
formData.append("user", user._id) setPost(post);
};
dispatch(createPost(formData))
setPost(post)
}
return ( return (
<div className="bg-gray-100 p-4"> <div className="bg-gray-100 p-4">
<form onSubmit={submitHandler} className="flex gap-4 flex-col items-center"> <form
onSubmit={submitHandler}
className="flex gap-4 flex-col items-center"
>
<input <input
name="title" name="title"
type="text" type="text"
placeholder="Title" placeholder="Title"
...@@ -83,7 +89,7 @@ const CreatePost: FunctionComponent = (): ReactElement => { ...@@ -83,7 +89,7 @@ const CreatePost: FunctionComponent = (): ReactElement => {
</button> </button>
</form> </form>
</div> </div>
) );
} };
export default CreatePost export default CreatePost;
import {createAsyncThunk, createSlice} from '@reduxjs/toolkit'; import {createAsyncThunk, createSlice} from '@reduxjs/toolkit';
import IPost from '../../interfaces/IPost' import IPost from '../../interfaces/IPost';
import { postApi } from '../../api/postApi' import {postApi} from '../../api/postApi';
import IResponse from '../../interfaces/IResponse' import IResponse from '../../interfaces/IResponse';
interface IPostState { interface IPostState {
posts: IPost[] posts: IPost[];
post: IPost post: IPost;
loadingPosts: boolean loadingPosts: boolean;
messagePosts: string messagePosts: string;
} }
const initialState:IPostState = { const initialState: IPostState = {
posts: [] as IPost[], posts: [] as IPost[],
post: {} as IPost, post: {} as IPost,
loadingPosts: false, loadingPosts: false,
messagePosts: '' messagePosts: '',
} };
const namespace = 'posts' const namespace = 'posts';
export const getPosts = createAsyncThunk( export const getPosts = createAsyncThunk(
`${namespace}/getPosts`, `${namespace}/getPosts`,
async (): Promise<IResponse<IPost[] | undefined>> => { async (): Promise<IResponse<IPost[] | undefined>> => {
return postApi.getPosts() return await postApi.getPosts();
} }
) );
export const getPostById = createAsyncThunk( export const getPostById = createAsyncThunk(
`${namespace}/getPostById`, `${namespace}/getPostById`,
async (id: string): Promise<IResponse<IPost | undefined>> => { async (id: string): Promise<IResponse<IPost | undefined>> => {
return postApi.getPostById(id) return await postApi.getPostById(id);
} }
) );
export const createPost = createAsyncThunk( export const createPost = createAsyncThunk(
`${namespace}/createPost`, `${namespace}/createPost`,
async (post: FormData) => { async (post: FormData) => {
return postApi.createPost(post) return await postApi.createPost(post);
} }
) );
export const deletePostById = createAsyncThunk( export const deletePostById = createAsyncThunk(
`${namespace}/deletePostById`, `${namespace}/deletePostById`,
async (id: string) => { async (id: string) => {
return postApi.deletePostById(id) return await postApi.deletePostById(id);
} }
) );
export const postsSlice = createSlice({ export const postsSlice = createSlice({
name: namespace, name: namespace,
...@@ -54,48 +53,49 @@ export const postsSlice = createSlice({ ...@@ -54,48 +53,49 @@ export const postsSlice = createSlice({
extraReducers: (builder) => { extraReducers: (builder) => {
builder builder
.addCase(getPosts.rejected, (state) => { .addCase(getPosts.rejected, (state) => {
state.loadingPosts = false state.loadingPosts = false;
}) })
.addCase(getPosts.pending, (state) => { .addCase(getPosts.pending, (state) => {
state.loadingPosts = true state.loadingPosts = true;
}) })
.addCase(getPosts.fulfilled, (state, action) => { .addCase(getPosts.fulfilled, (state, action) => {
state.loadingPosts = false state.loadingPosts = false;
state.posts = action.payload.result as IPost[] state.posts = action.payload.result as IPost[];
state.messagePosts = action.payload.message state.messagePosts = action.payload.message;
}) })
.addCase(getPostById.rejected, (state) => { .addCase(getPostById.rejected, (state) => {
state.loadingPosts = false state.loadingPosts = false;
}) })
.addCase(getPostById.pending, (state) => { .addCase(getPostById.pending, (state) => {
state.loadingPosts = true state.loadingPosts = true;
}) })
.addCase(getPostById.fulfilled, (state, action) => { .addCase(getPostById.fulfilled, (state, action) => {
state.loadingPosts = false state.loadingPosts = false;
state.post = action.payload.result as IPost state.post = action.payload.result as IPost;
state.messagePosts = action.payload.message state.messagePosts = action.payload.message;
}) })
.addCase(createPost.rejected, (state) => { .addCase(createPost.rejected, (state) => {
state.loadingPosts = false state.loadingPosts = false;
}) })
.addCase(createPost.pending, (state) => { .addCase(createPost.pending, (state) => {
state.loadingPosts = true state.loadingPosts = true;
}) })
.addCase(createPost.fulfilled, (state, action) => { .addCase(createPost.fulfilled, (state, action) => {
state.loadingPosts = false state.loadingPosts = false;
state.messagePosts = action.payload.message state.messagePosts = action.payload.message;
console.log(action.payload);
}) })
.addCase(deletePostById.rejected, (state) => { .addCase(deletePostById.rejected, (state) => {
state.loadingPosts = false state.loadingPosts = false;
}) })
.addCase(deletePostById.pending, (state) => { .addCase(deletePostById.pending, (state) => {
state.loadingPosts = true state.loadingPosts = true;
}) })
.addCase(deletePostById.fulfilled, (state, action) => { .addCase(deletePostById.fulfilled, (state, action) => {
state.loadingPosts = false state.loadingPosts = false;
state.messagePosts = action.payload.message state.messagePosts = action.payload.message;
}) });
} },
}) });
export default postsSlice.reducer; export default postsSlice.reducer;
...@@ -104,6 +104,7 @@ export const userSlice = createSlice({ ...@@ -104,6 +104,7 @@ export const userSlice = createSlice({
state.user.username = action.payload[0].username; state.user.username = action.payload[0].username;
state.loading = false; state.loading = false;
state.userLoggedIn = true; state.userLoggedIn = true;
state.user._id = action.payload[0]._id;
}); });
}, },
}); });
......
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