65 lines
1.9 KiB
TypeScript
65 lines
1.9 KiB
TypeScript
const API_BASE = "http://localhost:8000/button_pressed"; // Change depending on Pims interup agent/ correct endpoint
|
|
|
|
|
|
/**
|
|
* HELPER: Unified sender function
|
|
* In a real app, you might move this to a /services or /hooks folder
|
|
*/
|
|
const sendAPICall = async (type: string, context: string, endpoint?: string) => {
|
|
try {
|
|
const response = await fetch(`${API_BASE}${endpoint ?? ""}`, {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({ type, context }),
|
|
});
|
|
if (!response.ok) throw new Error("Backend response error");
|
|
console.log(`API Call send - Type: ${type}, Context: ${context} ${endpoint ? `, Endpoint: ${endpoint}` : ""}`);
|
|
} catch (err) {
|
|
console.error(`Failed to send api call:`, err);
|
|
}
|
|
};
|
|
|
|
|
|
/**
|
|
* Sends an API call to the CB for going to the next phase.
|
|
* In case we can't go to the next phase, the function will throw an error.
|
|
*/
|
|
export async function nextPhase(): Promise<void> {
|
|
const type = "next_phase"
|
|
const context = ""
|
|
sendAPICall(type, context)
|
|
}
|
|
|
|
|
|
/**
|
|
* Sends an API call to the CB for going to reset the currect phase
|
|
* In case we can't go to the next phase, the function will throw an error.
|
|
*/
|
|
export async function resetPhase(): Promise<void> {
|
|
const type = "reset_phase"
|
|
const context = ""
|
|
sendAPICall(type, context)
|
|
}
|
|
|
|
/**
|
|
* Sends an API call to the CB for going to reset the experiment
|
|
* In case we can't go to the next phase, the function will throw an error.
|
|
*/
|
|
export async function resetExperiment(): Promise<void> {
|
|
const type = "reset_experiment"
|
|
const context = ""
|
|
sendAPICall(type, context)
|
|
}
|
|
|
|
export async function pauseExperiment(): Promise<void> {
|
|
const type = "pause"
|
|
const context = "true"
|
|
sendAPICall(type, context)
|
|
}
|
|
|
|
export async function playExperiment(): Promise<void> {
|
|
const type = "pause"
|
|
const context = "false"
|
|
sendAPICall(type, context)
|
|
}
|