Add notification target update functionality

This commit is contained in:
Mo Tarbin 2024-12-14 02:11:59 -05:00
parent 36638b3a9d
commit aff432b74a
3 changed files with 132 additions and 64 deletions

View file

@ -225,6 +225,14 @@ const UpdateUserDetails = userDetails => {
}) })
} }
const UpdateNotificationTarget = notificationTarget => {
return Fetch(`${API_URL}/users/targets`, {
method: 'PUT',
headers: HEADERS(),
body: JSON.stringify(notificationTarget),
})
}
const GetSubscriptionSession = () => { const GetSubscriptionSession = () => {
return Fetch(API_URL + `/payments/create-subscription`, { return Fetch(API_URL + `/payments/create-subscription`, {
method: 'GET', method: 'GET',
@ -373,6 +381,7 @@ export {
UpdateChoreHistory, UpdateChoreHistory,
UpdateChorePriority, UpdateChorePriority,
UpdateLabel, UpdateLabel,
UpdateNotificationTarget,
UpdatePassword, UpdatePassword,
UpdateThingState, UpdateThingState,
UpdateUserDetails, UpdateUserDetails,

View file

@ -103,12 +103,13 @@ const SignupView = () => {
signUp(username, password, displayName, email).then(response => { signUp(username, password, displayName, email).then(response => {
if (response.status === 201) { if (response.status === 201) {
handleLogin(username, password) handleLogin(username, password)
} else if (response.status === 403) {
setError('Signup disabled, please contact admin')
} else { } else {
console.log('Signup failed') console.log('Signup failed')
response.json().then(res => { response.json().then(res => {
setError(res.error) setError(res.error)
} })
)
} }
}) })
} }

View file

@ -1,33 +1,77 @@
import { Button, Divider, Input, Option, Select, Typography } from '@mui/joy' import { Button, Divider, Input, Option, Select, Typography } from '@mui/joy'
import { useContext, useEffect, useState } from 'react' import { useContext, useState } from 'react'
import { UserContext } from '../../contexts/UserContext' import { UserContext } from '../../contexts/UserContext'
import { GetUserProfile, UpdateUserDetails } from '../../utils/Fetcher' import { UpdateNotificationTarget } from '../../utils/Fetcher'
const NotificationSetting = () => { const NotificationSetting = () => {
const { userProfile, setUserProfile } = useContext(UserContext) const { userProfile, setUserProfile } = useContext(UserContext)
useEffect(() => { const [notificationTarget, setNotificationTarget] = useState(
if (!userProfile) { userProfile?.notification_target
GetUserProfile().then(resp => { ? String(userProfile.notification_target.type)
resp.json().then(data => { : '0',
setUserProfile(data.res) )
setChatID(data.res.chatID)
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),
},
}) })
}) })
} }
}, [])
const [chatID, setChatID] = useState(userProfile?.chatID)
return ( return (
<div className='grid gap-4 py-4' id='notifications'> <div className='grid gap-4 py-4' id='notifications'>
<Typography level='h3'>Notification Settings</Typography> <Typography level='h3'>Notification Settings</Typography>
<Divider /> <Divider />
<Typography level='body-md'>Manage your notification settings</Typography> <Typography level='body-md'>Manage your notification settings</Typography>
<Select defaultValue='telegram' sx={{ maxWidth: '200px' }} disabled> <Select
<Option value='telegram'>Telegram</Option> value={notificationTarget}
<Option value='discord'>Discord</Option> sx={{ maxWidth: '200px' }}
onChange={(e, selected) => setNotificationTarget(selected)}
>
<Option value='0'>None</Option>
<Option value='1'>Telegram</Option>
<Option value='2'>Pushover</Option>
</Select> </Select>
{notificationTarget === '1' && (
<>
<Typography level='body-xs'> <Typography level='body-xs'>
You need to initiate a message to the bot in order for the Telegram You need to initiate a message to the bot in order for the Telegram
notification to work{' '} notification to work{' '}
@ -43,6 +87,8 @@ const NotificationSetting = () => {
to start a chat to start a chat
</Typography> </Typography>
<Typography level='body-sm'>Chat ID</Typography>
<Input <Input
value={chatID} value={chatID}
onChange={e => setChatID(e.target.value)} onChange={e => setChatID(e.target.value)}
@ -52,8 +98,8 @@ const NotificationSetting = () => {
}} }}
/> />
<Typography mt={0} level='body-xs'> <Typography mt={0} level='body-xs'>
If you don't know your Chat ID, start chat with userinfobot and it will If you don't know your Chat ID, start chat with userinfobot and it
send you your Chat ID.{' '} will send you your Chat ID.{' '}
<a <a
style={{ style={{
textDecoration: 'underline', textDecoration: 'underline',
@ -65,21 +111,33 @@ const NotificationSetting = () => {
</a>{' '} </a>{' '}
to start chat with userinfobot{' '} to start chat with userinfobot{' '}
</Typography> </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>
)}
<Button <Button
sx={{ sx={{
width: '110px', width: '110px',
mb: 1, mb: 1,
}} }}
onClick={() => { onClick={handleSave}
UpdateUserDetails({
chatID: Number(chatID),
}).then(resp => {
resp.json().then(data => {
setUserProfile(data)
})
})
}}
> >
Save Save
</Button> </Button>