Update regex
This commit is contained in:
@@ -168,13 +168,13 @@ public class UserService : IUserService
|
|||||||
|
|
||||||
public async Task<User> UpdateTelegramChannel(User user, string telegramChannel)
|
public async Task<User> UpdateTelegramChannel(User user, string telegramChannel)
|
||||||
{
|
{
|
||||||
// Validate Telegram channel format (must start with @ and contain only allowed characters)
|
// Validate Telegram channel format (numeric channel ID only)
|
||||||
if (!string.IsNullOrEmpty(telegramChannel))
|
if (!string.IsNullOrEmpty(telegramChannel))
|
||||||
{
|
{
|
||||||
string pattern = @"^@[a-zA-Z0-9_]{5,32}$";
|
string pattern = @"^[0-9]{5,15}$";
|
||||||
if (!Regex.IsMatch(telegramChannel, pattern))
|
if (!Regex.IsMatch(telegramChannel, pattern))
|
||||||
{
|
{
|
||||||
throw new Exception("Invalid Telegram channel format. Must start with @ and be 5-32 characters long, containing only letters, numbers, and underscores.");
|
throw new Exception("Invalid Telegram channel format. Must be numeric channel ID (5-15 digits).");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -14,9 +14,14 @@ type UpdateAvatarForm = {
|
|||||||
avatarUrl: string
|
avatarUrl: string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type UpdateTelegramChannelForm = {
|
||||||
|
telegramChannel: string
|
||||||
|
}
|
||||||
|
|
||||||
function UserInfoSettings() {
|
function UserInfoSettings() {
|
||||||
const [showUpdateModal, setShowUpdateModal] = useState(false)
|
const [showUpdateModal, setShowUpdateModal] = useState(false)
|
||||||
const [showAvatarModal, setShowAvatarModal] = useState(false)
|
const [showAvatarModal, setShowAvatarModal] = useState(false)
|
||||||
|
const [showTelegramModal, setShowTelegramModal] = useState(false)
|
||||||
const queryClient = useQueryClient()
|
const queryClient = useQueryClient()
|
||||||
const { apiUrl } = useApiUrlStore()
|
const { apiUrl } = useApiUrlStore()
|
||||||
const api = new UserClient({}, apiUrl)
|
const api = new UserClient({}, apiUrl)
|
||||||
@@ -38,6 +43,12 @@ function UserInfoSettings() {
|
|||||||
formState: { errors: avatarErrors },
|
formState: { errors: avatarErrors },
|
||||||
} = useForm<UpdateAvatarForm>()
|
} = useForm<UpdateAvatarForm>()
|
||||||
|
|
||||||
|
const {
|
||||||
|
register: registerTelegram,
|
||||||
|
handleSubmit: handleSubmitTelegram,
|
||||||
|
formState: { errors: telegramErrors },
|
||||||
|
} = useForm<UpdateTelegramChannelForm>()
|
||||||
|
|
||||||
const onSubmitAgentName = async (data: UpdateAgentNameForm) => {
|
const onSubmitAgentName = async (data: UpdateAgentNameForm) => {
|
||||||
const toast = new Toast('Updating agent name')
|
const toast = new Toast('Updating agent name')
|
||||||
try {
|
try {
|
||||||
@@ -64,6 +75,19 @@ function UserInfoSettings() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const onSubmitTelegram = async (data: UpdateTelegramChannelForm) => {
|
||||||
|
const toast = new Toast('Updating telegram channel')
|
||||||
|
try {
|
||||||
|
await api.user_UpdateTelegramChannel(data.telegramChannel)
|
||||||
|
queryClient.invalidateQueries({ queryKey: ['user'] })
|
||||||
|
setShowTelegramModal(false)
|
||||||
|
toast.update('success', 'Telegram channel updated successfully')
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error updating telegram channel:', error)
|
||||||
|
toast.update('error', 'Failed to update telegram channel')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="container mx-auto p-4">
|
<div className="container mx-auto p-4">
|
||||||
<div className="bg-base-200 rounded-lg p-6 shadow-lg">
|
<div className="bg-base-200 rounded-lg p-6 shadow-lg">
|
||||||
@@ -108,6 +132,17 @@ function UserInfoSettings() {
|
|||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<div>
|
||||||
|
<label className="font-semibold">Telegram Channel:</label>
|
||||||
|
<p>{user?.telegramChannel || 'Not set'}</p>
|
||||||
|
<button
|
||||||
|
className="btn btn-primary mt-2"
|
||||||
|
onClick={() => setShowTelegramModal(true)}
|
||||||
|
>
|
||||||
|
Update Telegram Channel
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
@@ -178,6 +213,44 @@ function UserInfoSettings() {
|
|||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
</Modal>
|
</Modal>
|
||||||
|
|
||||||
|
<Modal
|
||||||
|
showModal={showTelegramModal}
|
||||||
|
onClose={() => setShowTelegramModal(false)}
|
||||||
|
onSubmit={handleSubmitTelegram(onSubmitTelegram)}
|
||||||
|
titleHeader="Update Telegram Channel"
|
||||||
|
>
|
||||||
|
<div className="form-control w-full">
|
||||||
|
<label className="label">
|
||||||
|
<span className="label-text">Telegram Channel</span>
|
||||||
|
</label>
|
||||||
|
<input
|
||||||
|
type="text"
|
||||||
|
className="input input-bordered w-full"
|
||||||
|
{...registerTelegram('telegramChannel', {
|
||||||
|
required: 'Telegram channel is required',
|
||||||
|
pattern: {
|
||||||
|
value: /^[0-9]{5,15}$/,
|
||||||
|
message: 'Enter numeric channel ID (5-15 digits)'
|
||||||
|
}
|
||||||
|
})}
|
||||||
|
defaultValue={user?.telegramChannel || ''}
|
||||||
|
placeholder="2828543022"
|
||||||
|
/>
|
||||||
|
{telegramErrors.telegramChannel && (
|
||||||
|
<label className="label">
|
||||||
|
<span className="label-text-alt text-error">
|
||||||
|
{telegramErrors.telegramChannel.message}
|
||||||
|
</span>
|
||||||
|
</label>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
<div className="modal-action">
|
||||||
|
<button type="submit" className="btn">
|
||||||
|
Update
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</Modal>
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user