Skip to content

Commit

Permalink
feat: listar perguntas com loading
Browse files Browse the repository at this point in the history
  • Loading branch information
rrafaelc committed Nov 12, 2023
1 parent 80f7d8d commit 929dc6b
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 8 deletions.
21 changes: 13 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
import { apiUrl } from './scripts/constants/apiUrl.js';
import { listarPerguntas } from './scripts/perguntas/listarPerguntas.js';
import { toast } from './scripts/utils/toast.js';

const listarUrl = `${apiUrl}/pergunta`;
const spinnerContainer = document.querySelector('.spinnerContainer');
const questionsContainer = document.querySelector('.container');

const listarPerguntas = async () => {
const perguntas = await fetch(listarUrl);
let perguntas = [];

return perguntas.json();
};
const perguntas = await listarPerguntas();
const questionsContainer = document.querySelector('.container');
try {
perguntas = await listarPerguntas();
} catch (error) {
toast('Houve um erro ao carregar as perguntas', true);
console.log(error);
} finally {
spinnerContainer.classList.remove('mostrar');
}

const addLinksToContent = (content) => {
const linkRegex = /((http|https):\/\/[^\s.]+[^\s]*[^\s.])/g;
Expand Down
5 changes: 5 additions & 0 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
<link rel="manifest" href="./img/favicon/site.webmanifest" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" />
<script src="https://kit.fontawesome.com/1aacb3a88a.js" crossorigin="anonymous"></script>
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/toastify-js/src/toastify.min.css">
<script defer type="text/javascript" src="https://cdn.jsdelivr.net/npm/toastify-js"></script>
<script src="index.js" type="module" defer></script>
<script src="./scripts/global.js" defer></script>
</head>
Expand All @@ -39,6 +41,9 @@
</div>
</section>
<main>
<div class="spinnerContainer mostrar">
<div class="spinner loader"></div>
</div>
<div class="wrapper">
<div class="container"></div>
</div>
Expand Down
13 changes: 13 additions & 0 deletions scripts/perguntas/listarPerguntas.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { apiUrl } from '../constants/apiUrl.js';

export const listarPerguntas = async () => {
try {
const perguntas = await fetch(`${apiUrl}/pergunta`, {
method: 'GET',
});

return perguntas.json();
} catch (error) {
throw new Error(error.message);
}
};
10 changes: 10 additions & 0 deletions style.css
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,16 @@ main {
overflow: hidden;
}

.spinnerContainer {
display: none;
}

.spinnerContainer.mostrar {
display: flex;
justify-content: center;
margin-top: 20px;
}

.wrapper {
display: flex;
justify-content: center;
Expand Down

0 comments on commit 929dc6b

Please sign in to comment.