From 04a02439f415f6bb5f691770eb00a386922d1b9c Mon Sep 17 00:00:00 2001 From: Borja Gonzalez Date: Fri, 9 Apr 2021 19:18:53 +0200 Subject: [PATCH] Components renamed and little refactor --- src/components/AuthButtons.jsx | 38 ++++++++++++ .../{Footer.jsx => CalcButtons.jsx} | 28 +++------ src/components/Header.jsx | 58 ------------------ src/components/Main.jsx | 40 ------------- src/components/Router.jsx | 6 +- src/components/pages/Calc.jsx | 60 +++++++++++++++++++ src/components/{ => pages}/Stats.jsx | 6 +- src/constants/routes.js | 4 +- src/styles/components/Footer.module.css | 4 -- src/styles/components/Header.module.css | 6 -- src/styles/components/Loading.module.css | 11 +++- src/styles/components/Main.module.css | 11 ++++ src/styles/variables.css | 10 ---- 13 files changed, 136 insertions(+), 146 deletions(-) create mode 100644 src/components/AuthButtons.jsx rename src/components/{Footer.jsx => CalcButtons.jsx} (71%) delete mode 100644 src/components/Header.jsx delete mode 100644 src/components/Main.jsx create mode 100644 src/components/pages/Calc.jsx rename src/components/{ => pages}/Stats.jsx (87%) delete mode 100644 src/styles/components/Footer.module.css delete mode 100644 src/styles/components/Header.module.css diff --git a/src/components/AuthButtons.jsx b/src/components/AuthButtons.jsx new file mode 100644 index 0000000..841cd45 --- /dev/null +++ b/src/components/AuthButtons.jsx @@ -0,0 +1,38 @@ +import { PropTypes } from 'prop-types' + +import Button from 'components/Button' +import { LogInIcon, LogOutIcon } from 'components/icons' +import Loading from 'components/Loading' +import { useAuth } from 'contexts/AuthContext' +import { useModal } from 'contexts/ModalContext' + +const AuthButtons = ({ children }) => { + const { isAuthed, isLoading, signIn, signOut } = useAuth() + const { renderModal } = useModal() + + const handleSignOut = () => renderModal( + 'Do you want to sign out?', + signOut + ) + + if (isLoading) { + return + } + + if (!isAuthed) { + return + } + + return ( + <> + {children} + + + ) +} + +AuthButtons.propTypes = { + children: PropTypes.node, +} + +export default AuthButtons diff --git a/src/components/Footer.jsx b/src/components/CalcButtons.jsx similarity index 71% rename from src/components/Footer.jsx rename to src/components/CalcButtons.jsx index 11e7146..e74d33a 100644 --- a/src/components/Footer.jsx +++ b/src/components/CalcButtons.jsx @@ -6,9 +6,7 @@ import Button from 'components/Button' import Loading from 'components/Loading' import { CloseIcon, RestartIcon, TickIcon, UndoIcon } from 'components/icons' -import styles from 'styles/components/Footer.module.css' - -const Footer = ({ +const CalcButtons = ({ isCompleted, isLoading, isSolved, @@ -26,39 +24,31 @@ const Footer = ({ } if (isLoading) { - return ( -
- -
- ) + return } if (!isSolved) { return ( -
+ <> -
+ ) } if (!isCompleted) { return ( -
+ <> -
+ ) } - return ( -
- -
- ) + return } -Footer.propTypes = { +CalcButtons.propTypes = { isCompleted: PropTypes.bool.isRequired, isLoading: PropTypes.bool.isRequired, isSolved: PropTypes.bool.isRequired, @@ -67,4 +57,4 @@ Footer.propTypes = { handleSuccess: PropTypes.func.isRequired, } -export default Footer +export default CalcButtons diff --git a/src/components/Header.jsx b/src/components/Header.jsx deleted file mode 100644 index 58f7a9d..0000000 --- a/src/components/Header.jsx +++ /dev/null @@ -1,58 +0,0 @@ -import { useLocation } from 'wouter' -import { PropTypes } from 'prop-types' - -import Button from 'components/Button' -import { ChartIcon, LogInIcon, LogOutIcon } from 'components/icons' -import Loading from 'components/Loading' -import { ROUTE_STATS } from 'constants/routes' -import { useAuth } from 'contexts/AuthContext' -import { useModal } from 'contexts/ModalContext' - -import styles from 'styles/components/Header.module.css' - -const Header = ({ isCompleted }) => { - const [, setLocation] = useLocation() - const { isAuthed, isLoading, signIn, signOut } = useAuth() - const { renderModal } = useModal() - - const handleSignOut = () => renderModal( - 'Do you want to sign out?', - signOut - ) - - const handleStats = () => isCompleted - ? setLocation(ROUTE_STATS) - : renderModal( - 'Numbers will be discarded. Are you sure?', - () => setLocation(ROUTE_STATS) - ) - - if (isLoading) { - return ( -
- -
- ) - } - - if (isAuthed) { - return ( -
- - -
- ) - } - - return ( -
- -
- ) -} - -Header.propTypes = { - isCompleted: PropTypes.bool.isRequired, -} - -export default Header diff --git a/src/components/Main.jsx b/src/components/Main.jsx deleted file mode 100644 index 75a38e9..0000000 --- a/src/components/Main.jsx +++ /dev/null @@ -1,40 +0,0 @@ -import { DrawArea } from 'react-drawarea' - -import Footer from 'components/Footer' -import Header from 'components/Header' -import Numbers from 'components/Numbers' -import Result from 'components/Result' -import { useAttempts } from 'contexts/AttemptsContext' -import useCalc from 'hooks/useCalc' - -import styles from 'styles/components/Main.module.css' - -const Main = () => { - const { isStoring } = useAttempts() - const { isCompleted, isSolved, numbers, result, complete, solve, restart } = useCalc() - - return ( - <> -
- - {isSolved && } - -