-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
export class GithubUser { | ||
static search(username) { | ||
const endpoint = `https://api.github.com/users/${username}` | ||
|
||
return fetch(endpoint) | ||
.then(data => data.json()) | ||
.then(({ login, name, public_repos, followers }) => ({ | ||
login, | ||
name, | ||
public_repos, | ||
followers | ||
})) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
import { GithubUser } from "./GithubUser.js" | ||
|
||
export class Favorites { | ||
constructor(root) { | ||
this.root = document.querySelector(root) | ||
this.load() | ||
} | ||
|
||
load() { | ||
this.entries = JSON.parse(localStorage.getItem('@github-favorites:')) || [] | ||
} | ||
|
||
save() { | ||
localStorage.setItem('@github-favorites:', JSON.stringify(this.entries)) | ||
} | ||
|
||
async add(username) { | ||
try { | ||
const userExists = this.entries.find(entry => entry.login === username) | ||
|
||
if(userExists){ | ||
throw new Error('Usuario já cadastrado!!') | ||
} | ||
|
||
const user = await GithubUser.search(username) | ||
|
||
if(user.login === undefined) { | ||
throw new Error('Usuário não encontrado!!!') | ||
} | ||
|
||
this.entries = [user, ...this.entries] | ||
this.update() | ||
this.save() | ||
|
||
} catch (error){ | ||
alert(error.message) | ||
} | ||
} | ||
|
||
delete(user) { | ||
const filteredEntries = this.entries.filter(entry => entry.login !== user.login) | ||
this.entries = filteredEntries | ||
this.update() | ||
this.save() | ||
} | ||
} | ||
|
||
export class FavoritesView extends Favorites { | ||
constructor(root) { | ||
super(root) | ||
|
||
this.tbody = this.root.querySelector('table tbody') | ||
|
||
this.update() | ||
this.onadd() | ||
} | ||
|
||
onadd() { | ||
const addButton = this.root.querySelector('.search button') | ||
addButton.onclick = () => { | ||
const { value } = this.root.querySelector('.search input') | ||
this.add(value) | ||
} | ||
} | ||
|
||
update() { | ||
this.removeAllTr() | ||
|
||
this.entries.forEach(user => { | ||
const row = this.createRow() | ||
|
||
row.querySelector('.user img').src = `https://github.com/${user.login}.png` | ||
|
||
row.querySelector('.user img').alt = `Imagem de ${user.name}` | ||
|
||
row.querySelector('.user a').href = `https://github.com/${user.login}` | ||
|
||
row.querySelector('.user p').textContent = user.name | ||
|
||
row.querySelector('.user span').textContent = user.login | ||
|
||
row.querySelector('.repositories').textContent = user.public_repos | ||
|
||
row.querySelector('.followers').textContent = user.followers | ||
|
||
row.querySelector('.remove') | ||
.onclick = () => { | ||
const isOk = confirm('Tem certeza que deseja remover essa linha?') | ||
if(isOk) { | ||
this.delete(user) | ||
} | ||
} | ||
|
||
this.tbody.append(row) | ||
}) | ||
} | ||
|
||
createRow() { | ||
const tr = document.createElement('tr') | ||
|
||
tr.innerHTML = ` | ||
<td class="user"> | ||
<img | ||
src="https://github.com/JulianoMariano.png" | ||
alt="Img de juliano"> | ||
<a | ||
href="https://github.com/JulianoMariano" | ||
target="_blank"> | ||
<p>Juliano Mariano</p> | ||
<span>JulianoMariano</span> | ||
</a> | ||
</td> | ||
<td class="repositories">10</td> | ||
<td class="followers">2</td> | ||
<td> | ||
<button class="remove">×</button></td> | ||
` | ||
return tr | ||
} | ||
|
||
removeAllTr() { | ||
this.tbody.querySelectorAll('tr') | ||
.forEach((tr) => { | ||
tr.remove() | ||
}) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { FavoritesView } from "./favorites.js"; | ||
|
||
new FavoritesView('#app') |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<!DOCTYPE html> | ||
<html lang="pt-br"> | ||
<head> | ||
<link rel="preconnect" href="https://fonts.googleapis.com"> | ||
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin> | ||
|
||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>{🌟}GitFav</title> | ||
|
||
<link rel="shortcut icon" href="./assets/gitlogo.ico" type="image/x-icon"> | ||
<link rel="stylesheet" href="style.css"> | ||
<link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;600&display=swap" rel="stylesheet"> | ||
|
||
<script type="module" src="./JS/main.js" defer></script> | ||
</head> | ||
<body> | ||
<div id="app"> | ||
<header> | ||
<h1><img src="./assets/svg.svg" alt=""></h1> | ||
<div class="search"> | ||
<label class="sr-only" for="input-search">Usuario do Github</label> | ||
<input type="text" placeholder="Username do usuario que deseja favoritar"> | ||
<button><img src="./assets/starbutton.svg" alt=""> favoritar </button> | ||
</div> | ||
</header> | ||
<table> | ||
<thead> | ||
<tr> | ||
<th>Usuário</th> | ||
<th>Repositórios</th> | ||
<th>Seguidores</th> | ||
<th>Ação</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
</tbody> | ||
</table> | ||
</div> | ||
</body> | ||
</html> |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.