Commit 8f7518e4 authored by Цой Данил's avatar Цой Данил 💬

Rewrote slice logic

parent 8869621f
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, deleteOfflineUser } 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(deleteOfflineUser(decodedMessage.id));
}; };
}; };
......
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) => { deleteOfflineUser: (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
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
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
...@@ -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'));
......
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