Skip to content

Commit

Permalink
Merge pull request #12 from Scondic/GL-15
Browse files Browse the repository at this point in the history
GL-15: Фикс уведомлений
  • Loading branch information
Scondic authored Mar 12, 2024
2 parents 55ad920 + 6662050 commit 32d969d
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 80 deletions.
4 changes: 2 additions & 2 deletions src/features/install-client-form/lib/useConnectionHub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<HubConnection | null>(null);
Expand Down Expand Up @@ -39,7 +39,7 @@ export const useConnectionHub = () => {
});

connection.on("Message", (message) => {
toast.toast({
toast({
title: message,
});
});
Expand Down
6 changes: 3 additions & 3 deletions src/features/install-client-form/ui/InstallClientForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ interface InstallClientFormProps extends React.HTMLAttributes<HTMLDivElement> {}
export function InstallClientForm({ className, ...props }: InstallClientFormProps) {
const { connectionHub, process, percent } = useConnectionHub();
const { data: branches } = useGithubLauncherVersions();
const toast = useToast();
const { toast } = useToast();

const form = useForm<InstallClientFormSchemaType>({
values: { branch: "", host: getApiBaseUrl() || "", folder: "" },
Expand All @@ -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),
Expand Down
53 changes: 28 additions & 25 deletions src/shared/hooks/useAuthorization.ts
Original file line number Diff line number Diff line change
@@ -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],
});
}
Expand All @@ -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],
});
}
},
});
};
6 changes: 3 additions & 3 deletions src/shared/hooks/useIntegraions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export const useActiveAuthIntegrations = () => {
};

export const useEditIntegration = () => {
const toast = useToast();
const { toast } = useToast();
const queryClient = useQueryClient();

return useMutation({
Expand All @@ -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],
Expand Down
78 changes: 39 additions & 39 deletions src/shared/hooks/useProfiles.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -9,34 +9,34 @@ 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(),
});

return { data: data?.data, isLoading };
};

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],
});
}
Expand All @@ -46,33 +46,33 @@ export const useProfile = () => {

export const useCurrentProfile = () => {
const { data } = useQuery<ProfileBaseEntity>({
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],
});
}
Expand All @@ -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],
});
}
Expand All @@ -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],
});
}
Expand All @@ -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],
});
}
Expand Down
22 changes: 19 additions & 3 deletions src/shared/ui/Header/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
{
Expand Down Expand Up @@ -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 (
<nav className="flex flex-col gap-y-8 px-2 py-8 h-full w-[300px]">
Expand Down Expand Up @@ -94,6 +102,14 @@ export function Header() {
))}
</div>
))}

<button
className="flex items-center gap-x-3 text-base p-2.5 rounded-lg transition-colors hover:bg-muted mt-auto"
onClick={destroySession}
>
<LogOutIcon className="h-4 w-4" />
Выйти из аккаунта
</button>
</nav>
);
}
Loading

0 comments on commit 32d969d

Please sign in to comment.