Track query times. Updated refresh button.

This commit is contained in:
Mike Cao 2020-09-02 16:49:14 -07:00
parent e791990750
commit 142dba2e14
5 changed files with 48 additions and 19 deletions

View File

@ -1,19 +1,27 @@
import React from 'react'; import React, { useState, useEffect } from 'react';
import { useDispatch } from 'react-redux'; import { useDispatch, useSelector } from 'react-redux';
import { setDateRange } from 'redux/actions/websites'; import { setDateRange } from 'redux/actions/websites';
import Button from './Button'; import Button from './Button';
import Refresh from 'assets/redo.svg'; import Refresh from 'assets/redo.svg';
import Dots from 'assets/ellipsis-h.svg';
import { useDateRange } from 'hooks/useDateRange'; import { useDateRange } from 'hooks/useDateRange';
export default function RefreshButton({ websiteId }) { export default function RefreshButton({ websiteId }) {
const dispatch = useDispatch(); const dispatch = useDispatch();
const dateRange = useDateRange(websiteId); const dateRange = useDateRange(websiteId);
const [loading, setLoading] = useState(false);
const completed = useSelector(state => state.queries[`/api/website/${websiteId}/metrics`]);
function handleClick() { function handleClick() {
if (dateRange) { if (dateRange) {
setLoading(true);
dispatch(setDateRange(websiteId, dateRange)); dispatch(setDateRange(websiteId, dateRange));
} }
} }
return <Button icon={<Refresh />} size="small" onClick={handleClick} />; useEffect(() => {
setLoading(false);
}, [completed]);
return <Button icon={loading ? <Dots /> : <Refresh />} size="small" onClick={handleClick} />;
} }

View File

@ -1,7 +1,10 @@
import { useState, useEffect } from 'react'; import { useState, useEffect } from 'react';
import { useDispatch } from 'react-redux';
import { get } from 'lib/web'; import { get } from 'lib/web';
import { updateQuery } from 'redux/actions/queries';
export default function useFetch(url, params = {}, options = {}) { export default function useFetch(url, params = {}, options = {}) {
const dispatch = useDispatch();
const [data, setData] = useState(); const [data, setData] = useState();
const [error, setError] = useState(); const [error, setError] = useState();
const keys = Object.keys(params) const keys = Object.keys(params)
@ -12,7 +15,11 @@ export default function useFetch(url, params = {}, options = {}) {
async function loadData() { async function loadData() {
try { try {
setError(null); setError(null);
const time = performance.now();
const data = await get(url, params); const data = await get(url, params);
dispatch(updateQuery({ url, time: performance.now() - time, completed: Date.now() }));
setData(data); setData(data);
onDataLoad(data); onDataLoad(data);
} catch (e) { } catch (e) {

17
redux/actions/queries.js Normal file
View File

@ -0,0 +1,17 @@
import { createSlice } from '@reduxjs/toolkit';
const queries = createSlice({
name: 'queries',
initialState: {},
reducers: {
updateQuery(state, action) {
const { url, ...data } = action.payload;
state[url] = data;
return state;
},
},
});
export const { updateQuery } = queries.actions;
export default queries.reducer;

View File

@ -1,33 +1,29 @@
import { createSlice } from '@reduxjs/toolkit'; import { createSlice } from '@reduxjs/toolkit';
import produce from 'immer';
const websites = createSlice({ const websites = createSlice({
name: 'user', name: 'websites',
initialState: {}, initialState: {},
reducers: { reducers: {
updateWebsites(state, action) { updateWebsites(state, action) {
state = action.payload; state = action.payload;
return state; return state;
}, },
updateWebsite(state, action) {
const { websiteId, ...data } = action.payload;
state[websiteId] = data;
return state;
},
}, },
}); });
export const { updateWebsites } = websites.actions; export const { updateWebsites, updateWebsite } = websites.actions;
export default websites.reducer; export default websites.reducer;
export function setDateRange(websiteId, dateRange) { export function setDateRange(websiteId, dateRange) {
return (dispatch, getState) => { return dispatch => {
const state = getState(); return dispatch(
let { websites = {} } = state; updateWebsite({ websiteId, dateRange: { ...dateRange, modified: Date.now() } }),
);
websites = produce(websites, draft => {
if (!draft[websiteId]) {
draft[websiteId] = {};
}
draft[websiteId].dateRange = { ...dateRange, modified: Date.now() };
});
return dispatch(updateWebsites(websites));
}; };
} }

View File

@ -1,5 +1,6 @@
import { combineReducers } from 'redux'; import { combineReducers } from 'redux';
import user from './actions/user'; import user from './actions/user';
import websites from './actions/websites'; import websites from './actions/websites';
import queries from './actions/queries';
export default combineReducers({ user, websites }); export default combineReducers({ user, websites, queries });