From aff432b74ae33e46ba18c660e89dbae4d90461d6 Mon Sep 17 00:00:00 2001 From: Mo Tarbin Date: Sat, 14 Dec 2024 02:11:59 -0500 Subject: [PATCH] Add notification target update functionality --- src/utils/Fetcher.jsx | 9 ++ src/views/Authorization/Signup.jsx | 7 +- src/views/Settings/NotificationSetting.jsx | 180 ++++++++++++++------- 3 files changed, 132 insertions(+), 64 deletions(-) diff --git a/src/utils/Fetcher.jsx b/src/utils/Fetcher.jsx index 836431e..0bdeb21 100644 --- a/src/utils/Fetcher.jsx +++ b/src/utils/Fetcher.jsx @@ -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 = () => { return Fetch(API_URL + `/payments/create-subscription`, { method: 'GET', @@ -373,6 +381,7 @@ export { UpdateChoreHistory, UpdateChorePriority, UpdateLabel, + UpdateNotificationTarget, UpdatePassword, UpdateThingState, UpdateUserDetails, diff --git a/src/views/Authorization/Signup.jsx b/src/views/Authorization/Signup.jsx index 9e6cc70..d9665be 100644 --- a/src/views/Authorization/Signup.jsx +++ b/src/views/Authorization/Signup.jsx @@ -42,7 +42,7 @@ const SignupView = () => { }) } else { console.log('Login failed', response) - + // Navigate('/login') } }) @@ -103,12 +103,13 @@ const SignupView = () => { signUp(username, password, displayName, email).then(response => { if (response.status === 201) { handleLogin(username, password) + } else if (response.status === 403) { + setError('Signup disabled, please contact admin') } else { console.log('Signup failed') response.json().then(res => { setError(res.error) - } - ) + }) } }) } diff --git a/src/views/Settings/NotificationSetting.jsx b/src/views/Settings/NotificationSetting.jsx index 4ead3b9..5a09ecd 100644 --- a/src/views/Settings/NotificationSetting.jsx +++ b/src/views/Settings/NotificationSetting.jsx @@ -1,85 +1,143 @@ 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 { GetUserProfile, UpdateUserDetails } from '../../utils/Fetcher' +import { UpdateNotificationTarget } from '../../utils/Fetcher' const NotificationSetting = () => { const { userProfile, setUserProfile } = useContext(UserContext) - useEffect(() => { - if (!userProfile) { - GetUserProfile().then(resp => { - resp.json().then(data => { - setUserProfile(data.res) - setChatID(data.res.chatID) - }) - }) - } - }, []) - const [chatID, setChatID] = useState(userProfile?.chatID) + 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 - setNotificationTarget(selected)} + > + + + + {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 + - - 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{' '} - + 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} + + )}