You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
1.3 KiB

5 years ago
  1. export async function fetchBikes() {
  2. return await get("/bike");
  3. }
  4. export async function fetchPrograms() {
  5. return await get("/program");
  6. }
  7. export async function fetchActiveWorkouts() {
  8. return await get("/workout?active=true");
  9. }
  10. export async function createNewWorkout(bike, program) {
  11. const bikeId = bike.id;
  12. const programId = program.id;
  13. return await post("/workout", {bikeId, programId});
  14. }
  15. export async function connectWorkout(workout) {
  16. return await post(`/workout/${workout.id}/connect`);
  17. }
  18. export async function stopWorkout(workout) {
  19. return await post(`/workout/${workout.id}/stop`);
  20. }
  21. export async function startWorkout(workout) {
  22. return await post(`/workout/${workout.id}/start`);
  23. }
  24. export async function pauseWorkout(workout) {
  25. return await post(`/workout/${workout.id}/pause`);
  26. }
  27. export function openWebsocket(workout) {
  28. return new WebSocket(url(`/workout/${workout.id}/subscribe`, "ws"));
  29. }
  30. function get(path) {
  31. return fetch(url(path), {
  32. method: "GET",
  33. }).then(r => r.json());
  34. }
  35. function post(path, data = {}) {
  36. return fetch(url(path), {
  37. method: "POST",
  38. headers: {
  39. "Content-Type": "application/json",
  40. },
  41. body: JSON.stringify(data)
  42. }).then(r => r.json());
  43. }
  44. function url(path, prefix = "http") {
  45. return `${prefix}://127.0.0.1:9999/api${path}`;
  46. }