ft_transcendence/frontend/static/js/views/Search.js

75 lines
1.6 KiB
JavaScript
Raw Normal View History

2023-12-11 08:54:39 -05:00
import AbstractView from "./AbstractView.js";
import {client} from "../index.js";
2023-11-30 10:36:21 -05:00
2023-12-11 08:54:39 -05:00
export default class extends AbstractView {
2023-11-30 10:36:21 -05:00
constructor(params) {
super(params, "Search");
}
async postInit() {
2023-12-11 08:54:39 -05:00
let search = document.getElementById("form");
search.addEventListener("input", this.users)
this.users();
}
async users() {
let search = document.getElementById("form").value;
2023-12-11 10:14:27 -05:00
let logged = client.isAuthentificate();
2023-12-11 08:54:39 -05:00
let users = await client.profiles.all();
2023-11-30 10:36:21 -05:00
let list_users = document.getElementById('list_users');
2023-12-11 08:54:39 -05:00
list_users.innerHTML = "";
users.filter(user => user.username.startsWith(search) == true).forEach((user) => {
2023-11-30 10:36:21 -05:00
var new_user = document.createElement("li");
2023-12-11 08:54:39 -05:00
// username
let username = document.createElement("a");
username.href = `/profiles/${user.user_id}`;
username.appendChild(document.createTextNode(user.username));
new_user.appendChild(username);
2023-12-11 10:14:27 -05:00
// space
new_user.appendChild(document.createTextNode(" "));
// chat
let chat = document.createElement("a");
chat.href = `/chat`
chat.appendChild(document.createTextNode("Chat"));
new_user.appendChild(chat);
2023-12-11 08:54:39 -05:00
// break line
new_user.appendChild(document.createElement("br"));
// avatar
var img = document.createElement("img");
img.src = user.avatar_url;
new_user.appendChild(img);
2023-12-11 10:14:27 -05:00
2023-11-30 10:36:21 -05:00
list_users.appendChild(new_user);
2023-12-11 08:54:39 -05:00
});
2023-11-30 10:36:21 -05:00
console.log(list_users);
}
async getHtml() {
return `
2023-12-11 08:54:39 -05:00
<link rel="stylesheet" href="/static/css/search.css">
2023-11-30 10:36:21 -05:00
2023-12-11 08:54:39 -05:00
<input id="form" type="text" name="message" placeholder="userbozo"/>
2023-11-30 10:36:21 -05:00
<div id="users">
<ul id="list_users">
</ul>
</div>
`;
}
}