Yevheniia Akinshyna 2 years ago
parent
commit
6ac13f7c51

+ 1 - 1
src/components/Ava.js

@@ -14,5 +14,5 @@ function AvaLogo({ link }) {
 }
 
 export const ConnectedAvaLogo = connect(state => ({
-	link: state?.p?.findUser?.payload?.data?.UserFindOne?.avatar?.url,
+	link: state?.promise?.findUser?.payload?.data?.UserFindOne?.avatar?.url,
 }))(AvaLogo);

+ 1 - 1
src/components/Routers.js

@@ -47,6 +47,6 @@ const Routs = ({ token }) => {
 	);
 };
 
-const ConnectedRouts = connect(state => ({ token: state.a.token }), null)(Routs);
+const ConnectedRouts = connect(state => ({ token: state.auth.token }), null)(Routs);
 
 export default ConnectedRouts;

+ 1 - 1
src/components/User.js

@@ -11,6 +11,6 @@ const Nick = ({ nick }) => {
 };
 
 const ConnectNickName = connect(state => ({
-	nick: state?.a?.payload?.sub?.login,
+	nick: state?.auth?.payload?.sub?.login,
 }))(Nick);
 export default ConnectNickName;

+ 1 - 1
src/components/UserHeader.js

@@ -20,7 +20,7 @@ const NickName = ({ nick, onLogOut }) => {
 	);
 };
 
