Skip to content

Commit

Permalink
use react-query instead of reducers/actions
Browse files Browse the repository at this point in the history
  • Loading branch information
sknep committed Feb 26, 2025
1 parent e56d9c5 commit 5beac2a
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 322 deletions.
76 changes: 0 additions & 76 deletions frontend/actions/actionCreators/fileStorageActions.js

This file was deleted.

113 changes: 0 additions & 113 deletions frontend/actions/fileStorageActions.js

This file was deleted.

48 changes: 16 additions & 32 deletions frontend/hooks/useFileStorage.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,22 @@
import { useEffect, useState } from 'react';
import federalist from '../util/federalistApi';

const initState = {
data: [],
isLoading: true,
error: null,
};
import { useQuery } from '@tanstack/react-query';
import federalist from '@util/federalistApi';

export default function useFileStorage(fileStorageId, path = '') {
const [state, setState] = useState(initState);

async function fetchPublicFiles(id = fileStorageId, pathParam = path) {
setState(initState);
try {
const response = await federalist.fetchPublicFiles(id, pathParam);
setState({
data: [...response.data],
isLoading: false,
error: null,
});
} catch (error) {
setState({
data: [],
isLoading: false,
error: error.message,
});
const fetchPublicFiles = async () => {
const response = await federalist.fetchPublicFiles(fileStorageId, path);
if (response.error) {
throw new Error(response.error);
}
}
return response.data;
};

const { data = [], isLoading, error, refetch } = useQuery({
queryKey: ['fileStorage', fileStorageId, path],
queryFn: fetchPublicFiles,
enabled: !!fileStorageId,
onError: (err) => console.error('Error fetching public files:', err),
});

useEffect(() => {
if (fileStorageId) {
fetchPublicFiles();
}
}, [fileStorageId, path]);

return { ...state, fetchPublicFiles };
return { data, isLoading, error, refetch };
}
9 changes: 1 addition & 8 deletions frontend/pages/file-storage/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function FileStoragePage() {
data: fetchedPublicFiles,
isLoading,
error,
fetchPublicFiles
// refetch (if we need to manually update)
} = useFileStorage(fileStorageServiceId);

const [searchParams, setSearchParams] = useSearchParams();
Expand Down Expand Up @@ -129,13 +129,6 @@ function FileStoragePage() {
});
};

useEffect(() => {
if (fileStorageServiceId) {
fetchPublicFiles(fileStorageServiceId, path);
}
}, [fileStorageServiceId, path]);


if (isLoading) {
return <LoadingIndicator />;
}
Expand Down
93 changes: 0 additions & 93 deletions frontend/reducers/fileStorage.js

This file was deleted.

0 comments on commit 5beac2a

Please sign in to comment.