Not Another To-Do App: Part 10
Tags: openwc, javascript, data, litelement
Published: 2019-10-01 20:28:07

#### Getting your hands dirty and feet wet with Open Web Component Recommendations...sort of.

> _This a cross-post of a Feb 26, 2019 article from [Medium](https://medium.com/@westbrook/not-another-to-do-app-e4c699bd4777) that takes advantage of my recent decision to use Grammarly in my writing (so, small edits have been made here and there), thanks for looking again if you saw it there 🙇🏽‍♂️ and if this is your first time reading, welcome!_

_Welcome to “Not Another To-Do App”, an overly lengthy review of making one of the smallest applications every developer ends up writing at some point or another. If you’re here to read up on a specific technique to writing apps or have made your way from a previous installation, then likely you are in the right place and should read on! If not, it’s possible you want to [start from the beginning](https://dev.to/westbrook/not-another-to-do-app-2kj9) so you too can know [all of our characters’ backstories...](https://github.com/Westbrook/to-do-app)_

_If you’ve made it this far, why quit now?_

<hr/>

# Reusable and Scaleable Data Management
![Reusable and Scaleable Data Management](https://thepracticaldev.s3.amazonaws.com/i/3760eiuj1ss2q07go7m9.jpeg)
<figcaption>Photo by <a href="https://unsplash.com/@francesco_ungaro">Francesco Ungaro
</a> on <a href="https://unsplash.com/">Unsplash</a></figcaption>

You know what’s kind of popular right now? _[Functional programming.](https://en.wikipedia.org/wiki/Functional_programming)_ You know what’s a great way to deliver features build to take advantage of functional programming concepts? _[ES Modules.](https://hacks.mozilla.org/2018/03/es-modules-a-cartoon-deep-dive/)_ You know what’s a great way to manage logic trees without relying on `switch` or `if/else` statements? _[The strategies pattern.](https://twitter.com/jamesmh_dev/status/1092517108599193601)_ What do you get when you mix them all together? _Reusable and Scaleable Data Management._ Or, at least I certainly hope so (yes, that’s another good one to take down to the comments)...here’s how I did it.

## Reuse

On the reusable end of the spectrum, let’s start with the functional interface by which we interact with the lifecycle of our list of to-dos (creating it, adding to-dos to it, and completing to-dos on it). Access to this can be gained via `import { newToDos, addToDo, completeToDo } from './to-do-manager';`. Here we get functional controls to initialize a new set of to-dos (i.e. return []), add a to-do (i.e. `return [...todos, newTodo]`), and remove a todo (i.e. `return [...todos.filter(todo => todo.id !== completedId)]`. Across each of these, we establish and maintain the identity of our individual to-dos while generating a new identity for the resulting list of to-dos, which will trigger the render pipeline in our `LitElement` based application. Further, their being exports allows them to be used and reused across our application and tests while being prepared for being made an external dependency if at some point this data became useful across multiple applications. If you want to use them in your To-Do application, [fork me on GitHub](https://github.com/Westbrook/to-do-app)!

## Scale

On the scaleable side of the spectrum, we’ll be looking at the rest of the code in `[src/to-do-manger.js](https://github.com/Westbrook/to-do-app/blob/master/src/to-do-manager.js)` which covers the ability to deliver work level customized suggestions for the user via the `workLevelMessage(todos)` export. While this data is also structured for use across the application and possible externalization as well, what’s really nice about it is how it is structured to scale.

```js
function workLevelMeter(todos) {
    const workLevelCounts = Object.keys(workLevelByTodoCount);
    const count = workLevelCounts
        .find(todoCount => todos.length <= todoCount);
    return typeof count !== 'undefined'
        ? workLevelByTodoCount[count]
        : workLevelCounts.length;
}

export function workLevelMessage(todos) {
    return workLevelMessages[workLevelMeter(todos)];
}
```

Notice how rather than attempting to structure an extended `if/then` tree or `switch` statement to outline which the message to return from `[workLevelMessage](https://github.com/Westbrook/to-do-app/blob/master/src/to-do-manager.js#L52)` the `[workLevelMeter](https://github.com/Westbrook/to-do-app/blob/master/src/to-do-manager.js#L44)` method relies on a series of object and array methods (thanks to our previous commit linting experience) to resolve the appropriate message. This means that regardless of whether to the current five levels of workloads and their associated messages, to a single one, or to fifty levels, this same logic will apply. We don’t need to go about adjusting what could easily become an awkward logic tree when you choose to make additions to the messages delivered or alterations to the levels at which their delivered, we just add the associated information to the data structure:

```js
export const workLevelMessages = {
  0: 'Looks like you don\'t have anything to do right now. Take a break!',
  1: 'Looks like you\'re almost done. Keep it up!',
  2: 'Still a little more work to do. Don\'t loose focus!',
  3: 'It\'s a tough job, but somebody\'s got to do it.',
  4: 'This may look like a lot, but I know you can do it!',
  5: 'Maybe it\'s time to take a vacation? I won\'t judge.',
  /* New message */ 6: 'There\'s a new message in town!',
};

export const workLevelByTodoCount = {
  0: 0,
  3: 1,
  6: 2,
  9: 3,
  14: 4,
  /* New work level */ 50: 5,
};
```

The above additions will immediately be available to uses with 50 or more to-dos, no logic alterations needed. And, even better, with the additional export of `[workLevelMessages](workLevelMessages)` and `[workLevelByTodoCount](https://github.com/Westbrook/to-do-app/blob/master/src/to-do-manager.js#L36)` those additions are also immediately added to the testing process.

<hr/>

# And, in the end...

If you are sure I’m wrong, want to celebrate how right I am, or teach me ways I can be even crazier/insightful/progressive/right/et al. about anything you’ve read this far, I hope you know the refrain...the comments you take, and equal to the comments you make. Or at least I heard (something like) that somewhere.

As is noted above, the code shared throughout our conversation to date has not always been what ended up as final in my application. In case you’ve not gotten a chance to see what did, feel free to read the whole codebase on GitHub.

{% github Westbrook/to-do-app %}

You can also see the [_current_ final application](https://gifted-lamport-70b774.netlify.com/) on Netlify. It’s tied to the GitHub repo above, so by current...I really mean it. It’s putting all of the ideas we’ve discussed in action live on the internet, and I hope the conversations we’ve shared about the development of it have proved useful to you in some way.

To bring us back to where we started, here’s a little refresher on the concepts that I wanted to find their way into my little To-Do app:

* component-based code architecture
* custom property based style API
* event-based state management
* style sharing
* unit testing
* web component-based UI

Over the course of some tightly related and others wildly tangential approaches, I hope you agree that I did a decent job of making a pass at each and every one of them. Some of these techniques are enabled by the team behind Open Web Components and their brand new Starter App. Some of them are rooted deeply in the tools recommended therein. And, others are flights of fancy that I’ve cooked up or heard over the course of years working with applications on the web.

If I haven’t said it yet, or in some cases if you hadn’t heard it yet, I’m not planting a stake in the ground to say that any of them are bar none the best in their class, and even less so am I trying to say that any of them would apply to projects of any size or [holotype](https://jasonformat.com/application-holotypes/). What I do hope is that maybe like yet another [set of interesting voices I follow in the field](https://www.youtube.com/watch?v=Vg60lf92EkM) the subjects covered so far spark, not joy, but an interesting conversation between you and your other self, you and your team, you and me, or possibly even any small part of the larger javascript community. The more we get together to talk about our techniques and fret out the edge cases and the exactitudes that make them up, the happier we’ll be. (Oh, look, maybe I did mean for it to spark joy...) So, take the discussion to the comments below, the @ messages on [Twitter](https://twitter.com/WestbrookJ), your favorite Slack, or [this one](https://join.slack.com/t/polymer/shared_invite/enQtNTAzNzg3NjU4ODM4LTkzZGVlOGIxMmNiMjMzZDM1YzYyMzdiYTk0YjQyOWZhZTMwN2RlNjM5ZDFmZjMxZWRjMWViMDA1MjNiYWFhZWM) were working with the web platform, web components, LitElement, and Open Web Components are all trending (at least most of the time), and I’ll see you there!

_Fin._

<hr/>

# The Short Game

As voted on by a plurality of people with opinions on such topics that are both forced to see my tweets in their Twitter feed and had a free minute this last week, a 9000+ word article is a no, no.

{% twitter 1098196160613896192 %}

So, it is with the deepest reverence to you my dear reader that I’ve broken the upcoming conversations into a measly ten sections. Congratulations, you’re nearing the end of the first! If you’ve enjoyed yourself so far, or are one of those people that give a new sitcom a couple of episodes to hit its stride, here’s a list of the others for you to put on your Netflix queue:

- [Not Another To-Do App](https://dev.to/westbrook/not-another-to-do-app-2kj9)
- [Getting Started](https://dev.to/westbrook/not-another-to-do-app-3jem)
- [Test Early, Test Often](https://dev.to/westbrook/not-another-to-do-app-2m9a)
- [Measure Twice, Lint Once](https://dev.to/westbrook/not-another-to-do-app-part-4-58cd) (I, for one, welcome our robot overlords. Maybe they’ll do the writing every time I have an idea to deliver a crazy long series of articles like this...)
- [Make it a Component](https://dev.to/westbrook/not-another-to-do-app-part-5-5d7o)
- [Make it a Reusable Part](https://dev.to/westbrook/not-another-to-do-app-part-6-an)
- [Does Your Component Really Need to Know That?](https://dev.to/westbrook/not-another-to-do-app-part-7-3cm7)
- [Separate Things Early, Often, and Only as Needed](https://dev.to/westbrook/not-another-to-do-app-part-8-3lic)
- [Some Abstractions Aren’t (Just) For Your App](https://dev.to/westbrook/not-another-to-do-app-part-9-10j3)
- Reusable and Scaleable Data Management/And, in the end... (you are here)
- [See the app in action](https://gifted-lamport-70b774.netlify.com/)

<hr/>

Special thanks to the team at [Open Web Components](https://open-wc.org/) for the great set of tools and recommendations that they’ve been putting together to support the ever-growing community of engineers and companies bringing high-quality web components into the industry. [Visit them on GitHub](https://github.com/open-wc/open-wc) and create an issue, submit a PR, or fork a repo to get in on the action!