-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
793f742
commit fa2c385
Showing
13 changed files
with
36 additions
and
202 deletions.
There are no files selected for viewing
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 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,58 +1,19 @@ | ||
import { useEffect, useState } from 'react'; | ||
import { DataService } from '../infrastructure/data-service-base'; | ||
import { handleServerRestError } from '../services/notifications.service'; | ||
import { DataService, useData as originalUseData } from 'frontal-data-manager'; | ||
|
||
export interface DataHookOptions { | ||
/** Don't show toast message on data fetch failure */ | ||
skipErrorToastOnFailure?: boolean; | ||
} | ||
|
||
/** | ||
* Data hook, used to get the latest data and rerender in case of data update and also give the fetching mode. | ||
* Under the hood it's subscribe to the service data on mount and unsubscribe on unmount. | ||
* Data hook, wrapping the @see useData from 'frontal-data-manager' package, just with injection to generic errorHandler by @see handleServerRestError | ||
* @param dataService The data service to get. | ||
* @param options The hook options | ||
* @returns A a collection of data and the loading mode, as [data, loading, error]. | ||
*/ | ||
export function useData<T>(dataService: DataService<T>, options: DataHookOptions = {}): [T, boolean, boolean] { | ||
// The data state, init with default value of not fetched yet | ||
const [data, setData] = useState(dataService.data); | ||
// The loading state | ||
const [loading, setLoading] = useState<boolean>(true); | ||
// The error flag in case of fetch issue | ||
const [error, setError] = useState<boolean>(false); | ||
|
||
useEffect(() => { | ||
let dataDetacher: () => void; | ||
// Mark as "loading" | ||
setLoading(true); | ||
(async () => { | ||
try { | ||
// Subscribe to the data | ||
dataDetacher = await dataService.attachDataSubs((data) => { | ||
setError(false); | ||
setData(data); | ||
}); | ||
} catch (error) { | ||
// Mark as error accrue | ||
setError(true); | ||
console.error(`[useData] failed to load data ${JSON.stringify(error)}`); | ||
if (!options.skipErrorToastOnFailure) { | ||
await handleServerRestError(error); | ||
} | ||
} | ||
// Mark "loading" OFF | ||
setLoading(false); | ||
})(); | ||
|
||
return () => { | ||
// unsubscribe the feed on component unmount | ||
dataDetacher?.(); | ||
}; | ||
// Run useEffect only once on the component mount | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, []); | ||
|
||
const [data, loading, error] = originalUseData(dataService, { ...options, errorHandler: handleServerRestError }); | ||
// Return the latest data and loading state | ||
return [data, loading, error]; | ||
} |
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
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 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 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 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 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 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 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 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 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