import { Button, Divider, Input, Option, Select, Typography } from '@mui/joy' import { useContext, useState } from 'react' import { UserContext } from '../../contexts/UserContext' import { UpdateNotificationTarget } from '../../utils/Fetcher' const NotificationSetting = () => { const { userProfile, setUserProfile } = useContext(UserContext) const [notificationTarget, setNotificationTarget] = useState( userProfile?.notification_target ? String(userProfile.notification_target.type) : '0', ) const [chatID, setChatID] = useState( userProfile?.notification_target?.target_id, ) const [error, setError] = useState('') const SaveValidation = () => { switch (notificationTarget) { case '1': if (chatID === '') { setError('Chat ID is required') return false } else if (isNaN(chatID) || chatID === '0') { setError('Invalid Chat ID') return false } break case '2': if (chatID === '') { setError('User key is required') return false } break default: break } setError('') return true } const handleSave = () => { if (!SaveValidation()) return UpdateNotificationTarget({ target: chatID, type: Number(notificationTarget), }).then(resp => { alert('Notification target updated') setUserProfile({ ...userProfile, notification_target: { target: chatID, type: Number(notificationTarget), }, }) }) } return (
Notification Settings Manage your notification settings {notificationTarget === '1' && ( <> You need to initiate a message to the bot in order for the Telegram notification to work{' '} Click here {' '} to start a chat Chat ID setChatID(e.target.value)} placeholder='User ID / Chat ID' sx={{ width: '200px', }} /> If you don't know your Chat ID, start chat with userinfobot and it will send you your Chat ID.{' '} Click here {' '} to start chat with userinfobot{' '} )} {notificationTarget === '2' && ( <> User key setChatID(e.target.value)} placeholder='User ID' sx={{ width: '200px', }} /> )} {error && ( {error} )}
) } export default NotificationSetting