Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
H
Homework_95_Tsoy_Danil
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
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
Цой Данил
Homework_95_Tsoy_Danil
Commits
8f7518e4
Commit
8f7518e4
authored
Apr 13, 2023
by
Цой Данил
💬
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Rewrote slice logic
parent
8869621f
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
45 additions
and
36 deletions
+45
-36
Chat.js
frontend/src/Container/Chat/Chat.js
+2
-2
messagesSlice.js
frontend/src/Store/Services/messagesSlice.js
+5
-4
usersSlice.js
frontend/src/Store/Services/usersSlice.js
+27
-20
store.js
frontend/src/Store/store.js
+10
-0
index.js
frontend/src/index.js
+1
-10
No files found.
frontend/src/Container/Chat/Chat.js
View file @
8f7518e4
import
React
,
{
useState
,
useRef
,
useEffect
}
from
"react"
;
import
React
,
{
useState
,
useRef
,
useEffect
}
from
"react"
;
import
{
useSelector
,
shallowEqual
,
useDispatch
}
from
"react-redux"
;
import
{
useSelector
,
shallowEqual
,
useDispatch
}
from
"react-redux"
;
import
{
getAllMessages
,
getMessages
,
getOnlineUser
,
deleteOflineUser
}
from
"../../Store/Services/messagesSlice"
;
import
{
getAllMessages
,
getMessages
,
getOnlineUser
,
deleteOf
f
lineUser
}
from
"../../Store/Services/messagesSlice"
;
import
UserList
from
"../../Components/UserList/UserList"
;
import
UserList
from
"../../Components/UserList/UserList"
;
import
ChatWindow
from
"../../Components/ChatWindow/ChatWindow"
;
import
ChatWindow
from
"../../Components/ChatWindow/ChatWindow"
;
import
'./Chat.css'
;
import
'./Chat.css'
;
...
@@ -36,7 +36,7 @@ const Chat = () => {
...
@@ -36,7 +36,7 @@ const Chat = () => {
dispatch
(
getOnlineUser
(
decodedMessage
.
user
));
dispatch
(
getOnlineUser
(
decodedMessage
.
user
));
};
};
if
(
decodedMessage
.
type
===
'CLOSED_CHAT_USER'
)
{
if
(
decodedMessage
.
type
===
'CLOSED_CHAT_USER'
)
{
dispatch
(
deleteOflineUser
(
decodedMessage
.
id
));
dispatch
(
deleteOf
f
lineUser
(
decodedMessage
.
id
));
};
};
};
};
...
...
frontend/src/Store/Services/messagesSlice.js
View file @
8f7518e4
import
{
createSlice
}
from
"@reduxjs/toolkit"
;
import
{
createSlice
}
from
"@reduxjs/toolkit"
;
const
namespace
=
'messages'
const
initialState
=
{
const
initialState
=
{
messages
:
[],
messages
:
[],
...
@@ -8,7 +9,7 @@ const initialState = {
...
@@ -8,7 +9,7 @@ const initialState = {
const
messagesSlice
=
createSlice
({
const
messagesSlice
=
createSlice
({
name
:
'messages'
,
name
:
namespace
,
initialState
,
initialState
,
reducers
:
{
reducers
:
{
getMessages
:
(
state
,
action
)
=>
{
getMessages
:
(
state
,
action
)
=>
{
...
@@ -20,7 +21,7 @@ const messagesSlice = createSlice({
...
@@ -20,7 +21,7 @@ const messagesSlice = createSlice({
getOnlineUser
:
(
state
,
action
)
=>
{
getOnlineUser
:
(
state
,
action
)
=>
{
state
.
users
=
action
.
payload
;
state
.
users
=
action
.
payload
;
},
},
deleteOflineUser
:
(
state
,
action
)
=>
{
deleteOf
f
lineUser
:
(
state
,
action
)
=>
{
const
index
=
state
.
users
.
findIndex
(
user
=>
user
.
_id
===
action
.
payload
);
const
index
=
state
.
users
.
findIndex
(
user
=>
user
.
_id
===
action
.
payload
);
state
.
users
.
splice
(
index
,
1
);
state
.
users
.
splice
(
index
,
1
);
}
}
...
@@ -28,5 +29,5 @@ const messagesSlice = createSlice({
...
@@ -28,5 +29,5 @@ const messagesSlice = createSlice({
});
});
export
const
{
getMessages
,
getAllMessages
,
getOnlineUser
,
deleteOflineUser
}
=
messagesSlice
.
actions
;
export
const
{
getMessages
,
getAllMessages
,
getOnlineUser
,
deleteOfflineUser
}
=
messagesSlice
.
actions
;
export
default
messagesSlice
.
reducer
;
export
default
messagesSlice
;
\ No newline at end of file
\ No newline at end of file
frontend/src/Store/Services/usersSlice.js
View file @
8f7518e4
import
{
createSlice
,
createAsyncThunk
}
from
"@reduxjs/toolkit"
;
import
{
createSlice
,
createAsyncThunk
}
from
"@reduxjs/toolkit"
;
import
axios
from
"../../AxiosBaseUrl"
;
import
axios
from
"../../AxiosBaseUrl"
;
const
namespace
=
'users'
const
initialState
=
{
const
initialState
=
{
user
:
null
,
user
:
null
,
isLoading
:
false
,
isLoading
:
false
,
...
@@ -12,7 +14,7 @@ const initialState = {
...
@@ -12,7 +14,7 @@ const initialState = {
};
};
export
const
loginUser
=
createAsyncThunk
(
export
const
loginUser
=
createAsyncThunk
(
'users/login'
,
`
${
namespace
}
/loginUser`
,
async
(
payload
,
thunkApi
)
=>
{
async
(
payload
,
thunkApi
)
=>
{
try
{
try
{
const
res
=
await
axios
.
post
(
'/users/sessions'
,
payload
.
userData
);
const
res
=
await
axios
.
post
(
'/users/sessions'
,
payload
.
userData
);
...
@@ -29,7 +31,7 @@ export const loginUser = createAsyncThunk(
...
@@ -29,7 +31,7 @@ export const loginUser = createAsyncThunk(
);
);
export
const
registerUser
=
createAsyncThunk
(
export
const
registerUser
=
createAsyncThunk
(
'users/register'
,
`
${
namespace
}
/registerUser`
,
async
(
payload
,
thunkApi
)
=>
{
async
(
payload
,
thunkApi
)
=>
{
try
{
try
{
return
await
axios
.
post
(
'/users'
,
payload
.
userData
).
then
(
res
=>
res
.
data
);
return
await
axios
.
post
(
'/users'
,
payload
.
userData
).
then
(
res
=>
res
.
data
);
...
@@ -44,7 +46,7 @@ export const registerUser = createAsyncThunk(
...
@@ -44,7 +46,7 @@ export const registerUser = createAsyncThunk(
);
);
export
const
userLogout
=
createAsyncThunk
(
export
const
userLogout
=
createAsyncThunk
(
'logout/users'
,
`
${
namespace
}
/userLogout`
,
async
(
payload
,
thunkAPI
)
=>
{
async
(
payload
,
thunkAPI
)
=>
{
try
{
try
{
const
token
=
thunkAPI
.
getState
().
users
.
token
;
const
token
=
thunkAPI
.
getState
().
users
.
token
;
...
@@ -60,7 +62,7 @@ export const userLogout = createAsyncThunk(
...
@@ -60,7 +62,7 @@ export const userLogout = createAsyncThunk(
);
);
const
usersSlice
=
createSlice
({
const
usersSlice
=
createSlice
({
name
:
'users'
,
name
:
namespace
,
initialState
,
initialState
,
reducers
:
{
reducers
:
{
getMessages
:
(
state
,
action
)
=>
{
getMessages
:
(
state
,
action
)
=>
{
...
@@ -79,38 +81,43 @@ const usersSlice = createSlice({
...
@@ -79,38 +81,43 @@ const usersSlice = createSlice({
state
.
user
=
null
;
state
.
user
=
null
;
}
}
},
},
extraReducers
:
{
extraReducers
:
(
builder
)
=>
{
[
registerUser
.
pending
]:
(
state
)
=>
{
builder
.
addCase
(
registerUser
.
pending
,
(
state
)
=>
{
state
.
isLoading
=
true
;
state
.
isLoading
=
true
;
state
.
loginError
=
null
;
state
.
loginError
=
null
;
state
.
global
=
null
;
state
.
global
=
null
;
state
.
registerError
=
null
;
state
.
registerError
=
null
;
}
,
}
)
[
registerUser
.
rejected
]:
(
state
,
action
)
=>
{
.
addCase
(
registerUser
.
rejected
,
(
state
,
action
)
=>
{
state
.
isLoading
=
false
;
state
.
isLoading
=
false
;
state
.
error
=
action
.
error
;
state
.
error
=
action
.
error
;
},
})
[
registerUser
.
fulfilled
]:
(
state
,
action
)
=>
{
.
addCase
(
registerUser
.
fulfilled
,
(
state
,
action
)
=>
{
state
.
registerStatus
=
action
.
payload
.
user
;
state
.
isLoading
=
false
;
state
.
isLoading
=
false
;
},
if
(
action
.
payload
.
user
){
[
loginUser
.
pending
]:
(
state
)
=>
{
state
.
registerStatus
=
action
.
payload
.
user
;
}
})
.
addCase
(
loginUser
.
pending
,
(
state
)
=>
{
state
.
isLoading
=
true
;
state
.
isLoading
=
true
;
state
.
loginError
=
null
;
state
.
loginError
=
null
;
state
.
global
=
null
;
state
.
global
=
null
;
state
.
registerError
=
null
;
state
.
registerError
=
null
;
}
,
}
)
[
loginUser
.
rejected
]:
(
state
,
action
)
=>
{
.
addCase
(
loginUser
.
rejected
,
(
state
,
action
)
=>
{
state
.
isLoading
=
false
;
state
.
isLoading
=
false
;
state
.
error
=
action
.
error
;
state
.
error
=
action
.
error
;
},
})
[
loginUser
.
fulfilled
]:
(
state
,
action
)
=>
{
.
addCase
(
loginUser
.
fulfilled
,
(
state
,
action
)
=>
{
state
.
user
=
action
.
payload
;
state
.
isLoading
=
false
;
state
.
isLoading
=
false
;
}
if
(
action
.
payload
){
state
.
user
=
action
.
payload
;
}
})
}
}
});
});
export
const
{
catchLoginError
,
catchRegisterError
,
globalError
,
logoutUser
,
getMessages
}
=
usersSlice
.
actions
;
export
const
{
catchLoginError
,
catchRegisterError
,
globalError
,
logoutUser
,
getMessages
}
=
usersSlice
.
actions
;
export
default
usersSlice
.
reducer
;
export
default
usersSlice
;
\ No newline at end of file
\ No newline at end of file
frontend/src/Store/store.js
0 → 100644
View file @
8f7518e4
import
{
configureStore
}
from
"@reduxjs/toolkit"
;
import
usersReducer
from
'./Services/usersSlice'
import
messagesReducer
from
'./Services/messagesSlice'
export
const
store
=
configureStore
({
reducer
:
{
users
:
usersReducer
.
reducer
,
messages
:
messagesReducer
.
reducer
}
});
\ No newline at end of file
frontend/src/index.js
View file @
8f7518e4
...
@@ -3,17 +3,8 @@ import ReactDOM from 'react-dom/client';
...
@@ -3,17 +3,8 @@ import ReactDOM from 'react-dom/client';
import
'./index.css'
;
import
'./index.css'
;
import
App
from
'./App'
;
import
App
from
'./App'
;
import
{
Provider
}
from
'react-redux'
;
import
{
Provider
}
from
'react-redux'
;
import
{
configureStore
}
from
'@reduxjs/toolkit'
;
import
usersReducer
from
'./Store/Services/usersSlice.js'
;
import
messagesReducer
from
'./Store/Services/messagesSlice.js'
;
import
reportWebVitals
from
'./reportWebVitals'
;
import
reportWebVitals
from
'./reportWebVitals'
;
import
{
store
}
from
'./Store/store'
;
const
store
=
configureStore
({
reducer
:
{
users
:
usersReducer
,
messages
:
messagesReducer
}
});
const
root
=
ReactDOM
.
createRoot
(
document
.
getElementById
(
'root'
));
const
root
=
ReactDOM
.
createRoot
(
document
.
getElementById
(
'root'
));
...
...
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