-const ConnectedNick = connect(state => ({ nick: state?.a?.payload?.sub?.login, logedIn: state.a.token }), {
+const ConnectedNick = connect(state => ({ nick: state?.auth?.payload?.sub?.login, logedIn: state.auth.token }), {
 	onLogOut: actionAuthLogout,
 })(NickName);
 export default ConnectedNick;

+ 4 - 4
src/pages/Project.js

@@ -137,10 +137,10 @@ const ProjectSnippet = ({
 
 const ConnectedProject = connect(
 	state => ({
-		titleText: state?.p?.findSnippetById?.payload?.data?.SnippetFind?.[0]?.title,
-		descriptionText: state?.p?.findSnippetById?.payload?.data?.SnippetFind?.[0]?.description,
-		nameText: state?.p?.findSnippetById?.payload?.data?.SnippetFind?.[0]?.name,
-		filesArr: state?.p?.findSnippetById?.payload?.data?.SnippetFind?.[0]?.files,
+		titleText: state?.promise?.findSnippetById?.payload?.data?.SnippetFind?.[0]?.title,
+		descriptionText: state?.promise?.findSnippetById?.payload?.data?.SnippetFind?.[0]?.description,
+		nameText: state?.promise?.findSnippetById?.payload?.data?.SnippetFind?.[0]?.name,
+		filesArr: state?.promise?.findSnippetById?.payload?.data?.SnippetFind?.[0]?.files,
 	}),
 	{ getSnippet: actionSnippetById },
 )(ProjectSnippet);

+ 1 - 1
src/pages/Projects.js

@@ -50,6 +50,6 @@ const Projects = ({ snippets }) => {
 };
 
 const CProjects = connect(state => ({
-	snippets: state?.p?.findSnippet?.payload?.data?.SnippetFind,
+	snippets: state?.promise?.findSnippet?.payload?.data?.SnippetFind,
 }))(Projects);
 export default CProjects;

+ 1 - 1
src/pages/Search.js

@@ -56,7 +56,7 @@ const Search = ({ onSearch, snippets }) => {
 
 const ConnectFormSearch = connect(
 	state => ({
-		snippets: state?.p?.searchSnippet?.payload?.data?.SnippetFind,
+		snippets: state?.promise?.searchSnippet?.payload?.data?.SnippetFind,
 	}),
 	{ onSearch: actionSearch },
 )(Search);

+ 2 - 2
src/store/actions/actionFindUser.js

@@ -1,6 +1,6 @@
 import { actionPromise } from "./actionPromise";
-import { userFind } from "./requests";
+import { findUser } from "./requests";
 
 export const actionFindUser = () => async (dispatch, getState) => {
-	return await dispatch(actionPromise("findUser", userFind(getState()?.a?.payload?.sub?.id)));
+	return await dispatch(actionPromise("findUser", findUser(getState()?.auth?.payload?.sub?.id)));
 };

+ 2 - 2
src/store/actions/actionImgFind.js

@@ -1,6 +1,6 @@
 import { actionPromise } from "./actionPromise";
-import { imgFind } from "./requests";
+import { findImage } from "./requests";
 
 export const actionImgFind = () => async dispatch => {
-	return await dispatch(actionPromise("img", imgFind()));
+	return await dispatch(actionPromise("img", findImage()));
 };

+ 2 - 2
src/store/actions/actionLogin.js

@@ -1,4 +1,4 @@
 import { actionPromise } from "./actionPromise";
-import { log } from "./requests";
+import { login } from "./requests";
 
-export const actionLogin = (login, password) => actionPromise("login", log(login, password));
+export const actionLogin = (_login, password) => actionPromise("login", login(_login, password));

+ 2 - 2
src/store/actions/actionReg.js

@@ -1,4 +1,4 @@
 import { actionPromise } from "./actionPromise";
-import { reg } from "./requests";
+import { registration } from "./requests";
 
-export const actionReg = (login, password) => actionPromise("reg", reg(login, password));
+export const actionReg = (login, password) => actionPromise("reg", registration(login, password));

+ 2 - 2
src/store/actions/actionSetAva.js

@@ -1,6 +1,6 @@
 import { actionPromise } from "./actionPromise";
-import { setAva } from "./requests";
+import { setAvatar } from "./requests";
 
 export const actionSetAva = id => async (dispatch, getState) => {
-	return await dispatch(actionPromise("setAva", setAva(getState()?.a?.payload?.sub?.id, id)));
+	return await dispatch(actionPromise("setAva", setAvatar(getState()?.auth?.payload?.sub?.id, id)));
 };

+ 2 - 2
src/store/actions/actionSnippetAdd.js

@@ -1,6 +1,6 @@
 import { actionPromise } from "./actionPromise";
-import { snippetAdd } from "./requests";
+import { addSnippet } from "./requests";
 
 export const actionSnippetAdd = (title, description, files, idSnippet) => async dispatch => {
-	return await dispatch(actionPromise("addSnippet", snippetAdd(title, description, files, idSnippet)));
+	return await dispatch(actionPromise("addSnippet", addSnippet(title, description, files, idSnippet)));
 };

+ 2 - 2
src/store/actions/actionSnippetById.js

@@ -1,6 +1,6 @@
 import { actionPromise } from "./actionPromise";
-import { snippetById } from "./requests";
+import { getSnippetById } from "./requests";
 
 export const actionSnippetById = id => async dispatch => {
-	return await dispatch(actionPromise("findSnippetById", snippetById(id)));
+	return await dispatch(actionPromise("findSnippetById", getSnippetById(id)));
 };

+ 2 - 2
src/store/actions/actionSnippetFindByOwner.js

@@ -1,6 +1,6 @@
 import { actionPromise } from "./actionPromise";
-import { snippetByOwner } from "./requests";
+import { getSnippetByOwnerId } from "./requests";
 
 export const actionSnippetFindByOwner = id => async dispatch => {
-	return await dispatch(actionPromise("findSnippet", snippetByOwner(id)));
+	return await dispatch(actionPromise("findSnippet", getSnippetByOwnerId(id)));
 };

+ 102 - 116
src/store/actions/requests.js

@@ -1,150 +1,136 @@
 import { gql } from "./gql";
 
-export const log = async (login, password) => {
-	let query = `query login($login:String!, $password:String!) {
-        login(
-            login: $login,
-            password: $password
-         )}`;
-
-	let variables = { login, password };
-
-	let token = await gql(query, variables);
+export const login = async (_login, password) => {
+	const query = `query login($login:String!, $password:String!) {
+      login(
+         login: $login,
+         password: $password
+      )
+   }`;
+
+	const token = await gql(query, { login: _login, password });
 	localStorage.authToken = token.data.login;
+
 	console.log(token);
 	return token.data.login;
 };
 
-export const reg = async (login, password) => {
-	let query = `mutation reg($login:String!, $password:String!) {
-    createUser(
-        login: $login,
-        password: $password
+export const registration = async (login, password) => {
+	const query = `mutation reg($login:String!, $password:String!) {
+      createUser(
+         login: $login,
+         password: $password
+      ){
+         _id
+      }
+   }`;
 
-    ){
-       _id
-    }}`;
+	return await gql(query, { login, password });
+};
 
-	let variables = { login: login, password: password };
+export const findImage = async () => {
+	const query = `query imgFind {
+      ImageFind(query:"[{}]") {
+         url owner{
+            nick
+         }
+      }
+   }`;
 
-	let res = await gql(query, variables);
-	return res;
+	return await gql(query);
 };
 
-export const imgFind = async () => {
-	return await gql(`query imgFind{
-      ImageFind(query:"[{}]"){
-        url owner{
-          nick
-        }
+export const findUser = async _id => {
+	const query = `query userOne($query:String) {
+      UserFindOne(query:$query) {
+         _id avatar{
+            url
+         }
       }
-    }`);
-};
+   }`;
 
-export const userFind = _id => {
-	return gql(
-		`query userOne($query:String) {
-            UserFindOne(query:$query){
-                _id avatar{
-                url
-                }
-            }
-        }`,
-		{ query: JSON.stringify([{ _id }]) },
-	);
+	return await gql(query, { query: JSON.stringify([{ _id }]) });
 };
 
-export const setAva = async (idUser, id) => {
-	let query = `mutation setAvatar($idUser:String , $idAvatar:ID){ 
-      UserUpsert(user:{_id: $idUser, avatar: {_id: $idAvatar}}){
-          _id, avatar{
-              url
-          }
+export const setAvatar = async (idUser, idAvatar) => {
+	const query = `mutation setAvatar($idUser:String , $idAvatar:ID) { 
+      UserUpsert(user:{_id: $idUser, avatar: {_id: $idAvatar}}) {
+         _id, avatar{
+            url
+         }
       }
-  }`;
-
-	let variables = { idUser: idUser, idAvatar: id };
+   }`;
 
-	let res = await gql(query, variables);
-	return res;
+	return await gql(query, { idUser, idAvatar });
 };
 
-export const snippetAdd = async (title, description, files, idSnippet) => {
-	let query = `mutation newSnippet($snippet:SnippetInput) {
+export const addSnippet = async (title, description, files, idSnippet) => {
+	const query = `mutation newSnippet($snippet:SnippetInput) {
       SnippetUpsert(snippet:$snippet){
         _id
       }
-    }`;
+   }`;
 
-	let qVariables = { snippet: { title, description, files, _id: idSnippet } };
-
-	let res = await gql(query, qVariables);
-	return res;
+	return await gql(query, {
+		snippet: { title, description, files, _id: idSnippet },
+	});
 };
 
-export const snippetById = async id => {
-	let query = `query snippetFind($query:String){
-      SnippetFind(query:$query){
-                        owner{
-                          _id 
-                        }
-            title description _id files {
-             type text name
-           }
-        }
-  }`;
-
-	let variables = {
-		query: JSON.stringify([{ _id: id }]),
-	};
-	let res = gql(query, variables);
-	return res;
+export const getSnippetById = async id => {
+	const query = `query snippetFind($query:String) {
+      SnippetFind(query:$query) {
+         owner{
+            _id 
+         }
+         title description _id files {
+            type text name
+         }
+      }
+   }`;
+
+	return await gql(query, { query: JSON.stringify([{ _id: id }]) });
 };
 
-export const snippetByOwner = async id => {
-	let query = `query snippetFind($query:String){
-        SnippetFind(query:$query){
-                          owner{
-                            _id 
-                          }
-              title description _id files {
-               type text name
-            }
-        }
-  }`;
-
-	let variables = {
-		query: JSON.stringify([{ ___owner: id }, { sort: [{ _id: -1 }] }]),
-	};
+export const getSnippetByOwnerId = async id => {
+	const query = `query snippetFind($query:String) {
+      SnippetFind(query:$query) {
+         owner{
+            _id
+         }
+         title description _id files {
+            type text name
+         }
+      }
+   }`;
 
-	let res = gql(query, variables);
-	return res;
+	return await gql(query, {
+		query: JSON.stringify([{ ___owner: id }, { sort: [{ _id: -1 }] }]),
+	});
 };
 
 export const search = async string => {
-	return gql(
-		`query snippetFind($query:String){
-        SnippetFind(query:$query){
-          owner {
+	const query = `query snippetFind($query:String) {
+      SnippetFind(query:$query) {
+         owner {
             _id login
-          }
-              title description _id files {
-               type text name
-             }
-          }
-    }`,
-		{
-			query: JSON.stringify([
-				{
-					$or: [
-						{ title: `/${string.trim().split(" ").join("|")}/` },
-						{ description: `/${string.trim().split(" ").join("|")}/` },
-					],
-				},
-				{
-					sort: [{ title: 1 }],
-				},
-			]),
-		},
-	);
+         }
+         title description _id files {
+            type text name
+         }
+      }
+   }`;
+
+	return await gql(query, {
+		query: JSON.stringify([
+			{
+				$or: [
+					{ title: `/${string.trim().split(" ").join("|")}/` },
+					{ description: `/${string.trim().split(" ").join("|")}/` },
+				],
+			},
+			{
+				sort: [{ title: 1 }],
+			},
+		]),
+	});
 };

+ 4 - 3
src/store/reducers/index.js

@@ -8,15 +8,16 @@ import { actionSnippetFindByOwner } from "../actions/actionSnippetFindByOwner";
 import { actionFindUser } from "../actions/actionFindUser";
 
 let reducers = combineReducers({
-	p: promiseReducer,
-	a: authReducer,
+	promise: promiseReducer,
+	auth: authReducer,
 });
 
 const store = createStore(reducers, applyMiddleware(thunk));
 store.subscribe(() => console.log(store.getState()));
+
 if (localStorage.authToken) {
 	store.dispatch(actionFindUser());
-	store.dispatch(actionSnippetFindByOwner(store.getState().a.payload.sub.id));
+	store.dispatch(actionSnippetFindByOwner(store.getState().auth.payload.sub.id));
 }
 
 export default store;