Browse Source

1.08.2020

Emmanuil 4 years ago
parent
commit
2e1fe052a0

+ 2 - 10
myproject/src/App.js

@@ -19,19 +19,11 @@ const App = (props) => {
             <Route exact path={["/", "/sign_in"]} component={Login} />
             <Route exact path="/sign_up" component={Register} />
             <Route exact path="/put_password" component={putPassword} />
-            <Route
-              exact
-              path="/my_profile"
-              render={() => (
-                <MyProfile 
-                // message={props.message} users={props.users}
-                 />
-              )}
-            />
+            <Route exact path="/my_profile" component={MyProfile} />
           </div>
         </Switch>
       </Router>
-     </Provider>
+    </Provider>
   );
 };
 

+ 24 - 46
myproject/src/myProfile/chatGroups/actionChatGroup/actionChatGroup.js

@@ -1,36 +1,6 @@
 import { actionPromise, store } from "../../../store/store";
 // import history from '../../history';
 
-// store.dispatch({
-//   type: "PROMISE",
-//   name: "login",
-//   status: "RESOLVED",
-//   payload: "hi",
-// });
-
-// store.dispatch({
-//   type: "PROMISE",
-//   name: "chatList",
-//   status: "REJECTED",
-//   error: "bye",
-// });
-
-// function actionToken(token) {
-//   return {
-//     type: "TOKEN",
-//     token,
-//   };
-// }
-
-// function actionError(error) {
-//   return {
-//     type: "ERROR",
-//     error,
-//   };
-// }
-
-// const handleClick = () => history.push("/my_profile")
-
 const getGQL = (url, headers = {}) => (query = "", variables = {}) =>
   fetch(url, {
     method: "POST",
@@ -42,21 +12,6 @@ const getGQL = (url, headers = {}) => (query = "", variables = {}) =>
     body: JSON.stringify({ query, variables }),
   }).then((res) => res.json());
 
