Vue Collapsible Panel Component (with persisted state)

Today, ill create a simple re-usable collapsible panel component that saves state in localStorage.

Starting out

I start out by creating a basic vue project through the CLI (default settings)

Next up, add Fontawesome and Bulma (I ❤️ Bulma) yarn add bulma.

package.json after dependencies has been added

(I load in bulma through sass so I can override colors etc. so I’ve also installed node-sass and sass-loader as devDependencies)

In main.js I’ve added the basic FontAwesome setup (as per the link)

I just add ChevronUp (🔼) and ChevronDown (🔽) to the library and off we go.

On to the actual component

I’ve created the “panel” out of the bulma card component.
The component takes 2 props, namely id and title.
title is used for the card header and id is used for state management.

The component loads/saves the collapsible state in localStorage.

I’ve created a small extension to help (de)serialize objects to localStorage as localStorage only really does key/value pairs (which could have been used, but I find this to be much better).

We load the state during mounted() and we get/set via the following methods

The state is saved in an object in localStorage under the key collapsibles
and id‘s are added to that object with the collapsible state.
It creates something like this

Complete component

The component has 2 slots, a default one and a footer – you can see that in the usage below.

Usage

I’ve shown 2 usage scenarios in the following code. 1 with the footer and 1 without.

Below you can see the storage change when I change the state.

you can find the complete project on my github

Simple Vue splash screen

Today, ill just introduce a nifty little trick to create a loading screen, to let your users know that your app is still working, even though it takes a while to do the initial load.

Splash screen

The idea is to add the loading/splash screen to the .public/index.html file inside the <div id="app"></div> or whatever your mount point is, because vue will remove whatever is inside, when it is done loading the components.

I’ve just created a new vue app from the CLI.
To simulate the slow rendering I’ve put a delay function in the HelloWorld.vue which is then called inside of the beforeMount()

In the .public/index.html file I’ve added;

which is just some simple animation css and inside the <div id="app"></div>I’ve added the following:

You can find the full project on my github

Special thanks to https://codepen.io/luka-butina/ for the Loading Screen.

Versioning WebApi and documenting version with Swagger

Versioning in WebApi

Note: This post draws alot of points from https://www.troyhunt.com/your-api-versioning-is-wrong-which-is/ – but is more about implementation and documentation through swagger.
So please go read Troy’s post and then come back :).
There are a couple of ways of versioning a restful api
among them are:
1. Url versioning e.g. GET http://example.com/api/v1/cars
2. Request-Header e.g. GET http://example.com/api/cars — Header: api-version: 1
There are pros and cons in both regards, Troy gets around them quite nicely.
Time to code!

Url Versioning

Easy – simply use either the RoutePrefix- or RouteAttribute

Read more about the Routing-Attribute here

Request-Header

Drawing from Troy’s post
We create a new attribute which uses a custom routing constraint

** Usage **

Effectively matching routes based on the “api-version” header.

Swagger

When using swashbuckle (swagger implementation for .Net).
It knows about the default Route- and RoutePrefixAttribute, so Url Versioning is taken care of, out of the box.
But Header versioning is not.
Fortunately, it’s quite easy to implement an OperationFilter that adds the header as a parameter to the endpoints in question.
This implementation uses a list of versions and the endpoints relativepaths to know when to add the parameter and when not to.


How would you implement the OperationFilter?