Browse Source

reducers added

pocu46 4 years ago
parent
commit
7710c30f63

+ 1 - 1
hipstagram/src/Content/Messages/messages.js

@@ -1,5 +1,5 @@
 import React from "react";
-import { sendMessageCreator, updateNewMessageBodyCreator } from "../../Redux/state";
+import { sendMessageCreator, updateNewMessageBodyCreator } from "../../Redux/messages-reducer";
 import MessageItem from "./DialogItem/dialogItem";
 import Message from "./Message/message";
 import "./messages.css";

+ 1 - 1
hipstagram/src/Content/Profile/MyPosts/myPosts.js

@@ -1,7 +1,7 @@
 import React from 'react';
 import Post from './Post/post';
 import './myPosts.css';
-import { addPostActionCreator, updateNewPostTextActionCreator } from '../../../Redux/state.js';
+import { addPostActionCreator, updateNewPostTextActionCreator } from '../../../Redux/profile-reducer';
 
 function MyPosts(props) {
 

+ 27 - 0
hipstagram/src/Redux/messages-reducer.js

@@ -0,0 +1,27 @@
+const UPDATE_NEW_MESSAGE_BODY = 'UPDATE-NEW-MESSAGE-BODY';
+const SEND_MESSAGE = 'SEND-MESSAGE';
+
+const messagesReducer = (state, action) => {
+    switch (action.type) {
+        case UPDATE_NEW_MESSAGE_BODY:
+            state.newMessageBody = action.body;
+            return state;
+        case SEND_MESSAGE:
+            let body = state.newMessageBody;
+            state.newMessageBody = 0;
+            state.messagesData.push({ id: 4, message: body });
+            return state;
+        default:
+            return state;
+    }
+}
+
+export const sendMessageCreator = () => ({ type: 'SEND-MESSAGE' });
+export const updateNewMessageBodyCreator = (body) => {
+    return {
+        type: 'UPDATE-NEW-MESSAGE-BODY', 
+        body: body
+    }
+}
+
+export default messagesReducer;

+ 6 - 0
hipstagram/src/Redux/navbar-reducer.js

@@ -0,0 +1,6 @@
+const navbarReducer = (state, action) => {
+
+    return state;
+}
+
+export default navbarReducer;

+ 31 - 0
hipstagram/src/Redux/profile-reducer.js

@@ -0,0 +1,31 @@
+const ADD_POST = 'ADD-POST';
+const UPDATE_NEW_POST_TEXT = 'UPDATE-NEW-POST-TEXT';
+
+const profileReducer = (state, action) => {
+    switch (action.type) {
+        case ADD_POST:
+            let newPost = {
+                id: 5,
+                message: state.newPostText,
+                likesCount: 0,
+            };
+            state.post.push(newPost);
+            state.newPostText = '';
+            return state;
+        case UPDATE_NEW_POST_TEXT:
+            state.newPostText = action.newText;
+            return state;
+        default:
+            return state;
+    }
+}
+
+export const addPostActionCreator = (body) => ({ type: 'ADD-POST', body });
+export const updateNewPostTextActionCreator = (text) => {
+    return {
+        type: 'UPDATE-NEW-POST-TEXT', 
+        newText: text
+    }
+}
+
+export default profileReducer;

+ 9 - 40
hipstagram/src/Redux/state.js

@@ -1,7 +1,6 @@
-const ADD_POST = 'ADD-POST';
-const UPDATE_NEW_POST_TEXT = 'UPDATE-NEW-POST-TEXT';
-const UPDATE_NEW_MESSAGE_BODY = 'UPDATE-NEW-MESSAGE-BODY';
-const SEND_MESSAGE = 'SEND-MESSAGE';
+import messagesReducer from "./messages-reducer";
+import navbarReducer from "./navbar-reducer";
+import profileReducer from "./profile-reducer";
 
 let store = {
     _state: {
@@ -29,6 +28,7 @@ let store = {
             ],
             newMessageBody: ""
         },
+        navbar: {}
     },
     getState() {
         return this._state;
@@ -37,44 +37,13 @@ let store = {
         this._callSubscriber = observer;
     },
     dispatch(action) {
-        if (action.type === ADD_POST) {
-            let newPost = {
-                id: 5,
-                message: this._state.profilePage.newPostText,
-                likesCount: 0,
-            };
-            this._state.profilePage.post.push(newPost);
-            this._state.profilePage.newPostText = "";
-            this._callSubscriber(this._state);
-        } else if(action.type === UPDATE_NEW_POST_TEXT) {
-            this._state.profilePage.newPostText = action.newText;
-            this._callSubscriber(this._state);
-        } else if(action.type === UPDATE_NEW_MESSAGE_BODY) {
-            this._state.messagesPage.newMessageBody = action.body;
-            this._callSubscriber(this._state);
-        } else if(action.type === SEND_MESSAGE) {
-            let body = this._state.messagesPage.newMessageBody;
-            this._state.messagesPage.newMessageBody = 0;
-            this._state.messagesPage.messagesData.push({ id: 4, message: body });
-            this._callSubscriber(this._state);
-        }
-    }
-};
 
-export const addPostActionCreator = (body) => ({ type: 'ADD-POST', body });
-export const updateNewPostTextActionCreator = (text) => {
-    return {
-        type: 'UPDATE-NEW-POST-TEXT', 
-        newText: text
-    }
-}
+        this._state.profilePage = profileReducer(this._state.profilePage, action)
+        this._state.messagesPage = messagesReducer(this._state.messagesPage, action)
+        this._state.navbar = navbarReducer(this._state.navbar, action)
 
-export const sendMessageCreator = () => ({ type: 'SEND-MESSAGE' });
-export const updateNewMessageBodyCreator = (body) => {
-    return {
-        type: 'UPDATE-NEW-MESSAGE-BODY', 
-        body: body
+        this._callSubscriber(this._state);
     }
-}
+};
 
 export default store;