add submit handler to seed field with calendar data

This commit is contained in:
sorrelbri 2020-05-25 23:40:51 -07:00
parent 5d370bda47
commit ed9fcbe57b
7 changed files with 41 additions and 15 deletions

1
dist/app.bundle.js vendored

File diff suppressed because one or more lines are too long

1
dist/index.html vendored
View file

@ -1 +0,0 @@
<!doctype html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Game of Life</title></head><body><aside id="controls"><h1>Game of Life</h1></aside><main><canvas id="game-field" width="500" height="300"></canvas></main><script src="app.bundle.js"></script></body></html>

View file

@ -31,6 +31,12 @@ const init = (gameField) => {
this.pause();
gameField = gameField.reset();
},
seed(weeks) {
if (weeks.length) {
gameField = gameField.seed(weeks);
console.log(gameField);
}
},
forward() {
gameField.advance();
},
@ -83,7 +89,16 @@ const init = (gameField) => {
calendarFormEl.addEventListener("submit", (e) => {
e.preventDefault();
const user = e.target[0].value;
console.log(getCalendar(user));
getCalendar(user)
.then((data) => {
return data.map((week) => {
return week.contributionDays.map((day) => day.contributionCount);
});
})
.then((weeks) => controls.seed(weeks))
.catch((e) => {
calendarFormEl.elements[0].value = "enter valid handle";
});
});
return controls;
};

View file

@ -8,21 +8,17 @@ canvas2D.fillStyle = "white";
const parseSeed = (seed) => {
if (seed && Array.isArray(seed)) {
return {
fieldArray: [
[0, 1, 0],
[0, 0, 1],
[1, 1, 1],
],
fieldArray: seed,
};
}
return seed;
};
const fieldView = (seed) => {
console.log(seed);
// console.log(seed);
seed = parseSeed(seed);
// generateTable(true);
console.log(seed);
// // generateTable(true);
// console.log(seed);
const field = fieldStream(seed);
const view = {
draw(x, y) {
@ -46,6 +42,10 @@ const fieldView = (seed) => {
newField.updateView();
return newField;
},
seed(seed) {
console.log(seed);
return fieldView(seed);
},
advance() {
this.field = this.field.next.next;
this.updateView();

View file

@ -12,6 +12,6 @@
/>
</div>
<form action="/" id="calendar-form">
<input type="text" />
<input type="text" value="GitHub handle"/>
<input type="submit" value="seed" />
</form>

View file

@ -1,3 +1,5 @@
const token = process.env.GITHUB_API_TOKEN;
const query = (user) => `query {
user (login: "${user}") {
contributionsCollection {
@ -17,6 +19,7 @@ const options = (user) => ({
method: "post",
headers: {
"Content-Type": "application/json",
Authorization: `bearer ${token}`,
},
body: JSON.stringify({
query: query(user),
@ -24,9 +27,13 @@ const options = (user) => ({
});
const getCalendar = (user) => {
// return fetch(`https://api.github.com/graphql`, options(user)).then((res) =>
// res.json()
// );
return fetch(`https://api.github.com/graphql`, options(user))
.then((res) => res.json())
.then(
(result) =>
result.data.user.contributionsCollection.contributionCalendar.weeks
)
.catch((e) => e);
};
class Stream {

View file

@ -1,6 +1,7 @@
const path = require("path");
const { CleanWebpackPlugin } = require("clean-webpack-plugin");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const { DefinePlugin } = require("webpack");
module.exports = {
entry: {
@ -12,6 +13,11 @@ module.exports = {
new HtmlWebpackPlugin({
template: "index.html",
}),
new DefinePlugin({
"process.env.GITHUB_API_TOKEN": JSON.stringify(
process.env.GITHUB_API_TOKEN
),
}),
],
output: {
filename: "[name].bundle.js",