Ver código fonte

feat: new api calls

Oleksandr Honcharov 1 ano atrás
pai
commit
b109c75de3

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

@@ -3,11 +3,11 @@ import { regionsApi, PostGetRegionsReturn } from '../regions-api';
 import { BaseAxiosError } from '../../../../types';
 import { regionQueryKeys } from '../regions-query-keys';
 
-export const useGetRegionsQuery = (enabled: boolean) => {
+export const useGetRegionsWithFlagQuery = (enabled: boolean) => {
   return useQuery<PostGetRegionsReturn, BaseAxiosError>({
     queryKey: regionQueryKeys.getRegions(),
     queryFn: async () => {
-      const response = await regionsApi.getRegions();
+      const response = await regionsApi.getRegionsWithFlag();
       return response.data;
     },
     enabled

+ 2 - 2
src/modules/auth/regions/regions-api.tsx

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

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

@@ -0,0 +1,15 @@
+import { useQuery } from '@tanstack/react-query';
+import { userApi, PostGetProfileData } from '../user-api';
+import { BaseAxiosError } from '../../../../types';
+import { userQueryKeys } from '../user-query-keys';
+
+export const usePostGetProfileQuery = (token: string, enabled: boolean) => {
+  return useQuery<PostGetProfileData, BaseAxiosError>({
+    queryKey: userQueryKeys.getProfileData(),
+    queryFn: async () => {
+      const response = await userApi.getProfileData(token);
+      return response.data;
+    },
+    enabled
+  });
+};

+ 19 - 0
src/modules/auth/user/queries/use-post-set-profile.tsx

@@ -0,0 +1,19 @@
+import { useMutation } from '@tanstack/react-query';
+import { userApi, PostSetProfileDataReturn, PostSetProfileData } from '../user-api';
+import { BaseAxiosError } from '../../../../types';
+import { userQueryKeys } from '../user-query-keys';
+
+export const usePostSetProfileMutation = () => {
+  return useMutation<
+    PostSetProfileDataReturn,
+    BaseAxiosError,
+    PostSetProfileData,
+    PostSetProfileDataReturn
+  >({
+    mutationKey: userQueryKeys.setProfileData(),
+    mutationFn: async (data) => {
+      const response = await userApi.setProfileData(data);
+      return response.data;
+    }
+  });
+};

+ 75 - 0
src/modules/auth/user/user-api.tsx

@@ -0,0 +1,75 @@
+import { request } from '../../../utils';
+import { API } from '../../../types';
+import { ResponseType } from '../response-type';
+
+export interface PostGetProfileData extends ResponseType {
+  user_id: number;
+  email: string;
+  first_name: string;
+  last_name: string;
+  username: string;
+  avatar: string;
+  date_of_birth: string;
+  homebase: number;
+  homebase2: number | null;
+  bio: string;
+  links: {
+    f?: SocialData;
+    t?: SocialData;
+    i?: SocialData;
+    y?: SocialData;
+    www?: SocialData;
+    other?: SocialData;
+  };
+}
+
+export interface PostSetProfileData {
+  token: string;
+  user?: {
+    email?: string;
+    first_name?: string;
+    last_name?: string;
+    date_of_birth?: string;
+    username?: string;
+    homebase?: number;
+    homebase2?: number;
+    bio?: string;
+    f?: string;
+    t?: string;
+    i?: string;
+    y?: string;
+    www?: string;
+    other?: string;
+  };
+  photo?: {
+    type: string;
+    uri: string;
+    name: string;
+  };
+}
+
+export interface PostSetProfileDataReturn extends ResponseType {
+  updated: string[];
+  not_updated: string[];
+}
+
+type SocialData = {
+  link: string;
+  active: 0 | 1;
+};
+
+export const userApi = {
+  getProfileData: (token: string) =>
+    request.postForm<PostGetProfileData>(API.GET_USER_SETTINGS_DATA, { token }),
+  setProfileData: (data: PostSetProfileData) => {
+    const formData = new FormData();
+
+    formData.append('token', JSON.stringify(data.token));
+    formData.append('user', JSON.stringify(data.user));
+    formData.append('photo', {
+      ...data.photo
+    } as unknown as Blob);
+
+    return request.postForm<PostSetProfileDataReturn>(API.SET_USER_SETTINGS_DATA, formData);
+  }
+};

+ 4 - 0
src/modules/auth/user/user-query-keys.tsx

@@ -0,0 +1,4 @@
+export const userQueryKeys = {
+  getProfileData: () => ['getProfileData'] as const,
+  setProfileData: () => ['setProfileData'] as const
+};

+ 6 - 1
src/types/api.ts

@@ -10,8 +10,11 @@ export enum API_ENDPOINT {
   LOGIN = 'login',
   REGISTER = 'join',
   RESET_PASSWORD = 'recover-password',
+  GET_REGIONS_WITH_FLAGS = 'get-regions-with-flags',
   GET_REGIONS = 'get-regions',
   JOIN_TEST = 'pre-join-test',
+  GET_SETTINGS_APP = 'get-settings-app',
+  SET_SETTINGS_APP = 'set-settings-app',
   SERIES = 'get-for-regions'
 }
 
@@ -19,8 +22,10 @@ export enum API {
   LOGIN = `${API_ROUTE.USER}/${API_ENDPOINT.LOGIN}`,
   REGISTER = `${API_ROUTE.USER}/${API_ENDPOINT.REGISTER}`,
   RESET_PASSWORD = `${API_ROUTE.USER}/${API_ENDPOINT.RESET_PASSWORD}`,
-  GET_REGIONS = `${API_ROUTE.REGIONS}/${API_ENDPOINT.GET_REGIONS}`,
+  GET_REGIONS_WITH_FLAGS = `${API_ROUTE.REGIONS}/${API_ENDPOINT.GET_REGIONS_WITH_FLAGS}`,
   JOIN_TEST = `${API_ROUTE.USER}/${API_ENDPOINT.JOIN_TEST}`,
+  GET_USER_SETTINGS_DATA = `${API_ROUTE.USER}/${API_ENDPOINT.GET_SETTINGS_APP}`,
+  SET_USER_SETTINGS_DATA = `${API_ROUTE.USER}/${API_ENDPOINT.SET_SETTINGS_APP}`,
   SERIES = `${API_ROUTE.SERIES}/${API_ENDPOINT.SERIES}`
 }