-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathuseUpdateInterval.ts
More file actions
36 lines (32 loc) · 1008 Bytes
/
useUpdateInterval.ts
File metadata and controls
36 lines (32 loc) · 1008 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import { useInterval } from "usehooks-ts"
import type { Routes } from "../../../types"
import { sanitizeTransferData } from "../../../utils/Helpers"
import type { DetailViewActions, DetailViewState } from "../reducer"
export default function useUpdateInterval(
state: DetailViewState,
dispatcher: React.Dispatch<DetailViewActions>,
transferId: { id: string } | null,
routes: Routes,
): void {
const fetchUpdatedTransfer = async (): Promise<void> => {
const transfer = await routes.transfer(transferId!.id)
const sanitizedTransfer = sanitizeTransferData([transfer])
dispatcher({
type: "set_transfer_details",
payload: sanitizedTransfer[0],
})
}
useInterval(
() => {
if (state.transferDetails?.status !== "executed") {
void fetchUpdatedTransfer()
} else {
dispatcher({
type: "update_fetching_status",
payload: "idle",
})
}
},
state.fetchingStatus === "fetching" ? state.delay : null,
)
}