103 lines
3.3 KiB
TypeScript
103 lines
3.3 KiB
TypeScript
import { createFileRoute } from '@tanstack/react-router'
|
|
import React, { useState } from 'react'
|
|
import { AppContainer, MyButton, p as MyText, pInput as PInput } from 'web-components'
|
|
import { Send } from 'lucide-react'
|
|
import { alert } from '@/components/ui/dialog-host'
|
|
import { trpc } from '../lib/trpc-client'
|
|
import { ProtectedRoute } from '../components/ProtectedRoute'
|
|
import { AppShell } from '../components/AppShell'
|
|
|
|
export const Route = createFileRoute('/send-notifications')({
|
|
component: () => (
|
|
<ProtectedRoute>
|
|
<AppShell>
|
|
<SendAdminNotifications />
|
|
</AppShell>
|
|
</ProtectedRoute>
|
|
),
|
|
})
|
|
|
|
function SendAdminNotifications() {
|
|
const [title, setTitle] = useState('')
|
|
const [text, setText] = useState('')
|
|
const [imageUrl, setImageUrl] = useState('')
|
|
|
|
const { mutate, isPending } = trpc.superadmin.staff.sendAdminNotification.useMutation({
|
|
onSuccess: (response) => {
|
|
void alert(`Success\n\n${response.message}`)
|
|
setTitle('')
|
|
setText('')
|
|
setImageUrl('')
|
|
},
|
|
onError: (error) => {
|
|
void alert(`Error\n\n${error.message || 'Failed to send notification'}`)
|
|
},
|
|
})
|
|
|
|
const handleSubmit = (e: React.FormEvent) => {
|
|
e.preventDefault()
|
|
if (!title.trim() || !text.trim()) {
|
|
void alert('Title and message are required')
|
|
return
|
|
}
|
|
mutate({
|
|
title: title.trim(),
|
|
text: text.trim(),
|
|
...(imageUrl.trim() ? { imageUrl: imageUrl.trim() } : {}),
|
|
})
|
|
}
|
|
|
|
return (
|
|
<AppContainer>
|
|
<div className="bg-gray-50 p-4">
|
|
<MyText className="mb-1 block text-2xl font-bold text-gray-900">
|
|
Admin Notifs
|
|
</MyText>
|
|
<MyText className="mb-6 block text-gray-600">
|
|
This push notification is delivered to every admin device.
|
|
</MyText>
|
|
|
|
<div className="max-w-2xl rounded-xl border border-gray-200 bg-white p-6 shadow-sm">
|
|
<form onSubmit={handleSubmit} className="space-y-5">
|
|
<PInput
|
|
topLabel="Title"
|
|
placeholder="e.g. New order received"
|
|
value={title}
|
|
onChange={(e) => setTitle(e.target.value)}
|
|
/>
|
|
|
|
<div>
|
|
<MyText className="mb-2 block text-sm font-medium text-gray-700">Message</MyText>
|
|
<textarea
|
|
value={text}
|
|
onChange={(e) => setText(e.target.value)}
|
|
placeholder="Write the notification body..."
|
|
rows={4}
|
|
className="w-full rounded-lg border border-gray-300 bg-white p-3 text-gray-900 outline-none focus:border-blue-500"
|
|
/>
|
|
</div>
|
|
|
|
<PInput
|
|
topLabel="Image URL (optional)"
|
|
placeholder="https://..."
|
|
value={imageUrl}
|
|
onChange={(e) => setImageUrl(e.target.value)}
|
|
/>
|
|
|
|
<MyButton
|
|
type="submit"
|
|
fullWidth
|
|
disabled={isPending || !title.trim() || !text.trim()}
|
|
className={`flex h-12 items-center justify-center gap-2 rounded-lg bg-blue-600 text-white shadow-md transition-colors hover:bg-blue-700 ${
|
|
isPending ? 'opacity-50' : ''
|
|
}`}
|
|
>
|
|
<Send className="h-4 w-4" />
|
|
{isPending ? 'Sending...' : 'Send to all admins'}
|
|
</MyButton>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</AppContainer>
|
|
)
|
|
}
|