-// const actionChatGroupPromise = () => {
-//   var promise = getGQL("http://localhost:3330/graphql")(
-//     `query allChatGroup{
-//       getAllChatGroups {
-//         id, inId {
-//           id, login
-//         }, outId {
-//           id, login
-//         }
-//       }
-//     }`
-//   );
-//   return actionPromise("chatGroup", promise);
-// };
-
 const actionChatGroupPromise = (id) => {
   var promise = getGQL("http://localhost:3330/graphql")(
     `query allMessOneUser($id: ID!){
@@ -123,4 +78,27 @@ const actionUsers = (email) => {
   };
 };
 
-export { actionChatGroup, actionMessage, actionUsers };
+const actionAllMessOneUserPromise = (id) => {
+  var promise = getGQL("http://localhost:3330/graphql")(
+    `query allMessOneUser($id: ID!){
+      getAllMessagesOneUser(id:$id){
+        id, message, autorId{
+          id, login
+        }, partnerId{
+          id, login
+        }
+      }
+    }`,
+    { id }
+  );
+  return actionPromise("allMessOneUser", promise);
+};
+
+const actionAllMessOneUser = (id) => {
+  return async (dispatch) => {
+    var token = await dispatch(actionAllMessOneUserPromise(id));
+    // console.log(token);
+  };
+};
+
+export { actionChatGroup, actionMessage, actionUsers, actionAllMessOneUser };

+ 2 - 0
myproject/src/myProfile/chatGroups/chatGroups.js

@@ -17,6 +17,8 @@ const ChatGroupMessage = ({message}) => {
 };
 
 const ChatGroups = ({ id, login, state }) => {
+  const a = localStorage.setItem("idChat", id);
+// console.log(localStorage.getItem("idChat"));
   const way = (obj, resolverName, a) => {
     const stateObj = state[obj];
     for (var keys in stateObj) {

+ 3 - 5
myproject/src/myProfile/chatMessages/chatMessages.js

@@ -6,16 +6,15 @@ import "./chatMessages.css";
 
 const Messages = ({ id, login, message, autor, partner }) => {
   localStorage.setItem("login", login);
+  
   return (
     <div className="messages">
       <div className="messageAutor">
-        {+localStorage.getItem("id") === autor.id ? message : console.log("hi")}
+        {+localStorage.getItem("id") === autor.id ? message : null}
       </div>
 
       <div className="messagePartner">
-        {+localStorage.getItem("id") === partner.id
-          ? message
-          : console.log("hi")}
+        {+localStorage.getItem("id") === partner.id ? message : null}
       </div>
     </div>
   );
@@ -28,7 +27,6 @@ class ChatMessageInfo extends Component {
       for (var keyData in stateObj[keys]) {
         if (keyData === "data") {
           const allObj = stateObj[keys][keyData][resolverName];
-          console.log(allObj);
           if (allObj) return allObj.map(a);
         }
       }

+ 35 - 42
myproject/src/myProfile/myProfile.js

@@ -9,21 +9,14 @@ import {
   actionChatGroup,
   actionMessage,
   actionUsers,
+  actionAllMessOneUser,
 } from "./chatGroups/actionChatGroup/actionChatGroup";
 import history from "../history";
 
-const Create = () => {
-  return (
-    <div className="create">
-      <input type="text" />
-    </div>
-  );
-};
-
 class MyProfile1 extends Component {
   constructor(props) {
     super(props);
-    this.state = { text: "" };
+    this.state = { text: ""};
   }
 
   way(obj, resolverName, a) {
@@ -55,6 +48,8 @@ class MyProfile1 extends Component {
     const a = localStorage.getItem("id");
     this.props.chatGroup(a);
     this.props.message();
+    // const ar = localStorage.getItem("a");
+    // this.props.allMessOneUser("2");
   }
 
   render() {
@@ -68,7 +63,7 @@ class MyProfile1 extends Component {
                   <TeamOutlined />
                   <span className="span">Список чатов</span>
                 </div>
-                <FormOutlined className="createChat" onClick={() => {}} />
+                <FormOutlined className="createChat" />
               </div>
 
               <div className="search-input">
@@ -86,42 +81,39 @@ class MyProfile1 extends Component {
 
             <div className="chat-groups">
               {this.way1("users", "getUser") ||
-                this.way("chatGroup", "getAllChatGroupsOneUser", (el) => (
-                  // <div onClick={() => history.push(`/my_profile/${el.id}`)}>
-                  <ChatGroupsBar
-                    key={el.id}
-                    id={el.id}
-                    login={
-                      +localStorage.getItem("id") !== el.autorId.id
-                        ? el.autorId.login
-                        : el.partnerId.login
-                    }
-                  />
-                  // </div>
-                ))}
+                this.way("chatGroup", "getAllChatGroupsOneUser", (el) => {
+                  return (
+                    <div key={el.id} onClick={() => console.log(this.state.idChat)}>
+                      <ChatGroupsBar
+                        id={el.id}
+                        login={
+                          +localStorage.getItem("id") !== el.autorId.id
+                            ? el.autorId.login
+                            : el.partnerId.login
+                        }
+                      />
+                    </div>
+                  );
+                })}
             </div>
           </div>
-          {/* <Create /> */}
+
           <div className="chat-messages">
             <ChatMessage />
-            {/* {this.way("message", "getAllMessages", (el) => (
-              <ChatMessages
-                key={el.id}
-                id={
-                  +localStorage.getItem("id") !== el.autorId.id
-                    ? el.autorId.id
-                    : el.partnerId.id
-                }
-                message={el.message}
-                autor={el.autorId}
-                partner={el.partnerId}
-                login={
-                  +localStorage.getItem("id") !== el.autorId.id
-                    ? el.autorId.login
-                    : el.partnerId.login
-                }
-              />
-            ))} */}
+            {/* {this.way("allMessOneUser", "getAllMessagesOneUser", (el) => {
+              return (
+                <div key={el.id} >
+                  <ChatMessage
+                    id={el.id}
+                    // login={
+                    //   +localStorage.getItem("id") !== el.autorId.id
+                    //     ? el.autorId.login
+                    //     : el.partnerId.login
+                    // }
+                  />
+                </div>
+              );
+            })} */}
           </div>
         </div>
       </div>
@@ -139,6 +131,7 @@ const ConnectedChatGroups = connect(
     chatGroup: actionChatGroup,
     message: actionMessage,
     users: actionUsers,
+    allMessOneUser: actionAllMessOneUser,
   }
 )(MyProfile1);
 

+ 44 - 0
server/index.js

@@ -96,6 +96,7 @@ var schema = buildSchema(`
     getAllUsers: [User]
     getMessage(id: ID!): Message
     getAllMessages: [Message]
+    getAllMessagesOneUser(id: ID!): [Message]
     getAllChatGroups: [ChatGroup]
     getAllChatGroupsOneUser(id: ID!): [ChatGroup]
   }
@@ -183,6 +184,48 @@ const getMessage = async ({ id }) => {
   return message;
 };
 
+const getAllMessagesOneUser = async ({ id }) => {
+  const allMess = await Message.findAll();
+  for (var key of allMess) {
+    const autor = await User.findAll({ where: { id: key.autorId } });
+    const partner = await User.findAll({ where: { id: key.partnerId } });
+    const ma = autor[0].id === +id || partner[0].id === +id;
+
+    if (ma === true) {
+      const y = await Message.findAll({ where: { autorId: id } });
+      const u = await Message.findAll({ where: { partnerId: id } });
+      console.log(y);
+
+      if ((y.length && u.length) !== 0) {
+        const p = [...y, ...u];
+        for (var key of p) {
+          const r = await User.findAll({ where: { id: key.autorId } });
+          const r1 = await User.findAll({ where: { id: key.partnerId } });
+          key.autorId = r[0];
+          key.partnerId = r1[0];
+        }
+        return p;
+      } else if (y.length !== 0) {
+        const pt = [...y];
+        for (var key of pt) {
+          const r = await User.findAll({ where: { id: key.partnerId } });
+          key.autorId = autor[0];
+          key.partnerId = r[0];
+        }
+        return pt;
+      } else {
+        const ptu = [...u];
+        for (var key of ptu) {
+          const r = await User.findAll({ where: { id: key.autorId } });
+          key.autorId = r[0];
+          key.partnerId = partner[0];
+        }
+        return ptu;
+      }
+    }
+  }
+};
+
 const createMessage = async (obj) => {
   try {
     return await Message.create({ ...obj });
@@ -301,6 +344,7 @@ var root = {
   changeMessage,
   deleteMessage,
   getAllMessages,
+  getAllMessagesOneUser,
   getAllChatGroups,
   getAllChatGroupsOneUser,
   createChatGroup,