aboutsummaryrefslogtreecommitdiff
path: root/frontend/ts/src/pages/layout.ts
blob: 3140c17c247c4a329b93c31cfe3e8c5961cdc887 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import { h, Html } from 'lib/rx'
import * as rx from 'lib/rx'
import * as request from 'request'
import * as route from 'route'
import { User } from 'models/user'
import * as modal from 'ui/modal'

export function view(user: User, children: Html): Html {
    return [
        h('header',
            h('a', { href: '/' }, 'Maps'),
            h('div',
                user.name,
                rx.withState<string | undefined>(undefined, logoutError => [
                    h('button',
                        {
                            className: 'g-Logout__Button',
                            onclick: (event: Event) => {
                                request
                                    .post_('/api/logout')
                                    .then(_ => window.location.href = '')
                                    .catch(({ message }) => logoutError.update(_ => message))
                            }
                        },
                        '(Déconnexion)'
                    ),
                    logoutError.map(err => err && modal.error({
                        header: 'Erreur lors de la déconnexion',
                        message: err,
                        onClose: () => logoutError.update(_ => undefined)
                    }))
                ])
            )
        ),
        h('main', children)
    ]
}