diff --git a/src/components/DesktopCard.tsx b/src/components/DesktopCard.tsx
index 705cbed..122521b 100644
--- a/src/components/DesktopCard.tsx
+++ b/src/components/DesktopCard.tsx
@@ -1,5 +1,4 @@
-import { useMutation, useQueryClient } from "@tanstack/react-query";
-import api from "../utils/api";
+import { useQueryClient } from "@tanstack/react-query";
import { IServer } from "../types/IServer";
import useModalStore from "../stores/useModalStore";
import CreateSessionModal from "./modals/CreateSessionModal";
@@ -33,22 +32,13 @@ export default function DesktopCard({ server }: IDesktopCardProps) {
// onMutate: () => queryClient.invalidateQueries({ queryKey: ["sessions"] }),
// });
- const { mutate: endSession } = useMutation({
- mutationKey: ["sessions", server.sessions?.[0]?.id],
- mutationFn: () =>
- api.put(`sessions/${server.sessions?.[0]?.id}`, {
- json: { status: "ending" },
- }),
- onMutate: () => queryClient.invalidateQueries({ queryKey: ["sessions"] }),
- });
-
async function handleClickCreateSession() {
setPosition("right");
setModal();
}
return (
-
+
setModal()}
>
-
+
-
-
-
+
+
+
{server.name}
-
{server.location}
+
{server.location}
-
+
{server.sessions?.[0]?.status === "started" ? (
-
+
{
setModal();
}}
- className='flex gap-[0.556vw] items-center'
+ className="flex gap-[0.556vw] items-center"
>
-
+
- Идёт сеанс
-
+ Идёт сеанс
+
) : server.status === "offline" ? (
-
-
+
+
Проверьте соединение
) : (
-
-
+
+
-
Создать сеанс
+
Создать сеанс
)}
diff --git a/src/components/modals/CreateSessionModal.tsx b/src/components/modals/CreateSessionModal.tsx
index c72a859..babf83e 100644
--- a/src/components/modals/CreateSessionModal.tsx
+++ b/src/components/modals/CreateSessionModal.tsx
@@ -10,7 +10,7 @@ import NewInput from "../NewInput.tsx";
import StartSessionIcon from "../icons/StartSessionIcon.tsx";
import NewButton from "../NewButton.tsx";
import ProjectSelector from "../ProjectSelector.tsx";
-
+import { useQueryClient, useMutation } from "@tanstack/react-query";
interface Props {
servers: IServer[] | undefined;
targetServer: IServer | null;
@@ -28,89 +28,99 @@ export default function CreateSessionModal({
targetServer
);
const [selectedApp, setSelectedApp] = useState(null);
+ const queryClient = useQueryClient();
- async function createClient() {
- return await api
- .post("clients", {
- json: {
- name,
- phone,
- email,
- },
- })
- .json();
- }
+ const { mutate: createClient } = useMutation({
+ mutationFn: () => {
+ return api
+ .post("clients", {
+ json: {
+ name,
+ phone,
+ email,
+ },
+ })
+ .json();
+ },
+ });
- async function createSession(clientId: string) {
- return await api
- .post("sessions", {
- json: {
- clientId,
- serverId: selectedServer?.id,
- appId: selectedApp?.id,
- },
- })
- .json();
- }
+ const { mutate: createSession } = useMutation({
+ mutationFn: (clientId: string) => {
+ return api
+ .post("sessions", {
+ json: {
+ clientId,
+ serverId: selectedServer?.id,
+ appId: selectedApp?.id,
+ },
+ })
+ .json();
+ },
+ onSuccess: () => {
+ queryClient.invalidateQueries({ queryKey: ["sessions"] });
+ queryClient.invalidateQueries({ queryKey: ["servers"] });
+ setModal(null);
+ },
+ });
async function handleClickCreateSession(e: React.FormEvent) {
e.preventDefault();
if (!name || !phone || !selectedServer || !selectedApp) return;
- try {
- const client = await createClient();
- await createSession(client.id);
-
- setModal(null);
- } catch (error) {
- console.log(error);
- }
+ createClient(undefined, {
+ onSuccess: (client) => {
+ createSession(client.id);
+ },
+ onError: (error) => {
+ console.log(error);
+ },
+ });
}
const ref = useRef(null);
return (