Browse Source

myContacts

sheva77 3 years ago
parent
commit
b8c268571e

+ 47 - 26
chat_final_bootstrap/src/Actions/ActionsGql.js

@@ -19,7 +19,7 @@ const getGQL = (url) => (query, variables = {}) => {
 
 export const gql = getGQL(urlConst);
 
-const toQuery = (str, fields = ["title", "text"]) => {
+const toQuery = (str, fields = ["title", "text", "login", "nick", "_id"]) => {
     str = str.replace(/ +/g, " ").trim(); // "/ +/g" - оставляет только по одному пробелу в последовательностях пробелов
     str = "/" + str.split(" ").join("|") + "/";
 
@@ -32,6 +32,27 @@ const toQuery = (str, fields = ["title", "text"]) => {
 export const actionSearchMessagesByChatId = (_chatId, skip = 0, searchStr = "", limit = 10) => async (dispatch) => {
     let searchObj;
     searchStr = toQuery(searchStr);
+    // на самом деле searchStr - это теперь уже объект для поиска
+    // {
+    //     "$or": [
+    //         {
+    //             "title": "//"
+    //         },
+    //         {
+    //             "text": "//"
+    //         },
+    //         {
+    //             "login": "//"
+    //         },
+    //         {
+    //             "nick": "//"
+    //         },
+    //         {
+    //             "_id": "//"
+    //         }
+    //     ]
+    // }
+    // console.log(JSON.stringify(searchStr, null, 4));
 
     if (_chatId) {
         searchObj = { $and: [{ "chat._id": _chatId }] };
@@ -137,7 +158,7 @@ export const actionSearchChat = (_userId = "", str = "") => async (dispatch) =>
     console.log("actionSearchChat - searchData:", searchData);
 };
 
-export const actionUserFind = (skip = 0) => async (dispatch) => {
+export const actionAllUsersFind = (skip = 0) => async (dispatch) => {
     let users = await dispatch(
         actionPromise(
             "UserFind",
@@ -154,7 +175,6 @@ export const actionUserFind = (skip = 0) => async (dispatch) => {
             )
         )
     );
-    // console.log("actionUserCount - userCount:", userCount);
     if (!users.errors) {
         users = users.data;
         // console.log("actionUserFind - UserFind:", users);
@@ -167,26 +187,27 @@ export const actionUsersConcat = (userArr) => {
     return { type: "NEW_USER_PART", userArr };
 };
 
-export const actionGetAllUsers = () => async (dispatch) => {
-    let userCount = await dispatch(
-        actionPromise(
-            "userCount",
-            gql(
-                `query UsersCount{
-                    UserCount(query:"[{}]")
-                }`
-            )
-        )
-    );
-    // console.log("actionUserCount - userCount:", userCount.data.UserCount);
-    if (!userCount.errors) {
-        userCount = userCount.data.UserCount;
-        // console.log("actionUserCount - userCount:", userCount);
-
-        for (let i = 0; i < userCount; i += 100) {
-            // console.log("++++++++", i);
-            await actionUserFind(i)(dispatch);
-            dispatch(actionUsersConcat(store.getState().promise.UserFind.payload.data.UserFind));
-        }
-    }
-};
+// это НИЗЗЯ!! это - затянуть всех пользователей сети к себе на Front!!
+// export const actionGetAllUsers = () => async (dispatch) => {
+//     let userCount = await dispatch(
+//         actionPromise(
+//             "userCount",
+//             gql(
+//                 `query UsersCount{
+//                     UserCount(query:"[{}]")
+//                 }`
+//             )
+//         )
+//     );
+//     // console.log("actionUserCount - userCount:", userCount.data.UserCount);
+//     if (!userCount.errors) {
+//         userCount = userCount.data.UserCount;
+//         // console.log("actionUserCount - userCount:", userCount);
+
+//         for (let i = 0; i < userCount; i += 100) {
+//             // console.log("++++++++", i);
+//             await actionAllUsersFind(i)(dispatch);
+//             dispatch(actionUsersConcat(store.getState().promise.UserFind.payload.data.UserFind));
+//         }
+//     }
+// };

+ 3 - 4
chat_final_bootstrap/src/Actions/ActionsUser.js

@@ -1,4 +1,3 @@
-export const actionUser = (msgArr) => {
-    // console.log("actionMsgAdd - ", JSON.stringify(msgArr, null, 4));
-    return { type: "NEW_CHAT", msgs: { [msgArr[0].chat._id]: msgArr } };
-};
+//
+//
+//

+ 1 - 8
chat_final_bootstrap/src/Actions/index.js

@@ -1,11 +1,5 @@
 import { actionAuthLogin, actionAuthLogout, actionLogin, actionRegistration, actionUserInfo } from "./ActionLogin";
-import {
-    gql,
-    actionSearchMessagesByChatId,
-    actionGetMessagesByChatId,
-    actionSearchChat,
-    actionGetAllUsers,
-} from "./ActionsGql";
+import { gql, actionSearchMessagesByChatId, actionGetMessagesByChatId, actionSearchChat } from "./ActionsGql";
 import { actionMsgNewChat, actionCurChatId, actionMsgInsertInHead } from "./ActionsMsg";
 
 export {
@@ -21,5 +15,4 @@ export {
     actionMsgNewChat,
     actionCurChatId,
     actionMsgInsertInHead,
-    actionGetAllUsers,
 };

+ 1 - 1
chat_final_bootstrap/src/App.scss

@@ -59,7 +59,7 @@ body {
 }
 
 .allUsersList {
-    max-height: calc(100vh - 180px);
+    max-height: calc(100vh - 220px);
     overflow: auto;
 }
 

+ 20 - 7
chat_final_bootstrap/src/Pages/PageNewChat.js

@@ -102,16 +102,29 @@ const UserItem = ({ _id, login, nick, avatar, myId }) => {
     );
 };
 
-const AllUsers = ({ allUsersArray = [], getAllUsers = null, myId }) => {
+const AllUsers = ({ searchUserResultArr = [], findUsers = null, myId, myChats = [] }) => {
     const [users, setUsers] = useState([]);
+    // console.log(myChats);
 
     useEffect(() => {
-        if (typeof getAllUsers === "function") getAllUsers();
+        if (typeof getAllUsers === "function") findUsers();
     }, []);
 
     useEffect(() => {
+        if (!searchUserResultArr.length) {
+            let tempObj = {};
+            for (let chat of myChats) {
+                for (let member of chat.members) {
+                    // console.log(members);
+                    let { _id, ...restData } = member;
+                    tempObj = { ...tempObj, [_id]: member };
+                }
+            }
+            // console.log("+++++++++++++++++++", tempObj);
+            searchUserResultArr = Object.values(tempObj);
+        }
         setUsers(
-            allUsersArray
+            searchUserResultArr
                 .filter((el) => {
                     if (!el.login) return false;
                     if (el.nick && !el.nick.replace(/ +/g, " ").trim()) {
@@ -123,8 +136,8 @@ const AllUsers = ({ allUsersArray = [], getAllUsers = null, myId }) => {
                 .sort((a, b) => ((a.nick || a.login) > (b.nick || b.login) ? 1 : -1))
         );
         // выкидываем пользователей, у которых (ник из одних пробелов) или (нет ника но логин из одних пробелов)
-        // console.log("+++++++++++++++++", users, allUsersArray);
-    }, [allUsersArray]);
+        // console.log("+++++++++++++++++", users, searchUserResultArr);
+    }, [searchUserResultArr]);
 
     return (
         <div className="bg-light">
@@ -136,8 +149,8 @@ const AllUsers = ({ allUsersArray = [], getAllUsers = null, myId }) => {
     );
 };
 
-const CAllUsers = connect((s) => ({ allUsersArray: s.allUsers, myId: s.auth.payloadId }), {
-    getAllUsers: actionGetAllUsers,
+const CAllUsers = connect((s) => ({ searchUserResultArr: [], myId: s.auth.payloadId, myChats: s.auth.chats }), {
+    findUsers: null,
 })(AllUsers);
 
 export const PageNewChat = () => {

+ 0 - 10
chat_final_bootstrap/src/Reducers/index.js

@@ -80,15 +80,6 @@ function currentChatIdReduser(state = {}, action) {
     return state;
 }
 
-function allUsersReduser(state = [], action) {
-    if (["LOGOUT", "LOGIN", "CLEAR_USERS"].includes(action.type)) return [];
-    if (action.type === "NEW_USER_PART") {
-        return state.concat(action.userArr);
-    }
-
-    return state;
-}
-
 function promiseReducer(state = {}, action) {
     if (["LOGOUT", "LOGIN"].includes(action.type)) return {};
     if (action.type === "PROMISE") {
@@ -131,7 +122,6 @@ export const store = createStore(
         promise: promiseReducer,
         msg: msgReduser,
         curChatId: currentChatIdReduser,
-        allUsers: allUsersReduser,
     }),
     applyMiddleware(thunk)
 );