Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Search Feature to Search API For UserNames and Repositories #6

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<meta name='viewport' content='width=device-width, initial-scale=1.0'/>
<meta http-equiv='X-UA-Compatible' content='ie=edge'/>
<title>Document</title>
<script src='js/index.js'></script>
<script defer src='js/index.js'></script>
<link rel='stylesheet' href='index.css'/>
</head>
<body>
Expand All @@ -30,4 +30,4 @@ <h2>GitHub Search</h2>
</div>

</body>
</html>
</html>
118 changes: 118 additions & 0 deletions js/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
const userSearchForm = document.getElementById('github-form')
const userList = document.getElementById('user-list')
const reposList = document.getElementById('repos-list')
const createP = document.createElement('p')
createP.classList.add('search-toggle')
createP.innerText = 'Currently Searching By Username. Click To Search By Repository'
createP.addEventListener('click', () => {
if(createP.innerText ==='Currently Searching By Username. Click To Search By Repository'){
createP.innerText = 'Currently Searching By Repository. Click To Search By Username'
} else if(createP.innerText === 'Currently Searching By Repository. Click To Search By Username'){
createP.innerText = 'Currently Searching By Username. Click To Search By Repository'
}
})
userSearchForm.appendChild(createP)
const toggle = document.getElementsByClassName('search-toggle')[0]

const retrieveUser = (user) => {
fetch(`https://api.github.com/search/users?q=${user}`,{
headers:
{
"Content-Type": "application/json",
Accept: "application/vnd.github.v3+json"
},
})

.then(resp => resp.json())
.then(resp =>{

usersToDoM(resp)})
}

const retrieveUserRepos = (user) => {
fetch(`https://api.github.com/users/${user}/repos`,{
headers:
{
"Content-Type": "application/json",
Accept: "application/vnd.github.v3+json"
},
})
.then(resp => resp.json())
.then(userReposToDom)
}

const userReposToDom = (resp) => {
reposList.innerHTML = ''
resp.forEach(repo => {
const repoLink = document.createElement('a')
repoLink.setAttribute('href', `${repo.html_url}`)
repoLink.textContent = repo.name

const createLi = document.createElement('li')

reposList.appendChild(createLi).appendChild(repoLink)
});
}

const usersToDoM = (resp) => {
userList.innerHTML = ''

resp.items.forEach(user => {

const userLink = document.createElement('a')
userLink.setAttribute('href', `${user.html_url}`)

const userLinkTarget = document.createElement('p')
userLinkTarget.innerText = `^Click to visit above user's profile.^`

const userName = document.createElement('li')
userName.classList.add(user.login)
userName.textContent = user.login

const createImg = document.createElement('img')
createImg.setAttribute('src', `${user.avatar_url}`)

const userPhoto = document.createElement('li')

userList.appendChild(userName).appendChild(userPhoto).appendChild(createImg).addEventListener('click', () => retrieveUserRepos(user.login))
document.getElementsByClassName(`${user.login}`)[0].appendChild(userLink).appendChild(userLinkTarget)
})}

userSearchForm.addEventListener('submit', (event) => {
event.preventDefault()
if(toggle.textContent === 'Currently Searching By Username. Click To Search By Repository'){
const searchValue = document.getElementById('search').value
retrieveUser(searchValue)
} else{
const searchValue = document.getElementById('search').value
retrieveRepoSearch(searchValue)
}

})

const retrieveRepoSearch = (repo) => {
fetch(`https://api.github.com/search/repositories?q=${repo}`,{
headers:
{
"Content-Type": "application/json",
Accept: "application/vnd.github.v3+json"
},
})

.then(resp => resp.json())
.then(repoSearchToDom)
}

const repoSearchToDom = (repoArray) => {
reposList.innerHTML = ''
repoArray.items.forEach(repo => {
const repoLink = document.createElement('a')
repoLink.setAttribute('href', `${repo.html_url}`)
repoLink.textContent = repo.name

const createLi = document.createElement('li')

reposList.appendChild(createLi).appendChild(repoLink)
});
}