Thanks for your comment. it’s full codes
class Storage {
static getSearchedUsersFromStorage() {
let users;
if (localStorage.getItem("Searched") === null) {
users = []
} else {
users = JSON.parse(localStorage.getItem("Searched"))
}
return users
}
static addSearchedUserToStorage(userName) {
let users = this.getSearchedUsersFromStorage()
if (users.indexOf(userName) === -1) {
users.unshift(userName)
}
localStorage.setItem("Searched", JSON.stringify(users))
}
static clearAllSearchedUsrersFromStorage() {
localStorage.removeItem("Searched")
}
}
class UI {
constructor() {
this.profilDiv = document.getElementById('profile')
this.repoDiv = document.getElementById('repos')
this.lastUsers = document.getElementById('last-users')
this.inputField = document.getElementById('githubname')
this.cardBody = document.querySelector('.card-body')
}
clearInput() {
this.inputField.value = '';
}
showUserInfo(user) {
this.profilDiv.innerHTML = `
<div class="card card-body mb-3">
<div class="row">
<div class="col-md-4">
<a href="${user.html_url}" target = "_blank">
<img class="img-fluid mb-2" src="${user.avatar_url}"> </a>
<hr>
<div id="fullName"><strong>${user.name}</strong></div>
<hr>
<div id="bio">${user.bio == undefined ? "Biografi Bilgisi Bulunmuyor." : user.bio}</div>
</div>
<div class="col-md-8">
<button class="btn btn-secondary">
Takipçi <span class="badge badge-light">${user.followers}</span>
</button>
<button class="btn btn-info">
Takip Edilen <span class="badge badge-light">${user.following}</span>
</button>
<button class="btn btn-danger">
Repolar <span class="badge repos-amount badge-light">${user.public_repos}</span>
</button>
<hr>
<li class="list-group">
<li class="list-group-item borderzero">
<img src="images/company.png" width="30px"> <span id="company">${user.company == undefined ? "Ĺžirket Bilgisi Bulunmuyor" : user.company}</span>
</li>
<li class="list-group-item borderzero">
<img src="images/location.png" width="30px"> <span id = "location">${user.location == undefined ? "Lokasyon Bilgisi Bulunamıyor" : user.location}</a>
</li>
<li class="list-group-item borderzero">
<img src="images/mail.png" width="30px"> <span id="company">${user.email == undefined ? "Mail Bilgisi Bulunmuyor" : user.email}</span>
</li>
</div>
</div>
</div>
`
}
showError(message) {
let div = document.getElementById('alertID')
if (div == null) {
div = document.createElement('div')
div.className = "alert alert-danger"
div.textContent = message
div.id = "alertID"
this.cardBody.appendChild(div)
div.style.marginTop = "1rem"
document.querySelector('.btn').disabled = true
document.querySelector('.btn').style.opacity = "1"
} else {
div.style.display = "block"
document.querySelector('.btn').disabled = true
}
setTimeout(() => {
div.style.display = "none"
document.querySelector('.btn').disabled = false
}, 2000)
}
showRepoInfo(repos, repoAmount) {
this.repoDiv.innerHTML = ""
if (repoAmount == 0) {
this.repoDiv.innerHTML += "Repo Bilgisi Bulunmamaktadır"
}
let removed = repos.splice(0, 5)
removed.forEach(repo => {
if (repos.length) {
this.repoDiv.innerHTML += `
<div class="mb-2 card-body">
<div class="row">
<div class="col-md-2">
<a href="${repo.html_url}" target = "_blank" id = "repoName">${repo.name}</a>
</div>
<div class="col-md-6">
<button class="btn btn-secondary">
Starlar <span class="badge badge-light" id="repoStar">${repo.stargazers_count}</span>
</button>
<button class="btn btn-info">
Forklar <span class="badge badge-light" id ="repoFork">${repo.forks_count}</span>
</button>
</div>
</div>
</div>
`
}
})
}
addSearchedUserToUI(userName) {
let date = new Date().getTime()
let users = Storage.getSearchedUsersFromStorage();
if (users.indexOf(userName) === -1) {
const li = document.createElement("li")
li.className = "list-group-item"
li.id = date
li.textContent = userName;
this.lastUsers.insertBefore(li, this.lastUsers.firstChild)
}
}
clearAllSearchedFromUI() {
while (this.lastUsers.firstElementChild !== null) {
this.lastUsers.removeChild(this.lastUsers.firstElementChild)
}
}
}
const githubForm = document.getElementById('github-form')
const nameInput = document.getElementById('githubname')
const clearLastUsers = document.getElementById('clear-last-users')
const lastUsers = document.getElementById('last-users')
const github = new Github()
const ui = new UI()
eventListeners()
function eventListeners() {
githubForm.addEventListener('submit', getData)
clearLastUsers.addEventListener('click', clearAllSearched)
document.addEventListener('DOMContentLoaded', getAllSearched)
}
function getData(e) {
let userName = nameInput.value.trim().toLowerCase()
if (userName === '') {
ui.showError("Lütfen Bir Kullanıcı Adı Giriniz")
} else {
github.getGithubData(userName)
.then(response => {
if (response.user.message === "Not Found") {
ui.showError("Kullanıcı Bulunamadı")
} else {
ui.addSearchedUserToUI(userName)
Storage.addSearchedUserToStorage(userName)
ui.showUserInfo(response.user)
ui.showRepoInfo(response.repo, response.user.public_repos)
}
})
.catch(error => ui.showError(error))
}
ui.clearInput()
e.preventDefault()
}
function clearAllSearched() {
Storage.clearAllSearchedUsrersFromStorage()
ui.clearAllSearchedFromUI()
}
function getAllSearched() {
let users = Storage.getSearchedUsersFromStorage();
let result = ""
users.forEach(function (user) {
result += `<li class="list-group-item">${user}</li>`
})
lastUsers.innerHTML = result
}
class Github {
constructor() {
this.url = 'https://api.github.com/users/'
}
async getGithubData(username) {
const responseUser = await fetch(this.url + username)
const responseRepo = await fetch(this.url + username + "/repos")
const userData = await responseUser.json()
const repoData = await responseRepo.json()
return {
user:userData,
repo:repoData
}
}
}