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
ada9e5fc
Commit
ada9e5fc
authored
Apr 04, 2023
by
Zhanara
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Вывела посты на страницу
parent
f182237d
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
117 additions
and
37 deletions
+117
-37
CreatePost.jsx
frontend/src/components/CreatePost/CreatePost.jsx
+0
-0
Layout.tsx
frontend/src/components/Layout.tsx
+16
-13
Post.tsx
frontend/src/components/Post/Post.tsx
+36
-0
PostList.tsx
frontend/src/components/PostList/PostList.tsx
+26
-0
HomePage.tsx
frontend/src/containers/HomePage.tsx
+12
-1
index.css
frontend/src/index.css
+1
-1
IPostState.ts
frontend/src/store/posts/IPostState.ts
+0
-8
posts.slice.ts
frontend/src/store/posts/posts.slice.ts
+22
-14
store.ts
frontend/src/store/store.ts
+4
-0
No files found.
frontend/src/components/CreatePost/CreatePost.jsx
0 → 100644
View file @
ada9e5fc
frontend/src/components/Layout.tsx
View file @
ada9e5fc
import
React
,
{
FunctionComponent
,
ReactElement
}
from
'react'
;
import
{
NavLink
,
Outlet
}
from
'react-router-dom'
;
import
{
useAppDispatch
,
useAppSelector
}
from
'../store/hooks'
;
import
{
setInitialUser
}
from
'../store/users/users.slice'
;
import
{
useDispatch
}
from
'react-redux'
;
import
React
,
{
FunctionComponent
,
ReactElement
}
from
'react'
;
import
{
NavLink
,
Outlet
}
from
'react-router-dom'
;
import
{
useAppDispatch
,
useAppSelector
}
from
'../store/hooks'
;
import
{
setInitialUser
}
from
'../store/users/users.slice'
;
const
Layout
:
FunctionComponent
=
():
ReactElement
=>
{
const
{
user
}
=
useAppSelector
((
state
)
=>
state
.
user
);
const
{
user
}
=
useAppSelector
((
state
)
=>
state
.
user
);
const
dispatch
=
useAppDispatch
();
return
(
<
div
className=
"bg-gray-
50 dark:bg-gray-9
00"
>
<
div
className=
"
flex justify-between container mx-auto
"
>
<
p
>
Forum
</
p
>
<
div
className=
"bg-gray-
1
00"
>
<
div
className=
"
container mx-auto flex justify-between items-center py-4
"
>
<
p
className=
"text-lg font-medium"
>
Forum
</
p
>
{
!
user
.
username
?
(
<
div
>
<
NavLink
className=
{
'mr-3'
}
to=
{
'register'
}
>
<
NavLink
className=
"text-gray-700 hover:text-gray-900"
to=
{
'register'
}
>
Register
</
NavLink
>
</
div
>
)
:
(
<
div
className=
"flex items-center"
>
<
p
>
{
user
.
username
}
</
p
>
<
p
className=
"text-gray-700 mr-4"
>
<
span
className=
"text-indigo-600"
>
{
user
.
username
}
</
span
>
</
p
>
<
NavLink
className=
{
'mx-4'
}
to=
{
'/'
}
>
Add new Post
</
NavLink
>
...
...
@@ -29,13 +30,15 @@ const Layout: FunctionComponent = (): ReactElement => {
dispatch
(
setInitialUser
());
}
}
to=
{
'/'
}
>
className=
"text-gray-700 hover:text-gray-900"
>
Log out
</
NavLink
>
</
div
>
)
}
</
div
>
<
Outlet
/>
<
div
className=
"container mx-auto py-8"
>
<
Outlet
/>
</
div
>
</
div
>
);
};
...
...
frontend/src/components/Post/Post.tsx
0 → 100644
View file @
ada9e5fc
import
React
,
{
FunctionComponent
,
MutableRefObject
,
ReactElement
,
useRef
}
from
"react"
;
import
IPost
from
"../../interfaces/IPost"
;
import
defaultImage
from
'../../assets/default-image.jpg'
import
IUser
from
"../../interfaces/IUser"
;
interface
IPostProps
{
post
:
IPost
;
}
const
Post
:
FunctionComponent
<
IPostProps
>
=
({
post
}):
ReactElement
=>
{
return
(
<
div
className=
" rounded-lg shadow-md flex flex-col justify-between mb-5"
>
<
img
className=
"object-cover w-full h-80 rounded-t-lg"
src=
{
`${import.meta.env.VITE_BASE_URL}uploads/${post.image}`
}
alt=
{
post
.
title
}
onError=
{
(
e
)
=>
{
e
.
currentTarget
.
src
=
defaultImage
;
}
}
/>
<
div
className=
"p-4"
>
<
p
className=
"text-gray-400 text-xs"
>
{
post
.
datetime
}
</
p
>
<
p
className=
"text-lg font-bold mb-2"
>
{
post
.
title
}
</
p
>
<
p
className=
"text-sm mb-2"
>
{
post
.
description
}
</
p
>
<
p
className=
"text-sm mb-2 text-gray-400"
>
Посмотреть все комментарии
</
p
>
</
div
>
<
div
className=
"border-t p-4"
>
</
div
>
</
div
>
)
}
export
default
Post
\ No newline at end of file
frontend/src/components/PostList/PostList.tsx
0 → 100644
View file @
ada9e5fc
import
React
,
{
FunctionComponent
,
ReactElement
}
from
"react"
;
import
IPost
from
"../../interfaces/IPost"
;
import
Post
from
"../Post/Post"
;
interface
IPostListProps
{
posts
:
IPost
[]
}
const
PostsList
:
FunctionComponent
<
IPostListProps
>
=
(
posts
):
ReactElement
=>
{
return
(
<
div
className=
"flex justify-center items-center"
>
<
div
>
{
posts
.
posts
.
length
?
(
posts
.
posts
.
map
((
p
)
=>
{
return
<
Post
key=
{
p
.
_id
}
post=
{
p
}
/>;
})
)
:
(
<
h1
className=
"text-center text-2xl font-bold mt-10"
>
No Posts
</
h1
>
)
}
</
div
>
</
div
>
)
}
export
default
PostsList
\ No newline at end of file
frontend/src/containers/HomePage.tsx
View file @
ada9e5fc
...
...
@@ -2,9 +2,15 @@ import {FunctionComponent, ReactElement, useEffect} from 'react';
import
{
shallowEqual
,
useDispatch
}
from
'react-redux'
;
import
{
useAppDispatch
,
useAppSelector
}
from
'../store/hooks'
;
import
{
getUser
}
from
'../store/users/users.slice'
;
import
{
useSelector
}
from
'react-redux'
;
import
PostsList
from
'../components/PostList/PostList'
;
import
{
AppState
}
from
'../store/store'
;
import
{
getPosts
}
from
'../store/posts/posts.slice'
;
const
HomePage
:
FunctionComponent
=
():
ReactElement
=>
{
const
dispatch
=
useAppDispatch
();
const
{
posts
}
=
useSelector
((
state
:
AppState
)
=>
state
.
posts
,
shallowEqual
)
useEffect
(()
=>
{
const
token
=
window
.
sessionStorage
.
getItem
(
'token'
);
...
...
@@ -12,9 +18,14 @@ const HomePage: FunctionComponent = (): ReactElement => {
dispatch
(
getUser
(
token
));
}
});
useEffect
(()
=>
{
dispatch
(
getPosts
())
},
[])
return
(
<
div
className=
"bg-gray-50 dark:bg-gray-900 h-screen w-full container mx-auto"
>
homepage
<
PostsList
posts=
{
posts
}
/>
</
div
>
);
};
...
...
frontend/src/index.css
View file @
ada9e5fc
...
...
@@ -5,7 +5,7 @@
}
*
{
color
:
white
;
}
@tailwind
base
;
...
...
frontend/src/store/posts/IPostState.ts
deleted
100644 → 0
View file @
f182237d
import
IPost
from
"../../interfaces/IPost"
export
default
interface
IPostState
{
posts
:
IPost
[]
post
:
IPost
loadingPosts
:
boolean
messagePosts
:
string
}
\ No newline at end of file
frontend/src/store/posts/posts.slice.ts
View file @
ada9e5fc
import
{
createSlice
}
from
'@reduxjs/toolkit'
import
{
createAsyncThunk
,
createSlice
}
from
'@reduxjs/toolkit'
;
import
IPost
from
'../../interfaces/IPost'
import
IPostState
from
'./IPostState'
import
{
createAppAsyncThunk
}
from
'../createAppAsyncThunk'
import
{
postApi
}
from
'../../api/postApi'
import
IResponse
from
'../../interfaces/IResponse'
interface
IPostState
{
posts
:
IPost
[]
post
:
IPost
loadingPosts
:
boolean
messagePosts
:
string
}
const
initialState
:
IPostState
=
{
posts
:
[]
as
IPost
[],
post
:
{}
as
IPost
,
loadingPosts
:
false
,
messagePosts
:
''
}
const
namespace
=
'posts'
export
const
getPosts
=
createA
ppA
syncThunk
(
export
const
getPosts
=
createAsyncThunk
(
`
${
namespace
}
/getPosts`
,
async
():
Promise
<
IResponse
<
IPost
[]
|
undefined
>>
=>
{
return
postApi
.
getPosts
()
}
)
export
const
getPostById
=
createA
ppA
syncThunk
(
export
const
getPostById
=
createAsyncThunk
(
`
${
namespace
}
/getPostById`
,
async
(
id
:
string
):
Promise
<
IResponse
<
IPost
|
undefined
>>
=>
{
return
postApi
.
getPostById
(
id
)
}
)
export
const
createPost
=
createA
ppA
syncThunk
(
export
const
createPost
=
createAsyncThunk
(
`
${
namespace
}
/createPost`
,
async
(
post
:
FormData
)
=>
{
return
postApi
.
createPost
(
post
)
}
)
export
const
deletePostById
=
createA
ppA
syncThunk
(
export
const
deletePostById
=
createAsyncThunk
(
`
${
namespace
}
/deletePostById`
,
async
(
id
:
string
)
=>
{
return
postApi
.
deletePostById
(
id
)
...
...
@@ -38,12 +49,7 @@ export const deletePostById = createAppAsyncThunk(
export
const
postsSlice
=
createSlice
({
name
:
namespace
,
initialState
:
{
posts
:
[]
as
IPost
[],
post
:
{},
loadingPosts
:
false
,
messagePosts
:
''
}
as
IPostState
,
initialState
,
reducers
:
{},
extraReducers
:
(
builder
)
=>
{
builder
...
...
@@ -90,4 +96,6 @@ export const postsSlice = createSlice({
state
.
messagePosts
=
action
.
payload
.
message
})
}
})
\ No newline at end of file
})
export
default
postsSlice
.
reducer
;
\ No newline at end of file
frontend/src/store/store.ts
View file @
ada9e5fc
import
{
configureStore
}
from
'@reduxjs/toolkit'
;
import
userSlice
from
'./users/users.slice'
;
import
{
postsSlice
}
from
'./posts/posts.slice'
;
export
const
store
=
configureStore
({
reducer
:
{
user
:
userSlice
,
posts
:
postsSlice
.
reducer
,
},
});
export
type
RootState
=
ReturnType
<
typeof
store
.
getState
>
;
export
type
AppDispatch
=
typeof
store
.
dispatch
;
export
type
AppStore
=
ReturnType
<
typeof
store
.
getState
>
;
export
type
AppState
=
ReturnType
<
typeof
store
.
getState
>
;
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