StaffCoder
TypeScript
Vanilla TypeScriptTypeScript 7.0
6 modules · 24 challengesSign up
Module 5 · Modules & Code Organisation

Analytics Chart Loader

easyDynamic import() for lazy module loadingPromise-cache design—No data yet

Problem statement

A small analytics tile keeps its heavy chart module out of the first paint. It is loaded on demand when the user clicks Load, shows a loading indicator while the load is in flight, an error strip with Retry if it fails, and reuses the module without re-loading it on any click after the first success, using Vanilla TypeScript and standard DOM APIs.

Demo

The page, styles, seeded data, DOM helpers, the chart module itself, and the entry wiring are already scaffolded and wired. Only the loader in src/js/chartLoader.ts is missing. Implement it so every test passes. Do not edit any other file.

What to implement

  • Keep the chart module out of the page until the user clicks Load. It should never be loaded ahead of time.
  • Show a loading indicator as soon as Load is clicked, and hide it as soon as the load finishes.
  • Show the chart in the tile when the load succeeds.
  • Show an error strip with a Retry button when the load fails, and re-attempt the load when Retry is clicked.
  • Do not re-load the chart module on any click after the first success. Reuse the one already in memory.
  • Do not remember a failed attempt. The next attempt after a failure must start clean.
  • Everything shown must come from the seeded data. Nothing should be hardcoded.

More in Modules & Code Organisation

See the module
Storefront Utilities PackageSplit a monolithic entry into three named-export modules — format (formatPrice, formatSavingsPercent), validate (isValidEmail), and dom (setText, setDisabled) — and wire them from a thin entry inside a single DOMContentLoaded handler. No file may assign to window, importing any helper in isolation must not touch the document, and the helper import graph must have no cycles.—No data yeteasyStorefront Config Module Write config.ts — DEFAULTS, UnknownConfigKeyError, getConfig, and the generically-typed getConfigValue<K>(key) — as the single seam between a raw window.__STOREFRONT_CONFIG__ object and every consumer. Defaults document absent keys, injected values (including false and "") win over defaults, the returned object is frozen, unknown keys throw the named error, and no consumer file references the raw global.—No data yeteasyShared Storefront Bootstrap Write bootstrap.ts, homeEntry.ts, and productEntry.ts so nothing runs at import time. initBootstrap() runs its work exactly once no matter how many entries invoke it; each entry's init() writes only to its own view and registers only its own click listener; and any init() is safe to call multiple times without duplicating listeners or double-writing text. Idempotency lives in module-scope let flags; no file assigns to window.—No data yeteasy

We use cookies that are necessary to run Staffcoder, and optional analytics cookies to see which pages get used. Analytics stays off unless you accept. Read our Cookie Policy.