diff --git a/admin-spa/src/routes/Settings/Notifications/EditTemplate.tsx b/admin-spa/src/routes/Settings/Notifications/EditTemplate.tsx index cacdf98..d1f8f48 100644 --- a/admin-spa/src/routes/Settings/Notifications/EditTemplate.tsx +++ b/admin-spa/src/routes/Settings/Notifications/EditTemplate.tsx @@ -2,17 +2,27 @@ import React, { useState, useEffect } from 'react'; import { useNavigate, useSearchParams } from 'react-router-dom'; import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'; import { api } from '@/lib/api'; +import { SettingsLayout } from '../components/SettingsLayout'; +import { Card, CardContent } from '@/components/ui/card'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { RichTextEditor } from '@/components/ui/rich-text-editor'; import { Label } from '@/components/ui/label'; -import { Badge } from '@/components/ui/badge'; import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs'; -import { ArrowLeft, Eye, Edit, Save, RotateCcw } from 'lucide-react'; +import { ArrowLeft, Eye, Edit, RotateCcw } from 'lucide-react'; import { toast } from 'sonner'; import { __ } from '@/lib/i18n'; export default function EditTemplate() { + // Mobile responsive check + const [isMobile, setIsMobile] = useState(false); + + useEffect(() => { + const checkMobile = () => setIsMobile(window.innerWidth < 768); + checkMobile(); + window.addEventListener('resize', checkMobile); + return () => window.removeEventListener('resize', checkMobile); + }, []); const navigate = useNavigate(); const [searchParams] = useSearchParams(); const queryClient = useQueryClient(); @@ -43,40 +53,35 @@ export default function EditTemplate() { } }, [template]); - const saveMutation = useMutation({ - mutationFn: async () => { - return api.post('/notifications/templates', { + const handleSave = async () => { + try { + await api.post('/notifications/templates', { eventId, channelId, subject, body, }); - }, - onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['notification-templates'] }); queryClient.invalidateQueries({ queryKey: ['notification-template', eventId, channelId] }); toast.success(__('Template saved successfully')); - navigate(-1); - }, - onError: (error: any) => { + } catch (error: any) { toast.error(error?.message || __('Failed to save template')); - }, - }); + throw error; + } + }; - const resetMutation = useMutation({ - mutationFn: async () => { - return api.del(`/notifications/templates/${eventId}/${channelId}`); - }, - onSuccess: () => { + const handleReset = async () => { + if (!confirm(__('Are you sure you want to reset this template to default?'))) return; + + try { + await api.del(`/notifications/templates/${eventId}/${channelId}`); queryClient.invalidateQueries({ queryKey: ['notification-templates'] }); queryClient.invalidateQueries({ queryKey: ['notification-template', eventId, channelId] }); toast.success(__('Template reset to default')); - navigate(-1); - }, - onError: (error: any) => { + } catch (error: any) { toast.error(error?.message || __('Failed to reset template')); - }, - }); + } + }; // Get variable keys for the rich text editor const variableKeys = Object.keys(variables); @@ -166,88 +171,49 @@ export default function EditTemplate() { `; }; - if (isLoading) { - return ( -
-
{__('Loading...')}
-
- ); - } - if (!eventId || !channelId) { return ( -
+
{__('Invalid template parameters')}
-
+ ); } return ( -
- {/* Header */} -
-
-
-
- -
-

- {__('Edit Template')} -

-

- {template?.event_label} - {template?.channel_label} -

-

- {__('Customize the notification template. Use variables like {customer_name} to personalize messages.')} -

-
-
-
- - -
-
+ + +
- - {/* Subject */} -
- - setSubject(e.target.value)} - placeholder={__('Enter notification subject')} - className="mt-2" - /> -
-
- - {/* Body - Tabs */} -
-
+ } + > + {/* Tabs - Sticky in header area */} +
@@ -259,9 +225,30 @@ export default function EditTemplate() { {__('Preview')} + +
- {/* Editor Tab */} - + {/* Editor Tab */} + {activeTab === 'editor' && ( + + + {/* Subject */} +
+ + setSubject(e.target.value)} + placeholder={__('Enter notification subject')} + /> +

+ {channelId === 'email' + ? __('Email subject line') + : __('Push notification title')} +

+
+ + {/* Body */}
+

+ {__('Use the toolbar to format text and insert variables.')} +

-
+ + + )} - {/* Preview Tab */} - -
- -
+ {/* Preview Tab */} + {activeTab === 'preview' && ( + + + {/* Subject Preview */} +
+ +
+ {subject || {__('(No subject)')}} +
+
+ + {/* Email Preview */} +
+ +