From bc892bc15793c3b561f9d54393331ffad242aacc Mon Sep 17 00:00:00 2001 From: Xamora Date: Tue, 12 Dec 2023 10:04:46 +0100 Subject: [PATCH] Advance don't merge --- frontend/static/js/api/chat/channel.js | 9 +++++ frontend/static/js/api/chat/channels.js | 19 +++++++++ frontend/static/js/index.js | 2 - frontend/static/js/views/Chat.js | 4 +- frontend/static/js/views/Search.js | 23 ++++++++--- .../js/views/profiles/ProfilePageView.js | 2 +- .../static/js/views/profiles/ProfilesView.js | 40 ------------------- 7 files changed, 48 insertions(+), 51 deletions(-) create mode 100644 frontend/static/js/api/chat/channel.js create mode 100644 frontend/static/js/api/chat/channels.js delete mode 100644 frontend/static/js/views/profiles/ProfilesView.js diff --git a/frontend/static/js/api/chat/channel.js b/frontend/static/js/api/chat/channel.js new file mode 100644 index 0000000..d7d31ce --- /dev/null +++ b/frontend/static/js/api/chat/channel.js @@ -0,0 +1,9 @@ +class Channel { + constructor(id, members, messages) { + this.id = id; + this.members = members; + this.messages = messages; + } +} + +export {Channel} diff --git a/frontend/static/js/api/chat/channels.js b/frontend/static/js/api/chat/channels.js new file mode 100644 index 0000000..666d709 --- /dev/null +++ b/frontend/static/js/api/chat/channels.js @@ -0,0 +1,19 @@ +class Channels { + constructor(client) { + this.client = client; + } + + async createChannel(users_id) { + console.log(users_id); + let response = await this.client._post("/api/chat/", { + users_id:users_id + }); + + let data = await response.json(); + if (data == "Channel already exist") + return null; + return data; + } +} + +export {Channels} diff --git a/frontend/static/js/index.js b/frontend/static/js/index.js index b07362f..3db6fa5 100644 --- a/frontend/static/js/index.js +++ b/frontend/static/js/index.js @@ -11,7 +11,6 @@ import { Client } from "./api/client.js"; import AbstractRedirectView from "./views/AbstractRedirectView.js"; import MeView from "./views/MeView.js"; import ProfilePageView from "./views/profiles/ProfilePageView.js"; -import ProfilesView from "./views/profiles/ProfilesView.js"; let client = new Client(location.protocol + "//" + location.host) @@ -36,7 +35,6 @@ const navigateTo = async (uri) => { const router = async (uri) => { const routes = [ { path: "/", view: Dashboard }, - { path: "/profiles", view: ProfilesView}, { path: "/profiles/:id", view: ProfilePageView }, { path: "/settings", view: Settings }, { path: "/login", view: LoginView }, diff --git a/frontend/static/js/views/Chat.js b/frontend/static/js/views/Chat.js index 472be28..c84e2c2 100644 --- a/frontend/static/js/views/Chat.js +++ b/frontend/static/js/views/Chat.js @@ -1,6 +1,6 @@ -import AbstractAuthentifiedView from "./AbstractAuthentifiedView.js"; +import AbstractView from "./AbstractView.js"; -export default class extends AbstractAuthentifiedView { +export default class extends AbstractView { constructor(params) { super(params, "Chat"); diff --git a/frontend/static/js/views/Search.js b/frontend/static/js/views/Search.js index 4c1f4d4..883388f 100644 --- a/frontend/static/js/views/Search.js +++ b/frontend/static/js/views/Search.js @@ -19,7 +19,7 @@ export default class extends AbstractView { let search = document.getElementById("form").value; - let logged = client.isAuthentificate(); + let logged = await client.isAuthentificate(); let users = await client.profiles.all(); let list_users = document.getElementById('list_users'); @@ -38,10 +38,21 @@ export default class extends AbstractView { new_user.appendChild(document.createTextNode(" ")); // chat - let chat = document.createElement("a"); - chat.href = `/chat` - chat.appendChild(document.createTextNode("Chat")); - new_user.appendChild(chat); + if (logged) { + let chat = document.createElement("a"); + let array = [ + client.me.user_id, + user.user_id, + ]; + console.log(client.me.id); + chat.addEventListener("click", async function(){ + console.log("click"); + await client.channels.createChannel([client.me.user_id , user.user_id]); + }); + //chat.href = `/chat` + chat.appendChild(document.createTextNode("Chat")); + new_user.appendChild(chat); + } // break line new_user.appendChild(document.createElement("br")); @@ -54,7 +65,7 @@ export default class extends AbstractView { list_users.appendChild(new_user); }); - console.log(list_users); + //console.log(list_users); } diff --git a/frontend/static/js/views/profiles/ProfilePageView.js b/frontend/static/js/views/profiles/ProfilePageView.js index ffd9ee7..81247a4 100644 --- a/frontend/static/js/views/profiles/ProfilePageView.js +++ b/frontend/static/js/views/profiles/ProfilePageView.js @@ -26,4 +26,4 @@ export default class extends AbstractView { `; } -} \ No newline at end of file +} diff --git a/frontend/static/js/views/profiles/ProfilesView.js b/frontend/static/js/views/profiles/ProfilesView.js deleted file mode 100644 index d07284e..0000000 --- a/frontend/static/js/views/profiles/ProfilesView.js +++ /dev/null @@ -1,40 +0,0 @@ -import AbstractView from "../AbstractView.js"; -import { client } from "../../index.js" - -export default class extends AbstractView { - constructor(params) { - super(params, "Profiles"); - } - - async postInit() - { - let profiles = await client.profiles.all() - let profile_list_element = document.getElementById("profile-list") - - profiles.forEach((profile) => { - let profile_element = document.createElement("div"); - profile_element.className = "item"; - - let avatar = document.createElement("img"); - avatar.src = profile.avatar_url; - - let username = document.createElement("a"); - username.href = `/profiles/${profile.user_id}`; - username.appendChild(document.createTextNode(profile.username)); - - profile_element.appendChild(avatar); - profile_element.appendChild(username); - - profile_list_element.appendChild(profile_element) - }); - } - - async getHtml() { - return ` - -
- -
- `; - } -} \ No newline at end of file