From 66620501d1378fdf270af47a3e258182c1609339 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A2=D0=B5=D1=80=D0=B5=D0=BD=D1=82=D1=8C=D0=B5=D0=B2=20?= =?UTF-8?q?=D0=92=D0=B0=D0=B4=D0=B8=D0=BC=20=D0=90=D0=BB=D0=B5=D0=BA=D1=81?= =?UTF-8?q?=D0=B5=D0=B5=D0=B2=D0=B8=D1=87?= Date: Tue, 12 Mar 2024 14:31:48 +0300 Subject: [PATCH] =?UTF-8?q?GL-15:=20=D0=A4=D0=B8=D0=BA=D1=81=20=D1=83?= =?UTF-8?q?=D0=B2=D0=B5=D0=B4=D0=BE=D0=BC=D0=BB=D0=B5=D0=BD=D0=B8=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lib/useConnectionHub.ts | 4 +- .../ui/InstallClientForm.tsx | 6 +- src/shared/hooks/useAuthorization.ts | 53 +++++++------ src/shared/hooks/useIntegraions.ts | 6 +- src/shared/hooks/useProfiles.ts | 78 +++++++++---------- src/shared/ui/Header/index.tsx | 22 +++++- .../client-hub/lib/useConnectionHub.ts | 10 +-- 7 files changed, 99 insertions(+), 80 deletions(-) diff --git a/src/features/install-client-form/lib/useConnectionHub.ts b/src/features/install-client-form/lib/useConnectionHub.ts index 0fbd95a..3739c31 100644 --- a/src/features/install-client-form/lib/useConnectionHub.ts +++ b/src/features/install-client-form/lib/useConnectionHub.ts @@ -9,7 +9,7 @@ const CONNECTION_URL = (token: string) => `${process.env.NEXT_PUBLIC_BASE_URL}/ws/launcher/build?access_token=${token}`; export const useConnectionHub = () => { - const toast = useToast(); + const { toast } = useToast(); const accessToken = getStorageAccessToken(); const [connectionHub, setConnectionHub] = useState(null); @@ -39,7 +39,7 @@ export const useConnectionHub = () => { }); connection.on("Message", (message) => { - toast.toast({ + toast({ title: message, }); }); diff --git a/src/features/install-client-form/ui/InstallClientForm.tsx b/src/features/install-client-form/ui/InstallClientForm.tsx index 54a21de..26bb102 100644 --- a/src/features/install-client-form/ui/InstallClientForm.tsx +++ b/src/features/install-client-form/ui/InstallClientForm.tsx @@ -24,7 +24,7 @@ interface InstallClientFormProps extends React.HTMLAttributes {} export function InstallClientForm({ className, ...props }: InstallClientFormProps) { const { connectionHub, process, percent } = useConnectionHub(); const { data: branches } = useGithubLauncherVersions(); - const toast = useToast(); + const { toast } = useToast(); const form = useForm({ values: { branch: "", host: getApiBaseUrl() || "", folder: "" }, @@ -38,13 +38,13 @@ export function InstallClientForm({ className, ...props }: InstallClientFormProp connectionHub ?.invoke("Download", data.branch, data.host, data.folder) .then(() => { - toast.toast({ + toast({ title: "Успешно", description: "Лаунчер успешно собран!", }); }) .catch((error) => { - toast.toast({ + toast({ variant: "destructive", title: "Ошибка!", description: JSON.stringify(error), diff --git a/src/shared/hooks/useAuthorization.ts b/src/shared/hooks/useAuthorization.ts index 02da5c8..138d513 100644 --- a/src/shared/hooks/useAuthorization.ts +++ b/src/shared/hooks/useAuthorization.ts @@ -1,33 +1,33 @@ -import { useRouter } from 'next/navigation'; +import { useRouter } from "next/navigation"; -import { useMutation } from '@tanstack/react-query'; +import { useMutation } from "@tanstack/react-query"; -import { isAxiosError } from 'axios'; +import { isAxiosError } from "axios"; -import { TPostSignInRequest, TPostSignUpRequest } from '@/shared/api/contracts'; -import { DASHBOARD_PAGES } from '@/shared/routes'; -import { authService } from '@/shared/services'; -import { useToast } from '@/shared/ui/use-toast'; +import { TPostSignInRequest, TPostSignUpRequest } from "@/shared/api/contracts"; +import { DASHBOARD_PAGES } from "@/shared/routes"; +import { authService } from "@/shared/services"; +import { useToast } from "@/shared/ui/use-toast"; export const useRegistration = () => { const route = useRouter(); - const toast = useToast(); + const { toast } = useToast(); return useMutation({ - mutationKey: ['signup'], + mutationKey: ["signup"], mutationFn: (data: TPostSignUpRequest) => authService.signUp(data), onSuccess: () => { - toast.toast({ - title: 'Успешная регистрация', - description: 'Добро пожаловать в платформу', + toast({ + title: "Успешная регистрация", + description: "Добро пожаловать в платформу", }); route.push(DASHBOARD_PAGES.HOME); }, onError: (error) => { if (isAxiosError(error)) { - toast.toast({ - variant: 'destructive', - title: (error.response && error.response.data.message) || 'Ошибка!', + toast({ + variant: "destructive", + title: (error.response && error.response.data.message) || "Ошибка!", description: error.response && error.response.data.errors[0], }); } @@ -37,23 +37,26 @@ export const useRegistration = () => { export const useLogin = () => { const route = useRouter(); - const toast = useToast(); + const { toast } = useToast(); return useMutation({ - mutationKey: ['signin'], + mutationKey: ["signin"], mutationFn: (data: TPostSignInRequest) => authService.signIn(data), onSuccess: () => { - toast.toast({ - title: 'Успешная авторизация', - description: 'Добро пожаловать в платформу', + toast({ + title: "Успешная авторизация", + description: "Добро пожаловать в платформу", }); route.push(DASHBOARD_PAGES.HOME); }, - onError: () => { - toast.toast({ - title: 'Упс!', - description: 'Проверьте правильность введенных данных', - }); + onError: (error) => { + if (isAxiosError(error)) { + toast({ + variant: "destructive", + title: (error.response && error.response.data.message) || "Ошибка!", + description: error.response && error.response.data.errors[0], + }); + } }, }); }; diff --git a/src/shared/hooks/useIntegraions.ts b/src/shared/hooks/useIntegraions.ts index f3eac61..ca85dbc 100644 --- a/src/shared/hooks/useIntegraions.ts +++ b/src/shared/hooks/useIntegraions.ts @@ -33,7 +33,7 @@ export const useActiveAuthIntegrations = () => { }; export const useEditIntegration = () => { - const toast = useToast(); + const { toast } = useToast(); const queryClient = useQueryClient(); return useMutation({ @@ -45,14 +45,14 @@ export const useEditIntegration = () => { }, onSuccess: async (data) => { await queryClient.setQueryData(["integration"], () => null); - toast.toast({ + toast({ title: "Успешно", description: data.message, }); }, onError: (error) => { if (isAxiosError(error)) { - toast.toast({ + toast({ variant: "destructive", title: (error.response && error.response.data.message) || "Ошибка!", description: error.response && error.response.data.errors[0], diff --git a/src/shared/hooks/useProfiles.ts b/src/shared/hooks/useProfiles.ts index 735b762..8cd3f11 100644 --- a/src/shared/hooks/useProfiles.ts +++ b/src/shared/hooks/useProfiles.ts @@ -1,6 +1,6 @@ -import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'; +import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; -import { isAxiosError } from 'axios'; +import { isAxiosError } from "axios"; import { ProfileBaseEntity, @@ -9,13 +9,13 @@ import { TGetProfileRequest, TPostProfilesRequest, TPutProfileRequest, -} from '@/shared/api/contracts'; -import { profileService } from '@/shared/services/ProfileService'; -import { useToast } from '@/shared/ui/use-toast'; +} from "@/shared/api/contracts"; +import { profileService } from "@/shared/services/ProfileService"; +import { useToast } from "@/shared/ui/use-toast"; export const useProfiles = () => { const { data, isLoading } = useQuery({ - queryKey: ['profiles'], + queryKey: ["profiles"], queryFn: () => profileService.getProfiles(), }); @@ -23,20 +23,20 @@ export const useProfiles = () => { }; export const useProfile = () => { - const toast = useToast(); + const { toast } = useToast(); const queryClient = useQueryClient(); return useMutation({ - mutationKey: ['get-profile'], + mutationKey: ["get-profile"], mutationFn: (data: TGetProfileRequest) => profileService.getProfile(data), onSettled: async () => { - await queryClient.invalidateQueries({ queryKey: ['profiles'] }); + await queryClient.invalidateQueries({ queryKey: ["profiles"] }); }, onError: (error) => { if (isAxiosError(error)) { - toast.toast({ - variant: 'destructive', - title: (error.response && error.response.data.message) || 'Ошибка!', + toast({ + variant: "destructive", + title: (error.response && error.response.data.message) || "Ошибка!", description: error.response && error.response.data.errors[0], }); } @@ -46,33 +46,33 @@ export const useProfile = () => { export const useCurrentProfile = () => { const { data } = useQuery({ - queryKey: ['profile'], + queryKey: ["profile"], }); return data; }; export const useCreateProfile = () => { - const toast = useToast(); + const { toast } = useToast(); const queryClient = useQueryClient(); return useMutation({ - mutationKey: ['create-profile'], + mutationKey: ["create-profile"], mutationFn: (data: TPostProfilesRequest) => profileService.createProfile(data), onSettled: async () => { - await queryClient.invalidateQueries({ queryKey: ['profiles'] }); + await queryClient.invalidateQueries({ queryKey: ["profiles"] }); }, onSuccess: (data) => { - toast.toast({ - title: 'Успешно', + toast({ + title: "Успешно", description: `Профиль "${data.data.name}" успешно создан`, }); }, onError: (error) => { if (isAxiosError(error)) { - toast.toast({ - variant: 'destructive', - title: (error.response && error.response.data.message) || 'Ошибка!', + toast({ + variant: "destructive", + title: (error.response && error.response.data.message) || "Ошибка!", description: error.response && error.response.data.errors[0], }); } @@ -81,22 +81,22 @@ export const useCreateProfile = () => { }; export const useEditProfile = () => { - const toast = useToast(); + const { toast } = useToast(); return useMutation({ - mutationKey: ['edit-profile'], + mutationKey: ["edit-profile"], mutationFn: (data: TPutProfileRequest) => profileService.editProfile(data), onSuccess: (data) => { - toast.toast({ - title: 'Успешно', + toast({ + title: "Успешно", description: `Профиль "${data.data.name}" успешно обновлен`, }); }, onError: (error) => { if (isAxiosError(error)) { - toast.toast({ - variant: 'destructive', - title: (error.response && error.response.data.message) || 'Ошибка!', + toast({ + variant: "destructive", + title: (error.response && error.response.data.message) || "Ошибка!", description: error.response && error.response.data.errors[0], }); } @@ -109,23 +109,23 @@ export const useDeleteProfile = () => { const queryClient = useQueryClient(); return useMutation({ - mutationKey: ['delete-profile'], + mutationKey: ["delete-profile"], mutationFn: (body: TDeleteProfileRequest) => profileService.deleteProfile(body), onSettled: async () => { - await queryClient.invalidateQueries({ queryKey: ['profiles'] }); - await queryClient.setQueryData(['profile'], () => null); + await queryClient.invalidateQueries({ queryKey: ["profiles"] }); + await queryClient.setQueryData(["profile"], () => null); }, onSuccess: async (data) => { toast({ - title: 'Успешно', + title: "Успешно", description: data.message, }); }, onError: (error) => { if (isAxiosError(error)) { toast({ - variant: 'destructive', - title: (error.response && error.response.data.message) || 'Ошибка!', + variant: "destructive", + title: (error.response && error.response.data.message) || "Ошибка!", description: error.response && error.response.data.errors[0], }); } @@ -138,22 +138,22 @@ export const useDeleteProfiles = () => { const queryClient = useQueryClient(); return useMutation({ - mutationKey: ['delete-profiles'], + mutationKey: ["delete-profiles"], mutationFn: (body: TDeleteProfilesRequest) => profileService.deleteProfiles(body), onSettled: async () => { - await queryClient.invalidateQueries({ queryKey: ['profiles'] }); + await queryClient.invalidateQueries({ queryKey: ["profiles"] }); }, onSuccess: async (data) => { toast({ - title: 'Успешно', + title: "Успешно", description: data.message, }); }, onError: (error) => { if (isAxiosError(error)) { toast({ - variant: 'destructive', - title: (error.response && error.response.data.message) || 'Ошибка!', + variant: "destructive", + title: (error.response && error.response.data.message) || "Ошибка!", description: error.response && error.response.data.errors[0], }); } diff --git a/src/shared/ui/Header/index.tsx b/src/shared/ui/Header/index.tsx index d6c273a..6f873e6 100644 --- a/src/shared/ui/Header/index.tsx +++ b/src/shared/ui/Header/index.tsx @@ -3,11 +3,12 @@ import React from "react"; import Link from "next/link"; -import { usePathname } from "next/navigation"; -import { DASHBOARD_PAGES } from "@/shared/routes"; +import { usePathname, useRouter } from "next/navigation"; +import { AUTH_PAGES, DASHBOARD_PAGES } from "@/shared/routes"; import { cn } from "@/shared/lib/utils"; -import { BoxesIcon, PlusIcon } from "lucide-react"; +import { BoxesIcon, LogOutIcon, PlusIcon } from "lucide-react"; import { Tooltip, TooltipContent, TooltipTrigger } from "@/shared/ui/tooltip"; +import { removeStorageProfile, removeStorageTokens } from "@/shared/services"; const menu = [ { @@ -59,6 +60,13 @@ const menu = [ export function Header() { const pathname = usePathname(); + const router = useRouter(); + + const destroySession = () => { + removeStorageProfile(); + removeStorageTokens(); + router.push(AUTH_PAGES.SIGN_IN); + }; return ( ); } diff --git a/src/widgets/client-hub/lib/useConnectionHub.ts b/src/widgets/client-hub/lib/useConnectionHub.ts index 142d628..1947cb5 100644 --- a/src/widgets/client-hub/lib/useConnectionHub.ts +++ b/src/widgets/client-hub/lib/useConnectionHub.ts @@ -14,7 +14,7 @@ const CONNECTION_URL = (token: string) => export const useConnectionHub = (props: ConnectionHubProps) => { const { profileName, isLoading } = props; - const toast = useToast(); + const { toast } = useToast(); const accessToken = getStorageAccessToken(); const [connectionHub, setConnectionHub] = useState(null); @@ -72,13 +72,13 @@ export const useConnectionHub = (props: ConnectionHubProps) => { connectionHub ?.invoke("Restore", profileName) .then(() => { - toast.toast({ + toast({ title: "Успешно", description: "Клиент успешно загружен", }); }) .catch((error) => { - toast.toast({ + toast({ variant: "destructive", title: "Ошибка!", description: JSON.stringify(error), @@ -94,13 +94,13 @@ export const useConnectionHub = (props: ConnectionHubProps) => { connectionHub ?.invoke("Build", profileName) .then(() => { - toast.toast({ + toast({ title: "Успешно", description: "Клиент успешно собран", }); }) .catch((error) => { - toast.toast({ + toast({ variant: "destructive", title: "Ошибка!", description: JSON.stringify(error),