-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ditch reactquery in favor of custom handler hook
- Loading branch information
Showing
11 changed files
with
147 additions
and
77 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { useCallback, useEffect, useState } from 'react'; | ||
|
||
interface UseLoadData<DataType = undefined> { | ||
refresh: (refreshFunc?: () => Promise<DataType>) => void; | ||
append: (appendFunc: () => Promise<DataType>) => void; | ||
isLoading: boolean; | ||
isError: boolean; | ||
error?: string; | ||
data?: DataType; | ||
} | ||
|
||
interface Status<DataType = undefined> { | ||
isLoading: boolean; | ||
isError: boolean; | ||
error?: string; | ||
data?: DataType; | ||
} | ||
|
||
export const useLoadData = <ReturnType>( | ||
func: () => Promise<ReturnType>, | ||
): UseLoadData<ReturnType> => { | ||
const [status, setStatus] = useState<Status<ReturnType>>({ | ||
isLoading: true, | ||
isError: false, | ||
error: undefined, | ||
data: undefined, | ||
}); | ||
|
||
useEffect(() => { | ||
run(func); | ||
}, []); | ||
|
||
const run = useCallback((func: () => Promise<ReturnType>, append = false) => { | ||
if (!status.isLoading) { | ||
setStatus({ | ||
...status, | ||
isLoading: true, | ||
isError: false, | ||
error: undefined, | ||
}); | ||
} | ||
|
||
void func() | ||
.then((data) => { | ||
if (append) { | ||
setStatus({ | ||
isLoading: false, | ||
isError: false, | ||
error: undefined, | ||
data: { | ||
...status.data, | ||
...data, | ||
}, | ||
}); | ||
} else { | ||
setStatus({ | ||
isLoading: false, | ||
isError: false, | ||
error: undefined, | ||
data, | ||
}); | ||
} | ||
}) | ||
.catch((e) => { | ||
setStatus({ | ||
isLoading: false, | ||
isError: true, | ||
error: e.message, | ||
}); | ||
}); | ||
}, []); | ||
|
||
const refresh = useCallback((refreshFunc?: () => Promise<ReturnType>) => { | ||
if (refreshFunc == null) refreshFunc = func; | ||
|
||
run(refreshFunc); | ||
}, []); | ||
|
||
const append = useCallback((appendFunc: () => Promise<ReturnType>) => { | ||
run(appendFunc, true); | ||
}, []); | ||
|
||
return { | ||
...status, | ||
refresh, | ||
append, | ||
}; | ||
}; |
Oops, something went wrong.