Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save WreckedAvent/61a92d8fa2c12eda261161ed1f360ae6 to your computer and use it in GitHub Desktop.
Save WreckedAvent/61a92d8fa2c12eda261161ed1f360ae6 to your computer and use it in GitHub Desktop.
simple strategy for showing different mithril views based on permissions
// some service to get the user's permissions
import * as user from './user-service'
import * as m from 'mithril'
const adminView = (ctrl) => m('.container.admin', [
m('h1', 'Hello admin')
// show admin specific controls
])
const moderatorView = (ctrl) => m('.container.moderator', [
m('h1', 'Hello moderator')
// show moderator-specific controls
])
const userView = (ctrl) => m('.container.user', [
m('h1', 'Hello user')
// show user-specific controls
])
const loadingView = (ctrl) => m('.container.loading', [
m('.loading-spinner')
])
const component = {
controller: () => {
const permissions = m.prop('')
user.getPermissions().then(perm => {
permissions(perm)
m.redraw()
})
return { permissions }
},
view: (ctrl) => {
const permissions = ctrl.permissions()
if (permissions === 'admin') return adminView(ctrl)
if (permissions === 'moderator') return moderatorView(ctrl)
if (permissions === 'user') return userView(ctrl)
return loadingView(ctrl)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment