-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use react-query instead of reducers/actions
- Loading branch information
Showing
5 changed files
with
17 additions
and
322 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.