Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
GuiTavs7 committed Oct 25, 2024
0 parents commit a2120ee
Show file tree
Hide file tree
Showing 11 changed files with 398 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"cSpell.words": [
"cadudias"
]
}
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# projeto-inicial-fetch-github-api

Repositório contendo os arquivos iniciais do projeto de Fetch e GitHub API
33 changes: 33 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<!DOCTYPE html>
<html lang="pt-br">

<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>GitHub API - buscando usuários com fetch</title>

<link rel="stylesheet" href="src/css/reset.css">
<link rel="stylesheet" href="src/css/styles.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/devicons/[email protected]/devicon.min.css">
</head>

<body>
<main>
<i class="devicon-github-original"></i>

<section class="container">
<header>
<h2>Buscar Usuário</h2>
<input type="text" id="input-search" placeholder="Digite o nome do usuário no GitHub">
<input type="button" value="Buscar" id="btn-search">
</header>

<div class="profile-data"></div>
</section>
</main>

<script type="module" src="src/scripts/index.js"></script>
</body>

</html>
18 changes: 18 additions & 0 deletions src/css/reset.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
*{
margin: 0;
padding: 0;
box-sizing: border-box;
}

a{
text-decoration: none;
color: var(--cor-branca);
}

ul{
list-style: none;
}

img{
max-width: 100%;
}
195 changes: 195 additions & 0 deletions src/css/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
@import url("https://fonts.googleapis.com/css2?family=Kumbh+Sans:wght@400;700&display=swap");

* {
margin: 0;
padding: 0;
font-weight: 300;
}

img {
max-width: 100%;
}

body {
background-color:#24292e;
font-family: "Kumbh Sans", sans-serif;
text-align: center;
padding-bottom: 100px;
}

main {
display: flex;
justify-content: center;
flex-direction: column;
padding: 50px 0 0;
}

.devicon-github-original{
font-size: 60px;
margin-bottom: 30px;
color: #FFFFFF;
}

.section{
padding: 70px 0;
}

.container {
width: 60%; /* se for uma tela menor que 980px vai se adaptar a 70% */
max-width: 960px; /* quando chegar em 980px para de aumentar */
box-shadow: rgba(0, 0, 0, 0.3) 0px 25px 50px -12px;
background-color: #fff;
display: flex;
flex-direction: column;
align-items: center;
border-radius: 30px;
padding: 30px;
margin: 0 auto;
}

.container header {
display: flex;
align-items: center;
flex-direction: column;
padding: 30px 0;
}

.container header h2 {
font-size: 20px;
font-weight: 700;
margin-bottom: 30px;
}

.container header #input-search {
width: 400px;
font-size: 17px;
margin: 0 auto;
padding: 10px;
border-radius: 7px;
border: 1px solid grey;
}

.container header #btn-search {
width: 200px;
font-size: 17px;
margin: 20px auto 0;
padding: 10px;
border-radius: 7px;
cursor: pointer;
border: 1px solid grey;
}

.container header #btn-search:hover {
background-color: hotpink;
color: #ffffff;
border-color: #ffffff;
}

.container .profile-data{
display: flex;
flex-direction: column;
align-items: center;
padding: 40px;
}

.container .profile-data .info {
display: flex;
flex-wrap: wrap;
align-items: center;
}

.container .profile-data .info .data{
padding: 40px;
width: 50%;
}

.container .profile-data .info img{
width: 50%;
border-radius: 50%;
border: 10px solid hotpink;
}

.container .profile-data .info h1{
font-size: 20px;
margin-bottom: 20px;
font-weight: 700;
}

.container .profile-data .info p{
font-size: 15px;
font-weight: 400;
line-height: 23px;
}

/* REPOSITORIES */

.container .profile-data .repositories h2{
margin-bottom: 30px;
text-align: center;
}

.container .profile-data .repositories ul{
display: flex;
flex-wrap: wrap;
justify-content: center;
}

.container .profile-data .repositories ul li a{
padding: 20px;
border: 1px solid grey;
margin: 20px;
display: block;
border-radius: 7px;
transition: 0.3s ease-in-out;
}

.container .profile-data .repositories ul li a:hover{
background-color:hotpink;
color: #fff
}

