Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/features/applications/api/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './useApplications';
55 changes: 55 additions & 0 deletions src/features/applications/api/useApplications.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
import { toast } from 'sonner';

import { applicationsApi } from '@/shared/api/applications';
import { ExternalApplicationCreateDto } from '@/shared/api/contracts';

// Query key for applications
const APPLICATIONS_QUERY_KEY = ['applications'];

/**
* Hook to fetch all applications
*/
export const useApplications = () => {
return useQuery({
queryKey: APPLICATIONS_QUERY_KEY,
queryFn: () => applicationsApi.getApplications(),
});
};

/**
* Hook to create a new application
*/
export const useCreateApplication = () => {
const queryClient = useQueryClient();

return useMutation({
mutationFn: (data: ExternalApplicationCreateDto) => applicationsApi.createApplication(data),
onSuccess: (data) => {
toast.success('Приложение успешно создано');
queryClient.invalidateQueries({ queryKey: APPLICATIONS_QUERY_KEY });
return data;
},
onError: (error: any) => {
toast.error(error?.message || 'Ошибка при создании приложения');
},
});
};

/**
* Hook to delete an application
*/
export const useDeleteApplication = () => {
const queryClient = useQueryClient();

return useMutation({
mutationFn: (id: string) => applicationsApi.deleteApplication(id),
onSuccess: () => {
toast.success('Приложение успешно удалено');
queryClient.invalidateQueries({ queryKey: APPLICATIONS_QUERY_KEY });
},
onError: (error: any) => {
toast.error(error?.message || 'Ошибка при удалении приложения');
},
});
};
1 change: 1 addition & 0 deletions src/features/applications/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './api';
34 changes: 34 additions & 0 deletions src/shared/api/applications.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { $api } from '@/services/api.service';
import {
ExternalApplicationCreateDto,
ExternalApplicationListDto,
ExternalApplicationReadDto,
TGetApplicationsResponse,
TPostApplicationResponse,
TDeleteApplicationResponse
} from '@/shared/api/contracts';

export const applicationsApi = {
/**
* Get all applications for the current user
*/
async getApplications(): Promise<ExternalApplicationListDto[]> {
const { data } = await $api.get<TGetApplicationsResponse>('/applications');
return data.data;
},

/**
* Create a new external application
*/
async createApplication(payload: ExternalApplicationCreateDto): Promise<ExternalApplicationReadDto> {
const { data } = await $api.post<TPostApplicationResponse>('/applications', payload);
return data.data;
},

/**
* Delete an external application
*/
async deleteApplication(id: string): Promise<void> {
await $api.delete<TDeleteApplicationResponse>(`/applications/${id}`);
}
};
22 changes: 22 additions & 0 deletions src/shared/api/contracts/applications/requests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import {
ExternalApplicationListDto,
ExternalApplicationReadDto,
ExternalApplicationCreateDto
} from './schemas';

import { ResponseBaseEntity } from '@/shared/api/schemas';

// GET /api/v1/applications
export type TGetApplicationsResponse = ResponseBaseEntity & {
data: ExternalApplicationListDto[];
};

// POST /api/v1/applications
export type TPostApplicationRequest = ExternalApplicationCreateDto;

export type TPostApplicationResponse = ResponseBaseEntity & {
data: ExternalApplicationReadDto;
};

// DELETE /api/v1/applications/{id}
export type TDeleteApplicationResponse = ResponseBaseEntity;
25 changes: 25 additions & 0 deletions src/shared/api/contracts/applications/schemas.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
export interface PermissionDto {
id: number;
name: string;
description?: string;
}

export interface ExternalApplicationBaseEntity {
id: string;
name: string;
createdAtUtc: string;
}

export interface ExternalApplicationListDto extends ExternalApplicationBaseEntity {
permissions: PermissionDto[];
}

export interface ExternalApplicationReadDto extends ExternalApplicationBaseEntity {
token: string;
permissions: PermissionDto[];
}

export interface ExternalApplicationCreateDto {
name: string;
permissionIds: number[];
}
4 changes: 4 additions & 0 deletions src/shared/api/contracts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,7 @@ export * from '@/shared/api/contracts/gameservers/zod';
// Контракты для уведомлений
export * from '@/shared/api/contracts/notification/schemas';
export * from '@/shared/api/contracts/notification/requests';

// Applications
export * from '@/shared/api/contracts/applications/schemas';
export * from '@/shared/api/contracts/applications/requests';
Loading