Public
Edited
Jul 4, 2022
Importers
Insert cell
Insert cell
Insert cell
function writeWorkoutToGist(url, day, existingGistData, workoutJson) {
var xhr = new XMLHttpRequest();
xhr.open("PATCH", url);

xhr.setRequestHeader("Authorization", ghToken);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
// console.log(xhr.status);
// console.log(xhr.responseText);
}
};

if (existingGistData === "") {
existingGistData = "[]";
}

var prevWorkoutJson = JSON.parse(existingGistData); // always an array
prevWorkoutJson.push(workoutJson);
var combinedWorkoutData = JSON.stringify(prevWorkoutJson);

var workoutData = {
files: {
[day]: {
content: combinedWorkoutData
}
}
};

var data = JSON.stringify(workoutData);

xhr.send(data);
}
Insert cell
function appendToGistFromWorkout(workoutJson, day) {
var xhr = new XMLHttpRequest();
xhr.open("GET", gistUrl);

xhr.setRequestHeader("Authorization", ghToken);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
var files = JSON.parse(xhr.responseText).files;
var fileContent = files[day].content;
writeWorkoutToGist(gistUrl, day, fileContent, workoutJson);
}
};

xhr.send();
}
Insert cell
// will return null if file was empty
function getLastWorkoutSets(day) {
var data = d3.json(gistUrl).then(getFileContent.bind(null, day));
return data;
}
Insert cell
function getFileContent(day, data) {
var files = data.files;
var fileContent = files[day].content;
if (fileContent === "") {
fileContent = "[]";
return null;
}

var lastWorkoutStatsForDay = JSON.parse(fileContent).pop();
return lastWorkoutStatsForDay.sets;
}
Insert cell
function convertSetData(workoutId, day, setsInfo) {
return {
workoutId: workoutId,
day: day,
sets: setsInfo.map((d) => {
return {
exercise: d.exercise.name,
weight: getWeight(d.set),
reps: getReps(d.set),
setN: d.setN,
notes: getNotes(d.set)
};
})
};
}
Insert cell
function setGenerator(
lastWorkout,
e,
n,
disabled,
weightDefault = e.weightRange.median,
repsDefault = e.repsRange.median
) {
return inputsGroup([
[setNameGenerator(e, n)],
[
prevWorkoutNumsGenerator(lastWorkout, e, n),
Inputs.text({
disabled: disabled,
placeholder: "Set notes"
})
],
[
Inputs.range(e.weightRange, {
disabled: disabled,
value: weightDefault,
step: e.increment,
label: "Weght"
}),
Inputs.range(e.repsRange, {
disabled: disabled,
value: repsDefault,
step: 1,
label: "Reps"
})
]
]);
}
Insert cell
function setNameGenerator(e, n) {
return md`#### ${e.name} set ${n}`;
}
Insert cell
function prevWorkoutNumsGenerator(lastWorkout, e, n) {
const prevSet = getSetFromSetsArray(lastWorkout, e, n);

console.log(prevSet);

if (!lastWorkout || !prevSet) {
return md`No data from last workout`;
}

return md`*Prev **weight**: ${prevSet.weight}, **reps**: ${prevSet.reps}*`;
}
Insert cell
function getSetFromSetsArray(setsArray, exercise, setN) {
if (!setsArray) return null;
return setsArray
.filter((s) => s.exercise === exercise.name && s.setN === setN)
.pop();
}
Insert cell
function getReps(set) {
return set[2][1];
}
Insert cell
function getWeight(set) {
return set[2][0];
}
Insert cell
function getNotes(set) {
return set[1][1];
}
Insert cell
function inputsGroup(views, names, args) {
const form = html`<div class="inputs-group">${views.map(
(row) =>
html`<div class="inputs-group-row">${row.map(
(input) =>
html`<div class="inputs-group-cell" style="display:inline-block;min-width:300px">${input}</div>`
)}</div>`
)}</div>`;

form.oninput = () => {
const values = views.map((row) => row.map((input) => input.value));
form.value = values;

if (args && args.objOnly) form.value = {};
if (names) {
names.forEach((row, i) =>
row.forEach(
(c, j) => values[i][j] != null && (form.value[c] = values[i][j])
)
);
}
};
form.oninput();
return form;
}
Insert cell
d3 = require("d3")
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more