all repos — h3 @ 6e17f5c3f70ef7273e5b5ae7ee42d2254d785d4f

A tiny, extremely minimalist JavaScript microframework.

Refactoring: added mainArea region.
h3rald h3rald@h3rald.com
Sat, 11 Apr 2020 21:15:44 +0200
commit

6e17f5c3f70ef7273e5b5ae7ee42d2254d785d4f

parent

5109642920d0098be427f14dfd8312de4a94e176

M example/assets/js/app.jsexample/assets/js/app.js

@@ -7,26 +7,25 @@ import store from "./store.js";

//store.on("log", (state, data) => console.log(data, state)); -// Main rendering function (creates virtual dom) -const build = () => { +const app = () => { const { todos, filteredTodos, filter } = store.get(); - localStorage.setItem("h3_todo_list", JSON.stringify(todos)); - store.dispatch("todos/filter", filter); const [error, updateError] = region(EmptyTodoError); store.on("error/update", updateError); + const [mainArea, updateMainArea] = region(() => { + store.dispatch("todos/filter", filter); + localStorage.setItem("h3_todo_list", JSON.stringify(todos)); + return h3("div#main-area", [NavigationBar(), TodoList()]); + }); + store.on("mainArea/update", () => { + updateMainArea(); + }); return h3("div#todolist.todo-list-container", [ h3("h1", "To Do List"), AddTodoForm(), error, - NavigationBar(), - TodoList(), + mainArea, ]); }; store.dispatch("todos/load"); - -const [app, update] = region(build); - -store.on("app/update", update); - -mount("app", app); +mount("app", app());
M example/assets/js/components/addTodoForm.jsexample/assets/js/components/addTodoForm.js

@@ -16,7 +16,7 @@ key: `todo_${Date.now()}__${newTodo.value}`, // Make todos "unique-enough" to ensure they are processed correctly

text: newTodo.value, }); newTodo.value = ""; - store.dispatch("app/update"); + store.dispatch("mainArea/update"); document.getElementById("new-todo").focus(); }; const addTodoOnEnter = (event) => {
M example/assets/js/components/navigationBar.jsexample/assets/js/components/navigationBar.js

@@ -7,7 +7,7 @@ // Set the todo filter.

const setFilter = () => { let f = document.getElementById("filter-text"); store.dispatch("todos/filter", f.value); - store.dispatch("app/update"); + store.dispatch("mainArea/update"); f = document.getElementById("filter-text"); f.focus(); };
M example/assets/js/components/paginator.jsexample/assets/js/components/paginator.js

@@ -19,14 +19,14 @@ const page = store.get('page');

const newPage = page - 1; store.dispatch("pages/set", newPage); window.location.hash = `/?page=${newPage}`; - store.dispatch("app/update"); + store.dispatch("mainArea/update"); } function setNextPage() { const page = store.get('page'); const newPage = page + 1; store.dispatch("pages/set", newPage); window.location.hash = `/?page=${newPage}`; - store.dispatch("app/update"); + store.dispatch("mainArea/update"); } return h3("div.paginator", [ h3(
M example/assets/js/components/todo.jsexample/assets/js/components/todo.js

@@ -5,11 +5,11 @@ export default function Todo(data) {

const todoStateClass = data.done ? ".done" : ".todo"; const toggleTodo = (todo) => { store.dispatch("todos/toggle", data); - store.dispatch("app/update"); + store.dispatch("mainArea/update"); }; const removeTodo = (todo) => { store.dispatch("todos/remove", data); - store.dispatch("app/update"); + store.dispatch("mainArea/update"); }; return h3(`div#${data.key}.todo-item`, [ h3(`div.todo-content${todoStateClass}`, [