From 9937caa5694428c72ace4212e56878e0750182f1 Mon Sep 17 00:00:00 2001 From: Mike Cao Date: Tue, 22 Feb 2022 22:47:59 -0800 Subject: [PATCH] Replaced redux with zustand. Fixed login issue, closes #980. --- components/common/RefreshButton.js | 12 ++-- components/forms/LoginForm.js | 9 ++- components/layout/Header.js | 4 +- components/pages/Settings.js | 12 ++-- components/pages/TestConsole.js | 4 +- components/settings/DateRangeSetting.js | 10 ++- components/settings/ProfileSettings.js | 13 ++-- components/settings/UserButton.js | 4 +- components/settings/WebsiteSettings.js | 4 +- hooks/useDateRange.js | 17 +++-- hooks/useFetch.js | 6 +- hooks/useLocale.js | 10 +-- hooks/useRequireLogin.js | 12 ++-- hooks/useShareToken.js | 10 +-- hooks/useTheme.js | 12 ++-- hooks/useUser.js | 9 +++ hooks/useVersion.js | 8 +-- lib/web.js | 10 ++- package.json | 5 +- pages/_app.js | 11 +--- pages/api/auth/login.js | 5 +- pages/logout.js | 14 ++-- redux/actions/app.js | 87 ------------------------- redux/actions/queries.js | 17 ----- redux/actions/user.js | 16 ----- redux/actions/websites.js | 29 --------- redux/reducers.js | 7 -- redux/store.js | 40 ------------ store/app.js | 30 +++++++++ store/queries.js | 9 +++ store/version.js | 54 +++++++++++++++ store/websites.js | 20 ++++++ yarn.lock | 10 +++ 33 files changed, 234 insertions(+), 286 deletions(-) create mode 100644 hooks/useUser.js delete mode 100644 redux/actions/app.js delete mode 100644 redux/actions/queries.js delete mode 100644 redux/actions/user.js delete mode 100644 redux/actions/websites.js delete mode 100644 redux/reducers.js delete mode 100644 redux/store.js create mode 100644 store/app.js create mode 100644 store/queries.js create mode 100644 store/version.js create mode 100644 store/websites.js diff --git a/components/common/RefreshButton.js b/components/common/RefreshButton.js index 1e3dd9e7..7f000a20 100644 --- a/components/common/RefreshButton.js +++ b/components/common/RefreshButton.js @@ -1,23 +1,23 @@ -import React, { useState, useEffect } from 'react'; +import React, { useState, useEffect, useCallback } from 'react'; import PropTypes from 'prop-types'; -import { useDispatch, useSelector } from 'react-redux'; import { FormattedMessage } from 'react-intl'; -import { setDateRange } from 'redux/actions/websites'; +import useStore from 'store/queries'; +import { setDateRange } from 'store/websites'; import Button from './Button'; import Refresh from 'assets/redo.svg'; import Dots from 'assets/ellipsis-h.svg'; import useDateRange from 'hooks/useDateRange'; function RefreshButton({ websiteId }) { - const dispatch = useDispatch(); const [dateRange] = useDateRange(websiteId); const [loading, setLoading] = useState(false); - const completed = useSelector(state => state.queries[`/api/website/${websiteId}/stats`]); + const selector = useCallback(state => state[`/api/website/${websiteId}/stats`], [websiteId]); + const completed = useStore(selector); function handleClick() { if (dateRange) { setLoading(true); - dispatch(setDateRange(websiteId, dateRange)); + setDateRange(websiteId, dateRange); } } diff --git a/components/forms/LoginForm.js b/components/forms/LoginForm.js index 68c2775c..276a7a4e 100644 --- a/components/forms/LoginForm.js +++ b/components/forms/LoginForm.js @@ -10,11 +10,12 @@ import FormLayout, { FormRow, } from 'components/layout/FormLayout'; import Icon from 'components/common/Icon'; -import Logo from 'assets/logo.svg'; -import styles from './LoginForm.module.css'; import usePost from 'hooks/usePost'; import { setItem } from 'lib/web'; -import { AUTH_TOKEN } from '../../lib/constants'; +import { AUTH_TOKEN } from 'lib/constants'; +import { setUser } from 'store/app'; +import Logo from 'assets/logo.svg'; +import styles from './LoginForm.module.css'; const validate = ({ username, password }) => { const errors = {}; @@ -43,6 +44,8 @@ export default function LoginForm() { if (ok) { setItem(AUTH_TOKEN, data.token); + setUser(data.user); + return router.push('/'); } else { setMessage( diff --git a/components/layout/Header.js b/components/layout/Header.js index 65abc47a..0baaaed7 100644 --- a/components/layout/Header.js +++ b/components/layout/Header.js @@ -1,6 +1,5 @@ import React, { useState } from 'react'; import { FormattedMessage } from 'react-intl'; -import { useSelector } from 'react-redux'; import classNames from 'classnames'; import Link from 'components/common/Link'; import Icon from 'components/common/Icon'; @@ -14,9 +13,10 @@ import styles from './Header.module.css'; import useLocale from 'hooks/useLocale'; import XMark from 'assets/xmark.svg'; import Bars from 'assets/bars.svg'; +import useUser from 'hooks/useUser'; export default function Header() { - const user = useSelector(state => state.user); + const { user } = useUser(); const [active, setActive] = useState(false); const { dir } = useLocale(); diff --git a/components/pages/Settings.js b/components/pages/Settings.js index 35d039df..d38be07d 100644 --- a/components/pages/Settings.js +++ b/components/pages/Settings.js @@ -1,19 +1,19 @@ import React, { useState } from 'react'; +import { FormattedMessage } from 'react-intl'; import { useRouter } from 'next/router'; import Page from 'components/layout/Page'; import MenuLayout from 'components/layout/MenuLayout'; -import WebsiteSettings from '../settings/WebsiteSettings'; -import AccountSettings from '../settings/AccountSettings'; -import ProfileSettings from '../settings/ProfileSettings'; -import { useSelector } from 'react-redux'; -import { FormattedMessage } from 'react-intl'; +import WebsiteSettings from 'components/settings/WebsiteSettings'; +import AccountSettings from 'components/settings/AccountSettings'; +import ProfileSettings from 'components/settings/ProfileSettings'; +import useUser from 'hooks/useUser'; const WEBSITES = '/settings'; const ACCOUNTS = '/settings/accounts'; const PROFILE = '/settings/profile'; export default function Settings() { - const user = useSelector(state => state.user); + const { user } = useUser(); const [option, setOption] = useState(WEBSITES); const router = useRouter(); const { pathname } = router; diff --git a/components/pages/TestConsole.js b/components/pages/TestConsole.js index 8c0d29b9..fff60516 100644 --- a/components/pages/TestConsole.js +++ b/components/pages/TestConsole.js @@ -1,5 +1,4 @@ import React, { useState } from 'react'; -import { useSelector } from 'react-redux'; import classNames from 'classnames'; import Head from 'next/head'; import Link from 'next/link'; @@ -13,11 +12,12 @@ import Button from 'components/common/Button'; import EmptyPlaceholder from 'components/common/EmptyPlaceholder'; import Icon from 'components/common/Icon'; import useFetch from 'hooks/useFetch'; +import useUser from 'hooks/useUser'; import ChevronDown from 'assets/chevron-down.svg'; import styles from './TestConsole.module.css'; export default function TestConsole() { - const user = useSelector(state => state.user); + const { user } = useUser(); const [website, setWebsite] = useState(); const [show, setShow] = useState(true); const { basePath } = useRouter(); diff --git a/components/settings/DateRangeSetting.js b/components/settings/DateRangeSetting.js index 53dfe983..092e2427 100644 --- a/components/settings/DateRangeSetting.js +++ b/components/settings/DateRangeSetting.js @@ -14,6 +14,14 @@ export default function DateRangeSetting() { const { startDate, endDate, value } = dateRange; const options = filterOptions.filter(e => e.value !== 'all'); + function handleChange(value) { + if (typeof value === 'string') { + setDateRange(getDateRange(value, locale)); + } else { + setDateRange(value); + } + } + function handleReset() { setDateRange(getDateRange(DEFAULT_DATE_RANGE, locale)); } @@ -25,7 +33,7 @@ export default function DateRangeSetting() { value={value} startDate={startDate} endDate={endDate} - onChange={setDateRange} + onChange={handleChange} />