fix(striker-ui): add periodic check to update files list

main
Tsu-ba-me 3 years ago
parent b04a7c0233
commit dc701fe9c5
  1. 24
      striker-ui/components/Files/Files.tsx
  2. 5
      striker-ui/lib/fetchers/periodicFetch.ts

@ -13,13 +13,16 @@ import { BLUE } from '../../lib/consts/DEFAULT_THEME';
import FileEditForm from './FileEditForm'; import FileEditForm from './FileEditForm';
import FileList from './FileList'; import FileList from './FileList';
import FileUploadForm from './FileUploadForm'; import FileUploadForm from './FileUploadForm';
import IconButton from '../IconButton';
import { Panel } from '../Panels'; import { Panel } from '../Panels';
import MessageBox from '../MessageBox'; import MessageBox from '../MessageBox';
import Spinner from '../Spinner'; import Spinner from '../Spinner';
import { HeaderText } from '../Text'; import { HeaderText } from '../Text';
import fetchJSON from '../../lib/fetchers/fetchJSON'; import fetchJSON from '../../lib/fetchers/fetchJSON';
import IconButton from '../IconButton'; import periodicFetch from '../../lib/fetchers/periodicFetch';
const FILES_ENDPOINT_URL = `${API_BASE_URL}/files`;
const Files = (): JSX.Element => { const Files = (): JSX.Element => {
const [rawFilesOverview, setRawFilesOverview] = useState<string[][]>([]); const [rawFilesOverview, setRawFilesOverview] = useState<string[][]>([]);
@ -44,7 +47,7 @@ const Files = (): JSX.Element => {
setIsLoadingRawFilesOverview(true); setIsLoadingRawFilesOverview(true);
try { try {
const data = await fetchJSON<string[][]>(`${API_BASE_URL}/files`); const data = await fetchJSON<string[][]>(FILES_ENDPOINT_URL);
setRawFilesOverview(data); setRawFilesOverview(data);
} catch (fetchError) { } catch (fetchError) {
setFetchRawFilesError('Failed to get files due to a network issue.'); setFetchRawFilesError('Failed to get files due to a network issue.');
@ -82,6 +85,23 @@ const Files = (): JSX.Element => {
return elements; return elements;
}; };
/**
* Check for new files periodically and update the file list.
*
* We need this because adding new files is done async; adding the file may
* not finish before the request returns.
*
* We don't care about edit because database updates are done before the
* edit request returns.
*/
periodicFetch<string[][]>(FILES_ENDPOINT_URL, {
onSuccess: (periodicFilesOverview) => {
if (periodicFilesOverview.length !== rawFilesOverview.length) {
setRawFilesOverview(periodicFilesOverview);
}
},
});
useEffect(() => { useEffect(() => {
if (!isEditMode) { if (!isEditMode) {
fetchRawFilesOverview(); fetchRawFilesOverview();

@ -1,10 +1,10 @@
import useSWR from 'swr'; import useSWR, { SWRConfiguration } from 'swr';
import fetcher from './fetchJSON'; import fetcher from './fetchJSON';
const periodicFetch = <T>( const periodicFetch = <T>(
url: string, url: string,
refreshInterval = 5000, { refreshInterval = 5000, onSuccess }: SWRConfiguration = {},
): GetResponses => { ): GetResponses => {
// The purpose of react-hooks/rules-of-hooks is to ensure that react hooks // The purpose of react-hooks/rules-of-hooks is to ensure that react hooks
// are called in order (i.e., not potentially skipped due to conditionals). // are called in order (i.e., not potentially skipped due to conditionals).
@ -12,6 +12,7 @@ const periodicFetch = <T>(
// eslint-disable-next-line react-hooks/rules-of-hooks // eslint-disable-next-line react-hooks/rules-of-hooks
const { data, error } = useSWR<T>(url, fetcher, { const { data, error } = useSWR<T>(url, fetcher, {
refreshInterval, refreshInterval,
onSuccess,
}); });
return { return {

Loading…
Cancel
Save