.container .profile-data.hide{
display: none;
}

@media (max-width: 1280px) {
.container {
flex-direction: column;
width: 90%;
border-radius: 20px;
}

.container header {
width: 100%;
}

.container header h1 {
font-size: 24px;
text-align: center;
}
}

@media (max-width: 768px) {
.container header #input-search,
.container header #btn-search
{
width: 100%;
}
}

@media (max-width: 425px) {
main {
padding: 15% 0;
}

.container .profile-data .info img{
margin-bottom: 20px;
}

.container .profile-data .info img,
.container .profile-data .info .data
{
width: 100%;
padding: 0;
}
}
65 changes: 65 additions & 0 deletions src/scripts/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Importando serviços

import { fetchUser } from "/src/scripts/services/user.js";
import { getRepositories } from "/src/scripts/services/repositories.js";

// Importando objetos

import { user } from "/src/scripts/objects/user.js";
import { screen } from "/src/scripts/objects/screen.js";

// 1) Criando o evento de buscar os dados do usuário através do clique do botão

document.getElementById("btn-search").addEventListener("click", () => {

const userName = document.getElementById("input-search").value;

if(validateEmptyInput(userName)) return;

getUserData(userName);
})

// 2) Criando o evento de buscar os dados do usuário através do pressionamento da tecla "enter"

document.getElementById("input-search").addEventListener("keyup", (e) => {
const userName = e.target.value;
const key = e.which || e.keyCode;
const isEnterKeyPressed = key === 13;

if(isEnterKeyPressed){

if(validateEmptyInput(userName)) return;

getUserData(userName);
}
})

// 3) Função de validação

function validateEmptyInput(userName){

if(userName.length === 0){
alert("Preencha o campo com o nome do usuário do GitHub");
return true;
}

}

// 4) Criação da função que altera o HTML através dos dados obtidos do usuário via API - dados do usuário

async function getUserData(userName){

const userResponse = await fetchUser(userName);

if(userResponse.message === "Not Found"){
screen.renderNotFound();
return;
}

const repositoriesResponse = await getRepositories(userName);

user.setInfo(userResponse);
user.setRepositories(repositoriesResponse);

screen.renderUser(user);
}
37 changes: 37 additions & 0 deletions src/scripts/objects/screen.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const screen = {

userProfile: document.querySelector(".profile-data"),

renderUser(user){

// Usuário

this.userProfile.innerHTML = `<div class = "info">
<img src = "${user.avatarUrl}" alt = "Foto do perfil usuário" />
<div class = "data">
<h1>${user.name ?? "Não possui nome cadastrado 😢"}</h1>
<p>${user.bio ?? "Não possui bio cadastrada 😢"}</p>
</div>
</div>`

// Repositórios

let repositoriesItens = ""

user.repositories.forEach(repo => repositoriesItens += `<li><a href="${repo.html_url}" target = "_blank">${repo.name}</a></li>`);

if(user.repositories.length > 0){
this.userProfile.innerHTML += `<div class="repositories section">
<h2>Repositórios</h2>
<ul>${repositoriesItens}</ul>
</div>`
}
},

renderNotFound(){
this.userProfile.innerHTML = "<h3>Usuário não encontrado!</h3>"
}

}

export { screen };
18 changes: 18 additions & 0 deletions src/scripts/objects/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const user = {
avatarUrl: '',
name: '',
bio: '',
userName: '',
repositories: [],
setInfo(gitHubUser){
this.avatarUrl = gitHubUser.avatar_url;
this.name = gitHubUser.name;
this.bio = gitHubUser.bio;
this.userName = gitHubUser.login;
},
setRepositories(repositories){
this.repositories = repositories;
}
}

export { user };
10 changes: 10 additions & 0 deletions src/scripts/services/repositories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// 1) Criação da função assíncrona para buscar os dados de repositórios

import { baseUrl, repositoriesQuantity } from "/src/scripts/variables.js";

async function getRepositories(userName){
const response = await fetch(`${baseUrl}/${userName}/repos?per_page=${repositoriesQuantity}`);
return await response.json();
}

export { getRepositories };
Loading

0 comments on commit a2120ee

Please sign in to comment.