Stian Fredrik Aune
4 years ago
7 changed files with 70 additions and 43 deletions
-
38my-bois/background.js
-
1my-bois/build.sh
-
3my-bois/public/manifest.json
-
5my-bois/src/components/Bois.jsx
-
2my-bois/src/components/Score.jsx
-
6my-bois/src/helpers/diff.test.js
-
58my-bois/src/hooks/net.js
@ -0,0 +1,38 @@ |
|||||
|
function sendRequest(path, method, body, sendResp) { |
||||
|
fetch(`http://127.0.0.1:9999/api${path}`, { |
||||
|
method: method, |
||||
|
headers: method !== "GET" ? { |
||||
|
"Content-Type": "application/json", |
||||
|
} : undefined, |
||||
|
body: body ? JSON.stringify(body) : undefined, |
||||
|
}).then(r => r.json()) |
||||
|
.then(data => sendResp({success: true, data})) |
||||
|
.catch(e => sendResp({success: false, message: e.message})); |
||||
|
} |
||||
|
|
||||
|
chrome.runtime.onMessage.addListener( |
||||
|
function (request, sender, sendResponse) { |
||||
|
if (request.query === "fetchBikes") { |
||||
|
sendRequest("/bike", "GET", null, sendResponse); |
||||
|
} else if (request.query === "fetchPrograms") { |
||||
|
sendRequest("/program", "GET", null, sendResponse); |
||||
|
} else if (request.query === "fetchActiveWorkouts") { |
||||
|
sendRequest("/workout?active=true", "GET", null, sendResponse); |
||||
|
} else if (request.query === "createNewWorkout") { |
||||
|
sendRequest("/workout", "POST", request, sendResponse); |
||||
|
} else if (request.query === "connectWorkout") { |
||||
|
sendRequest(`/workout/${request.id}/connect`, "POST", {}, sendResponse); |
||||
|
} else if (request.query === "stopWorkout") { |
||||
|
sendRequest(`/workout/${request.id}/stop`, "POST", {}, sendResponse); |
||||
|
} else if (request.query === "startWorkout") { |
||||
|
sendRequest(`/workout/${request.id}/start`, "POST", {}, sendResponse); |
||||
|
} else if (request.query === "pauseWorkout") { |
||||
|
sendRequest(`/workout/${request.id}/pause`, "POST", {}, sendResponse); |
||||
|
} else if (request.query === "updateCooldownMins") { |
||||
|
sendRequest(`/workout/${request.id}`, "PUT", request, sendResponse); |
||||
|
} else { |
||||
|
return false; |
||||
|
} |
||||
|
|
||||
|
return true; |
||||
|
}); |
@ -1,72 +1,54 @@ |
|||||
export async function fetchBikes() { |
export async function fetchBikes() { |
||||
return await get("/bike"); |
|
||||
|
return await runBack({query: "fetchBikes"}); |
||||
} |
} |
||||
|
|
||||
export async function fetchPrograms() { |
export async function fetchPrograms() { |
||||
return await get("/program"); |
|
||||
|
return await runBack({query: "fetchPrograms"}); |
||||
} |
} |
||||
|
|
||||
export async function fetchActiveWorkouts() { |
export async function fetchActiveWorkouts() { |
||||
return await get("/workout?active=true"); |
|
||||
|
return await runBack({query: "fetchActiveWorkouts"}); |
||||
} |
} |
||||
|
|
||||
export async function createNewWorkout(bike, program) { |
export async function createNewWorkout(bike, program) { |
||||
const bikeId = bike.id; |
const bikeId = bike.id; |
||||
const programId = program.id; |
const programId = program.id; |
||||
|
|
||||
return await post("/workout", {bikeId, programId}); |
|
||||
|
return await runBack({query: "fetchActiveWorkouts", bikeId, programId}); |
||||
} |
} |
||||
|
|
||||
export async function connectWorkout(workout) { |
export async function connectWorkout(workout) { |
||||
return await post(`/workout/${workout.id}/connect`); |
|
||||
|
return await runBack({query: "connectWorkout", ...workout}); |
||||
} |
} |
||||
|
|
||||
export async function stopWorkout(workout) { |
export async function stopWorkout(workout) { |
||||
return await post(`/workout/${workout.id}/stop`); |
|
||||
|
return await runBack({query: "stopWorkout", ...workout}); |
||||
} |
} |
||||
|
|
||||
export async function startWorkout(workout) { |
export async function startWorkout(workout) { |
||||
return await post(`/workout/${workout.id}/start`); |
|
||||
|
return await runBack({query: "startWorkout", ...workout}); |
||||
} |
} |
||||
|
|
||||
export async function pauseWorkout(workout) { |
export async function pauseWorkout(workout) { |
||||
return await post(`/workout/${workout.id}/pause`); |
|
||||
|
return await runBack({query: "pauseWorkout", ...workout}); |
||||
} |
} |
||||
|
|
||||
export async function updateCooldownMins(workout, cooldownMin) { |
export async function updateCooldownMins(workout, cooldownMin) { |
||||
return await put(`/workout/${workout.id}`, {cooldownMin}); |
|
||||
|
return await runBack({query: "pauseWorkout", cooldownMin, ...workout}); |
||||
} |
} |
||||
|
|
||||
export function openWebsocket(workout) { |
export function openWebsocket(workout) { |
||||
return new WebSocket(url(`/workout/${workout.id}/subscribe`, "ws")); |
|
||||
|
return new WebSocket(`ws://127.0.0.1:9999/api/workout/${workout.id}/subscribe`); |
||||
} |
} |
||||
|
|
||||
function get(path) { |
|
||||
return fetch(url(path), { |
|
||||
method: "GET", |
|
||||
}).then(r => r.json()); |
|
||||
|
async function runBack(query) { |
||||
|
return new Promise((resolve, reject) => { |
||||
|
chrome.runtime.sendMessage(query, result => { |
||||
|
if (result.success) { |
||||
|
resolve(result.data) |
||||
|
} else { |
||||
|
reject(result.message); |
||||
|
} |
||||
|
}); |
||||
|
}); |
||||
} |
} |
||||
|
|
||||
function post(path, data = {}) { |
|
||||
return fetch(url(path), { |
|
||||
method: "POST", |
|
||||
headers: { |
|
||||
"Content-Type": "application/json", |
|
||||
}, |
|
||||
body: JSON.stringify(data) |
|
||||
}).then(r => r.json()); |
|
||||
} |
|
||||
|
|
||||
function put(path, data = {}) { |
|
||||
return fetch(url(path), { |
|
||||
method: "PUT", |
|
||||
headers: { |
|
||||
"Content-Type": "application/json", |
|
||||
}, |
|
||||
body: JSON.stringify(data) |
|
||||
}).then(r => r.json()); |
|
||||
} |
|
||||
|
|
||||
function url(path, prefix = "http") { |
|
||||
return `${prefix}://127.0.0.1:9999/api${path}`; |
|
||||
} |
|
Write
Preview
Loading…
Cancel
Save
Reference in new issue