From 6c9b1060ee52b6536bf38acef81248685e20a307 Mon Sep 17 00:00:00 2001 From: Konstantin Zharich Date: Mon, 8 Jul 2024 09:28:11 +0200 Subject: [PATCH] - rewrote with react-query --- src/api/mutations.ts | 17 ++++++++++++++++- src/api/schema.d.ts | 10 +++++++++- src/pages/Join.tsx | 21 +++++++-------------- 3 files changed, 32 insertions(+), 16 deletions(-) diff --git a/src/api/mutations.ts b/src/api/mutations.ts index dd92156..74ca019 100644 --- a/src/api/mutations.ts +++ b/src/api/mutations.ts @@ -1,4 +1,4 @@ -import { useMutation, useQueryClient } from '@tanstack/react-query'; +import { useMutation } from '@tanstack/react-query'; import { CreatedTempImage } from '@/interfaces/CreatedTempImage'; import { apiClient } from './apiClient'; @@ -108,3 +108,18 @@ export function useDeleteReward() { ).data }); } + +export function useReferralJoin() { + return useMutation({ + mutationKey: [], + mutationFn: async ( + body: paths['/backend/referrals/join']['post']['requestBody']['content']['application/json'] + ) => + ( + await apiClient.POST('/backend/referrals/join', { + params: { header: { tmaInitData: '' } }, + body + }) + ).data as unknown as void + }); +} diff --git a/src/api/schema.d.ts b/src/api/schema.d.ts index bd0f937..aecf45c 100644 --- a/src/api/schema.d.ts +++ b/src/api/schema.d.ts @@ -704,7 +704,15 @@ export interface operations { path?: never; cookie?: never; }; - requestBody?: never; + requestBody: { + content: { + "application/json": { + chatId?: number; + ownerId?: number; + title?: string; + }; + }; + }; responses: { 201: { headers: { diff --git a/src/pages/Join.tsx b/src/pages/Join.tsx index 580d409..35ef207 100644 --- a/src/pages/Join.tsx +++ b/src/pages/Join.tsx @@ -2,9 +2,9 @@ import { FC } from 'react'; import { Typography } from '@mui/material'; import { Button, Title } from '@telegram-apps/telegram-ui'; import { MiniApp, useMiniApp, useUtils, Utils } from '@tma.js/sdk-react'; -import axios from 'axios'; import styled from 'styled-components'; +import { useReferralJoin } from '@/api/mutations'; import { useCurrentUser } from '@/api/queries'; import { AvatarJoinIcon } from '@/icons/AvatarJoinIcon'; import { GroupIcon } from '@/icons/GroupIcon'; @@ -33,23 +33,16 @@ export const Join: FC<{ linkOwner: LinkOwner }> = ({ linkOwner }) => { /* ignore */ } - console.log('linkOwner', linkOwner); - const { data: currentUser, isLoading } = useCurrentUser(); + const { mutateAsync: joinReferral } = useReferralJoin(); const handleJoin = async () => { try { - await axios.post( - `${import.meta.env.VITE_BACKEND_URL}/backend/referrals/join`, - { - chatId: Number(linkOwner.chatId), - ownerId: linkOwner.ownerId, - title: linkOwner.title - }, - { - headers: { tmaInitData: window.Telegram.WebApp.initData } - } - ); + await joinReferral({ + chatId: Number(linkOwner.chatId), + ownerId: linkOwner.ownerId, + title: linkOwner.title + }); } catch (error) { console.error(error); throw new Error(`${error}`);