Skip to content

Commit

Permalink
Merge pull request #34 from tonalty/refactored-referral-link-flow
Browse files Browse the repository at this point in the history
Refactored referral link flow
  • Loading branch information
Konstantin1996 authored Jul 8, 2024
2 parents dee6ff5 + 6c9b106 commit 12bbd61
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 2 deletions.
17 changes: 16 additions & 1 deletion src/api/mutations.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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
});
}
43 changes: 43 additions & 0 deletions src/api/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -679,6 +695,33 @@ export interface operations {
};
};
};
ReferralsController_joinUserByReferralLink: {
parameters: {
query?: never;
header: {
tmaInitData: string;
};
path?: never;
cookie?: never;
};
requestBody: {
content: {
"application/json": {
chatId?: number;
ownerId?: number;
title?: string;
};
};
};
responses: {
201: {
headers: {
[name: string]: unknown;
};
content?: never;
};
};
};
HistoryController_getUserHistory: {
parameters: {
query: {
Expand Down
15 changes: 14 additions & 1 deletion src/pages/Join.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Button, Title } from '@telegram-apps/telegram-ui';
import { MiniApp, useMiniApp, useUtils, Utils } from '@tma.js/sdk-react';
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';
Expand Down Expand Up @@ -33,8 +34,20 @@ export const Join: FC<{ linkOwner: LinkOwner }> = ({ linkOwner }) => {
}

const { data: currentUser, isLoading } = useCurrentUser();
const { mutateAsync: joinReferral } = useReferralJoin();

const handleJoin = async () => {
try {
await joinReferral({
chatId: Number(linkOwner.chatId),
ownerId: linkOwner.ownerId,
title: linkOwner.title
});
} catch (error) {
console.error(error);
throw new Error(`${error}`);
}

const handleJoin = () => {
if (utils) {
utils.openTelegramLink(linkOwner.telegramInviteLink);
} else {
Expand Down

0 comments on commit 12bbd61

Please sign in to comment.