-
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.
Refactor IRS monitor page container as #806
- Loading branch information
1 parent
695ddc5
commit 871af57
Showing
4 changed files
with
130 additions
and
34 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { SupersetFormData } from '@onaio/superset-connector/dist/types'; | ||
import React, { useEffect, useState } from 'react'; | ||
import { ActionCreator } from 'redux'; | ||
import { SUPERSET_IRS_REPORTING_PLANS_SLICE } from '../../../../configs/env'; | ||
import { displayError } from '../../../../helpers/errors'; | ||
import supersetFetch from '../../../../services/superset'; | ||
|
||
interface AsyncSupersetPlans<TData, TAction> { | ||
superset: typeof supersetFetch; | ||
data: TData[]; | ||
fetchPlans: ActionCreator<TAction>; | ||
supersetSlice: string; | ||
supersetOptions: SupersetFormData | null; | ||
} | ||
|
||
export const useAsyncSupersetPlans = <TData, TAction>({ | ||
superset = supersetFetch, | ||
data = [], | ||
fetchPlans, | ||
supersetSlice = SUPERSET_IRS_REPORTING_PLANS_SLICE, | ||
supersetOptions = null, | ||
}: AsyncSupersetPlans<TData, TAction>) => { | ||
const [loading, setLoading] = useState<boolean>(false); | ||
const mounted = React.useRef<boolean>(false); | ||
|
||
/** async function to load the data */ | ||
async function loadData() { | ||
try { | ||
setLoading(data.length === 0); // only set loading when there are no plans | ||
const asyncOperation = supersetOptions | ||
? superset(supersetSlice, supersetOptions) | ||
: superset(supersetSlice); | ||
|
||
await asyncOperation.then((result: TData[]) => fetchPlans(result)); | ||
} catch (e) { | ||
throw e; | ||
} finally { | ||
if (mounted) { | ||
setLoading(false); | ||
} | ||
} | ||
} | ||
|
||
useEffect(() => { | ||
mounted.current = true; | ||
loadData().catch(error => displayError(error)); | ||
return () => { | ||
mounted.current = false; | ||
}; | ||
}, []); | ||
|
||
return loading; | ||
}; |
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
4 changes: 4 additions & 0 deletions
4
src/containers/pages/IRS/plans/tests/__snapshots__/index.test.tsx.snap
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