1
0
Fork 0
mirror of https://github.com/muerwre/vault-frontend.git synced 2025-04-24 20:36:40 +07:00

telegram: now able to delete it

This commit is contained in:
Fedor Katurov 2023-03-13 18:26:16 +06:00
parent e994176bff
commit 47eee7d166
10 changed files with 112 additions and 26 deletions

View file

@ -1,24 +1,45 @@
import React, { FC, useEffect } from 'react'; import React, { FC } from 'react';
import TelegramLoginButton, { import TelegramLoginButton, {
TelegramUser, TelegramUser,
} from '@v9v/ts-react-telegram-login'; } from '@v9v/ts-react-telegram-login';
import { LoaderCircle } from '~/components/input/LoaderCircle';
import styles from './styles.module.scss';
interface TelegramLoginFormProps { interface TelegramLoginFormProps {
botName: string; botName: string;
loading?: boolean;
onSuccess?: (token: TelegramUser) => void; onSuccess?: (token: TelegramUser) => void;
} }
const TelegramLoginForm: FC<TelegramLoginFormProps> = ({ const TelegramLoginForm: FC<TelegramLoginFormProps> = ({
botName, botName,
loading,
onSuccess, onSuccess,
}) => { }) => {
return ( return (
<div className={styles.container}>
<div className={styles.text}>
{loading ? (
<LoaderCircle />
) : (
<div>
После успешной авторизации аккаунт появится в настройках вашего
профиля
</div>
)}
</div>
<div className={styles.button}>
<TelegramLoginButton <TelegramLoginButton
dataOnAuth={onSuccess} dataOnAuth={onSuccess}
botName={botName} botName={botName}
requestAccess requestAccess
/> />
</div>
</div>
); );
}; };

View file

@ -0,0 +1,22 @@
@import 'src/styles/variables';
.button {
flex: 0 0 48px;
}
.container {
min-height: 200px;
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
padding: $gap;
}
.text {
text-align: center;
flex: 1;
display: flex;
align-items: center;
justify-content: center;
}

View file

@ -3,4 +3,5 @@ import { OAuthProvider } from '~/types/auth';
export const SOCIAL_ICONS: Record<OAuthProvider, string> = { export const SOCIAL_ICONS: Record<OAuthProvider, string> = {
vkontakte: 'vk', vkontakte: 'vk',
google: 'google', google: 'google',
telegram: 'telegram',
}; };

View file

@ -1,5 +1,9 @@
import React, { FC } from 'react'; import React, { FC, useCallback, useMemo } from 'react';
import { TelegramUser } from '@v9v/ts-react-telegram-login';
import { Padder } from '~/components/containers/Padder';
import { Button } from '~/components/input/Button';
import { useTelegramAccount } from '~/hooks/auth/useTelegramAccount'; import { useTelegramAccount } from '~/hooks/auth/useTelegramAccount';
import { DialogComponentProps } from '~/types/modal'; import { DialogComponentProps } from '~/types/modal';
@ -15,16 +19,30 @@ const TelegramAttachDialog: FC<TelegramAttachDialogProps> = ({
}) => { }) => {
const { attach } = useTelegramAccount(); const { attach } = useTelegramAccount();
const onAttach = useCallback(
(data: TelegramUser) => attach(data, onRequestClose),
[onRequestClose],
);
const buttons = useMemo(
() => (
<Padder>
<Button stretchy onClick={onRequestClose}>
Отмена
</Button>
</Padder>
),
[onRequestClose],
);
if (!botName) { if (!botName) {
// TODO: show something onRequestClose();
return null; return null;
} }
return ( return (
<BetterScrollDialog width={300} onClose={onRequestClose}> <BetterScrollDialog width={300} onClose={onRequestClose} footer={buttons}>
<div> <TelegramLoginForm botName={botName} onSuccess={onAttach} />
<TelegramLoginForm botName={botName} onSuccess={attach} />
</div>
</BetterScrollDialog> </BetterScrollDialog>
); );
}; };

View file

