From 55b58c53efa752055268ce75e9b62e83269465cb Mon Sep 17 00:00:00 2001 From: Konstantin Zharich Date: Mon, 8 Jul 2024 09:01:15 +0200 Subject: [PATCH 1/2] - working variant --- src/api/schema.d.ts | 35 +++++++++++++++++++++++++++++++++++ src/pages/Join.tsx | 22 +++++++++++++++++++++- 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/src/api/schema.d.ts b/src/api/schema.d.ts index c5c50ba..bd0f937 100644 --- a/src/api/schema.d.ts +++ b/src/api/schema.d.ts @@ -212,6 +212,22 @@ export interface paths { patch?: never; trace?: never; }; + "/backend/referrals/join": { + parameters: { + query?: never; + header?: never; + path?: never; + cookie?: never; + }; + get?: never; + put?: never; + post: operations["ReferralsController_joinUserByReferralLink"]; + delete?: never; + options?: never; + head?: never; + patch?: never; + trace?: never; + }; "/backend/history/chat/{chatId}": { parameters: { query?: never; @@ -679,6 +695,25 @@ export interface operations { }; }; }; + ReferralsController_joinUserByReferralLink: { + parameters: { + query?: never; + header: { + tmaInitData: string; + }; + path?: never; + cookie?: never; + }; + requestBody?: never; + responses: { + 201: { + headers: { + [name: string]: unknown; + }; + content?: never; + }; + }; + }; HistoryController_getUserHistory: { parameters: { query: { diff --git a/src/pages/Join.tsx b/src/pages/Join.tsx index a61f00b..580d409 100644 --- a/src/pages/Join.tsx +++ b/src/pages/Join.tsx @@ -2,6 +2,7 @@ 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 { useCurrentUser } from '@/api/queries'; @@ -32,9 +33,28 @@ export const Join: FC<{ linkOwner: LinkOwner }> = ({ linkOwner }) => { /* ignore */ } + console.log('linkOwner', linkOwner); + const { data: currentUser, isLoading } = useCurrentUser(); - const handleJoin = () => { + 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 } + } + ); + } catch (error) { + console.error(error); + throw new Error(`${error}`); + } + if (utils) { utils.openTelegramLink(linkOwner.telegramInviteLink); } else { From 6c9b1060ee52b6536bf38acef81248685e20a307 Mon Sep 17 00:00:00 2001 From: Konstantin Zharich Date: Mon, 8 Jul 2024 09:28:11 +0200 Subject: [PATCH 2/2] - 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}`);