This repository has been archived by the owner on Sep 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
some refactoring and basic authentication
- Loading branch information
1 parent
7e57754
commit 5e8b228
Showing
11 changed files
with
534 additions
and
159 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
export class Authentication { | ||
constructor() { | ||
if (!Authentication.inst) { | ||
Authentication.inst = this; | ||
} else { | ||
throw new Error('use instance'); | ||
} | ||
|
||
var unparsedAuth = localStorage.getItem('auth'); | ||
if (unparsedAuth) { | ||
this._auth = JSON.parse(unparsedAuth); | ||
} | ||
this._callbacks = []; | ||
return Authentication.inst; | ||
} | ||
|
||
get auth() { | ||
if (this._auth) { | ||
var unparsedAuth = localStorage.getItem('auth'); | ||
if (unparsedAuth) { | ||
this._auth = JSON.parse(unparsedAuth); | ||
} | ||
} | ||
return JSON.parse(localStorage.getItem('auth')); | ||
} | ||
|
||
set auth(value) { | ||
localStorage.setItem('auth', JSON.stringify(value)); | ||
} | ||
|
||
onAuthenticate(callback) { | ||
this._callbacks.push(callback); | ||
} | ||
|
||
|
||
doAuthentication(email, password) { | ||
let user = { | ||
'user': { | ||
'email': email, 'password': password | ||
} | ||
}; | ||
return new Promise((resolve, reject) => { | ||
fetch('https://conduit.productionready.io/api/users/login', { | ||
method: 'post', | ||
headers: { | ||
'Accept': 'application/json, text/plain, */*', | ||
'Content-Type': 'application/json' | ||
}, | ||
body: JSON.stringify(user) | ||
}).then(res=>res.json()).then(res => { | ||
if (res.user) { | ||
this.auth = res.user; | ||
resolve(res.user); | ||
setTimeout(() => { | ||
this._callbacks.forEach(callback => callback(res.user)) | ||
}); | ||
} else { | ||
reject(res.errors) | ||
} | ||
}); | ||
}); | ||
|
||
} | ||
|
||
static get instance() { | ||
return Authentication.inst; | ||
} | ||
} | ||
Authentication.inst = null; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
"use strict"; | ||
|
||
export class PopularTagsComponent extends HTMLElement { | ||
|
||
constructor() { | ||
super(); | ||
// var event = new CustomEvent('build', { 'detail': elem.dataset.time }); | ||
// | ||
// // Listen for the event. | ||
// elem.addEventListener('build', function (e) { ... }, false); | ||
// | ||
// // Dispatch the event. | ||
// elem.dispatchEvent(event); | ||
} | ||
|
||
static get observedAttributes() { | ||
return []; | ||
} | ||
|
||
attributeChangedCallback(name, oldValue, newValue) { | ||
|
||
} | ||
|
||
disconnectedCallback() { | ||
|
||
} | ||
|
||
connectedCallback() { | ||
this.innerHTML = this.render(); | ||
this.popularTags(); | ||
} | ||
|
||
popularTags() { | ||
let tagList = this.querySelector('#tagList'); | ||
|
||
fetch('https://conduit.productionready.io/api/tags').then(function (response) { | ||
return response.json(); | ||
}).then(r => { | ||
if (tagList) { | ||
while (tagList.firstChild) { | ||
tagList.removeChild(tagList.firstChild); | ||
} | ||
} | ||
r.tags.forEach(tag => { | ||
let tagEl = this.createNewTagElement(tag); | ||
tagEl.addEventListener('click', () => { | ||
this.tagOnClick(tag); | ||
}); | ||
tagList.appendChild(tagEl); | ||
}); | ||
}); | ||
} | ||
|
||
tagOnClick(tag) { | ||
var event = new CustomEvent('filter', { 'detail': tag }); | ||
this.dispatchEvent(event); | ||
} | ||
|
||
createNewTagElement(tag) { | ||
const tagEl = document.createElement('a'); | ||
tagEl.className = 'tag-pill tag-default'; | ||
tagEl.innerHTML = tag; | ||
tagEl.href = '#/'; | ||
tagEl.style = "cursor: pointer;"; | ||
return tagEl; | ||
} | ||
|
||
|
||
render() { | ||
return ` | ||
<div class="sidebar"> | ||
<p>Popular Tags</p> | ||
<div id="tagList" class="tag-list"> | ||
Loading tags ... | ||
</div> | ||
</div> | ||
`; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.