1
Fork 0
mirror of https://github.com/Steffo99/festa.git synced 2024-12-23 07:04:22 +00:00
festa/hooks/useTelegramToFestaCallback.ts

30 lines
1.1 KiB
TypeScript
Raw Normal View History

2022-05-29 02:01:56 +00:00
import { default as axios, AxiosError } from "axios"
import { useCallback, Dispatch, SetStateAction } from "react"
import { ApiError, ApiResult } from "../types/api"
import { FestaLoginData, TelegramLoginData } from "../types/user"
2022-05-29 03:00:48 +00:00
export function useTelegramToFestaCallback(setLogin: Dispatch<SetStateAction<FestaLoginData | null>>, setError: Dispatch<SetStateAction<ApiError | null | undefined>>, setWorking: Dispatch<SetStateAction<boolean>>): (data: TelegramLoginData) => Promise<void> {
2022-05-29 02:01:56 +00:00
return useCallback(
async (data: TelegramLoginData) => {
setError(null)
2022-05-29 03:00:48 +00:00
setWorking(true)
2022-05-29 02:01:56 +00:00
try {
var response = await axios.post<ApiResult<FestaLoginData>>("/api/login?provider=telegram", data)
}
catch(e) {
const axe = e as AxiosError
setError(axe?.response?.data as ApiError | undefined)
return
}
2022-05-29 03:00:48 +00:00
finally {
setWorking(false)
}
2022-05-29 02:01:56 +00:00
setLogin(response.data as FestaLoginData)
localStorage.setItem("login", JSON.stringify(response.data))
},
[]
)
}