From 1354d4cfe22ea558ac3aaf4bc49ae0d161246792 Mon Sep 17 00:00:00 2001 From: Oigen43 Date: Mon, 11 Oct 2021 11:48:20 +0300 Subject: [PATCH] add hook --- package-lock.json | 84 ++++++++--------------------------- src/hooks/useCurrentUser.js | 15 +++++++ src/pages/sign-in/sign-in.jsx | 14 +++--- 3 files changed, 40 insertions(+), 73 deletions(-) create mode 100644 src/hooks/useCurrentUser.js diff --git a/package-lock.json b/package-lock.json index efcef3b..10b531c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12,7 +12,7 @@ "axios": "0.21.1", "classnames": "2.3.1", "core-js": "3.16.4", - "history": "5.0.1", + "history": "4.10.1", "lodash": "4.17.21", "normalize.css": "8.0.1", "postcss": "8.3.6", @@ -20377,11 +20377,16 @@ } }, "node_modules/history": { - "version": "5.0.1", - "resolved": "https://registry.npmjs.org/history/-/history-5.0.1.tgz", - "integrity": "sha512-5qC/tFUKfVci5kzgRxZxN5Mf1CV8NmJx9ByaPX0YTLx5Vz3Svh7NYp6eA4CpDq4iA9D0C1t8BNIfvQIrUI3mVw==", + "version": "4.10.1", + "resolved": "https://registry.npmjs.org/history/-/history-4.10.1.tgz", + "integrity": "sha512-36nwAD620w12kuzPAsyINPWJqlNbij+hpK1k9XRloDtym8mxzGYl2c17LnV6IAGB2Dmg4tEa7G7DlawS0+qjew==", "dependencies": { - "@babel/runtime": "^7.7.6" + "@babel/runtime": "^7.1.2", + "loose-envify": "^1.2.0", + "resolve-pathname": "^3.0.0", + "tiny-invariant": "^1.0.2", + "tiny-warning": "^1.0.0", + "value-equal": "^1.0.1" } }, "node_modules/hmac-drbg": { @@ -29124,32 +29129,6 @@ "react": ">=15" } }, - "node_modules/react-router-dom/node_modules/history": { - "version": "4.10.1", - "resolved": "https://registry.npmjs.org/history/-/history-4.10.1.tgz", - "integrity": "sha512-36nwAD620w12kuzPAsyINPWJqlNbij+hpK1k9XRloDtym8mxzGYl2c17LnV6IAGB2Dmg4tEa7G7DlawS0+qjew==", - "dependencies": { - "@babel/runtime": "^7.1.2", - "loose-envify": "^1.2.0", - "resolve-pathname": "^3.0.0", - "tiny-invariant": "^1.0.2", - "tiny-warning": "^1.0.0", - "value-equal": "^1.0.1" - } - }, - "node_modules/react-router/node_modules/history": { - "version": "4.10.1", - "resolved": "https://registry.npmjs.org/history/-/history-4.10.1.tgz", - "integrity": "sha512-36nwAD620w12kuzPAsyINPWJqlNbij+hpK1k9XRloDtym8mxzGYl2c17LnV6IAGB2Dmg4tEa7G7DlawS0+qjew==", - "dependencies": { - "@babel/runtime": "^7.1.2", - "loose-envify": "^1.2.0", - "resolve-pathname": "^3.0.0", - "tiny-invariant": "^1.0.2", - "tiny-warning": "^1.0.0", - "value-equal": "^1.0.1" - } - }, "node_modules/react-select": { "version": "4.3.1", "resolved": "https://registry.npmjs.org/react-select/-/react-select-4.3.1.tgz", @@ -51648,11 +51627,16 @@ "dev": true }, "history": { - "version": "5.0.1", - "resolved": "https://registry.npmjs.org/history/-/history-5.0.1.tgz", - "integrity": "sha512-5qC/tFUKfVci5kzgRxZxN5Mf1CV8NmJx9ByaPX0YTLx5Vz3Svh7NYp6eA4CpDq4iA9D0C1t8BNIfvQIrUI3mVw==", + "version": "4.10.1", + "resolved": "https://registry.npmjs.org/history/-/history-4.10.1.tgz", + "integrity": "sha512-36nwAD620w12kuzPAsyINPWJqlNbij+hpK1k9XRloDtym8mxzGYl2c17LnV6IAGB2Dmg4tEa7G7DlawS0+qjew==", "requires": { - "@babel/runtime": "^7.7.6" + "@babel/runtime": "^7.1.2", + "loose-envify": "^1.2.0", + "resolve-pathname": "^3.0.0", + "tiny-invariant": "^1.0.2", + "tiny-warning": "^1.0.0", + "value-equal": "^1.0.1" } }, "hmac-drbg": { @@ -58251,21 +58235,6 @@ "react-is": "^16.6.0", "tiny-invariant": "^1.0.2", "tiny-warning": "^1.0.0" - }, - "dependencies": { - "history": { - "version": "4.10.1", - "resolved": "https://registry.npmjs.org/history/-/history-4.10.1.tgz", - "integrity": "sha512-36nwAD620w12kuzPAsyINPWJqlNbij+hpK1k9XRloDtym8mxzGYl2c17LnV6IAGB2Dmg4tEa7G7DlawS0+qjew==", - "requires": { - "@babel/runtime": "^7.1.2", - "loose-envify": "^1.2.0", - "resolve-pathname": "^3.0.0", - "tiny-invariant": "^1.0.2", - "tiny-warning": "^1.0.0", - "value-equal": "^1.0.1" - } - } } }, "react-router-dom": { @@ -58280,21 +58249,6 @@ "react-router": "5.2.1", "tiny-invariant": "^1.0.2", "tiny-warning": "^1.0.0" - }, - "dependencies": { - "history": { - "version": "4.10.1", - "resolved": "https://registry.npmjs.org/history/-/history-4.10.1.tgz", - "integrity": "sha512-36nwAD620w12kuzPAsyINPWJqlNbij+hpK1k9XRloDtym8mxzGYl2c17LnV6IAGB2Dmg4tEa7G7DlawS0+qjew==", - "requires": { - "@babel/runtime": "^7.1.2", - "loose-envify": "^1.2.0", - "resolve-pathname": "^3.0.0", - "tiny-invariant": "^1.0.2", - "tiny-warning": "^1.0.0", - "value-equal": "^1.0.1" - } - } } }, "react-select": { diff --git a/src/hooks/useCurrentUser.js b/src/hooks/useCurrentUser.js new file mode 100644 index 0000000..a8ab02b --- /dev/null +++ b/src/hooks/useCurrentUser.js @@ -0,0 +1,15 @@ +import { useSelector, useDispatch } from 'react-redux'; + +import { userActions } from 'resources/user/user.slice'; + +export default function useCurrentUser() { + const dispatch = useDispatch(); + const currentUser = useSelector(({ user }) => user); + + const setCurrentUser = (v) => dispatch(userActions.setUser(v)); + + return { + currentUser, + setCurrentUser, + }; +} diff --git a/src/pages/sign-in/sign-in.jsx b/src/pages/sign-in/sign-in.jsx index b19c5fe..132985f 100644 --- a/src/pages/sign-in/sign-in.jsx +++ b/src/pages/sign-in/sign-in.jsx @@ -1,11 +1,10 @@ import React from 'react'; -import { useDispatch, useSelector } from 'react-redux'; import { Link, Redirect } from 'react-router-dom'; import { routes } from 'routes'; -import * as userSelectors from 'resources/user/user.selectors'; -import { userActions } from 'resources/user/user.slice'; +import { signIn } from 'resources/user/user.api'; +import useCurrentUser from 'hooks/useCurrentUser'; import Input from 'components/input'; import Button from 'components/button'; @@ -14,15 +13,14 @@ import Form from 'components/form'; import styles from './sign-in.pcss'; function SignIn() { - const user = useSelector(userSelectors.selectUser); - - const dispatch = useDispatch(); + const { currentUser, setCurrentUser } = useCurrentUser(); const handleSubmit = async (submitValues) => { - await dispatch(userActions.signIn(submitValues)); + const user = await signIn(submitValues); + setCurrentUser(user); }; - if (user) { + if (currentUser) { const redirectPath = new URLSearchParams(window.location.search).get('to'); return ;