2024-06-30 18:55:39 -04:00
|
|
|
import { Button, Divider, Input, Option, Select, Typography } from '@mui/joy'
|
2024-12-14 02:11:59 -05:00
|
|
|
import { useContext, useState } from 'react'
|
2024-06-30 18:55:39 -04:00
|
|
|
import { UserContext } from '../../contexts/UserContext'
|
2024-12-14 02:11:59 -05:00
|
|
|
import { UpdateNotificationTarget } from '../../utils/Fetcher'
|
2024-06-30 18:55:39 -04:00
|
|
|
|
|
|
|
const NotificationSetting = () => {
|
|
|
|
const { userProfile, setUserProfile } = useContext(UserContext)
|
2024-12-14 02:11:59 -05:00
|
|
|
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
|
2024-06-30 18:55:39 -04:00
|
|
|
}
|
2024-12-14 02:11:59 -05:00
|
|
|
setError('')
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
const handleSave = () => {
|
|
|
|
if (!SaveValidation()) return
|
2024-06-30 18:55:39 -04:00
|
|
|
|
2024-12-14 02:11:59 -05:00
|
|
|
UpdateNotificationTarget({
|
|
|
|
target: chatID,
|
|
|
|
type: Number(notificationTarget),
|
|
|
|
}).then(resp => {
|
|
|
|
alert('Notification target updated')
|
|
|
|
setUserProfile({
|
|
|
|
...userProfile,
|
|
|
|
notification_target: {
|
|
|
|
target: chatID,
|
|
|
|
type: Number(notificationTarget),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
2024-06-30 18:55:39 -04:00
|
|
|
return (
|
|
|
|
<div className='grid gap-4 py-4' id='notifications'>
|
|
|
|
<Typography level='h3'>Notification Settings</Typography>
|
|
|
|
<Divider />
|
|
|
|
<Typography level='body-md'>Manage your notification settings</Typography>
|
|
|
|
|
2024-12-14 02:11:59 -05:00
|
|
|
<Select
|
|
|
|
value={notificationTarget}
|
|
|
|
sx={{ maxWidth: '200px' }}
|
|
|
|
onChange={(e, selected) => setNotificationTarget(selected)}
|
|
|
|
>
|
|
|
|
<Option value='0'>None</Option>
|
|
|
|
<Option value='1'>Telegram</Option>
|
|
|
|
<Option value='2'>Pushover</Option>
|
2024-06-30 18:55:39 -04:00
|
|
|
</Select>
|
2024-12-14 02:11:59 -05:00
|
|
|
{notificationTarget === '1' && (
|
|
|
|
<>
|
|
|
|
<Typography level='body-xs'>
|
|
|
|
You need to initiate a message to the bot in order for the Telegram
|
|
|
|
notification to work{' '}
|
|
|
|
<a
|
|
|
|
style={{
|
|
|
|
textDecoration: 'underline',
|
|
|
|
color: '#0891b2',
|
|
|
|
}}
|
|
|
|
href='https://t.me/DonetickBot'
|
|
|
|
>
|
|
|
|
Click here
|
|
|
|
</a>{' '}
|
|
|
|
to start a chat
|
|
|
|
</Typography>
|
2024-06-30 18:55:39 -04:00
|
|
|
|
2024-12-14 02:11:59 -05:00
|
|
|
<Typography level='body-sm'>Chat ID</Typography>
|
2024-06-30 18:55:39 -04:00
|
|
|
|
2024-12-14 02:11:59 -05:00
|
|
|
<Input
|
|
|
|
value={chatID}
|
|
|
|
onChange={e => setChatID(e.target.value)}
|
|
|
|
placeholder='User ID / Chat ID'
|
|
|
|
sx={{
|
|
|
|
width: '200px',
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
<Typography mt={0} level='body-xs'>
|
|
|
|
If you don't know your Chat ID, start chat with userinfobot and it
|
|
|
|
will send you your Chat ID.{' '}
|
|
|
|
<a
|
|
|
|
style={{
|
|
|
|
textDecoration: 'underline',
|
|
|
|
color: '#0891b2',
|
|
|
|
}}
|
|
|
|
href='https://t.me/userinfobot'
|
|
|
|
>
|
|
|
|
Click here
|
|
|
|
</a>{' '}
|
|
|
|
to start chat with userinfobot{' '}
|
|
|
|
</Typography>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
{notificationTarget === '2' && (
|
|
|
|
<>
|
|
|
|
<Typography level='body-sm'>User key</Typography>
|
|
|
|
<Input
|
|
|
|
value={chatID}
|
|
|
|
onChange={e => setChatID(e.target.value)}
|
|
|
|
placeholder='User ID'
|
|
|
|
sx={{
|
|
|
|
width: '200px',
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
{error && (
|
|
|
|
<Typography color='warning' level='body-sm'>
|
|
|
|
{error}
|
|
|
|
</Typography>
|
|
|
|
)}
|
2024-06-30 18:55:39 -04:00
|
|
|
|
|
|
|
<Button
|
|
|
|
sx={{
|
|
|
|
width: '110px',
|
|
|
|
mb: 1,
|
|
|
|
}}
|
2024-12-14 02:11:59 -05:00
|
|
|
onClick={handleSave}
|
2024-06-30 18:55:39 -04:00
|
|
|
>
|
|
|
|
Save
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default NotificationSetting
|