1
1
import { R , X } from '../node_modules/brutalist-web/r.js' ;
2
- import AuthIn from './views/AuthIn.js' ;
3
2
import * as API from './api.js' ;
3
+ import AuthIn from './views/AuthIn.js' ;
4
+ import App from './views/App.js' ;
5
+
6
+ let retries = 5 ;
4
7
5
8
const appState = {
6
9
name : 'anon' ,
@@ -10,80 +13,33 @@ start();
10
13
11
14
async function start ( ) {
12
15
const code = getCode ( ) ;
13
- if ( ! code ) {
16
+ if ( ! code || code == 'logout' ) {
14
17
const token = localStorage . getItem ( 'token' ) ;
15
18
appState . loggedIn = token ;
19
+ if ( ! token ) return AuthIn ( { } ) . to ( 'main.app' , 'innerHTML' ) ;
16
20
} else {
17
21
const { token} = await fetch ( `https://gistbarn.herokuapp.com/authenticate/${ code } ` ) . then ( r => r . json ( ) ) ;
18
22
console . log ( { loggedIn :token } ) ;
19
23
if ( ! ! token ) {
20
24
localStorage . setItem ( 'token' , token ) ;
25
+ } else {
26
+ return AuthIn ( { } ) . to ( 'main.app' , 'innerHTML' ) ;
21
27
}
22
28
appState . loggedIn = token ;
23
29
}
24
- await API . getProfile ( appState ) ;
30
+ try {
31
+ await API . getProfile ( appState ) ;
32
+ } catch ( e ) {
33
+ if ( retries -- ) return AuthIn ( { } ) . to ( 'main.app' , 'innerHTML' ) ;
34
+ else return console . error ( "Stopping" , e ) ;
35
+ return ;
36
+ }
37
+ const newURL = new URL ( location . href ) ;
38
+ newURL . search = '' ;
39
+ history . pushState ( { } , "Gistbarn | LoggedIn" , newURL ) ;
25
40
( await App ( appState ) ) . to ( 'main.app' , 'innerHTML' ) ;
26
41
}
27
42
28
- async function App ( state ) {
29
- const s = Object . assign ( { } , state ) ;
30
- return R `
31
- <article class="holygrail debug">
32
- <header>
33
- <span class=heading>Gistbarn</span>
34
- <section class=auth>
35
- ${ AuthIn ( state ) }
36
- </section>
37
- </header>
38
- <nav>
39
- <header>Other posts</header>
40
- <ul>
41
- <li><a href=#post1>Post 1</a>
42
- <li><a href=#post2>Post 2</a>
43
- </ul>
44
- </nav>
45
- <article>
46
- ${ Post ( s ) }
47
- <section class="post-stream">
48
- ${ Post ( s ) }
49
- ${ Post ( s ) }
50
- ${ Post ( s ) }
51
- </section>
52
- </article>
53
- <aside>Suggested posts?</aside>
54
- <footer>Footer</footer>
55
- </article>
56
- ` ;
57
- }
58
-
59
- function Post ( state ) {
60
- const s = Object . assign ( { } , state ) ;
61
- s . key = Math . random ( ) ;
62
- return R `
63
- <article ${ s } class="post view">
64
- <section class="post">
65
- <header>Post title</header>
66
- <section class="post paragraphs">
67
- <p>First para
68
- <p>Second para
69
- </section>
70
- <footer>
71
- <time>Post date</time>
72
- <author>Post author</author>
73
- </footer>
74
- </section>
75
- <section class="post edit">
76
- <p>
77
- <input name=tags placeholder=tags>
78
- <p>
79
- <textarea name=post class="markdown live-mode"></textarea>
80
- <p>
81
- <button class=post>Post</button>
82
- </section>
83
- </article>
84
- ` ;
85
- }
86
-
87
43
function getCode ( ) {
88
44
try {
89
45
const code = window . location . search . match ( / \? c o d e = ( .* ) / ) [ 1 ] ;
0 commit comments