Skip to content

Commit

Permalink
Make mega basic sections page
Browse files Browse the repository at this point in the history
  • Loading branch information
grepsedawk committed Aug 5, 2024
1 parent 64b3a2a commit 2386bf5
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 45 deletions.
3 changes: 2 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
<nav>
<ul>
<li><a href="#/">Home</a></li>
<li><a href="#/food">Food Planner (demo)</a></li>
<li><a href="#/sections">Section Planner (demo)</a></li>
<li><a href="#/food">Food Planner (demo)</a></li>
</ul>
</nav>
<div id="content"></div>
Expand Down
7 changes: 5 additions & 2 deletions js/Page.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
class Page {
async renderPage() {
try {
if (this.template) {

this.parent.innerHTML = await this.templateContent()
}

document.title = this.title

Expand All @@ -16,7 +15,11 @@ class Page {
}

async templateContent() {
if (this.template) {
return await fetch(this.template).then((response) => response.text())
} else {
return ""
}
}
}

Expand Down
8 changes: 4 additions & 4 deletions js/Router.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import FoodPage from "../pages/food/FoodPage.js"

import SectionsPage from "../pages/sections/SectionsPage.js"
class Router {
static routes = {
404: FoodPage, // NotFoundPage,
"/": FoodPage, // HomePage,
"/food": FoodPage
"/food": FoodPage,
"/sections": SectionsPage
}

static parseUrl() {
Expand Down Expand Up @@ -53,10 +54,9 @@ class Router {
const { path, urlParams } = this.parseUrl()
const page = this.matchRoute(path, urlParams)

page.render()
await page.render()
} catch (e) {
console.error(`Error Routing [${window.location.hash}]: ${e.message}`)
console.error(e.location)
}
}

Expand Down
26 changes: 26 additions & 0 deletions js/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,29 @@
import Router from "./Router.js"
import Food from "./Food.js"
import Section from "./Section.js"

window.db = new Dexie("planmyhike")

db.version(1).stores({
foods: `
++id,
name,
calories,
carbs,
protein,
fat`,
sections: `
++id,
name,
startMile,
endMile,
currentMile,
caloriesPerDay,
milesPerDay
`,
})

db.foods.mapToClass(Food)
db.sections.mapToClass(Section)
console.log("db done")
Router.init()
30 changes: 0 additions & 30 deletions pages/food/FoodPage.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,10 @@
import Renderer from "../../js/Renderer.js"
import Section from "../../js/Section.js"
import Food from "../../js/Food.js"
import ShowTotals from "../../js/ShowTotals.js"
import NewFood from "../../js/NewFood.js"
import EditSection from "../../js/EditSection.js"
import ShowFood from "../../js/ShowFood.js"
import BarcodeScannerRenderer from "../../js/BarcodeScannerRenderer.js"
import Page from "../../js/Page.js"

window.db = new Dexie("planmyhike")

db.version(1).stores({
foods: `
++id,
name,
calories,
carbs,
protein,
fat`,
sections: `
++id,
name,
startMile,
endMile,
currentMile,
caloriesPerDay,
milesPerDay
`,
})

db.foods.mapToClass(Food)
db.sections.mapToClass(Section)

class FoodPage extends Page {
constructor(parent) {
super()
Expand All @@ -42,17 +15,14 @@ class FoodPage extends Page {
}

async render() {
console.log("render()")
await this.renderPage()
console.log("rendered page")

const quickActions = document.getElementById("quickActions")

ShowTotals.render(document.getElementById("totals"))
ShowFood.render()
BarcodeScannerRenderer.renderTrigger(quickActions)
NewFood.renderTrigger(quickActions)
EditSection.renderTrigger(quickActions)
}
}

Expand Down
1 change: 0 additions & 1 deletion pages/index.html

This file was deleted.

14 changes: 7 additions & 7 deletions js/EditSection.js → pages/sections/AddSection.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import Renderer from "./Renderer.js"
import Section from "./Section.js"
import Renderer from "../../js/Renderer.js"
import Section from "../../js/Section.js"

class EditSection extends Renderer {
class AddSection extends Renderer {
constructor(section) {
super()
this.section = section
}

static renderTrigger(parent) {
const button = document.createElement("button")
button.innerText = "🔧"
button.addEventListener("click", () => EditSection.render())
button.innerText = ""
button.addEventListener("click", () => AddSection.render())

parent.appendChild(button)
}

static render(section = new Section()) {
return new EditSection(section).render()
return new AddSection(section).render()
}

render() {
Expand Down Expand Up @@ -78,4 +78,4 @@ class EditSection extends Renderer {
}
}

export default EditSection
export default AddSection
35 changes: 35 additions & 0 deletions pages/sections/SectionsPage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import Page from "../../js/Page.js"
import AddSection from "./AddSection.js"

class SectionPage extends Page {
constructor(parent) {
super()
this.parent = parent
// this.template = "/pages/food/index.html"
this.title = "Section Planner"
this.description = "plan hike fast"
}

async render() {
await this.renderPage()

const quickActions = document.createElement("div")
quickActions.classList.add("quickActions")
this.parent.appendChild(quickActions)

AddSection.renderTrigger(quickActions)

db.sections
.toArray()
.then((sections) => sections.forEach((s) => this.renderSection(s)))
}

renderSection(section) {
let display = document.createElement("div")
display.innerText = section.name

this.parent.appendChild(display)
}
}

export default SectionPage

0 comments on commit 2386bf5

Please sign in to comment.