Forráskód Böngészése

feat: added new API calls | refactor existing

Oleksandr Honcharov 1 éve
szülő
commit
503444c2a0

+ 21 - 9
src/modules/auth/api/auth-api.tsx

@@ -1,19 +1,31 @@
 import { request } from '../../../utils';
 import { API } from '../../../types';
+import { ResponseType } from '../response-type';
+import { UserRegistrationData } from './queries/use-post-register';
 
-export enum ResultTypes {
-  ERROR = 'ERROR',
-  OK = 'OK'
+export interface PostLoginUserReturn extends ResponseType {
+  token: string;
 }
 
-export type PostLoginUserReturn = {
+export interface PostRegisterUserReturn extends ResponseType {
   token: string;
-  status?: string;
-  result: ResultTypes;
-  result_description?: string;
-};
+  uid: string;
+  mail: 1 | 0;
+}
 
 export const authApi = {
   loginUser: (data: { login: string; pass: string }) =>
-    request.postForm<PostLoginUserReturn>(API.LOGIN, data)
+    request.post<PostLoginUserReturn>(API.LOGIN, data),
+  registerUser: (data: UserRegistrationData) => {
+    const formData = new FormData();
+
+    formData.append('user', JSON.stringify(data.user));
+    formData.append('photo', {
+      type: data.photo.type,
+      uri: data.photo.uri,
+      name: data.photo.name
+    } as unknown as Blob);
+
+    return request.postForm<PostRegisterUserReturn>(API.REGISTER, formData);
+  }
 };

+ 2 - 1
src/modules/auth/api/auth-query-keys.tsx

@@ -1,3 +1,4 @@
 export const authQueryKeys = {
-  loginUser: () => ['loginUser'] as const
+  loginUser: () => ['loginUser'] as const,
+  registerUser: () => ['registerUser'] as const
 };

+ 32 - 0
src/modules/auth/api/queries/use-post-register.tsx

@@ -0,0 +1,32 @@
+import { useQuery } from '@tanstack/react-query';
+import { authApi, PostLoginUserReturn } from '../auth-api';
+import { BaseAxiosError } from '../../../../types';
+import { authQueryKeys } from '../auth-query-keys';
+
+export type UserRegistrationData = {
+  user: {
+    username: string;
+    email: string;
+    password: string;
+    first_name: string;
+    last_name: string;
+    date_of_birth: Date;
+    homebase: number;
+  };
+  photo: {
+    type: string;
+    uri: string;
+    name: string;
+  };
+};
+
+export const usePostRegister = (data: UserRegistrationData, enabled: boolean) => {
+  return useQuery<PostLoginUserReturn, BaseAxiosError>({
+    queryKey: authQueryKeys.registerUser(),
+    queryFn: async () => {
+      const response = await authApi.registerUser(data);
+      return response.data;
+    },
+    enabled
+  });
+};

+ 15 - 0
src/modules/auth/regions/queries/use-post-get-regions.tsx

@@ -0,0 +1,15 @@
+import { useQuery } from '@tanstack/react-query';
+import { regionsApi, PostGetRegionsReturn } from '../regions-api';
+import { BaseAxiosError } from '../../../../types';
+import { regionQueryKeys } from '../regions-query-keys';
+
+export const usePostGetRegions = (enabled: boolean) => {
+  return useQuery<PostGetRegionsReturn, BaseAxiosError>({
+    queryKey: regionQueryKeys.getRegions(),
+    queryFn: async () => {
+      const response = await regionsApi.getRegions();
+      return response.data;
+    },
+    enabled
+  });
+};

+ 11 - 0
src/modules/auth/regions/regions-api.tsx

@@ -0,0 +1,11 @@
+import { request } from '../../../utils';
+import { API } from '../../../types';
+import { ResponseType } from '../response-type';
+
+export interface PostGetRegionsReturn extends ResponseType {
+  data: { id: number; name: string }[];
+}
+
+export const regionsApi = {
+  getRegions: () => request.postForm<PostGetRegionsReturn>(API.GET_REGIONS)
+};

+ 3 - 0
src/modules/auth/regions/regions-query-keys.tsx

@@ -0,0 +1,3 @@
+export const regionQueryKeys = {
+  getRegions: () => ['getRegions'] as const
+};

+ 15 - 0
src/modules/auth/response-type.ts

@@ -0,0 +1,15 @@
+enum ResultTypes {
+  ERROR = 'ERROR',
+  OK = 'OK'
+}
+
+enum StatusTypes {
+  INIT_NEEDED = 'INIT_NEEDED',
+  TERMS_NEEDED = 'TERMS_NEEDED'
+}
+
+export interface ResponseType {
+  result: ResultTypes;
+  status?: StatusTypes;
+  result_description?: string;
+}

+ 8 - 3
src/types/api.ts

@@ -1,15 +1,20 @@
 import { AxiosError } from 'axios';
 
 export enum API_ROUTE {
-  USER = 'user'
+  USER = 'user',
+  REGIONS = 'regions'
 }
 
 export enum API_ENDPOINT {
-  LOGIN = 'login'
+  LOGIN = 'login',
+  REGISTER = 'join',
+  GET_REGIONS = 'get-regions'
 }
 
 export enum API {
-  LOGIN = `${API_ROUTE.USER}/${API_ENDPOINT.LOGIN}`
+  LOGIN = `${API_ROUTE.USER}/${API_ENDPOINT.LOGIN}`,
+  REGISTER = `${API_ROUTE.USER}/${API_ENDPOINT.REGISTER}`,
+  GET_REGIONS = `${API_ROUTE.REGIONS}/${API_ENDPOINT.GET_REGIONS}`
 }
 
 export type BaseAxiosError = AxiosError;