This repository has been archived by the owner on Jul 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8e686ba
commit 6cc29a2
Showing
2 changed files
with
16 additions
and
30 deletions.
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 |
---|---|---|
@@ -1,34 +1,22 @@ | ||
import axios from "axios"; | ||
import { useState } from "react"; | ||
const baseURL = "http://localhost:8000/" | ||
|
||
interface GetResponse { | ||
interface PostState { | ||
response: string | undefined, | ||
error: string | undefined, | ||
postData: (url: string, body: any) => Promise<void> | ||
error: string | undefined | ||
} | ||
|
||
export function useAPIPost(): GetResponse { | ||
interface State { | ||
response: string | undefined, | ||
error: string | undefined | ||
} | ||
const [state, setState] = useState<State>({ | ||
export async function APIPost(url: string, body: any): Promise<PostState> { | ||
let state: PostState = { | ||
response: undefined, | ||
error: undefined | ||
}); | ||
async function postData(url: string, body: any) { | ||
try { | ||
let response = await axios.post<string>(baseURL + url, { data: body }); | ||
setState({ ...state, response: response.data }); | ||
} catch (error: any) { | ||
setState({ ...state, error: error }); | ||
} | ||
} | ||
}; | ||
|
||
return { | ||
response: state.response, | ||
error: state.error, | ||
postData: postData | ||
try { | ||
let response = await axios.post<string>(baseURL + url, { data: body }); | ||
state = { ...state, response: response.data }; | ||
} catch (error: any) { | ||
state = { ...state, error: error }; | ||
} finally { | ||
return state | ||
} | ||
} |
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