From d0ca0819c6bc0ab209585ca1be37f60d3adf3b6b Mon Sep 17 00:00:00 2001 From: Mike Cao Date: Sun, 30 Aug 2020 15:29:31 -0700 Subject: [PATCH 1/7] Added useFetch hook. Updated database check. --- components/WebsiteDetails.js | 18 ++------- components/WebsiteList.js | 14 ++----- components/metrics/ActiveUsers.js | 24 +++--------- components/metrics/EventsChart.js | 52 ++++++++++---------------- components/metrics/MetricsBar.js | 27 ++++++------- components/metrics/MetricsTable.js | 35 +++++++---------- components/metrics/WebsiteChart.js | 35 +++++++---------- components/settings/AccountSettings.js | 16 ++------ components/settings/WebsiteSettings.js | 14 ++----- hooks/useFetch.js | 39 +++++++++++++++++++ lib/constants.js | 47 ++++++++++------------- lib/queries.js | 20 +--------- pages/share/[...id].js | 38 ++++--------------- scripts/copy-db-schema.js | 4 +- 14 files changed, 146 insertions(+), 237 deletions(-) create mode 100644 hooks/useFetch.js diff --git a/components/WebsiteDetails.js b/components/WebsiteDetails.js index 60367050..61e740e5 100644 --- a/components/WebsiteDetails.js +++ b/components/WebsiteDetails.js @@ -1,4 +1,4 @@ -import React, { useEffect, useState } from 'react'; +import React, { useState } from 'react'; import classNames from 'classnames'; import WebsiteChart from 'components/metrics/WebsiteChart'; import WorldMap from 'components/common/WorldMap'; @@ -7,7 +7,6 @@ import WebsiteHeader from 'components/metrics/WebsiteHeader'; import MenuLayout from 'components/layout/MenuLayout'; import Button from 'components/common/Button'; import { getDateRange } from 'lib/date'; -import { get } from 'lib/web'; import Arrow from 'assets/arrow-right.svg'; import styles from './WebsiteDetails.module.css'; import PagesTable from './metrics/PagesTable'; @@ -18,15 +17,16 @@ import DevicesTable from './metrics/DevicesTable'; import CountriesTable from './metrics/CountriesTable'; import EventsTable from './metrics/EventsTable'; import EventsChart from './metrics/EventsChart'; +import useFetch from '../hooks/useFetch'; export default function WebsiteDetails({ websiteId, defaultDateRange = '7day' }) { - const [data, setData] = useState(); const [chartLoaded, setChartLoaded] = useState(false); const [countryData, setCountryData] = useState(); const [eventsData, setEventsData] = useState(); - const [dateRange, setDateRange] = useState(getDateRange(defaultDateRange)); const [expand, setExpand] = useState(); + const [dateRange, setDateRange] = useState(getDateRange(defaultDateRange)); const { startDate, endDate, unit } = dateRange; + const { data } = useFetch(`/api/website/${websiteId}`, { websiteId }); const BackButton = () => ( - )} + + + {showLink && ( + + )} + ); } diff --git a/hooks/useFetch.js b/hooks/useFetch.js index 9f14e45e..b9b36c0f 100644 --- a/hooks/useFetch.js +++ b/hooks/useFetch.js @@ -35,5 +35,5 @@ export default function useFetch(url, params = {}, options = {}) { } }, [url, ...keys, ...update]); - return { data, error }; + return { data, error, loadData }; } diff --git a/package.json b/package.json index 17b79794..80bf5f9c 100644 --- a/package.json +++ b/package.json @@ -30,8 +30,7 @@ ], "**/*.css": [ "stylelint --fix", - "prettier --write", - "eslint" + "prettier --write" ] }, "husky": { @@ -53,6 +52,7 @@ "dotenv": "^8.2.0", "formik": "^2.1.5", "geolite2-redist": "^1.0.7", + "immer": "^7.0.8", "is-localhost-ip": "^1.4.0", "jose": "^1.28.0", "maxmind": "^4.1.4", diff --git a/yarn.lock b/yarn.lock index 588dd177..73f0d4d6 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4456,7 +4456,7 @@ ignore@^5.1.4, ignore@^5.1.8: resolved "https://registry.yarnpkg.com/ignore/-/ignore-5.1.8.tgz#f150a8b50a34289b33e22f5889abd4d8016f0e57" integrity sha512-BMpfD7PpiETpBl/A6S498BaIJ6Y/ABT93ETbby2fP00v4EbvPBXWEoaR1UBPKs3iR53pJY7EtZk5KACI57i1Uw== -immer@^7.0.3: +immer@^7.0.3, immer@^7.0.8: version "7.0.8" resolved "https://registry.yarnpkg.com/immer/-/immer-7.0.8.tgz#41dcbc5669a76500d017bef3ad0d03ce0a1d7c1e" integrity sha512-XnpIN8PXBBaOD43U8Z17qg6RQiKQYGDGGCIbz1ixmLGwBkSWwmrmx5X7d+hTtXDM8ur7m5OdLE0PiO+y5RB3pw== From c5cb19a3bf162d0c889250ad72a1acd569e1db82 Mon Sep 17 00:00:00 2001 From: Mike Cao Date: Mon, 31 Aug 2020 14:11:30 -0700 Subject: [PATCH 3/7] Move date range selection into redux. --- components/WebsiteDetails.js | 48 ++++++---------------------- components/WebsiteList.js | 4 +-- components/common/DropDown.js | 6 ++-- components/common/RefreshButton.js | 15 +++++++-- components/metrics/BrowsersTable.js | 4 +-- components/metrics/CountriesTable.js | 11 +------ components/metrics/DevicesTable.js | 4 +-- components/metrics/EventsChart.js | 23 ++++++++----- components/metrics/EventsTable.js | 11 +------ components/metrics/MetricsBar.js | 20 +++++++++--- components/metrics/MetricsTable.js | 13 +++++--- components/metrics/OSTable.js | 4 +-- components/metrics/PagesTable.js | 11 +------ components/metrics/PageviewsChart.js | 5 ++- components/metrics/QuickButtons.js | 6 ++-- components/metrics/ReferrersTable.js | 13 ++------ components/metrics/WebsiteChart.js | 36 ++++++++++++--------- components/metrics/WebsiteHeader.js | 4 +-- hooks/useFetch.js | 4 +-- redux/actions/websites.js | 34 ++++++++++++++++++++ redux/reducers.js | 3 +- 21 files changed, 141 insertions(+), 138 deletions(-) create mode 100644 redux/actions/websites.js diff --git a/components/WebsiteDetails.js b/components/WebsiteDetails.js index 07f68ce8..bd8025ee 100644 --- a/components/WebsiteDetails.js +++ b/components/WebsiteDetails.js @@ -3,10 +3,8 @@ import classNames from 'classnames'; import WebsiteChart from 'components/metrics/WebsiteChart'; import WorldMap from 'components/common/WorldMap'; import Page from 'components/layout/Page'; -import WebsiteHeader from 'components/metrics/WebsiteHeader'; import MenuLayout from 'components/layout/MenuLayout'; import Button from 'components/common/Button'; -import { getDateRange } from 'lib/date'; import Arrow from 'assets/arrow-right.svg'; import styles from './WebsiteDetails.module.css'; import PagesTable from './metrics/PagesTable'; @@ -20,15 +18,12 @@ import EventsChart from './metrics/EventsChart'; import useFetch from 'hooks/useFetch'; import Loading from 'components/common/Loading'; -export default function WebsiteDetails({ websiteId, defaultDateRange = '7day' }) { +export default function WebsiteDetails({ websiteId }) { + const { data } = useFetch(`/api/website/${websiteId}`); const [chartLoaded, setChartLoaded] = useState(false); const [countryData, setCountryData] = useState(); const [eventsData, setEventsData] = useState(); const [expand, setExpand] = useState(); - const [refresh, setRefresh] = useState(0); - const [dateRange, setDateRange] = useState(getDateRange(defaultDateRange)); - const { startDate, endDate, unit } = dateRange; - const { data } = useFetch(`/api/website/${websiteId}`); const BackButton = () => (