@ -1,4 +1,4 @@
import React, { FC, Fragment, useCallback } from 'react'; import React, { FC, Fragment, useCallback, useMemo } from 'react';
import { Superpower } from '~/components/boris/Superpower'; import { Superpower } from '~/components/boris/Superpower';
import { Group } from '~/components/containers/Group'; import { Group } from '~/components/containers/Group';
@ -18,6 +18,11 @@ const ProfileAccounts: FC<ProfileAccountsProps> = () => {
const { isLoading, accounts, dropAccount, openOauthWindow } = useOAuth(); const { isLoading, accounts, dropAccount, openOauthWindow } = useOAuth();
const { showModal } = useModal(); const { showModal } = useModal();
const hasTelegram = useMemo(
() => accounts.some((acc) => acc.provider === 'telegram'),
[accounts],
);
const showTelegramModal = useCallback( const showTelegramModal = useCallback(
() => showModal(Dialog.TelegramAttach, {}), () => showModal(Dialog.TelegramAttach, {}),
[], [],
@ -64,7 +69,9 @@ const ProfileAccounts: FC<ProfileAccountsProps> = () => {
</div> </div>
</div> </div>
<div className={styles.account__name}>{it.name || it.id}</div> <div className={styles.account__name}>
{it.name || it.id} - {it.id} - {it.provider}
</div>
<div className={styles.account__drop}> <div className={styles.account__drop}>
<Icon <Icon
@ -86,6 +93,7 @@ const ProfileAccounts: FC<ProfileAccountsProps> = () => {
iconLeft="telegram" iconLeft="telegram"
color="gray" color="gray"
onClick={showTelegramModal} onClick={showTelegramModal}
disabled={hasTelegram}
> >
Телеграм Телеграм
</Button> </Button>

View file

@ -43,8 +43,6 @@ export const useOAuth = () => {
setToken(result.token); setToken(result.token);
hideModal(); hideModal();
} catch (error) { } catch (error) {
console.log(path(['response'], error));
const needsRegister = path(['response', 'status'], error) === 428; const needsRegister = path(['response', 'status'], error) === 428;
if (needsRegister && token) { if (needsRegister && token) {
@ -97,6 +95,7 @@ export const useOAuth = () => {
); );
const accounts = useMemo(() => data || [], [data]); const accounts = useMemo(() => data || [], [data]);
const refresh = useCallback(() => mutate(), []);
return { return {
openOauthWindow, openOauthWindow,
@ -106,5 +105,6 @@ export const useOAuth = () => {
dropAccount, dropAccount,
accounts, accounts,
isLoading: !data && isLoading, isLoading: !data && isLoading,
refresh,
}; };
}; };

View file

@ -21,8 +21,6 @@ export const useOauthEventListeners = () => {
return; return;
} }
console.log('caught event:', type, event.data);
switch (type) { switch (type) {
case EventMessageType.OAuthLogin: case EventMessageType.OAuthLogin:
loginWithSocial(path(['data', 'payload', 'token'], event)); loginWithSocial(path(['data', 'payload', 'token'], event));

View file

@ -1,13 +1,32 @@
import { useCallback } from 'react'; import { useCallback, useState } from 'react';
import { TelegramUser } from '@v9v/ts-react-telegram-login'; import { TelegramUser } from '@v9v/ts-react-telegram-login';
import { showErrorToast } from '~/utils/errors/showToast';
import { apiAttachTelegram } from '../../api/auth/index'; import { apiAttachTelegram } from '../../api/auth/index';
export const useTelegramAccount = () => { import { useOAuth } from './useOAuth';
const attach = useCallback((data: TelegramUser) => {
apiAttachTelegram(data);
}, []);
return { attach }; export const useTelegramAccount = () => {
const [loading, setLoading] = useState(false);
const { refresh } = useOAuth();
const attach = useCallback(
async (data: TelegramUser, callback?: () => void) => {
setLoading(true);
try {
await apiAttachTelegram(data);
await refresh();
callback?.();
} catch (error) {
showErrorToast(error);
} finally {
setLoading(false);
}
},
[],
);
return { attach, loading };
}; };

View file

@ -20,7 +20,7 @@ export interface IUser {
is_user: boolean; is_user: boolean;
} }
export type OAuthProvider = 'vkontakte' | 'google'; export type OAuthProvider = 'vkontakte' | 'google' | 'telegram';
export interface ISocialAccount { export interface ISocialAccount {
provider: OAuthProvider; provider: OAuthProvider;

View file

@ -54,7 +54,6 @@ export const useSettingsForm = (
const validationErrors = getValidationErrors(error); const validationErrors = getValidationErrors(error);
if (validationErrors) { if (validationErrors) {
console.log(validationErrors);
setErrors(validationErrors); setErrors(validationErrors);
} }
} }