Skip to content

Commit

Permalink
Adicionando função de ordenar posts
Browse files Browse the repository at this point in the history
  • Loading branch information
andreeluis committed Dec 1, 2023
1 parent 94bea7f commit fce0170
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 10 deletions.
29 changes: 29 additions & 0 deletions src/forum/assets/css/forum.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,32 @@
/* Search bar */
.actions {
display: flex;
flex-wrap: wrap;
justify-content: space-between;
align-items: center;
margin: 2em 0;
}

.actions .order {
display: flex;
flex-wrap: wrap;
align-items: center;
}
.actions .order>* {
margin: 0 .4em .6em;
}

.actions #add-post {
margin: auto .4em;
}


.order>* {
font-size: 1em;
}



#posts article .reactions {
float: right;
vertical-align: middle;
Expand Down
22 changes: 22 additions & 0 deletions src/forum/assets/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,14 @@ async function handleAddSubmit(event) {
const newPost = {
id: generateUUID(8),
titulo: document.querySelector('#modal form #title').value,
dataPostagem: Date.now(),
autor: JSON.parse(sessionStorage.getItem('user')).name,
usuario: JSON.parse(sessionStorage.getItem('user')).id,
imagem: '',
categoria: document.querySelector('#modal form #category').value,
conteudo: document.querySelector('#modal form #content').value,
curtidas: 0,
usuariosCurtidas: [],
comentarios: [],
};

Expand Down Expand Up @@ -180,3 +182,23 @@ async function deletePost() {
}
}
}


// ORDER POSTS
document.querySelector('.order').addEventListener('input', order, false);

function order() {
const orderType = document.querySelector('#order').value;
console.log(orderType);

const sortFunctions = {
'date': (a, b) => new Date(b.dataPostagem) - new Date(a.dataPostagem),
'likes': (a, b) => b.curtidas - a.curtidas
};

if (sortFunctions[orderType]) {
const sortedPosts = postsList.sort(sortFunctions[orderType]);
const textoHTML = sortedPosts.map(generateHTML).join('');
document.querySelector("#posts").innerHTML = textoHTML;
}
}
11 changes: 10 additions & 1 deletion src/forum/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,16 @@
<section class="post-list">
<h2>Fórum</h2>

<button class="add-item" id="add-post">Novo Post</button>
<section class="actions">
<form class="order">
<select name="order" id="order">
<option value="date" selected>Mais recentes</option>
<option value="likes">Mais curtidas</option>
</select>
</form>

<button class="add-item" id="add-post">Novo Post</button>
</section>

<section class="itens-list" id="posts"></section>
</section>
Expand Down
9 changes: 0 additions & 9 deletions src/projetos/assets/css/projetos.css
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,6 @@
color: var(--black);
}

.search-bar button.search {
width: auto;
height: 100%;
margin: auto 1em;
padding: 0.5em 0.8em;
font-size: 1em;
}


.itens-list article p.dificuldade {
display: inline-block;
padding: .3em .6em;
Expand Down

0 comments on commit fce0170

Please sign in to comment.