Fix refresh button. Closes #994, #985.

This commit is contained in:
Mike Cao 2022-03-01 21:13:15 -08:00
parent f547f2b0ec
commit 69b81880cb
7 changed files with 38 additions and 38 deletions

View File

@ -11,13 +11,17 @@ import useDateRange from 'hooks/useDateRange';
function RefreshButton({ websiteId }) {
const [dateRange] = useDateRange(websiteId);
const [loading, setLoading] = useState(false);
const selector = useCallback(state => state[`/api/website/${websiteId}/stats`], [websiteId]);
const selector = useCallback(state => state[`/website/${websiteId}/stats`], [websiteId]);
const completed = useStore(selector);
function handleClick() {
if (dateRange) {
if (!loading && dateRange) {
setLoading(true);
setDateRange(websiteId, dateRange);
if (/^[\d]+/.test(dateRange.value)) {
setDateRange(websiteId, dateRange.value);
} else {
setDateRange(websiteId, dateRange);
}
}
}

View File

@ -26,8 +26,7 @@ export default function MetricsTable({
...props
}) {
const shareToken = useShareToken();
const [dateRange] = useDateRange(websiteId);
const { startDate, endDate, modified } = dateRange;
const [{ startDate, endDate, modified }] = useDateRange(websiteId);
const {
resolve,
router,

View File

@ -1,6 +1,5 @@
import React, { useMemo } from 'react';
import classNames from 'classnames';
import { useRouter } from 'next/router';
import PageviewsChart from './PageviewsChart';
import MetricsBar from './MetricsBar';
import WebsiteHeader from './WebsiteHeader';
@ -12,11 +11,10 @@ import useFetch from 'hooks/useFetch';
import useDateRange from 'hooks/useDateRange';
import useTimezone from 'hooks/useTimezone';
import usePageQuery from 'hooks/usePageQuery';
import useLocale from 'hooks/useLocale';
import { getDateArray, getDateLength, getDateRange, getDateRangeValues } from 'lib/date';
import { getDateArray, getDateLength, getDateRangeValues } from 'lib/date';
import useShareToken from 'hooks/useShareToken';
import useApi from 'hooks/useApi';
import { TOKEN_HEADER } from 'lib/constants';
import { get } from 'lib/web';
import styles from './WebsiteChart.module.css';
export default function WebsiteChart({
@ -31,14 +29,13 @@ export default function WebsiteChart({
const shareToken = useShareToken();
const [dateRange, setDateRange] = useDateRange(websiteId);
const { startDate, endDate, unit, value, modified } = dateRange;
const { locale } = useLocale();
const [timezone] = useTimezone();
const { basePath } = useRouter();
const {
router,
resolve,
query: { url, ref },
} = usePageQuery();
const { get } = useApi();
const { data, loading, error } = useFetch(
`/website/${websiteId}/pageviews`,
@ -73,12 +70,10 @@ export default function WebsiteChart({
async function handleDateChange(value) {
if (value === 'all') {
const { data, ok } = await get(`${basePath}/api/website/${websiteId}`);
const { data, ok } = await get(`/website/${websiteId}`);
if (ok) {
setDateRange({ value, ...getDateRangeValues(new Date(data.created_at), Date.now()) });
}
} else if (typeof value === 'string') {
setDateRange(getDateRange(value, locale));
} else {
setDateRange(value);
}

View File

@ -9,20 +9,20 @@ import MenuLayout from 'components/layout/MenuLayout';
import Link from 'components/common/Link';
import Loading from 'components/common/Loading';
import Arrow from 'assets/arrow-right.svg';
import styles from './WebsiteDetails.module.css';
import PagesTable from '../metrics/PagesTable';
import ReferrersTable from '../metrics/ReferrersTable';
import BrowsersTable from '../metrics/BrowsersTable';
import OSTable from '../metrics/OSTable';
import DevicesTable from '../metrics/DevicesTable';
import CountriesTable from '../metrics/CountriesTable';
import LanguagesTable from '../metrics/LanguagesTable';
import EventsTable from '../metrics/EventsTable';
import EventsChart from '../metrics/EventsChart';
import PagesTable from 'components/metrics/PagesTable';
import ReferrersTable from 'components/metrics/ReferrersTable';
import BrowsersTable from 'components/metrics/BrowsersTable';
import OSTable from 'components/metrics/OSTable';
import DevicesTable from 'components/metrics/DevicesTable';
import CountriesTable from 'components/metrics/CountriesTable';
import LanguagesTable from 'components/metrics/LanguagesTable';
import EventsTable from 'components/metrics/EventsTable';
import EventsChart from 'components/metrics/EventsChart';
import useFetch from 'hooks/useFetch';
import usePageQuery from 'hooks/usePageQuery';
import useShareToken from 'hooks/useShareToken';
import { DEFAULT_ANIMATION_DURATION, TOKEN_HEADER } from 'lib/constants';
import styles from './WebsiteDetails.module.css';
const views = {
url: PagesTable,

View File

@ -14,14 +14,6 @@ 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));
}
@ -33,7 +25,7 @@ export default function DateRangeSetting() {
value={value}
startDate={startDate}
endDate={endDate}
onChange={handleChange}
onChange={setDateRange}
/>
<Button className={styles.button} size="small" onClick={handleReset}>
<FormattedMessage id="label.reset" defaultMessage="Reset" />

View File

@ -1,16 +1,15 @@
import { useEffect } from 'react';
import { useRouter } from 'next/router';
import { get } from 'lib/web';
import useStore, { setShareToken } from 'store/app';
import useApi from './useApi';
const selector = state => state.shareToken;
export default function useShareToken(shareId) {
const { basePath } = useRouter();
const shareToken = useStore(selector);
const { get } = useApi();
async function loadToken(id) {
const { data } = await get(`${basePath}/api/share/${id}`);
const { data } = await get(`/share/${id}`);
if (data) {
setShareToken(data);

View File

@ -1,15 +1,26 @@
import create from 'zustand';
import produce from 'immer';
import app from './app';
import { getDateRange } from '../lib/date';
const store = create(() => ({}));
export function setDateRange(websiteId, dateRange) {
export function setDateRange(websiteId, value) {
store.setState(
produce(state => {
if (!state[websiteId]) {
state[websiteId] = {};
}
let dateRange = value;
if (typeof value === 'string') {
const { locale } = app.getState();
dateRange = getDateRange(value, locale);
}
console.log({ websiteId, value, dateRange });
state[websiteId].dateRange = { ...dateRange, modified: Date.now() };
return state;