-
Notifications
You must be signed in to change notification settings - Fork 25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[REFACTOR] History data fetching #1698
Merged
aristidesstaffieri
merged 6 commits into
release/5.26.0
from
refactor/history-data-flow
Nov 27, 2024
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1e11e18
adds use-get-history hook, and replaces data fetching for history usi…
aristidesstaffieri 2b8d3aa
removes stray log
aristidesstaffieri 8812240
move loading render branch out of main render to avoid ternary
aristidesstaffieri c1612f5
tweak hasEmptyHistory check to account for an empty operations list
aristidesstaffieri be39fcd
renames use get history hook file to match repo conventions
aristidesstaffieri 68003d3
renames var to follow code convention
aristidesstaffieri File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we use
fetchData
as a dep of theuseEffect
it triggers another load of the data. React officially recommends using an empty array to run effects only on mount.I think we should remove this lint rule but if we really don't want to we can also use the loading state to step around the extra call, but we won't be able to avoid the extra render.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm fine using the empty array as useEffect dependency and disabling the lint rule on a case-by-case manner, I think it makes sense for cases where we need to run the code only once on mount 👍
I'm on the fence about removing the lint rule from the project, it could be useful in other cases where it's important to add more deps
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah good point, it has helped me spot missing deps before so maybe it's ok to just step around it for these cases?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed - let's keep the lint rule and just disable when we explicitly just want to run on mount