Skip to content

Commit

Permalink
- rewrote with react-query
Browse files Browse the repository at this point in the history
  • Loading branch information
Konstantin Zharich committed Jul 8, 2024
1 parent 55b58c5 commit 6c9b106
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 16 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
});
}
10 changes: 9 additions & 1 deletion src/api/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
21 changes: 7 additions & 14 deletions src/pages/Join.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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}`);
Expand Down

0 comments on commit 6c9b106

Please sign in to comment.