diff options
Diffstat (limited to 'js/seed/src/app.js')
-rw-r--r-- | js/seed/src/app.js | 40 |
1 files changed, 38 insertions, 2 deletions
diff --git a/js/seed/src/app.js b/js/seed/src/app.js index 5fd577c..34b4579 100644 --- a/js/seed/src/app.js +++ b/js/seed/src/app.js @@ -5,9 +5,11 @@ import { initialState, cloneState } from './state.js'; import { update } from './update.js'; import { view } from './view.js'; import { fetchPokemon } from './api.js'; +import { initDevMode } from './dev.js'; const root = document.getElementById('app'); let state = cloneState(initialState); +let dev; /** * Entrypoint for the app. @@ -90,6 +92,7 @@ function dispatch(action) { const prevState = state; state = update(state, action); if (devMode) { + dev.pushState(state); console.groupCollapsed(`Action: ${action.type}`); console.log('Payload:', action.payload); console.log('Prev state:', prevState); @@ -111,7 +114,7 @@ function handleInput(e) { /** * Handles form submission, triggers async fetch, and dispatches state updates. * - * Why handle async here? Keeps update/view pure and side-effect free, following functional programming best practices. + * Why handle async here? Keeps update/view pure and centralizes side-effect. */ async function handleSubmit(e) { e.preventDefault(); @@ -126,4 +129,37 @@ async function handleSubmit(e) { } // Initial render -doRender(); \ No newline at end of file +doRender(); + +// After devMode is set +if (devMode) { + dev = initDevMode({ + getState: () => state, + setState: s => { state = s; }, + render: doRender + }); +} + +function updateHistoryInfo() { + if (!devMode || !dev) return; + dev.update(); +} + +function setHistoryPointer(idx) { + const info = dev.getHistoryInfo(); + if (idx < 1 || idx > info.length) return; + const newState = dev.setPointer(idx - 1); + if (newState) { + state = newState; + doRender(); + updateHistoryInfo(); + } +} + +function handleSliderChange(e) { + setHistoryPointer(Number(e.target.value)); +} + +function handleStepperChange(e) { + setHistoryPointer(Number(e.target.value)); +} \ No newline at end of file |