import RequestError from "../models/RequestError"; import UserStatsResponse from "../models/UserStatsResponse"; export default class UserStatsService { private static apiURL = 'https://api.tsotr.humenius.me/stats' // private static apiURL = 'http://localhost:3500/stats' //private static apiURL = 'https://mock.codes/501' private static requestInit: RequestInit = { method: 'GET', headers: { 'Content-Type': 'application/json', }, }; public static async getStats(seasonId: string): Promise { return fetch(`${this.apiURL}/season/${seasonId}`, this.requestInit) .then(res => UserStatsService.checkResponse(res)) .then(data => data.json()) .then(data => { data.dates.start = new Date(data.dates.start) data.dates.end = new Date(data.dates.end) return data }) } private static checkResponse(response: any): any { if (!response.ok) { console.log(response); throw new RequestError(response.status, response.body); } return response; } } // const checkResponse = (response: T): T => { // if (!response.ok) { // console.log(response); // let error = new RequestError(response.statusText); // error.response = response; // throw error; // } // return response; // } // // const useApiService = (url: string, method: string = 'GET'): { response: T | null; error: RequestError | null } => { // const baseUrl = "https://api.tsotr.humenius.me" // const [response, setResponse] = useState(null) // const [error, setError] = useState(null) // const requestInit = { // method: method, // headers: { // 'Content-Type': 'application/json' // } // } // // useEffect(() => { // const fetchData = async (): Promise => { // const result = await fetch(`${baseUrl}${url}`, requestInit) // .then(res => checkResponse(res)) // .then(data => data.json()) // .catch(err => setError(err)) // setResponse(result) // } // // fetchData() // }, [url]) // // return { response, error } // } // // // const seasonStatsQuery = (seasonId: number): { response: UserStatsResponse | null; error: RequestError | null } => { // // const baseUrl = "https://api.tsotr.humenius.me/stats/season/" // // const [response, setResponse] = useState(null) // // const [error, setError] = useState(null) // // const requestInit = { // // method: 'GET', // // headers: { // // 'Content-Type': 'application/json' // // } // // } // // // // useEffect(() => { // // const fetchData = async (): Promise => { // // const result = await fetch(baseUrl, requestInit) // // .then(res => checkResponse(res)) // // .then(data => data.json()) // // .catch(err => setError(err)) // // setResponse(result) // // } // // // // fetchData() // // }, [seasonId]) // // // // return { response, error } // // } // // const currentSeasonStatsQuery = useApiService('/stats') // // export default useApiService;