CreoCot commited on
Commit
951c0af
·
1 Parent(s): 3717d81

frontend/backend connection

Browse files
src/App.tsx CHANGED
@@ -1,8 +1,6 @@
1
- // In App.tsx
2
- import React, { lazy, Suspense, useEffect } from "react";
3
  import { BrowserRouter as Router, Routes, Route } from "react-router-dom";
4
  import NotFoundPage from "./pages/NotFoundPage";
5
- import Sidebar from "./components/Sidebar/Sidebar";
6
  import Messenger from "./pages/Messenger/Messenger";
7
 
8
  function App() {
@@ -41,9 +39,8 @@ function App() {
41
  }
42
  >
43
  <Routes>
44
- <Route path={"sidebar"} element={<Sidebar />} />
45
  <Route path={"/"} element={<Messenger />} />
46
- <Route path={"/chats/:chatId"} element={<Messenger />} />
47
  <Route path={"*"} element={<NotFoundPage />} />
48
  </Routes>
49
  </Suspense>
 
1
+ import React, { Suspense, useEffect } from "react";
 
2
  import { BrowserRouter as Router, Routes, Route } from "react-router-dom";
3
  import NotFoundPage from "./pages/NotFoundPage";
 
4
  import Messenger from "./pages/Messenger/Messenger";
5
 
6
  function App() {
 
39
  }
40
  >
41
  <Routes>
 
42
  <Route path={"/"} element={<Messenger />} />
43
+ <Route path={"/chats/id=:chatId"} element={<Messenger />} />
44
  <Route path={"*"} element={<NotFoundPage />} />
45
  </Routes>
46
  </Suspense>
src/components/Chat/Chat.tsx CHANGED
@@ -37,7 +37,7 @@ function Chat({ openFileViewer }: Readonly<ChatProps>) {
37
  const response = await fetch("/new_chat", { method: "POST" });
38
  if (response.ok) {
39
  const data = await response.json();
40
- navigate(`/chats/${data.chatId}`, { replace: true });
41
  }
42
  } catch (error) {
43
  console.error("Failed to create a new chat:", error);
@@ -48,16 +48,20 @@ function Chat({ openFileViewer }: Readonly<ChatProps>) {
48
  }, [chat_id, navigate]);
49
 
50
  useEffect(() => {
 
51
  if (chat_id) {
52
  const fetchChatHistory = async () => {
53
  try {
54
- const response = await fetch(`/api/chats/${chat_id}`);
55
  if (response.ok) {
56
  const data = await response.json();
 
57
  setMessages(data.messages || []);
 
 
58
  }
59
  } catch (error) {
60
- console.error("Failed to fetch chat history:", error);
61
  }
62
  };
63
  fetchChatHistory();
 
37
  const response = await fetch("/new_chat", { method: "POST" });
38
  if (response.ok) {
39
  const data = await response.json();
40
+ navigate(`/chats/id=${data.chat_id}`, { replace: true });
41
  }
42
  } catch (error) {
43
  console.error("Failed to create a new chat:", error);
 
48
  }, [chat_id, navigate]);
49
 
50
  useEffect(() => {
51
+ console.log(`Fetching history for chatId: ${chat_id}`);
52
  if (chat_id) {
53
  const fetchChatHistory = async () => {
54
  try {
55
+ const response = await fetch(`/chats/id=${chat_id}`);
56
  if (response.ok) {
57
  const data = await response.json();
58
+ console.log(`Fetched messages for ${chat_id}:`, data.messages);
59
  setMessages(data.messages || []);
60
+ } else {
61
+ console.error(`Fetch failed with status: ${response.status}`);
62
  }
63
  } catch (error) {
64
+ console.error("Fetch error:", error);
65
  }
66
  };
67
  fetchChatHistory();
src/components/Sidebar/Sidebar.tsx CHANGED
@@ -7,53 +7,58 @@ import ThemeSwitcher from "../ThemeSwitcher/ThemeSwitcher";
7
  import { useNavigate } from "react-router-dom";
8
 
9
  interface ChatButton {
 
10
  title: string;
11
  }
12
 
13
- interface ChatGroup {
14
- title: string;
15
- chats: ChatButton[];
16
- }
17
-
18
  function Sidebar() {
19
  const [isSearchExpanded, setIsSearchExpanded] = useState(false);
 
20
  const handleSearchToggle = (expanded: boolean) => {
21
  setIsSearchExpanded(expanded);
22
  };
23
- const [chatGroups, setChatGroups] = useState<ChatGroup[]>([]);
24
  const [chats, setChats] = useState<ChatButton[]>([]);
25
- const navigate = useNavigate();
26
- const baseButtonHeight = "40px"; // Базовая высота кнопки
27
 
28
  useEffect(() => {
29
- const fetchChatGroups = async () => {
30
- const response = await fetch("/api/user_chats");
31
- if (response.ok) {
32
- const data = await response.json();
33
- setChatGroups(data);
34
- } else {
35
- console.error("Failed to fetch chats groups");
 
36
  }
37
  };
38
 
39
- fetchChatGroups();
40
  }, []);
41
 
42
- const refetchChats = async () => {
43
- const response = await fetch("/api/user_chats");
44
- if (response.ok) {
45
- const data = await response.json();
46
- setChatGroups(data);
47
- }
48
- };
49
-
50
  const handleAddChat = async () => {
51
- const response = await fetch("/new_chat", { method: "POST" });
52
- if (response.ok) {
53
- const data = await response.json();
54
- navigate(`/chats/${data.chat_id}`);
55
- refetchChats();
 
 
 
 
56
  }
 
 
 
 
 
 
 
 
 
 
 
 
57
  };
58
 
59
  const handleSearchSubmit = (query: string) => {
@@ -70,12 +75,13 @@ function Sidebar() {
70
  }
71
  >
72
  <Button
73
- children={"+ Add chat"}
74
  height={baseButtonHeight}
75
  width="100%"
76
  borderRadius="round"
77
  onClick={handleAddChat}
78
- />
 
 
79
  </div>
80
  <nav className={styles.searchButton}>
81
  <SearchButton
@@ -87,7 +93,7 @@ function Sidebar() {
87
  </div>
88
  <div className={styles.chatsContainer}>
89
  {chats.map((chat, index) => (
90
- <SideBarChatButton key={index} label={chat.title} />
91
  ))}
92
  </div>
93
 
 
7
  import { useNavigate } from "react-router-dom";
8
 
9
  interface ChatButton {
10
+ id: string;
11
  title: string;
12
  }
13
 
 
 
 
 
 
14
  function Sidebar() {
15
  const [isSearchExpanded, setIsSearchExpanded] = useState(false);
16
+ const navigate = useNavigate();
17
  const handleSearchToggle = (expanded: boolean) => {
18
  setIsSearchExpanded(expanded);
19
  };
20
+
21
  const [chats, setChats] = useState<ChatButton[]>([]);
22
+ const baseButtonHeight = "40px";
 
23
 
24
  useEffect(() => {
25
+ const fetchChats = async () => {
26
+ const response = await fetch("/list_chats");
27
+ if (!response.ok) {
28
+ throw new Error(`Error, status: ${response.status}`);
29
+ }
30
+ const data = await response.json();
31
+ if (data.chats) {
32
+ setChats(data.chats);
33
  }
34
  };
35
 
36
+ fetchChats();
37
  }, []);
38
 
 
 
 
 
 
 
 
 
39
  const handleAddChat = async () => {
40
+ const response = await fetch("/new_chat", {
41
+ method: "POST",
42
+ headers: { "Content-Type": "application/json" },
43
+ body: JSON.stringify({ title: "New chat" }),
44
+ });
45
+
46
+ if (!response.ok) {
47
+ console.error(`New chat failed: ${response.status}`);
48
+ return;
49
  }
50
+ const data = await response.json();
51
+ navigate(`/chats/id=${data.chat_id}`);
52
+
53
+ const fetchChats = async () => {
54
+ const res = await fetch("/list_chats");
55
+ if (res.ok) {
56
+ const chatData = await res.json();
57
+ console.log("Updated chats:", chatData.chats);
58
+ setChats(chatData.chats || []);
59
+ }
60
+ };
61
+ fetchChats();
62
  };
63
 
64
  const handleSearchSubmit = (query: string) => {
 
75
  }
76
  >
77
  <Button
 
78
  height={baseButtonHeight}
79
  width="100%"
80
  borderRadius="round"
81
  onClick={handleAddChat}
82
+ >
83
+ + Add chat
84
+ </Button>
85
  </div>
86
  <nav className={styles.searchButton}>
87
  <SearchButton
 
93
  </div>
94
  <div className={styles.chatsContainer}>
95
  {chats.map((chat, index) => (
96
+ <SideBarChatButton key={index} chatId={chat.id} title={chat.title} />
97
  ))}
98
  </div>
99
 
src/components/buttons/SideBarChatButton/SideBarChatButton.tsx CHANGED
@@ -4,8 +4,8 @@ import styles from "./SideBarChatButton.module.css";
4
  import Text from "../../Text/Text";
5
 
6
  interface ChatButtonProps {
7
- label: string;
8
- key: number;
9
  }
10
 
11
  function SideBarChatButton(props: Readonly<ChatButtonProps>) {
@@ -13,8 +13,7 @@ function SideBarChatButton(props: Readonly<ChatButtonProps>) {
13
  const [isActive, setIsActive] = useState(false);
14
 
15
  const handleClick = () => {
16
- navigate(`/chat/${props.key}`);
17
- setIsActive(true);
18
  };
19
 
20
  const handleMouseDown = () => {
@@ -30,8 +29,14 @@ function SideBarChatButton(props: Readonly<ChatButtonProps>) {
30
  };
31
 
32
  return (
33
- <button className={styles.chatButton}>
34
- <Text colorVariant={isActive ? "button" : "primary"}>{props.label}</Text>
 
 
 
 
 
 
35
  </button>
36
  );
37
  }
 
4
  import Text from "../../Text/Text";
5
 
6
  interface ChatButtonProps {
7
+ title: string;
8
+ chatId: string;
9
  }
10
 
11
  function SideBarChatButton(props: Readonly<ChatButtonProps>) {
 
13
  const [isActive, setIsActive] = useState(false);
14
 
15
  const handleClick = () => {
16
+ navigate(`/chats/id=${props.chatId}`);
 
17
  };
18
 
19
  const handleMouseDown = () => {
 
29
  };
30
 
31
  return (
32
+ <button
33
+ className={styles.chatButton}
34
+ onClick={handleClick}
35
+ onMouseDown={handleMouseDown}
36
+ onMouseUp={handleMouseUp}
37
+ onMouseLeave={handleMouseLeave}
38
+ >
39
+ <Text colorVariant={isActive ? "button" : "primary"}>{props.title}</Text>
40
  </button>
41
  );
42
  }
src/pages/Messenger/Messenger.tsx CHANGED
@@ -1,19 +1,21 @@
1
  import Chat from "../../components/Chat/Chat";
2
  import Sidebar from "../../components/Sidebar/Sidebar";
3
  import styles from "./Messenger.module.css";
4
-
5
 
6
  function Messenger() {
 
 
7
  return (
8
  <body className={styles.messenger}>
9
- <aside className={styles.sidebar}>
10
- <Sidebar/>
11
- </aside>
12
- <div className={styles.chatContainer}>
13
- <Chat/>
14
- </div>
15
  </body>
16
  );
17
  }
18
 
19
- export default Messenger;
 
1
  import Chat from "../../components/Chat/Chat";
2
  import Sidebar from "../../components/Sidebar/Sidebar";
3
  import styles from "./Messenger.module.css";
4
+ import { useParams } from "react-router-dom";
5
 
6
  function Messenger() {
7
+ const { chatId } = useParams();
8
+
9
  return (
10
  <body className={styles.messenger}>
11
+ <aside className={styles.sidebar}>
12
+ <Sidebar />
13
+ </aside>
14
+ <div className={styles.chatContainer}>
15
+ <Chat key={chatId ?? "default"} />
16
+ </div>
17
  </body>
18
  );
19
  }
20
 
21
+ export default Messenger;
vite.config.ts CHANGED
@@ -23,6 +23,14 @@ export default defineConfig({
23
  target: "http://127.0.0.1:5050",
24
  changeOrigin: true,
25
  },
 
 
 
 
 
 
 
 
26
  },
27
  },
28
  });
 
23
  target: "http://127.0.0.1:5050",
24
  changeOrigin: true,
25
  },
26
+ "/new_chat": {
27
+ target: "http://127.0.0.1:5050",
28
+ changeOrigin: true,
29
+ },
30
+ "/chats/": {
31
+ target: "http://127.0.0.1:5050",
32
+ changeOrigin: true,
33
+ },
34
  },
35
  },
36
  });