2023-11-05 23:33:40 +00:00
|
|
|
import { useEffect, useLayoutEffect, useRef } from "react"
|
|
|
|
import { devlog } from "@frontend/kitui"
|
2023-06-06 13:13:58 +00:00
|
|
|
|
2023-11-05 23:33:40 +00:00
|
|
|
import { ServiceKeyToPrivilegesMap } from "@root/model/privilege"
|
|
|
|
import { getCustomTariffs } from "@root/api/tariff"
|
2023-06-06 13:13:58 +00:00
|
|
|
|
2023-08-31 10:02:11 +00:00
|
|
|
export function useCustomTariffs({
|
2023-11-05 23:33:40 +00:00
|
|
|
onError,
|
|
|
|
onNewUser,
|
2023-08-31 10:02:11 +00:00
|
|
|
}: {
|
|
|
|
onNewUser: (response: ServiceKeyToPrivilegesMap) => void;
|
|
|
|
onError: (error: any) => void;
|
2023-06-06 13:13:58 +00:00
|
|
|
}) {
|
2023-11-05 23:33:40 +00:00
|
|
|
const onNewUserRef = useRef(onNewUser)
|
|
|
|
const onErrorRef = useRef(onError)
|
2023-07-07 13:46:26 +00:00
|
|
|
|
2023-11-05 23:33:40 +00:00
|
|
|
useLayoutEffect(() => {
|
|
|
|
onNewUserRef.current = onNewUser
|
|
|
|
onErrorRef.current = onError
|
|
|
|
})
|
2023-07-07 13:46:26 +00:00
|
|
|
|
2023-11-05 23:33:40 +00:00
|
|
|
useEffect(() => {
|
|
|
|
const controller = new AbortController()
|
2023-06-06 13:13:58 +00:00
|
|
|
|
2023-11-05 23:33:40 +00:00
|
|
|
getCustomTariffs(controller.signal)
|
|
|
|
.then(([customTariffs]) => {
|
|
|
|
if (customTariffs) {
|
|
|
|
onNewUserRef.current(customTariffs)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(([_, error]) => {
|
|
|
|
devlog("Error fetching custom tariffs", error)
|
|
|
|
onErrorRef.current(error)
|
|
|
|
})
|
2023-06-06 13:13:58 +00:00
|
|
|
|
2023-11-05 23:33:40 +00:00
|
|
|
return () => controller.abort()
|
|
|
|
}, [])
|
2023-07-13 18:59:23 +00:00
|
|
|
}
|