From 143ba63e094f9b5a862e526f91c6dea84af9069a Mon Sep 17 00:00:00 2001 From: inmake Date: Mon, 24 Mar 2025 12:24:58 +0500 Subject: [PATCH] upd --- src/components/CustomSelect.tsx | 27 ++++ src/components/DesktopCard.tsx | 32 ++-- src/components/DesktopSelect.tsx | 49 +++--- src/components/Select.tsx | 45 ++++-- src/components/modals/CreateSessionModal.tsx | 162 ++++++++++++------- src/pages/DashboardPage.tsx | 18 +-- src/pages/LoginPage.tsx | 19 ++- src/pages/ProtectedPage.tsx | 2 +- src/types/ICompany.ts | 6 +- src/types/IServer.ts | 4 +- src/types/IUser.ts | 2 +- 11 files changed, 232 insertions(+), 134 deletions(-) create mode 100644 src/components/CustomSelect.tsx diff --git a/src/components/CustomSelect.tsx b/src/components/CustomSelect.tsx new file mode 100644 index 0000000..8e08b73 --- /dev/null +++ b/src/components/CustomSelect.tsx @@ -0,0 +1,27 @@ +import { useEffect, useState } from "react"; + +interface Props { + options: string[]; + customOption?: React.ReactNode; +} + +function CustomSelect({ options, customOption }: Props) { + const [selectedOption, setSelectedOption] = useState(null); + + // Сбрасываем selectedOption при изменении options + useEffect(() => { + setSelectedOption(null); + }, [options]); + + return ( +
+ {options.map((option, index) => ( +
setSelectedOption(option)}> + {customOption ? customOption(option) : option} +
+ ))} +
+ ); +} + +export default CustomSelect; \ No newline at end of file diff --git a/src/components/DesktopCard.tsx b/src/components/DesktopCard.tsx index cb99c46..30d7e78 100644 --- a/src/components/DesktopCard.tsx +++ b/src/components/DesktopCard.tsx @@ -4,25 +4,28 @@ import MoreIcon from "./icons/MoreIcon"; import api from "../utils/api"; import PlusIcon from "./icons/PlusIcon"; import { IServer } from "../types/IServer"; +import useModalStore from "../stores/useModalStore"; +import CreateSessionModal from "./modals/CreateSessionModal"; interface IDesktopCardProps { server: IServer; } export default function DesktopCard({ server }: IDesktopCardProps) { + const { setModal, setPosition } = useModalStore(); const queryClient = useQueryClient(); - const { mutate: createSession } = useMutation({ - mutationFn: () => - api.post(`sessions`, { - json: { - serverId: server.id, - clientId: "abcfd570-2fa8-4f55-957b-5007f84f8f96", - appId: "b8a9995c-a799-4593-8f96-03942050cb21", - }, - }), - onMutate: () => queryClient.invalidateQueries({ queryKey: ["sessions"] }), - }); + // const { mutate: createSession } = useMutation({ + // mutationFn: () => + // api.post(`sessions`, { + // json: { + // serverId: server.id, + // clientId: "abcfd570-2fa8-4f55-957b-5007f84f8f96", + // appId: "b8a9995c-a799-4593-8f96-03942050cb21", + // }, + // }), + // onMutate: () => queryClient.invalidateQueries({ queryKey: ["sessions"] }), + // }); const { mutate: endSession } = useMutation({ mutationKey: ["sessions", server.sessions?.[0]?.id], @@ -33,6 +36,11 @@ export default function DesktopCard({ server }: IDesktopCardProps) { onMutate: () => queryClient.invalidateQueries({ queryKey: ["sessions"] }), }); + async function handleClickCreateSession() { + setPosition("right"); + setModal(); + } + return (
@@ -102,7 +110,7 @@ export default function DesktopCard({ server }: IDesktopCardProps) { -
-
+ ); } diff --git a/src/pages/DashboardPage.tsx b/src/pages/DashboardPage.tsx index 29096cc..72484df 100644 --- a/src/pages/DashboardPage.tsx +++ b/src/pages/DashboardPage.tsx @@ -4,14 +4,11 @@ import api from "../utils/api"; import Sidebar from "../components/Sidebar"; import { IServer } from "../types/IServer"; import DesktopCard from "../components/DesktopCard"; -import Button from "../components/Button"; -import useModalStore from "../stores/useModalStore"; -import CreateSessionModal from "../components/modals/CreateSessionModal"; function DashboardPage() { const { data: me } = useQuery({ queryKey: ["me"], - queryFn: () => api.get("users/me").json(), + queryFn: () => api.get("auth/me").json(), }); const { data: servers } = useQuery({ @@ -21,13 +18,6 @@ function DashboardPage() { refetchInterval: 1000, }); - const { setModal, setPosition } = useModalStore(); - - function handleClickCreateSession() { - setPosition("right"); - setModal(); - } - // async function logout() { // return await api.get("auth/logout").json(); // } @@ -42,7 +32,7 @@ function DashboardPage() { // } return ( -
+
@@ -62,9 +52,7 @@ function DashboardPage() { ))}
-
- -
+
...
); diff --git a/src/pages/LoginPage.tsx b/src/pages/LoginPage.tsx index f371428..d201d51 100644 --- a/src/pages/LoginPage.tsx +++ b/src/pages/LoginPage.tsx @@ -10,7 +10,7 @@ import { useNavigate } from "react-router"; function LoginPage() { const navigate = useNavigate(); - const { token, setToken } = useAuthStore(); + const { setToken } = useAuthStore(); const [email, setEmail] = useState(""); const [password, setPassword] = useState(""); @@ -28,16 +28,20 @@ function LoginPage() { async function handleSubmit(e: React.FormEvent) { e.preventDefault(); + let result; + try { - const { token } = await login(); - setToken(token); - - toast.success("Вы успешно вошли в систему"); - - navigate("/"); + result = await login(); } catch (error) { toast.error((await (error as HTTPError).response.json()).error); } + + if (!result?.token) { + return; + } + + setToken(result.token); + navigate("/"); } return ( @@ -45,7 +49,6 @@ function LoginPage() {
logo -

{token}

diff --git a/src/pages/ProtectedPage.tsx b/src/pages/ProtectedPage.tsx index 83d5571..63e8a5a 100644 --- a/src/pages/ProtectedPage.tsx +++ b/src/pages/ProtectedPage.tsx @@ -9,7 +9,7 @@ function ProtectedPage() { const { data: user, isLoading } = useQuery({ queryKey: ['me'], - queryFn: () => api.get('users/me').json(), + queryFn: () => api.get('auth/me').json(), enabled: !!token, }); diff --git a/src/types/ICompany.ts b/src/types/ICompany.ts index 8157d64..90a52ee 100644 --- a/src/types/ICompany.ts +++ b/src/types/ICompany.ts @@ -5,7 +5,7 @@ import { IUser } from "./IUser"; export interface ICompany { id: string; name: string; - apps: IApp[]; - servers: IServer[]; - users: IUser[]; + apps?: IApp[]; + servers?: IServer[]; + users?: IUser[]; } diff --git a/src/types/IServer.ts b/src/types/IServer.ts index b816d38..77972b9 100644 --- a/src/types/IServer.ts +++ b/src/types/IServer.ts @@ -1,4 +1,5 @@ -import { ISession } from './ISession'; +import { IApp } from "./IApp"; +import { ISession } from "./ISession"; export interface IServer { id: string; @@ -7,4 +8,5 @@ export interface IServer { location: string; companyId: string; sessions?: ISession[]; + apps?: IApp[]; } diff --git a/src/types/IUser.ts b/src/types/IUser.ts index 5cab8ba..ad0b0c4 100644 --- a/src/types/IUser.ts +++ b/src/types/IUser.ts @@ -5,5 +5,5 @@ export interface IUser { email: string; fullname: string; companyId: string; - company: ICompany + company?: ICompany; }