Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
H
hw92
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
5
Issues
5
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Болатов Ален
hw92
Commits
31f5d478
Commit
31f5d478
authored
Apr 04, 2023
by
Болатов Ален
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
master
parent
1dd2db46
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
170 additions
and
163 deletions
+170
-163
main.tsx
frontend/main.tsx
+0
-0
CreatePost.tsx
frontend/src/components/CreatePost/CreatePost.tsx
+84
-78
posts.slice.ts
frontend/src/store/posts/posts.slice.ts
+85
-85
users.slice.ts
frontend/src/store/users/users.slice.ts
+1
-0
No files found.
frontend/main.tsx
0 → 100644
View file @
31f5d478
frontend/src/components/CreatePost/CreatePost.tsx
View file @
31f5d478
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
;
frontend/src/store/posts/posts.slice.ts
View file @
31f5d478
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
;
frontend/src/store/users/users.slice.ts
View file @
31f5d478
...
@@ -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
;
});
});
},
},
});
});
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment