Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f6dfaa6

Browse files
committed
Cool.
1 parent e404117 commit f6dfaa6

File tree

3 files changed

+45
-70
lines changed

3 files changed

+45
-70
lines changed

src/app.js

Lines changed: 18 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import {R,X} from '../node_modules/brutalist-web/r.js';
2-
import AuthIn from './views/AuthIn.js';
32
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;
47

58
const appState = {
69
name: 'anon',
@@ -10,80 +13,33 @@ start();
1013

1114
async function start() {
1215
const code = getCode();
13-
if ( ! code ) {
16+
if ( ! code || code == 'logout' ) {
1417
const token = localStorage.getItem('token');
1518
appState.loggedIn = token;
19+
if ( ! token ) return AuthIn({}).to('main.app', 'innerHTML');
1620
} else {
1721
const {token} = await fetch(`https://gistbarn.herokuapp.com/authenticate/${code}`).then(r => r.json());
1822
console.log({loggedIn:token});
1923
if( !!token ) {
2024
localStorage.setItem('token', token);
25+
} else {
26+
return AuthIn({}).to('main.app', 'innerHTML');
2127
}
2228
appState.loggedIn = token;
2329
}
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);
2540
(await App(appState)).to('main.app', 'innerHTML');
2641
}
2742

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-
8743
function getCode() {
8844
try {
8945
const code = window.location.search.match(/\?code=(.*)/)[1];

src/views/App.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,32 @@
11
import {R,X} from '../../node_modules/brutalist-web/r.js';
2+
import AuthIn from './AuthIn.js';
3+
import Post from './Post.js';
24

35
export default function App(state) {
46
return R`
5-
<article class="app holygrail debug">
7+
<article class="holygrail debug">
68
<header>
79
<span class=heading>Gistbarn</span>
810
<span class=auth>
9-
Auth Section
11+
${AuthIn(state)}
1012
</span>
1113
</header>
1214
<nav>
13-
<header>Nav Section</header>
15+
<header>Other posts</header>
16+
<ul>
17+
<li><a href=#post1>Post 1</a>
18+
<li><a href=#post2>Post 2</a>
19+
</ul>
1420
</nav>
1521
<article>
16-
Main Section
22+
${Post(state)}
23+
<section class="post-stream">
24+
${Post(state)}
25+
${Post(state)}
26+
${Post(state)}
27+
</section>
1728
</article>
18-
<aside>Aside Section</aside>
29+
<aside>Suggested posts?</aside>
1930
<footer>Footer</footer>
2031
</article>
2132
`;

src/views/AuthIn.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,19 @@ import {R,X} from '../../node_modules/brutalist-web/r.js';
22

33
export default function AuthIn(state) {
44
return R`
5-
${state.loggedIn ? R`<span class=username>${state.name}</span>` :
5+
${state.loggedIn ? R`
6+
<span class=username>${state.name}</span>
7+
<span class=logout><a click=${logout} href=#logout>Logout</a></span>` :
68
R`
7-
<section class="authin">
9+
<span class="authin">
810
<a href=https://github.com/login/oauth/authorize?client_id=d10c47b12243b3cdfd86&scope=gist>Login</a>
9-
</section>
11+
</span>
1012
`}
1113
`;
1214
}
15+
16+
function logout() {
17+
localStorage.setItem('token', '');
18+
history.popState();
19+
location.search = '?code=logout';
20+
}

0 commit comments

Comments
 (0)