-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[REFACTOR] History data fetching (#1698)
* adds use-get-history hook, and replaces data fetching for history using it * removes stray log * move loading render branch out of main render to avoid ternary * tweak hasEmptyHistory check to account for an empty operations list * renames use get history hook file to match repo conventions * renames var to follow code convention
- Loading branch information
1 parent
6ff6938
commit 0be856e
Showing
3 changed files
with
166 additions
and
103 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,85 @@ | ||
import { useReducer } from "react"; | ||
|
||
import { getAccountHistory } from "@shared/api/internal"; | ||
import { NetworkDetails } from "@shared/constants/stellar"; | ||
import { ServerApi } from "stellar-sdk/lib/horizon"; | ||
|
||
enum RequestState { | ||
IDLE = "IDLE", | ||
LOADING = "LOADING", | ||
SUCCESS = "SUCCESS", | ||
ERROR = "ERROR", | ||
} | ||
|
||
interface SuccessState { | ||
state: RequestState.SUCCESS; | ||
data: ServerApi.OperationRecord[]; | ||
error: null; | ||
} | ||
|
||
interface ErrorState { | ||
state: RequestState.ERROR; | ||
data: null; | ||
error: unknown; | ||
} | ||
|
||
interface IdleState { | ||
state: RequestState.IDLE; | ||
data: null; | ||
error: null; | ||
} | ||
|
||
interface LoadingState { | ||
state: RequestState.LOADING; | ||
data: null; | ||
error: null; | ||
} | ||
|
||
type State = IdleState | LoadingState | SuccessState | ErrorState; | ||
|
||
type Action = | ||
| { type: "FETCH_DATA_START" } | ||
| { type: "FETCH_DATA_SUCCESS"; payload: SuccessState["data"] } | ||
| { type: "FETCH_DATA_ERROR"; payload: ErrorState["error"] }; | ||
|
||
const initialState: IdleState = { | ||
state: RequestState.IDLE, | ||
data: null, | ||
error: null, | ||
}; | ||
|
||
const reducer = (state: State, action: Action): State => { | ||
switch (action.type) { | ||
case "FETCH_DATA_START": | ||
return { state: RequestState.LOADING, error: null, data: null }; | ||
case "FETCH_DATA_SUCCESS": | ||
return { error: null, state: RequestState.SUCCESS, data: action.payload }; | ||
case "FETCH_DATA_ERROR": | ||
return { data: null, state: RequestState.ERROR, error: action.payload }; | ||
default: | ||
return state; | ||
} | ||
}; | ||
|
||
function useGetHistory(publicKey: string, networkDetails: NetworkDetails) { | ||
const [state, dispatch] = useReducer(reducer, initialState); | ||
|
||
const fetchData = async () => { | ||
dispatch({ type: "FETCH_DATA_START" }); | ||
try { | ||
const data = await getAccountHistory(publicKey, networkDetails); | ||
dispatch({ type: "FETCH_DATA_SUCCESS", payload: data }); | ||
return data; | ||
} catch (error) { | ||
dispatch({ type: "FETCH_DATA_ERROR", payload: error }); | ||
return error; | ||
} | ||
}; | ||
|
||
return { | ||
state, | ||
fetchData, | ||
}; | ||
} | ||
|
||
export { useGetHistory, RequestState }; |
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