Browse Source

DialogReducer state copy added

pocu46 4 years ago
parent
commit
93d0416bf3

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

@@ -7,8 +7,8 @@ const Messages = (props) => {
 
     let state = props.messagesPage;
 
-    let dialog = state.dialogsData.map(d => <MessageItem name={d.name} id={d.id} />);
-    let messages = state.messagesData.map(m => <Message message={m.message} />);
+    let dialog = state.dialogsData.map(d => <MessageItem name={d.name} key={d.id} id={d.id} />);
+    let messages = state.messagesData.map(m => <Message message={m.message} key={m.id} />);
     let newMessageBody = state.newMessageBody;
 
     let onSendMessageClick = () => {

+ 3 - 3
hipstagram/src/Content/Messages/messagesContainer.js

@@ -11,11 +11,11 @@ let mapStateToProps = (state) => {
 
 let mapDispatchToProps = (dispatch) => {
     return {
-        updateNewMessageBody: () => {
-            dispatch(sendMessageCreator());
+        updateNewMessageBody: (body) => {
+            dispatch(updateNewMessageBodyCreator(body));
         },
         sendMessage: (body) => {
-            dispatch(updateNewMessageBodyCreator(body));
+            dispatch(sendMessageCreator());
         },
     }
 }

+ 9 - 5
hipstagram/src/Redux/messages-reducer.js

@@ -20,13 +20,17 @@ let initialState = {
 const messagesReducer = (state = initialState, action) => {
     switch (action.type) {
         case UPDATE_NEW_MESSAGE_BODY:
-            state.newMessageBody = action.body;
-            return state;
+            return {
+                ...state,
+                newMessageBody: action.body
+            };
         case SEND_MESSAGE:
             let body = state.newMessageBody;
-            state.newMessageBody = 0;
-            state.messagesData.push({ id: 4, message: body });
-            return state;
+            return {
+                ...state, 
+                newMessageBody: '',
+                messagesData: [...state.messagesData, { id: 4, message: body }]
+            };
         default:
             return state;
     }

+ 9 - 8
hipstagram/src/Redux/profile-reducer.js

@@ -19,16 +19,17 @@ const profileReducer = (state = initialState, action) => {
                 message: state.newPostText,
                 likesCount: 0,
             };
-            let stateCopy = {...state};
-            stateCopy.post = [...state.post];
-            stateCopy.post.push(newPost);
-            stateCopy.newPostText = '';
-            return stateCopy;
+            return {
+                ...state,
+                post: [...state.post, newPost],
+                newPostText: ''
+            };
         }
         case UPDATE_NEW_POST_TEXT: {
-            let stateCopy = {...state};
-            stateCopy.newPostText = action.newText;
-            return stateCopy; 
+            return {
+                ...state,
+                newPostText: action.newText
+            };
         }
         default:
             return state;