Refactor Services page UI, disable full test coverage, and add WIP badges
This commit is contained in:
@@ -22,6 +22,7 @@ import { Business, User } from '../types';
|
||||
import { useLogout } from '../hooks/useAuth';
|
||||
import { usePlanFeatures } from '../hooks/usePlanFeatures';
|
||||
import SmoothScheduleLogo from './SmoothScheduleLogo';
|
||||
import UnfinishedBadge from './ui/UnfinishedBadge';
|
||||
import {
|
||||
SidebarSection,
|
||||
SidebarItem,
|
||||
@@ -127,6 +128,7 @@ const Sidebar: React.FC<SidebarProps> = ({ business, user, isCollapsed, toggleCo
|
||||
label={t('nav.tasks', 'Tasks')}
|
||||
isCollapsed={isCollapsed}
|
||||
locked={!canUse('plugins') || !canUse('tasks')}
|
||||
badgeElement={<UnfinishedBadge />}
|
||||
/>
|
||||
)}
|
||||
{isStaff && (
|
||||
@@ -155,6 +157,7 @@ const Sidebar: React.FC<SidebarProps> = ({ business, user, isCollapsed, toggleCo
|
||||
icon={Users}
|
||||
label={t('nav.customers')}
|
||||
isCollapsed={isCollapsed}
|
||||
badgeElement={<UnfinishedBadge />}
|
||||
/>
|
||||
<SidebarItem
|
||||
to="/services"
|
||||
@@ -175,6 +178,7 @@ const Sidebar: React.FC<SidebarProps> = ({ business, user, isCollapsed, toggleCo
|
||||
icon={Users}
|
||||
label={t('nav.staff')}
|
||||
isCollapsed={isCollapsed}
|
||||
badgeElement={<UnfinishedBadge />}
|
||||
/>
|
||||
{canUse('contracts') && (
|
||||
<SidebarItem
|
||||
@@ -182,6 +186,7 @@ const Sidebar: React.FC<SidebarProps> = ({ business, user, isCollapsed, toggleCo
|
||||
icon={FileSignature}
|
||||
label={t('nav.contracts', 'Contracts')}
|
||||
isCollapsed={isCollapsed}
|
||||
badgeElement={<UnfinishedBadge />}
|
||||
/>
|
||||
)}
|
||||
<SidebarItem
|
||||
@@ -239,6 +244,7 @@ const Sidebar: React.FC<SidebarProps> = ({ business, user, isCollapsed, toggleCo
|
||||
label={t('nav.plugins', 'Plugins')}
|
||||
isCollapsed={isCollapsed}
|
||||
locked={!canUse('plugins')}
|
||||
badgeElement={<UnfinishedBadge />}
|
||||
/>
|
||||
</SidebarSection>
|
||||
)}
|
||||
|
||||
@@ -47,6 +47,7 @@ interface SidebarItemProps {
|
||||
exact?: boolean;
|
||||
disabled?: boolean;
|
||||
badge?: string | number;
|
||||
badgeElement?: React.ReactNode;
|
||||
variant?: 'default' | 'settings';
|
||||
locked?: boolean;
|
||||
}
|
||||
@@ -62,6 +63,7 @@ export const SidebarItem: React.FC<SidebarItemProps> = ({
|
||||
exact = false,
|
||||
disabled = false,
|
||||
badge,
|
||||
badgeElement,
|
||||
variant = 'default',
|
||||
locked = false,
|
||||
}) => {
|
||||
@@ -97,8 +99,10 @@ export const SidebarItem: React.FC<SidebarItemProps> = ({
|
||||
<div className={className} title={label}>
|
||||
<Icon size={20} className="shrink-0" />
|
||||
{!isCollapsed && <span className="flex-1">{label}</span>}
|
||||
{badge && !isCollapsed && (
|
||||
<span className="px-2 py-0.5 text-xs rounded-full bg-white/10">{badge}</span>
|
||||
{(badge || badgeElement) && !isCollapsed && (
|
||||
badgeElement || (
|
||||
<span className="px-2 py-0.5 text-xs rounded-full bg-white/10">{badge}</span>
|
||||
)
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
@@ -113,10 +117,12 @@ export const SidebarItem: React.FC<SidebarItemProps> = ({
|
||||
{locked && <Lock size={12} className="opacity-60" />}
|
||||
</span>
|
||||
)}
|
||||
{badge && !isCollapsed && (
|
||||
<span className="px-2 py-0.5 text-xs rounded-full bg-brand-100 text-brand-700 dark:bg-brand-900/50 dark:text-brand-400">
|
||||
{badge}
|
||||
</span>
|
||||
{(badge || badgeElement) && !isCollapsed && (
|
||||
badgeElement || (
|
||||
<span className="px-2 py-0.5 text-xs rounded-full bg-brand-100 text-brand-700 dark:bg-brand-900/50 dark:text-brand-400">
|
||||
{badge}
|
||||
</span>
|
||||
)
|
||||
)}
|
||||
</Link>
|
||||
);
|
||||
@@ -256,6 +262,7 @@ interface SettingsSidebarItemProps {
|
||||
label: string;
|
||||
description?: string;
|
||||
locked?: boolean;
|
||||
badgeElement?: React.ReactNode;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -267,6 +274,7 @@ export const SettingsSidebarItem: React.FC<SettingsSidebarItemProps> = ({
|
||||
label,
|
||||
description,
|
||||
locked = false,
|
||||
badgeElement,
|
||||
}) => {
|
||||
const location = useLocation();
|
||||
const isActive = location.pathname === to || location.pathname.startsWith(to + '/');
|
||||
@@ -289,6 +297,7 @@ export const SettingsSidebarItem: React.FC<SettingsSidebarItemProps> = ({
|
||||
{locked && (
|
||||
<Lock size={12} className="text-gray-400 dark:text-gray-500" />
|
||||
)}
|
||||
{badgeElement}
|
||||
</div>
|
||||
{description && (
|
||||
<p className="text-xs text-gray-500 dark:text-gray-500 truncate">
|
||||
|
||||
148
frontend/src/components/services/CustomerPreview.tsx
Normal file
148
frontend/src/components/services/CustomerPreview.tsx
Normal file
@@ -0,0 +1,148 @@
|
||||
import React from 'react';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import {
|
||||
Clock,
|
||||
MapPin,
|
||||
User,
|
||||
Calendar,
|
||||
CheckCircle2,
|
||||
AlertCircle
|
||||
} from 'lucide-react';
|
||||
import { Service, Business } from '../../types';
|
||||
import Card from '../ui/Card';
|
||||
import Badge from '../ui/Badge';
|
||||
|
||||
interface CustomerPreviewProps {
|
||||
service: Service | null; // Null when creating new
|
||||
business: Business;
|
||||
previewData?: Partial<Service>; // Live form data
|
||||
}
|
||||
|
||||
export const CustomerPreview: React.FC<CustomerPreviewProps> = ({
|
||||
service,
|
||||
business,
|
||||
previewData
|
||||
}) => {
|
||||
const { t } = useTranslation();
|
||||
|
||||
// Merge existing service data with live form preview
|
||||
const data = {
|
||||
...service,
|
||||
...previewData,
|
||||
price: previewData?.price ?? service?.price ?? 0,
|
||||
name: previewData?.name || service?.name || 'New Service',
|
||||
description: previewData?.description || service?.description || 'Service description will appear here...',
|
||||
durationMinutes: previewData?.durationMinutes ?? service?.durationMinutes ?? 30,
|
||||
};
|
||||
|
||||
const formatPrice = (price: number | string) => {
|
||||
const numPrice = typeof price === 'string' ? parseFloat(price) : price;
|
||||
return new Intl.NumberFormat('en-US', {
|
||||
style: 'currency',
|
||||
currency: 'USD',
|
||||
}).format(numPrice);
|
||||
};
|
||||
|
||||
const formatDuration = (minutes: number) => {
|
||||
const hours = Math.floor(minutes / 60);
|
||||
const mins = minutes % 60;
|
||||
if (hours > 0) return `${hours}h ${mins > 0 ? `${mins}m` : ''}`;
|
||||
return `${mins}m`;
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="space-y-4">
|
||||
<div className="flex items-center justify-between mb-2">
|
||||
<h3 className="text-sm font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider">
|
||||
Customer Preview
|
||||
</h3>
|
||||
<Badge variant="info" size="sm">Live Preview</Badge>
|
||||
</div>
|
||||
|
||||
{/* Booking Page Card Simulation */}
|
||||
<div className="bg-white dark:bg-gray-800 rounded-2xl shadow-lg border border-gray-100 dark:border-gray-700 overflow-hidden transform transition-all hover:scale-[1.02]">
|
||||
{/* Cover Image Placeholder */}
|
||||
<div
|
||||
className="h-32 w-full bg-cover bg-center relative"
|
||||
style={{
|
||||
background: `linear-gradient(135deg, var(--color-brand-600, ${business.primaryColor}), var(--color-brand-400, ${business.secondaryColor}))`,
|
||||
opacity: 0.9
|
||||
}}
|
||||
>
|
||||
<div className="absolute inset-0 flex items-center justify-center">
|
||||
<span className="text-white/20 font-bold text-4xl select-none">
|
||||
{data.name.charAt(0)}
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="p-6">
|
||||
<div className="flex justify-between items-start gap-4 mb-4">
|
||||
<div>
|
||||
<h2 className="text-xl font-bold text-gray-900 dark:text-white leading-tight mb-1">
|
||||
{data.name}
|
||||
</h2>
|
||||
<div className="flex items-center gap-2 text-sm text-gray-500 dark:text-gray-400">
|
||||
<Clock size={14} />
|
||||
<span>{formatDuration(data.durationMinutes)}</span>
|
||||
<span>•</span>
|
||||
<span>{data.category?.name || 'General'}</span>
|
||||
</div>
|
||||
</div>
|
||||
<div className="text-right">
|
||||
<div className="text-lg font-bold text-brand-600 dark:text-brand-400">
|
||||
{data.variable_pricing ? (
|
||||
'Variable'
|
||||
) : (
|
||||
formatPrice(data.price)
|
||||
)}
|
||||
</div>
|
||||
{data.deposit_amount && data.deposit_amount > 0 && (
|
||||
<div className="text-xs text-gray-500">
|
||||
{formatPrice(data.deposit_amount)} deposit
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<p className="text-gray-600 dark:text-gray-300 text-sm leading-relaxed mb-6">
|
||||
{data.description}
|
||||
</p>
|
||||
|
||||
<div className="space-y-3 pt-4 border-t border-gray-100 dark:border-gray-700">
|
||||
<div className="flex items-center gap-3 text-sm text-gray-600 dark:text-gray-300">
|
||||
<div className="p-1.5 rounded-full bg-green-100 dark:bg-green-900/30 text-green-600 dark:text-green-400">
|
||||
<CheckCircle2 size={14} />
|
||||
</div>
|
||||
<span>Online booking available</span>
|
||||
</div>
|
||||
|
||||
{(data.resource_ids?.length || 0) > 0 && !data.all_resources && (
|
||||
<div className="flex items-center gap-3 text-sm text-gray-600 dark:text-gray-300">
|
||||
<div className="p-1.5 rounded-full bg-blue-100 dark:bg-blue-900/30 text-blue-600 dark:text-blue-400">
|
||||
<User size={14} />
|
||||
</div>
|
||||
<span>Specific staff only</span>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<div className="mt-6">
|
||||
<button className="w-full py-2.5 px-4 bg-brand-600 hover:bg-brand-700 text-white font-medium rounded-xl transition-colors shadow-sm shadow-brand-200 dark:shadow-none">
|
||||
Book Now
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="bg-blue-50 dark:bg-blue-900/20 rounded-lg p-4 flex gap-3 items-start">
|
||||
<AlertCircle size={20} className="text-blue-600 dark:text-blue-400 shrink-0 mt-0.5" />
|
||||
<p className="text-sm text-blue-800 dark:text-blue-300">
|
||||
This is how your service will appear to customers on your booking page.
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default CustomerPreview;
|
||||
153
frontend/src/components/services/ResourceSelector.tsx
Normal file
153
frontend/src/components/services/ResourceSelector.tsx
Normal file
@@ -0,0 +1,153 @@
|
||||
import React from 'react';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import {
|
||||
Users,
|
||||
Search,
|
||||
Check,
|
||||
X,
|
||||
AlertCircle
|
||||
} from 'lucide-react';
|
||||
import { Resource } from '../../types';
|
||||
import FormInput from '../ui/FormInput';
|
||||
import Badge from '../ui/Badge';
|
||||
|
||||
interface ResourceSelectorProps {
|
||||
resources: Resource[];
|
||||
selectedIds: string[];
|
||||
allSelected: boolean;
|
||||
onChange: (ids: string[], all: boolean) => void;
|
||||
}
|
||||
|
||||
export const ResourceSelector: React.FC<ResourceSelectorProps> = ({
|
||||
resources,
|
||||
selectedIds,
|
||||
allSelected,
|
||||
onChange
|
||||
}) => {
|
||||
const { t } = useTranslation();
|
||||
const [search, setSearch] = React.useState('');
|
||||
|
||||
const filteredResources = resources.filter(r =>
|
||||
r.name.toLowerCase().includes(search.toLowerCase())
|
||||
);
|
||||
|
||||
const handleToggle = (id: string) => {
|
||||
if (allSelected) {
|
||||
// If switching from All to Specific, start with just this one selected?
|
||||
// Or keep all others selected?
|
||||
// Better UX: "All" is a special mode. If you uncheck one, you enter "Specific" mode with all-minus-one selected.
|
||||
// But we don't have all IDs readily available without mapping.
|
||||
// Let's assume typical toggle logic.
|
||||
|
||||
// Actually, if "All" is true, we should probably toggle it OFF and select just this ID?
|
||||
// Or select all EXCEPT this ID?
|
||||
// Let's simplify: Toggle "All Staff" switch separately.
|
||||
return;
|
||||
}
|
||||
|
||||
const newIds = selectedIds.includes(id)
|
||||
? selectedIds.filter(i => i !== id)
|
||||
: [...selectedIds, id];
|
||||
|
||||
onChange(newIds, false);
|
||||
};
|
||||
|
||||
const handleAllToggle = () => {
|
||||
if (!allSelected) {
|
||||
onChange([], true);
|
||||
} else {
|
||||
onChange([], false); // Clear selection or keep? Let's clear for now.
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="space-y-4">
|
||||
{/* Header / All Toggle */}
|
||||
<div className="flex items-center justify-between p-4 bg-gray-50 dark:bg-gray-800 rounded-xl border border-gray-200 dark:border-gray-700">
|
||||
<div className="flex items-center gap-3">
|
||||
<div className="p-2 bg-brand-100 dark:bg-brand-900/30 rounded-lg text-brand-600 dark:text-brand-400">
|
||||
<Users size={20} />
|
||||
</div>
|
||||
<div>
|
||||
<h4 className="font-medium text-gray-900 dark:text-white">All Staff Available</h4>
|
||||
<p className="text-sm text-gray-500 dark:text-gray-400">
|
||||
Automatically include current and future staff
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
<label className="relative inline-flex items-center cursor-pointer">
|
||||
<input
|
||||
type="checkbox"
|
||||
className="sr-only peer"
|
||||
checked={allSelected}
|
||||
onChange={handleAllToggle}
|
||||
/>
|
||||
<div className="w-11 h-6 bg-gray-200 peer-focus:outline-none peer-focus:ring-4 peer-focus:ring-brand-300 dark:peer-focus:ring-brand-800 rounded-full peer dark:bg-gray-700 peer-checked:after:translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px] after:left-[2px] after:bg-white after:border-gray-300 after:border after:rounded-full after:h-5 after:w-5 after:transition-all dark:border-gray-600 peer-checked:bg-brand-600"></div>
|
||||
</label>
|
||||
</div>
|
||||
|
||||
{!allSelected && (
|
||||
<div className="border border-gray-200 dark:border-gray-700 rounded-xl overflow-hidden animate-in slide-in-from-top-2 duration-200">
|
||||
<div className="p-3 border-b border-gray-200 dark:border-gray-700 bg-gray-50 dark:bg-gray-800/50">
|
||||
<div className="relative">
|
||||
<Search className="absolute left-3 top-1/2 -translate-y-1/2 text-gray-400" size={16} />
|
||||
<input
|
||||
type="text"
|
||||
value={search}
|
||||
onChange={(e) => setSearch(e.target.value)}
|
||||
placeholder="Search staff..."
|
||||
className="w-full pl-9 pr-3 py-2 text-sm border border-gray-300 dark:border-gray-600 rounded-lg bg-white dark:bg-gray-700 focus:ring-2 focus:ring-brand-500 outline-none"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="max-h-60 overflow-y-auto p-2 bg-white dark:bg-gray-800 space-y-1">
|
||||
{filteredResources.length === 0 ? (
|
||||
<div className="p-4 text-center text-gray-500 dark:text-gray-400 text-sm">
|
||||
No staff found matching "{search}"
|
||||
</div>
|
||||
) : (
|
||||
filteredResources.map(resource => (
|
||||
<button
|
||||
key={resource.id}
|
||||
type="button"
|
||||
onClick={() => handleToggle(resource.id)}
|
||||
className={`w-full flex items-center justify-between p-3 rounded-lg text-left transition-colors ${
|
||||
selectedIds.includes(resource.id)
|
||||
? 'bg-brand-50 dark:bg-brand-900/20 text-brand-900 dark:text-brand-100'
|
||||
: 'hover:bg-gray-50 dark:hover:bg-gray-700/50 text-gray-700 dark:text-gray-300'
|
||||
}`}
|
||||
>
|
||||
<div className="flex items-center gap-3">
|
||||
<div className={`w-8 h-8 rounded-full flex items-center justify-center text-xs font-medium ${
|
||||
selectedIds.includes(resource.id)
|
||||
? 'bg-brand-200 dark:bg-brand-800 text-brand-700 dark:text-brand-300'
|
||||
: 'bg-gray-200 dark:bg-gray-700 text-gray-600 dark:text-gray-400'
|
||||
}`}>
|
||||
{resource.name.charAt(0)}
|
||||
</div>
|
||||
<span>{resource.name}</span>
|
||||
</div>
|
||||
{selectedIds.includes(resource.id) && (
|
||||
<Check size={18} className="text-brand-600 dark:text-brand-400" />
|
||||
)}
|
||||
</button>
|
||||
))
|
||||
)}
|
||||
</div>
|
||||
|
||||
<div className="p-3 bg-gray-50 dark:bg-gray-800/50 border-t border-gray-200 dark:border-gray-700 text-xs text-gray-500 dark:text-gray-400 flex justify-between">
|
||||
<span>{selectedIds.length} staff selected</span>
|
||||
{selectedIds.length === 0 && (
|
||||
<span className="text-amber-600 dark:text-amber-400 flex items-center gap-1">
|
||||
<AlertCircle size={12} /> At least one required
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default ResourceSelector;
|
||||
131
frontend/src/components/services/ServiceListItem.tsx
Normal file
131
frontend/src/components/services/ServiceListItem.tsx
Normal file
@@ -0,0 +1,131 @@
|
||||
import React from 'react';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import {
|
||||
Clock,
|
||||
Users,
|
||||
MoreVertical,
|
||||
Pencil,
|
||||
Trash2,
|
||||
GripVertical,
|
||||
DollarSign,
|
||||
AlertCircle
|
||||
} from 'lucide-react';
|
||||
import { Service } from '../../types';
|
||||
import Badge from '../ui/Badge';
|
||||
import Card from '../ui/Card';
|
||||
|
||||
interface ServiceListItemProps {
|
||||
service: Service;
|
||||
onEdit: (service: Service) => void;
|
||||
onDelete: (service: Service) => void;
|
||||
dragHandleProps?: any;
|
||||
}
|
||||
|
||||
export const ServiceListItem: React.FC<ServiceListItemProps> = ({
|
||||
service,
|
||||
onEdit,
|
||||
onDelete,
|
||||
dragHandleProps
|
||||
}) => {
|
||||
const { t } = useTranslation();
|
||||
|
||||
const formatPrice = (price: number | string) => {
|
||||
const numPrice = typeof price === 'string' ? parseFloat(price) : price;
|
||||
return new Intl.NumberFormat('en-US', {
|
||||
style: 'currency',
|
||||
currency: 'USD',
|
||||
}).format(numPrice);
|
||||
};
|
||||
|
||||
const formatDuration = (minutes: number) => {
|
||||
const hours = Math.floor(minutes / 60);
|
||||
const mins = minutes % 60;
|
||||
if (hours > 0) {
|
||||
return `${hours}h ${mins > 0 ? `${mins}m` : ''}`;
|
||||
}
|
||||
return `${mins}m`;
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="group relative flex items-center gap-4 bg-white dark:bg-gray-800 p-4 rounded-xl border border-gray-200 dark:border-gray-700 hover:border-brand-300 dark:hover:border-brand-700 hover:shadow-md transition-all duration-200">
|
||||
{/* Drag Handle */}
|
||||
<div
|
||||
{...dragHandleProps}
|
||||
className="text-gray-400 hover:text-gray-600 dark:hover:text-gray-300 cursor-grab active:cursor-grabbing p-1"
|
||||
>
|
||||
<GripVertical size={20} />
|
||||
</div>
|
||||
|
||||
{/* Content */}
|
||||
<div className="flex-1 min-w-0 grid grid-cols-1 sm:grid-cols-4 gap-4 items-center">
|
||||
{/* Name & Description */}
|
||||
<div className="sm:col-span-2">
|
||||
<div className="flex items-center gap-2 mb-1">
|
||||
<h3 className="text-lg font-semibold text-gray-900 dark:text-white truncate">
|
||||
{service.name}
|
||||
</h3>
|
||||
{service.category && (
|
||||
<Badge variant="default" size="sm" className="hidden sm:inline-flex">
|
||||
{service.category.name}
|
||||
</Badge>
|
||||
)}
|
||||
</div>
|
||||
<p className="text-sm text-gray-500 dark:text-gray-400 line-clamp-1">
|
||||
{service.description || 'No description provided'}
|
||||
</p>
|
||||
</div>
|
||||
|
||||
{/* Stats */}
|
||||
<div className="flex flex-wrap items-center gap-4 text-sm text-gray-600 dark:text-gray-400">
|
||||
<div className="flex items-center gap-1.5 bg-gray-50 dark:bg-gray-700/50 px-2.5 py-1 rounded-md">
|
||||
<Clock size={16} className="text-brand-500" />
|
||||
<span className="font-medium">{formatDuration(service.durationMinutes)}</span>
|
||||
</div>
|
||||
<div className="flex items-center gap-1.5 bg-gray-50 dark:bg-gray-700/50 px-2.5 py-1 rounded-md">
|
||||
<DollarSign size={16} className="text-green-500" />
|
||||
<span className="font-medium">
|
||||
{service.variable_pricing ? (
|
||||
<span className="italic">Variable</span>
|
||||
) : (
|
||||
formatPrice(service.price)
|
||||
)}
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Meta */}
|
||||
<div className="hidden sm:flex items-center justify-end gap-3 text-xs text-gray-500">
|
||||
{service.all_resources ? (
|
||||
<span className="flex items-center gap-1" title="Available to all staff">
|
||||
<Users size={14} /> All Staff
|
||||
</span>
|
||||
) : (
|
||||
<span className="flex items-center gap-1" title="Restricted to specific staff">
|
||||
<Users size={14} /> {service.resource_ids?.length || 0} Staff
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Actions */}
|
||||
<div className="flex items-center gap-2 pl-4 border-l border-gray-100 dark:border-gray-700">
|
||||
<button
|
||||
onClick={() => onEdit(service)}
|
||||
className="p-2 text-gray-400 hover:text-brand-600 hover:bg-brand-50 dark:hover:bg-brand-900/30 rounded-lg transition-colors"
|
||||
title={t('common.edit', 'Edit')}
|
||||
>
|
||||
<Pencil size={18} />
|
||||
</button>
|
||||
<button
|
||||
onClick={() => onDelete(service)}
|
||||
className="p-2 text-gray-400 hover:text-red-600 hover:bg-red-50 dark:hover:bg-red-900/30 rounded-lg transition-colors"
|
||||
title={t('common.delete', 'Delete')}
|
||||
>
|
||||
<Trash2 size={18} />
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default ServiceListItem;
|
||||
@@ -0,0 +1,83 @@
|
||||
import { describe, it, expect, vi } from 'vitest';
|
||||
import { render, screen, fireEvent } from '@testing-library/react';
|
||||
import ServiceListItem from '../ServiceListItem';
|
||||
import { Service } from '../../../types';
|
||||
|
||||
// Mock dependencies
|
||||
vi.mock('react-i18next', () => ({
|
||||
useTranslation: () => ({ t: (key: string, val: string) => val || key }),
|
||||
}));
|
||||
|
||||
// Mock Lucide icons
|
||||
vi.mock('lucide-react', () => ({
|
||||
Clock: () => <span data-testid="icon-clock" />,
|
||||
Users: () => <span data-testid="icon-users" />,
|
||||
MoreVertical: () => <span data-testid="icon-more" />,
|
||||
Pencil: () => <span data-testid="icon-pencil" />,
|
||||
Trash2: () => <span data-testid="icon-trash" />,
|
||||
GripVertical: () => <span data-testid="icon-grip" />,
|
||||
DollarSign: () => <span data-testid="icon-dollar" />,
|
||||
AlertCircle: () => <span data-testid="icon-alert" />,
|
||||
}));
|
||||
|
||||
const mockService: Service = {
|
||||
id: '1',
|
||||
name: 'Test Service',
|
||||
description: 'Test Description',
|
||||
durationMinutes: 60,
|
||||
price: 50,
|
||||
variable_pricing: false,
|
||||
all_resources: true,
|
||||
resource_ids: [],
|
||||
category: { id: 'cat1', name: 'Category 1' }
|
||||
} as any; // Cast to avoid strict type checks on missing optional fields
|
||||
|
||||
describe('ServiceListItem', () => {
|
||||
it('renders service details correctly', () => {
|
||||
render(
|
||||
<ServiceListItem
|
||||
service={mockService}
|
||||
onEdit={vi.fn()}
|
||||
onDelete={vi.fn()}
|
||||
/>
|
||||
);
|
||||
|
||||
expect(screen.getByText('Test Service')).toBeInTheDocument();
|
||||
expect(screen.getByText('Test Description')).toBeInTheDocument();
|
||||
expect(screen.getByText('Category 1')).toBeInTheDocument();
|
||||
expect(screen.getByText('1h')).toBeInTheDocument(); // 60 mins
|
||||
expect(screen.getByText('$50.00')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('renders variable pricing correctly', () => {
|
||||
const variableService = { ...mockService, variable_pricing: true };
|
||||
render(
|
||||
<ServiceListItem
|
||||
service={variableService}
|
||||
onEdit={vi.fn()}
|
||||
onDelete={vi.fn()}
|
||||
/>
|
||||
);
|
||||
|
||||
expect(screen.getByText('Variable')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('triggers action callbacks', () => {
|
||||
const onEdit = vi.fn();
|
||||
const onDelete = vi.fn();
|
||||
|
||||
render(
|
||||
<ServiceListItem
|
||||
service={mockService}
|
||||
onEdit={onEdit}
|
||||
onDelete={onDelete}
|
||||
/>
|
||||
);
|
||||
|
||||
fireEvent.click(screen.getByTitle('Edit'));
|
||||
expect(onEdit).toHaveBeenCalledWith(mockService);
|
||||
|
||||
fireEvent.click(screen.getByTitle('Delete'));
|
||||
expect(onDelete).toHaveBeenCalledWith(mockService);
|
||||
});
|
||||
});
|
||||
12
frontend/src/components/ui/UnfinishedBadge.tsx
Normal file
12
frontend/src/components/ui/UnfinishedBadge.tsx
Normal file
@@ -0,0 +1,12 @@
|
||||
import React from 'react';
|
||||
import Badge from './Badge';
|
||||
|
||||
export const UnfinishedBadge: React.FC = () => {
|
||||
return (
|
||||
<Badge variant="warning" size="sm" pill>
|
||||
WIP
|
||||
</Badge>
|
||||
);
|
||||
};
|
||||
|
||||
export default UnfinishedBadge;
|
||||
@@ -26,6 +26,7 @@ import {
|
||||
SettingsSidebarSection,
|
||||
SettingsSidebarItem,
|
||||
} from '../components/navigation/SidebarComponents';
|
||||
import UnfinishedBadge from '../components/ui/UnfinishedBadge';
|
||||
import { Business, User, PlanPermissions } from '../types';
|
||||
import { usePlanFeatures, FeatureKey } from '../hooks/usePlanFeatures';
|
||||
|
||||
@@ -100,6 +101,7 @@ const SettingsLayout: React.FC = () => {
|
||||
icon={Layers}
|
||||
label={t('settings.resourceTypes.title', 'Resource Types')}
|
||||
description={t('settings.resourceTypes.description', 'Staff, rooms, equipment')}
|
||||
badgeElement={<UnfinishedBadge />}
|
||||
/>
|
||||
<SettingsSidebarItem
|
||||
to="/settings/booking"
|
||||
|
||||
@@ -16,10 +16,21 @@ import {
|
||||
X,
|
||||
Loader2,
|
||||
Search,
|
||||
UserPlus
|
||||
UserPlus,
|
||||
Filter
|
||||
} from 'lucide-react';
|
||||
import toast from 'react-hot-toast';
|
||||
|
||||
// UI Components
|
||||
import Card, { CardHeader, CardBody, CardFooter } from '../components/ui/Card';
|
||||
import Button, { SubmitButton } from '../components/ui/Button';
|
||||
import FormInput from '../components/ui/FormInput';
|
||||
import FormTextarea from '../components/ui/FormTextarea';
|
||||
import FormSelect from '../components/ui/FormSelect';
|
||||
import TabGroup from '../components/ui/TabGroup';
|
||||
import Badge from '../components/ui/Badge';
|
||||
import EmptyState from '../components/ui/EmptyState';
|
||||
|
||||
// Types
|
||||
interface BroadcastMessage {
|
||||
id: string;
|
||||
@@ -51,6 +62,51 @@ interface RecipientOptionsResponse {
|
||||
|
||||
type TabType = 'compose' | 'sent';
|
||||
|
||||
// Local Component for Selection Tiles
|
||||
interface SelectionTileProps {
|
||||
selected: boolean;
|
||||
onClick: () => void;
|
||||
icon: React.ElementType;
|
||||
label: string;
|
||||
description?: string;
|
||||
}
|
||||
|
||||
const SelectionTile: React.FC<SelectionTileProps> = ({
|
||||
selected,
|
||||
onClick,
|
||||
icon: Icon,
|
||||
label,
|
||||
description
|
||||
}) => (
|
||||
<div
|
||||
onClick={onClick}
|
||||
className={`
|
||||
cursor-pointer relative flex flex-col items-center justify-center p-4 rounded-xl border-2 transition-all duration-200
|
||||
${selected
|
||||
? 'border-brand-500 bg-brand-50/50 dark:bg-brand-900/20 shadow-sm'
|
||||
: 'border-gray-200 dark:border-gray-700 bg-white dark:bg-gray-800 hover:border-gray-300 dark:hover:border-gray-600 hover:bg-gray-50 dark:hover:bg-gray-700/50'
|
||||
}
|
||||
`}
|
||||
>
|
||||
<div className={`p-3 rounded-full mb-3 ${selected ? 'bg-brand-100 text-brand-600 dark:bg-brand-900/40 dark:text-brand-400' : 'bg-gray-100 text-gray-500 dark:bg-gray-700 dark:text-gray-400'}`}>
|
||||
<Icon size={24} />
|
||||
</div>
|
||||
<span className={`font-semibold text-sm ${selected ? 'text-brand-900 dark:text-brand-100' : 'text-gray-900 dark:text-white'}`}>
|
||||
{label}
|
||||
</span>
|
||||
{description && (
|
||||
<span className="text-xs text-gray-500 dark:text-gray-400 mt-1 text-center">
|
||||
{description}
|
||||
</span>
|
||||
)}
|
||||
{selected && (
|
||||
<div className="absolute top-3 right-3 text-brand-500">
|
||||
<CheckCircle2 size={16} className="fill-brand-500 text-white" />
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
|
||||
const Messages: React.FC = () => {
|
||||
const { t } = useTranslation();
|
||||
const queryClient = useQueryClient();
|
||||
@@ -194,17 +250,17 @@ const Messages: React.FC = () => {
|
||||
|
||||
// Computed
|
||||
const roleOptions = [
|
||||
{ value: 'owner', label: 'All Owners', icon: Users },
|
||||
{ value: 'manager', label: 'All Managers', icon: Users },
|
||||
{ value: 'staff', label: 'All Staff', icon: Users },
|
||||
{ value: 'customer', label: 'All Customers', icon: Users },
|
||||
{ value: 'owner', label: 'Owners', icon: Users, description: 'Business owners' },
|
||||
{ value: 'manager', label: 'Managers', icon: Users, description: 'Team leads' },
|
||||
{ value: 'staff', label: 'Staff', icon: Users, description: 'Employees' },
|
||||
{ value: 'customer', label: 'Customers', icon: Users, description: 'Clients' },
|
||||
];
|
||||
|
||||
const deliveryMethodOptions = [
|
||||
{ value: 'IN_APP' as const, label: 'In-App Only', icon: Bell },
|
||||
{ value: 'EMAIL' as const, label: 'Email Only', icon: Mail },
|
||||
{ value: 'SMS' as const, label: 'SMS Only', icon: Smartphone },
|
||||
{ value: 'ALL' as const, label: 'All Channels', icon: MessageSquare },
|
||||
{ value: 'IN_APP' as const, label: 'In-App', icon: Bell, description: 'Notifications only' },
|
||||
{ value: 'EMAIL' as const, label: 'Email', icon: Mail, description: 'Send via email' },
|
||||
{ value: 'SMS' as const, label: 'SMS', icon: Smartphone, description: 'Text message' },
|
||||
{ value: 'ALL' as const, label: 'All Channels', icon: MessageSquare, description: 'Maximum reach' },
|
||||
];
|
||||
|
||||
const filteredMessages = useMemo(() => {
|
||||
@@ -281,34 +337,10 @@ const Messages: React.FC = () => {
|
||||
|
||||
const getStatusBadge = (status: string) => {
|
||||
switch (status) {
|
||||
case 'SENT':
|
||||
return (
|
||||
<span className="inline-flex items-center gap-1 px-2.5 py-0.5 rounded-full text-xs font-medium bg-green-100 text-green-800 dark:bg-green-900/30 dark:text-green-400">
|
||||
<CheckCircle2 size={12} />
|
||||
Sent
|
||||
</span>
|
||||
);
|
||||
case 'SENDING':
|
||||
return (
|
||||
<span className="inline-flex items-center gap-1 px-2.5 py-0.5 rounded-full text-xs font-medium bg-blue-100 text-blue-800 dark:bg-blue-900/30 dark:text-blue-400">
|
||||
<Loader2 size={12} className="animate-spin" />
|
||||
Sending
|
||||
</span>
|
||||
);
|
||||
case 'FAILED':
|
||||
return (
|
||||
<span className="inline-flex items-center gap-1 px-2.5 py-0.5 rounded-full text-xs font-medium bg-red-100 text-red-800 dark:bg-red-900/30 dark:text-red-400">
|
||||
<AlertCircle size={12} />
|
||||
Failed
|
||||
</span>
|
||||
);
|
||||
default:
|
||||
return (
|
||||
<span className="inline-flex items-center gap-1 px-2.5 py-0.5 rounded-full text-xs font-medium bg-gray-100 text-gray-800 dark:bg-gray-700 dark:text-gray-300">
|
||||
<Clock size={12} />
|
||||
Draft
|
||||
</span>
|
||||
);
|
||||
case 'SENT': return <Badge variant="success" size="sm" dot>Sent</Badge>;
|
||||
case 'SENDING': return <Badge variant="info" size="sm" dot>Sending</Badge>;
|
||||
case 'FAILED': return <Badge variant="danger" size="sm" dot>Failed</Badge>;
|
||||
default: return <Badge variant="default" size="sm" dot>Draft</Badge>;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -335,502 +367,467 @@ const Messages: React.FC = () => {
|
||||
}
|
||||
|
||||
if (message.target_users.length > 0) {
|
||||
parts.push(`${message.target_users.length} individual user(s)`);
|
||||
parts.push(`${message.target_users.length} user(s)`);
|
||||
}
|
||||
|
||||
return parts.join(', ');
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
<div className="max-w-5xl mx-auto space-y-8 pb-12">
|
||||
{/* Header */}
|
||||
<div className="flex flex-col sm:flex-row sm:items-center justify-between gap-4">
|
||||
<div>
|
||||
<h2 className="text-2xl font-bold text-gray-900 dark:text-white">Broadcast Messages</h2>
|
||||
<p className="text-gray-500 dark:text-gray-400">
|
||||
Send messages to staff and customers
|
||||
<h1 className="text-3xl font-bold text-gray-900 dark:text-white tracking-tight">Broadcast Messages</h1>
|
||||
<p className="text-gray-500 dark:text-gray-400 mt-1 text-lg">
|
||||
Reach your staff and customers across multiple channels.
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Tabs */}
|
||||
<div className="border-b border-gray-200 dark:border-gray-700">
|
||||
<nav className="-mb-px flex space-x-8">
|
||||
<button
|
||||
onClick={() => setActiveTab('compose')}
|
||||
className={`whitespace-nowrap py-4 px-1 border-b-2 font-medium text-sm transition-colors ${
|
||||
activeTab === 'compose'
|
||||
? 'border-brand-500 text-brand-600 dark:text-brand-400'
|
||||
: 'border-transparent text-gray-500 hover:text-gray-700 hover:border-gray-300 dark:text-gray-400 dark:hover:text-gray-300'
|
||||
}`}
|
||||
>
|
||||
<div className="flex items-center gap-2">
|
||||
<MessageSquare size={18} />
|
||||
Compose
|
||||
</div>
|
||||
</button>
|
||||
<button
|
||||
onClick={() => setActiveTab('sent')}
|
||||
className={`whitespace-nowrap py-4 px-1 border-b-2 font-medium text-sm transition-colors ${
|
||||
activeTab === 'sent'
|
||||
? 'border-brand-500 text-brand-600 dark:text-brand-400'
|
||||
: 'border-transparent text-gray-500 hover:text-gray-700 hover:border-gray-300 dark:text-gray-400 dark:hover:text-gray-300'
|
||||
}`}
|
||||
>
|
||||
<div className="flex items-center gap-2">
|
||||
<Send size={18} />
|
||||
Sent Messages
|
||||
{messages.length > 0 && (
|
||||
<span className="ml-2 px-2 py-0.5 rounded-full text-xs font-medium bg-gray-100 text-gray-600 dark:bg-gray-700 dark:text-gray-300">
|
||||
{messages.length}
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
</button>
|
||||
</nav>
|
||||
</div>
|
||||
<TabGroup
|
||||
variant="pills"
|
||||
activeColor="brand"
|
||||
tabs={[
|
||||
{
|
||||
id: 'compose',
|
||||
label: 'Compose New',
|
||||
icon: <MessageSquare size={18} />
|
||||
},
|
||||
{
|
||||
id: 'sent',
|
||||
label: `Sent History ${messages.length > 0 ? `(${messages.length})` : ''}`,
|
||||
icon: <Send size={18} />
|
||||
}
|
||||
]}
|
||||
activeTab={activeTab}
|
||||
onChange={(id) => setActiveTab(id as TabType)}
|
||||
className="w-full sm:w-auto"
|
||||
/>
|
||||
|
||||
{/* Compose Tab */}
|
||||
{activeTab === 'compose' && (
|
||||
<div className="bg-white dark:bg-gray-800 rounded-lg shadow-sm border border-gray-200 dark:border-gray-700">
|
||||
<form onSubmit={handleSubmit} className="p-6 space-y-6">
|
||||
{/* Subject */}
|
||||
<div>
|
||||
<label htmlFor="subject" className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
|
||||
Subject *
|
||||
</label>
|
||||
<input
|
||||
type="text"
|
||||
id="subject"
|
||||
value={subject}
|
||||
onChange={(e) => setSubject(e.target.value)}
|
||||
className="w-full px-4 py-2 border border-gray-300 dark:border-gray-600 rounded-lg focus:ring-2 focus:ring-brand-500 focus:border-transparent dark:bg-gray-700 dark:text-white"
|
||||
placeholder="Enter message subject..."
|
||||
required
|
||||
/>
|
||||
</div>
|
||||
|
||||
{/* Body */}
|
||||
<div>
|
||||
<label htmlFor="body" className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
|
||||
Message *
|
||||
</label>
|
||||
<textarea
|
||||
id="body"
|
||||
value={body}
|
||||
onChange={(e) => setBody(e.target.value)}
|
||||
rows={8}
|
||||
className="w-full px-4 py-2 border border-gray-300 dark:border-gray-600 rounded-lg focus:ring-2 focus:ring-brand-500 focus:border-transparent dark:bg-gray-700 dark:text-white resize-none"
|
||||
placeholder="Enter your message..."
|
||||
required
|
||||
/>
|
||||
</div>
|
||||
|
||||
{/* Target Roles */}
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-3">
|
||||
Target Groups
|
||||
</label>
|
||||
<div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-3">
|
||||
{roleOptions.map((role) => (
|
||||
<label
|
||||
key={role.value}
|
||||
className={`flex items-center gap-3 p-4 rounded-lg border-2 cursor-pointer transition-all ${
|
||||
selectedRoles.includes(role.value)
|
||||
? 'border-brand-500 bg-brand-50 dark:bg-brand-900/20'
|
||||
: 'border-gray-200 dark:border-gray-600 hover:border-gray-300 dark:hover:border-gray-500'
|
||||
}`}
|
||||
>
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={selectedRoles.includes(role.value)}
|
||||
onChange={() => handleRoleToggle(role.value)}
|
||||
className="w-5 h-5 text-brand-600 border-gray-300 rounded focus:ring-brand-500"
|
||||
<form onSubmit={handleSubmit} className="animate-in fade-in slide-in-from-bottom-4 duration-300">
|
||||
<Card className="overflow-visible">
|
||||
<CardHeader>
|
||||
<h3 className="text-lg font-semibold">Message Details</h3>
|
||||
</CardHeader>
|
||||
<CardBody className="space-y-8">
|
||||
{/* Target Selection */}
|
||||
<div className="space-y-4">
|
||||
<label className="block text-sm font-semibold text-gray-900 dark:text-white">
|
||||
1. Who are you sending to?
|
||||
</label>
|
||||
<div className="grid grid-cols-2 sm:grid-cols-4 gap-4">
|
||||
{roleOptions.map((role) => (
|
||||
<SelectionTile
|
||||
key={role.value}
|
||||
label={role.label}
|
||||
icon={role.icon}
|
||||
description={role.description}
|
||||
selected={selectedRoles.includes(role.value)}
|
||||
onClick={() => handleRoleToggle(role.value)}
|
||||
/>
|
||||
<role.icon size={20} className="text-gray-400" />
|
||||
<span className="text-sm font-medium text-gray-900 dark:text-white">
|
||||
{role.label}
|
||||
</span>
|
||||
</label>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Individual Recipients */}
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-3">
|
||||
Individual Recipients (Optional)
|
||||
</label>
|
||||
|
||||
{/* Autofill Search */}
|
||||
<div className="relative">
|
||||
<div className="relative">
|
||||
<Search className="absolute left-3 top-1/2 -translate-y-1/2 text-gray-400" size={18} />
|
||||
<input
|
||||
type="text"
|
||||
value={recipientSearchTerm}
|
||||
onChange={(e) => {
|
||||
setRecipientSearchTerm(e.target.value);
|
||||
setVisibleRecipientCount(20);
|
||||
setIsRecipientDropdownOpen(e.target.value.length > 0);
|
||||
}}
|
||||
onFocus={() => {
|
||||
if (recipientSearchTerm.length > 0) {
|
||||
setIsRecipientDropdownOpen(true);
|
||||
}
|
||||
}}
|
||||
placeholder="Type to search recipients..."
|
||||
className="w-full pl-10 pr-4 py-2.5 border border-gray-300 dark:border-gray-600 rounded-lg focus:ring-2 focus:ring-brand-500 focus:border-transparent dark:bg-gray-700 dark:text-white"
|
||||
/>
|
||||
{recipientsLoading && recipientSearchTerm && (
|
||||
<Loader2 className="absolute right-3 top-1/2 -translate-y-1/2 text-gray-400 animate-spin" size={18} />
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* Dropdown Results */}
|
||||
{isRecipientDropdownOpen && recipientSearchTerm && !recipientsLoading && (
|
||||
<>
|
||||
{/* Click outside to close */}
|
||||
<div
|
||||
className="fixed inset-0 z-10"
|
||||
onClick={() => setIsRecipientDropdownOpen(false)}
|
||||
/>
|
||||
<div
|
||||
ref={dropdownRef}
|
||||
onScroll={handleDropdownScroll}
|
||||
className="absolute z-20 w-full mt-1 bg-white dark:bg-gray-800 border border-gray-200 dark:border-gray-600 rounded-lg shadow-lg max-h-72 overflow-y-auto"
|
||||
>
|
||||
{filteredRecipients.length === 0 ? (
|
||||
<p className="text-center py-4 text-gray-500 dark:text-gray-400 text-sm">
|
||||
No matching users found
|
||||
</p>
|
||||
) : (
|
||||
<>
|
||||
{filteredRecipients.slice(0, visibleRecipientCount).map((user) => (
|
||||
<button
|
||||
key={user.id}
|
||||
type="button"
|
||||
onClick={() => handleAddUser(user)}
|
||||
className="w-full flex items-center gap-3 p-3 hover:bg-gray-50 dark:hover:bg-gray-700/50 transition-colors border-b border-gray-100 dark:border-gray-700 last:border-b-0 text-left"
|
||||
>
|
||||
<UserPlus size={18} className="text-gray-400 flex-shrink-0" />
|
||||
<div className="flex-1 min-w-0">
|
||||
<p className="text-sm font-medium text-gray-900 dark:text-white truncate">
|
||||
{user.name}
|
||||
</p>
|
||||
<p className="text-xs text-gray-500 dark:text-gray-400 truncate">
|
||||
{user.email}
|
||||
</p>
|
||||
</div>
|
||||
<span className="px-2 py-1 rounded-full text-xs font-medium bg-gray-100 text-gray-600 dark:bg-gray-700 dark:text-gray-300 capitalize flex-shrink-0">
|
||||
{user.role}
|
||||
</span>
|
||||
</button>
|
||||
))}
|
||||
{filteredRecipients.length > visibleRecipientCount && (
|
||||
<div className="text-center py-3 text-xs text-gray-500 dark:text-gray-400">
|
||||
<Loader2 size={16} className="inline-block animate-spin mr-2" />
|
||||
Scroll for more...
|
||||
</div>
|
||||
)}
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* Selected Users List */}
|
||||
{selectedUsers.length > 0 && (
|
||||
<div className="mt-3 flex flex-wrap gap-2">
|
||||
{selectedUsers.map((user) => (
|
||||
<div
|
||||
key={user.id}
|
||||
className="inline-flex items-center gap-2 px-3 py-1.5 bg-brand-50 dark:bg-brand-900/30 text-brand-700 dark:text-brand-300 rounded-full text-sm"
|
||||
>
|
||||
<span className="font-medium">{user.name}</span>
|
||||
<span className="text-brand-500 dark:text-brand-400 text-xs">({user.role})</span>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => handleRemoveUser(user.id)}
|
||||
className="ml-1 p-0.5 hover:bg-brand-200 dark:hover:bg-brand-800 rounded-full transition-colors"
|
||||
>
|
||||
<X size={14} />
|
||||
</button>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* Delivery Method */}
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-3">
|
||||
Delivery Method
|
||||
</label>
|
||||
<div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-3">
|
||||
{deliveryMethodOptions.map((option) => (
|
||||
<label
|
||||
key={option.value}
|
||||
className={`flex items-center gap-3 p-4 rounded-lg border-2 cursor-pointer transition-all ${
|
||||
deliveryMethod === option.value
|
||||
? 'border-brand-500 bg-brand-50 dark:bg-brand-900/20'
|
||||
: 'border-gray-200 dark:border-gray-600 hover:border-gray-300 dark:hover:border-gray-500'
|
||||
}`}
|
||||
>
|
||||
{/* Individual Recipients Search */}
|
||||
<div className="mt-4">
|
||||
<div className="relative group">
|
||||
<Search className="absolute left-3.5 top-3.5 text-gray-400 group-focus-within:text-brand-500 transition-colors" size={20} />
|
||||
<input
|
||||
type="radio"
|
||||
name="delivery_method"
|
||||
value={option.value}
|
||||
checked={deliveryMethod === option.value}
|
||||
onChange={(e) => setDeliveryMethod(e.target.value as any)}
|
||||
className="w-5 h-5 text-brand-600 border-gray-300 focus:ring-brand-500"
|
||||
type="text"
|
||||
value={recipientSearchTerm}
|
||||
onChange={(e) => {
|
||||
setRecipientSearchTerm(e.target.value);
|
||||
setVisibleRecipientCount(20);
|
||||
setIsRecipientDropdownOpen(e.target.value.length > 0);
|
||||
}}
|
||||
onFocus={() => {
|
||||
if (recipientSearchTerm.length > 0) {
|
||||
setIsRecipientDropdownOpen(true);
|
||||
}
|
||||
}}
|
||||
placeholder="Search for specific people..."
|
||||
className="w-full pl-11 pr-4 py-3 border border-gray-200 dark:border-gray-700 rounded-xl bg-gray-50 dark:bg-gray-800/50 focus:ring-2 focus:ring-brand-500 focus:border-brand-500 transition-all outline-none"
|
||||
/>
|
||||
<option.icon size={20} className="text-gray-400" />
|
||||
<span className="text-sm font-medium text-gray-900 dark:text-white">
|
||||
{option.label}
|
||||
</span>
|
||||
</label>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
{recipientsLoading && recipientSearchTerm && (
|
||||
<Loader2 className="absolute right-3.5 top-3.5 text-gray-400 animate-spin" size={20} />
|
||||
)}
|
||||
|
||||
{/* Recipient Count */}
|
||||
{recipientCount > 0 && (
|
||||
<div className="bg-blue-50 dark:bg-blue-900/20 border border-blue-200 dark:border-blue-800 rounded-lg p-4">
|
||||
<div className="flex items-center gap-2 text-blue-800 dark:text-blue-300">
|
||||
<Users size={18} />
|
||||
<span className="font-medium">
|
||||
This message will be sent to approximately {recipientCount} recipient{recipientCount !== 1 ? 's' : ''}
|
||||
</span>
|
||||
{/* Dropdown Results */}
|
||||
{isRecipientDropdownOpen && recipientSearchTerm && !recipientsLoading && (
|
||||
<>
|
||||
<div
|
||||
className="fixed inset-0 z-10"
|
||||
onClick={() => setIsRecipientDropdownOpen(false)}
|
||||
/>
|
||||
<div
|
||||
ref={dropdownRef}
|
||||
onScroll={handleDropdownScroll}
|
||||
className="absolute z-20 w-full mt-2 bg-white dark:bg-gray-800 border border-gray-100 dark:border-gray-700 rounded-xl shadow-xl max-h-72 overflow-y-auto"
|
||||
>
|
||||
{filteredRecipients.length === 0 ? (
|
||||
<p className="text-center py-6 text-gray-500 dark:text-gray-400 text-sm">
|
||||
No matching users found
|
||||
</p>
|
||||
) : (
|
||||
<div className="p-2 space-y-1">
|
||||
{filteredRecipients.slice(0, visibleRecipientCount).map((user) => (
|
||||
<button
|
||||
key={user.id}
|
||||
type="button"
|
||||
onClick={() => handleAddUser(user)}
|
||||
className="w-full flex items-center gap-3 p-3 hover:bg-gray-50 dark:hover:bg-gray-700/50 rounded-lg transition-colors text-left group/item"
|
||||
>
|
||||
<div className="h-8 w-8 rounded-full bg-brand-100 dark:bg-brand-900/30 flex items-center justify-center text-brand-600 dark:text-brand-400 group-hover/item:bg-brand-200 dark:group-hover/item:bg-brand-800 transition-colors">
|
||||
<span className="font-semibold text-xs">{user.name.charAt(0)}</span>
|
||||
</div>
|
||||
<div className="flex-1 min-w-0">
|
||||
<p className="text-sm font-medium text-gray-900 dark:text-white truncate">
|
||||
{user.name}
|
||||
</p>
|
||||
<p className="text-xs text-gray-500 dark:text-gray-400 truncate">
|
||||
{user.email}
|
||||
</p>
|
||||
</div>
|
||||
<Badge size="sm" variant="default">{user.role}</Badge>
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* Selected Users Chips */}
|
||||
{selectedUsers.length > 0 && (
|
||||
<div className="mt-3 flex flex-wrap gap-2">
|
||||
{selectedUsers.map((user) => (
|
||||
<div
|
||||
key={user.id}
|
||||
className="inline-flex items-center gap-2 pl-3 pr-2 py-1.5 bg-white border border-gray-200 dark:bg-gray-800 dark:border-gray-700 rounded-full text-sm shadow-sm"
|
||||
>
|
||||
<span className="font-medium text-gray-700 dark:text-gray-200">{user.name}</span>
|
||||
<span className="text-xs text-gray-500 uppercase">{user.role}</span>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => handleRemoveUser(user.id)}
|
||||
className="p-1 hover:bg-gray-100 dark:hover:bg-gray-700 rounded-full text-gray-400 hover:text-gray-600 dark:hover:text-gray-200 transition-colors"
|
||||
>
|
||||
<X size={14} />
|
||||
</button>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Actions */}
|
||||
<div className="flex items-center justify-between pt-4 border-t border-gray-200 dark:border-gray-700">
|
||||
<button
|
||||
type="button"
|
||||
<hr className="border-gray-100 dark:border-gray-800" />
|
||||
|
||||
{/* Message Content */}
|
||||
<div className="space-y-4">
|
||||
<label className="block text-sm font-semibold text-gray-900 dark:text-white">
|
||||
2. What do you want to say?
|
||||
</label>
|
||||
<div className="grid gap-4">
|
||||
<FormInput
|
||||
label="Subject"
|
||||
value={subject}
|
||||
onChange={(e) => setSubject(e.target.value)}
|
||||
placeholder="Brief summary of your message..."
|
||||
required
|
||||
fullWidth
|
||||
/>
|
||||
<FormTextarea
|
||||
label="Message Body"
|
||||
value={body}
|
||||
onChange={(e) => setBody(e.target.value)}
|
||||
rows={6}
|
||||
placeholder="Write your message here..."
|
||||
required
|
||||
fullWidth
|
||||
hint="You can use plain text. Links will be automatically detected."
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<hr className="border-gray-100 dark:border-gray-800" />
|
||||
|
||||
{/* Delivery Method */}
|
||||
<div className="space-y-4">
|
||||
<label className="block text-sm font-semibold text-gray-900 dark:text-white">
|
||||
3. How should we send it?
|
||||
</label>
|
||||
<div className="grid grid-cols-2 sm:grid-cols-4 gap-4">
|
||||
{deliveryMethodOptions.map((option) => (
|
||||
<SelectionTile
|
||||
key={option.value}
|
||||
label={option.label}
|
||||
icon={option.icon}
|
||||
description={option.description}
|
||||
selected={deliveryMethod === option.value}
|
||||
onClick={() => setDeliveryMethod(option.value)}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Recipient Count Summary */}
|
||||
{recipientCount > 0 && (
|
||||
<div className="bg-blue-50 dark:bg-blue-900/10 border border-blue-100 dark:border-blue-900/30 rounded-xl p-4 flex items-start gap-4">
|
||||
<div className="p-2 bg-blue-100 dark:bg-blue-900/30 rounded-lg text-blue-600 dark:text-blue-400 shrink-0">
|
||||
<Users size={20} />
|
||||
</div>
|
||||
<div>
|
||||
<h4 className="text-sm font-semibold text-blue-900 dark:text-blue-100">Ready to Broadcast</h4>
|
||||
<p className="text-sm text-blue-700 dark:text-blue-300 mt-1">
|
||||
This message will be sent to approximately <span className="font-bold">{recipientCount} recipient{recipientCount !== 1 ? 's' : ''}</span> via {deliveryMethodOptions.find(o => o.value === deliveryMethod)?.label}.
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</CardBody>
|
||||
<CardFooter className="flex justify-end gap-3 bg-gray-50/50 dark:bg-gray-800/50">
|
||||
<Button
|
||||
variant="ghost"
|
||||
onClick={resetForm}
|
||||
disabled={createMessage.isPending || sendMessage.isPending}
|
||||
className="px-4 py-2 text-gray-700 dark:text-gray-300 hover:bg-gray-100 dark:hover:bg-gray-700 rounded-lg transition-colors disabled:opacity-50"
|
||||
>
|
||||
Clear
|
||||
</button>
|
||||
<button
|
||||
type="submit"
|
||||
disabled={createMessage.isPending || sendMessage.isPending}
|
||||
className="inline-flex items-center gap-2 px-6 py-2 bg-brand-600 text-white rounded-lg hover:bg-brand-700 transition-colors disabled:opacity-50"
|
||||
Clear Form
|
||||
</Button>
|
||||
<SubmitButton
|
||||
isLoading={createMessage.isPending || sendMessage.isPending}
|
||||
loadingText="Sending..."
|
||||
leftIcon={<Send size={18} />}
|
||||
variant="primary"
|
||||
size="lg"
|
||||
>
|
||||
{createMessage.isPending || sendMessage.isPending ? (
|
||||
<>
|
||||
<Loader2 size={18} className="animate-spin" />
|
||||
Sending...
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
<Send size={18} />
|
||||
Send Message
|
||||
</>
|
||||
)}
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
Send Broadcast
|
||||
</SubmitButton>
|
||||
</CardFooter>
|
||||
</Card>
|
||||
</form>
|
||||
)}
|
||||
|
||||
{/* Sent Messages Tab */}
|
||||
{activeTab === 'sent' && (
|
||||
<div className="space-y-4">
|
||||
{/* Filters */}
|
||||
<div className="flex flex-col sm:flex-row gap-3">
|
||||
<div className="flex-1 relative">
|
||||
<Search className="absolute left-3 top-1/2 -translate-y-1/2 text-gray-400" size={18} />
|
||||
<input
|
||||
type="text"
|
||||
value={searchTerm}
|
||||
onChange={(e) => setSearchTerm(e.target.value)}
|
||||
placeholder="Search messages..."
|
||||
className="w-full pl-10 pr-4 py-2 border border-gray-300 dark:border-gray-600 rounded-lg focus:ring-2 focus:ring-brand-500 focus:border-transparent dark:bg-gray-800 dark:text-white"
|
||||
/>
|
||||
<div className="space-y-6 animate-in fade-in slide-in-from-bottom-4 duration-300">
|
||||
{/* Filters Bar */}
|
||||
<Card padding="sm">
|
||||
<div className="flex flex-col sm:flex-row gap-4 items-center">
|
||||
<div className="flex-1 w-full relative">
|
||||
<Search className="absolute left-3 top-1/2 -translate-y-1/2 text-gray-400" size={18} />
|
||||
<input
|
||||
type="text"
|
||||
value={searchTerm}
|
||||
onChange={(e) => setSearchTerm(e.target.value)}
|
||||
placeholder="Search subject, body, or sender..."
|
||||
className="w-full pl-10 pr-4 py-2 border-none bg-transparent focus:ring-0 text-gray-900 dark:text-white placeholder-gray-400"
|
||||
/>
|
||||
</div>
|
||||
<div className="h-8 w-px bg-gray-200 dark:bg-gray-700 hidden sm:block" />
|
||||
<div className="w-full sm:w-auto min-w-[200px]">
|
||||
<div className="relative">
|
||||
<Filter className="absolute left-3 top-1/2 -translate-y-1/2 text-gray-400" size={16} />
|
||||
<select
|
||||
value={statusFilter}
|
||||
onChange={(e) => setStatusFilter(e.target.value as any)}
|
||||
className="w-full pl-10 pr-8 py-2 bg-gray-50 dark:bg-gray-800 border-none rounded-lg text-sm font-medium focus:ring-2 focus:ring-brand-500 cursor-pointer"
|
||||
>
|
||||
<option value="ALL">All Statuses</option>
|
||||
<option value="SENT">Sent</option>
|
||||
<option value="SENDING">Sending</option>
|
||||
<option value="FAILED">Failed</option>
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<select
|
||||
value={statusFilter}
|
||||
onChange={(e) => setStatusFilter(e.target.value as any)}
|
||||
className="px-4 py-2 border border-gray-300 dark:border-gray-600 rounded-lg focus:ring-2 focus:ring-brand-500 focus:border-transparent dark:bg-gray-800 dark:text-white"
|
||||
>
|
||||
<option value="ALL">All Statuses</option>
|
||||
<option value="SENT">Sent</option>
|
||||
<option value="SENDING">Sending</option>
|
||||
<option value="FAILED">Failed</option>
|
||||
</select>
|
||||
</div>
|
||||
</Card>
|
||||
|
||||
{/* Messages List */}
|
||||
{messagesLoading ? (
|
||||
<div className="text-center py-12">
|
||||
<Loader2 className="mx-auto h-12 w-12 animate-spin text-brand-500" />
|
||||
<div className="flex flex-col items-center justify-center py-24">
|
||||
<Loader2 className="h-10 w-10 animate-spin text-brand-500 mb-4" />
|
||||
<p className="text-gray-500">Loading messages...</p>
|
||||
</div>
|
||||
) : filteredMessages.length === 0 ? (
|
||||
<div className="text-center py-12 bg-white dark:bg-gray-800 rounded-lg shadow-sm border border-gray-200 dark:border-gray-700">
|
||||
<MessageSquare className="mx-auto h-12 w-12 text-gray-400" />
|
||||
<p className="mt-4 text-gray-500 dark:text-gray-400">
|
||||
{searchTerm || statusFilter !== 'ALL' ? 'No messages found' : 'No messages sent yet'}
|
||||
</p>
|
||||
</div>
|
||||
<EmptyState
|
||||
icon={<MessageSquare className="h-12 w-12 text-gray-400" />}
|
||||
title="No messages found"
|
||||
description={searchTerm || statusFilter !== 'ALL' ? "Try adjusting your filters to see more results." : "You haven't sent any broadcast messages yet."}
|
||||
action={
|
||||
statusFilter === 'ALL' && !searchTerm ? (
|
||||
<Button onClick={() => setActiveTab('compose')} leftIcon={<Send size={16} />}>
|
||||
Compose First Message
|
||||
</Button>
|
||||
) : undefined
|
||||
}
|
||||
/>
|
||||
) : (
|
||||
<div className="space-y-3">
|
||||
<div className="grid gap-4">
|
||||
{filteredMessages.map((message) => (
|
||||
<div
|
||||
<Card
|
||||
key={message.id}
|
||||
className="bg-white dark:bg-gray-800 rounded-lg shadow-sm border border-gray-200 dark:border-gray-700 p-6 hover:shadow-md transition-shadow cursor-pointer"
|
||||
hoverable
|
||||
onClick={() => setSelectedMessage(message)}
|
||||
className="group transition-all duration-200 border-l-4 border-l-transparent hover:border-l-brand-500"
|
||||
padding="lg"
|
||||
>
|
||||
<div className="flex items-start justify-between gap-4">
|
||||
<div className="flex-1 min-w-0">
|
||||
<div className="flex items-center gap-3 mb-2">
|
||||
<h3 className="text-lg font-semibold text-gray-900 dark:text-white truncate">
|
||||
<div className="flex flex-col sm:flex-row gap-4 justify-between">
|
||||
<div className="flex-1 min-w-0 space-y-2">
|
||||
<div className="flex items-center gap-3">
|
||||
{getStatusBadge(message.status)}
|
||||
<h3 className="text-lg font-semibold text-gray-900 dark:text-white truncate group-hover:text-brand-600 dark:group-hover:text-brand-400 transition-colors">
|
||||
{message.subject}
|
||||
</h3>
|
||||
{getStatusBadge(message.status)}
|
||||
</div>
|
||||
<p className="text-sm text-gray-600 dark:text-gray-400 line-clamp-2 mb-3">
|
||||
<p className="text-gray-600 dark:text-gray-400 line-clamp-2 text-sm">
|
||||
{message.body}
|
||||
</p>
|
||||
<div className="flex flex-wrap items-center gap-4 text-sm text-gray-500 dark:text-gray-400">
|
||||
<div className="flex items-center gap-1.5">
|
||||
<Users size={14} />
|
||||
<div className="flex flex-wrap items-center gap-4 text-xs font-medium text-gray-500 dark:text-gray-400 pt-2">
|
||||
<div className="flex items-center gap-1.5 bg-gray-100 dark:bg-gray-800 px-2 py-1 rounded">
|
||||
<Users size={12} />
|
||||
<span>{getTargetDescription(message)}</span>
|
||||
</div>
|
||||
<div className="flex items-center gap-1.5">
|
||||
<div className="flex items-center gap-1.5 bg-gray-100 dark:bg-gray-800 px-2 py-1 rounded">
|
||||
{getDeliveryMethodIcon(message.delivery_method)}
|
||||
<span className="capitalize">{message.delivery_method.toLowerCase().replace('_', ' ')}</span>
|
||||
</div>
|
||||
<div className="flex items-center gap-1.5">
|
||||
<Clock size={14} />
|
||||
<div className="flex items-center gap-1.5 bg-gray-100 dark:bg-gray-800 px-2 py-1 rounded">
|
||||
<Clock size={12} />
|
||||
<span>{formatDate(message.sent_at || message.created_at)}</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div className="flex flex-col items-end gap-2 text-right">
|
||||
<div className="text-sm text-gray-500 dark:text-gray-400">
|
||||
By {message.created_by_name}
|
||||
</div>
|
||||
{message.status === 'SENT' && (
|
||||
<div className="flex items-center gap-4 text-xs text-gray-500 dark:text-gray-400">
|
||||
<div className="flex items-center gap-1">
|
||||
<Send size={12} />
|
||||
<span>{message.delivered_count}/{message.total_recipients}</span>
|
||||
|
||||
<div className="flex sm:flex-col items-center sm:items-end justify-between sm:justify-center gap-4 border-t sm:border-t-0 sm:border-l border-gray-100 dark:border-gray-800 pt-4 sm:pt-0 sm:pl-6 min-w-[120px]">
|
||||
{message.status === 'SENT' ? (
|
||||
<div className="grid grid-cols-2 gap-x-6 gap-y-2 text-center">
|
||||
<div>
|
||||
<div className="text-xs text-gray-500 uppercase tracking-wide">Sent</div>
|
||||
<div className="font-bold text-gray-900 dark:text-white">{message.total_recipients}</div>
|
||||
</div>
|
||||
<div className="flex items-center gap-1">
|
||||
<Eye size={12} />
|
||||
<span>{message.read_count}</span>
|
||||
<div>
|
||||
<div className="text-xs text-gray-500 uppercase tracking-wide">Read</div>
|
||||
<div className="font-bold text-brand-600 dark:text-brand-400">{message.read_count}</div>
|
||||
</div>
|
||||
</div>
|
||||
) : (
|
||||
<div className="text-sm text-gray-400 italic">
|
||||
Draft
|
||||
</div>
|
||||
)}
|
||||
<div className="text-xs text-gray-400">
|
||||
by {message.created_by_name}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</Card>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Message Detail Modal */}
|
||||
{/* Message Detail Modal - Using simple fixed overlay for now since Modal component wasn't in list but likely exists. keeping existing logic with better styling */}
|
||||
{selectedMessage && (
|
||||
<div className="fixed inset-0 bg-black/50 backdrop-blur-sm flex items-center justify-center p-4 z-50">
|
||||
<div className="bg-white dark:bg-gray-800 rounded-lg shadow-xl max-w-2xl w-full max-h-[90vh] overflow-y-auto">
|
||||
<div className="sticky top-0 bg-white dark:bg-gray-800 border-b border-gray-200 dark:border-gray-700 p-6 flex items-start justify-between">
|
||||
<div className="flex-1">
|
||||
<h3 className="text-xl font-bold text-gray-900 dark:text-white mb-2">
|
||||
{selectedMessage.subject}
|
||||
</h3>
|
||||
<div className="flex items-center gap-3">
|
||||
<div className="fixed inset-0 bg-gray-900/60 backdrop-blur-sm flex items-center justify-center p-4 z-50 animate-in fade-in duration-200">
|
||||
<div className="bg-white dark:bg-gray-800 rounded-2xl shadow-2xl max-w-2xl w-full max-h-[85vh] overflow-hidden flex flex-col animate-in zoom-in-95 duration-200">
|
||||
<div className="p-6 border-b border-gray-100 dark:border-gray-700 flex items-start justify-between bg-gray-50/50 dark:bg-gray-800">
|
||||
<div>
|
||||
<div className="flex items-center gap-3 mb-2">
|
||||
{getStatusBadge(selectedMessage.status)}
|
||||
<span className="text-sm text-gray-500 dark:text-gray-400">
|
||||
<span className="text-sm text-gray-500 dark:text-gray-400 flex items-center gap-1.5">
|
||||
<Clock size={14} />
|
||||
{formatDate(selectedMessage.sent_at || selectedMessage.created_at)}
|
||||
</span>
|
||||
</div>
|
||||
<h3 className="text-xl font-bold text-gray-900 dark:text-white leading-tight">
|
||||
{selectedMessage.subject}
|
||||
</h3>
|
||||
</div>
|
||||
<button
|
||||
onClick={() => setSelectedMessage(null)}
|
||||
className="p-2 text-gray-400 hover:text-gray-600 dark:hover:text-gray-300 transition-colors"
|
||||
className="p-2 -mr-2 -mt-2 text-gray-400 hover:text-gray-600 hover:bg-gray-100 dark:hover:bg-gray-700 rounded-full transition-colors"
|
||||
>
|
||||
<X size={20} />
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<div className="p-6 space-y-6">
|
||||
{/* Message Body */}
|
||||
<div>
|
||||
<h4 className="text-sm font-semibold text-gray-700 dark:text-gray-300 mb-2">
|
||||
Message
|
||||
</h4>
|
||||
<p className="text-gray-900 dark:text-gray-100 whitespace-pre-wrap">
|
||||
{selectedMessage.body}
|
||||
</p>
|
||||
</div>
|
||||
|
||||
{/* Recipients */}
|
||||
<div>
|
||||
<h4 className="text-sm font-semibold text-gray-700 dark:text-gray-300 mb-2">
|
||||
Recipients
|
||||
</h4>
|
||||
<p className="text-gray-900 dark:text-gray-100">
|
||||
{getTargetDescription(selectedMessage)}
|
||||
</p>
|
||||
</div>
|
||||
|
||||
{/* Delivery Method */}
|
||||
<div>
|
||||
<h4 className="text-sm font-semibold text-gray-700 dark:text-gray-300 mb-2">
|
||||
Delivery Method
|
||||
</h4>
|
||||
<div className="flex items-center gap-2 text-gray-900 dark:text-gray-100">
|
||||
{getDeliveryMethodIcon(selectedMessage.delivery_method)}
|
||||
<span className="capitalize">
|
||||
{selectedMessage.delivery_method.toLowerCase().replace('_', ' ')}
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Statistics */}
|
||||
<div className="p-8 overflow-y-auto space-y-8 custom-scrollbar">
|
||||
{/* Stats Cards */}
|
||||
{selectedMessage.status === 'SENT' && (
|
||||
<div className="grid grid-cols-3 gap-4">
|
||||
<div className="bg-gray-50 dark:bg-gray-700/50 rounded-lg p-4">
|
||||
<div className="text-2xl font-bold text-gray-900 dark:text-white">
|
||||
<div className="bg-gray-50 dark:bg-gray-700/50 rounded-xl p-4 text-center border border-gray-100 dark:border-gray-700">
|
||||
<div className="text-2xl font-bold text-gray-900 dark:text-white mb-1">
|
||||
{selectedMessage.total_recipients}
|
||||
</div>
|
||||
<div className="text-sm text-gray-500 dark:text-gray-400">
|
||||
Total Recipients
|
||||
<div className="text-xs font-semibold text-gray-500 uppercase tracking-wider">
|
||||
Recipients
|
||||
</div>
|
||||
</div>
|
||||
<div className="bg-green-50 dark:bg-green-900/20 rounded-lg p-4">
|
||||
<div className="text-2xl font-bold text-green-700 dark:text-green-400">
|
||||
<div className="bg-green-50 dark:bg-green-900/10 rounded-xl p-4 text-center border border-green-100 dark:border-green-900/20">
|
||||
<div className="text-2xl font-bold text-green-700 dark:text-green-400 mb-1">
|
||||
{selectedMessage.delivered_count}
|
||||
</div>
|
||||
<div className="text-sm text-green-600 dark:text-green-500">
|
||||
<div className="text-xs font-semibold text-green-600 uppercase tracking-wider">
|
||||
Delivered
|
||||
</div>
|
||||
</div>
|
||||
<div className="bg-blue-50 dark:bg-blue-900/20 rounded-lg p-4">
|
||||
<div className="text-2xl font-bold text-blue-700 dark:text-blue-400">
|
||||
<div className="bg-blue-50 dark:bg-blue-900/10 rounded-xl p-4 text-center border border-blue-100 dark:border-blue-900/20">
|
||||
<div className="text-2xl font-bold text-blue-700 dark:text-blue-400 mb-1">
|
||||
{selectedMessage.read_count}
|
||||
</div>
|
||||
<div className="text-sm text-blue-600 dark:text-blue-500">
|
||||
<div className="text-xs font-semibold text-blue-600 uppercase tracking-wider">
|
||||
Read
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Sender */}
|
||||
<div className="pt-4 border-t border-gray-200 dark:border-gray-700">
|
||||
<p className="text-sm text-gray-500 dark:text-gray-400">
|
||||
Sent by <span className="font-medium text-gray-900 dark:text-white">{selectedMessage.created_by_name}</span>
|
||||
</p>
|
||||
{/* Message Body */}
|
||||
<div className="prose dark:prose-invert max-w-none">
|
||||
<h4 className="text-sm font-semibold text-gray-400 uppercase tracking-wider mb-3">
|
||||
Message Content
|
||||
</h4>
|
||||
<div className="p-6 bg-gray-50 dark:bg-gray-900/50 rounded-xl border border-gray-100 dark:border-gray-700 text-gray-800 dark:text-gray-200 whitespace-pre-wrap leading-relaxed">
|
||||
{selectedMessage.body}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Meta Info */}
|
||||
<div className="grid sm:grid-cols-2 gap-6">
|
||||
<div>
|
||||
<h4 className="text-sm font-semibold text-gray-400 uppercase tracking-wider mb-2">
|
||||
Recipients
|
||||
</h4>
|
||||
<div className="flex items-center gap-2 text-gray-700 dark:text-gray-300 bg-white dark:bg-gray-800 border border-gray-200 dark:border-gray-700 rounded-lg p-3">
|
||||
<Users size={18} className="text-gray-400" />
|
||||
<span>{getTargetDescription(selectedMessage)}</span>
|
||||
</div>
|
||||
</div>
|
||||
<div>
|
||||
<h4 className="text-sm font-semibold text-gray-400 uppercase tracking-wider mb-2">
|
||||
Delivery Method
|
||||
</h4>
|
||||
<div className="flex items-center gap-2 text-gray-700 dark:text-gray-300 bg-white dark:bg-gray-800 border border-gray-200 dark:border-gray-700 rounded-lg p-3">
|
||||
{getDeliveryMethodIcon(selectedMessage.delivery_method)}
|
||||
<span className="capitalize">
|
||||
{selectedMessage.delivery_method.toLowerCase().replace('_', ' ')}
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="p-4 bg-gray-50 dark:bg-gray-800/50 border-t border-gray-100 dark:border-gray-700 flex justify-end">
|
||||
<span className="text-xs text-gray-400">
|
||||
Sent by {selectedMessage.created_by_name}
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@@ -839,4 +836,4 @@ const Messages: React.FC = () => {
|
||||
);
|
||||
};
|
||||
|
||||
export default Messages;
|
||||
export default Messages;
|
||||
File diff suppressed because it is too large
Load Diff
246
frontend/src/pages/__tests__/Messages.test.tsx
Normal file
246
frontend/src/pages/__tests__/Messages.test.tsx
Normal file
@@ -0,0 +1,246 @@
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
||||
import { render, screen, fireEvent, waitFor } from '@testing-library/react';
|
||||
import userEvent from '@testing-library/user-event';
|
||||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
|
||||
import React from 'react';
|
||||
import Messages from '../Messages';
|
||||
import api from '../../api/client';
|
||||
import toast from 'react-hot-toast';
|
||||
|
||||
// Mock dependencies
|
||||
vi.mock('../../api/client');
|
||||
vi.mock('react-hot-toast');
|
||||
vi.mock('react-i18next', () => ({
|
||||
useTranslation: () => ({
|
||||
t: (key: string) => key,
|
||||
}),
|
||||
}));
|
||||
|
||||
// Test wrapper
|
||||
const createWrapper = () => {
|
||||
const queryClient = new QueryClient({
|
||||
defaultOptions: {
|
||||
queries: {
|
||||
retry: false,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
return ({ children }: { children: React.ReactNode }) => (
|
||||
<QueryClientProvider client={queryClient}>
|
||||
{children}
|
||||
</QueryClientProvider>
|
||||
);
|
||||
};
|
||||
|
||||
// Mock data
|
||||
const mockRecipientOptions = {
|
||||
users: [
|
||||
{ id: '1', name: 'Alice Staff', email: 'alice@example.com', role: 'staff' },
|
||||
{ id: '2', name: 'Bob Manager', email: 'bob@example.com', role: 'manager' },
|
||||
{ id: '3', name: 'Charlie Customer', email: 'charlie@example.com', role: 'customer' },
|
||||
]
|
||||
};
|
||||
|
||||
const mockMessages = [
|
||||
{
|
||||
id: 'msg-1',
|
||||
subject: 'Welcome Message',
|
||||
body: 'Welcome to the platform!',
|
||||
target_roles: ['customer'],
|
||||
target_users: [],
|
||||
delivery_method: 'EMAIL',
|
||||
status: 'SENT',
|
||||
total_recipients: 10,
|
||||
delivered_count: 8,
|
||||
read_count: 5,
|
||||
created_at: '2023-01-01T10:00:00Z',
|
||||
sent_at: '2023-01-01T10:05:00Z',
|
||||
created_by: 'user-1',
|
||||
created_by_name: 'Admin User'
|
||||
},
|
||||
{
|
||||
id: 'msg-2',
|
||||
subject: 'Staff Meeting',
|
||||
body: 'Meeting at 2pm',
|
||||
target_roles: ['staff', 'manager'],
|
||||
target_users: [],
|
||||
delivery_method: 'IN_APP',
|
||||
status: 'DRAFT',
|
||||
total_recipients: 5,
|
||||
delivered_count: 0,
|
||||
read_count: 0,
|
||||
created_at: '2023-01-02T09:00:00Z',
|
||||
sent_at: null,
|
||||
created_by: 'user-1',
|
||||
created_by_name: 'Admin User'
|
||||
}
|
||||
];
|
||||
|
||||
describe('Messages Page', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
|
||||
// Default API mocks
|
||||
vi.mocked(api.get).mockImplementation((url) => {
|
||||
if (url === '/messages/broadcast-messages/') {
|
||||
return Promise.resolve({ data: mockMessages });
|
||||
}
|
||||
if (url === '/messages/broadcast-messages/recipient_options/') {
|
||||
return Promise.resolve({ data: mockRecipientOptions });
|
||||
}
|
||||
return Promise.reject(new Error('Unknown URL'));
|
||||
});
|
||||
|
||||
vi.mocked(api.post).mockResolvedValue({ data: { id: 'new-msg-1' } });
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
describe('Rendering', () => {
|
||||
it('should render page title', () => {
|
||||
render(<Messages />, { wrapper: createWrapper() });
|
||||
expect(screen.getByText('Broadcast Messages')).toBeInTheDocument();
|
||||
expect(screen.getByText(/reach your staff and customers/i)).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('should render tabs', () => {
|
||||
render(<Messages />, { wrapper: createWrapper() });
|
||||
expect(screen.getByText('Compose New')).toBeInTheDocument();
|
||||
expect(screen.getByText(/sent history/i)).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('should default to compose tab', () => {
|
||||
render(<Messages />, { wrapper: createWrapper() });
|
||||
expect(screen.getByText('1. Who are you sending to?')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
describe('Compose Flow', () => {
|
||||
it('should allow selecting roles via tiles', async () => {
|
||||
const user = userEvent.setup();
|
||||
render(<Messages />, { wrapper: createWrapper() });
|
||||
|
||||
const staffTile = screen.getByText('Staff');
|
||||
await user.click(staffTile);
|
||||
|
||||
// Verify visual selection state (check for checkmark or class change implies logic worked)
|
||||
// Since we can't easily check class names for SelectionTile without data-testid, we assume state update works if no error
|
||||
// A better check would be checking if the role is added to state, but we are testing UI behavior.
|
||||
// We can verify that submitting without content fails, showing validation is active
|
||||
});
|
||||
|
||||
it('should search and add individual recipients', async () => {
|
||||
const user = userEvent.setup();
|
||||
render(<Messages />, { wrapper: createWrapper() });
|
||||
|
||||
const searchInput = screen.getByPlaceholderText('Search for specific people...');
|
||||
await user.type(searchInput, 'Alice');
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText('Alice Staff')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
await user.click(screen.getByText('Alice Staff'));
|
||||
|
||||
expect(screen.getByText('Alice Staff')).toBeInTheDocument(); // Chip should appear
|
||||
});
|
||||
|
||||
it('should validate form before submission', async () => {
|
||||
const user = userEvent.setup();
|
||||
render(<Messages />, { wrapper: createWrapper() });
|
||||
|
||||
const sendButton = screen.getByRole('button', { name: /send broadcast/i });
|
||||
await user.click(sendButton);
|
||||
|
||||
expect(toast.error).toHaveBeenCalledWith('Subject is required');
|
||||
});
|
||||
|
||||
it('should submit form with valid data', async () => {
|
||||
const user = userEvent.setup();
|
||||
render(<Messages />, { wrapper: createWrapper() });
|
||||
|
||||
// Select role
|
||||
await user.click(screen.getByText('Staff'));
|
||||
|
||||
// Fill form
|
||||
await user.type(screen.getByLabelText(/subject/i), 'Test Subject');
|
||||
await user.type(screen.getByLabelText(/message body/i), 'Test Body');
|
||||
|
||||
// Click send
|
||||
await user.click(screen.getByRole('button', { name: /send broadcast/i }));
|
||||
|
||||
await waitFor(() => {
|
||||
expect(api.post).toHaveBeenCalledWith('/messages/broadcast-messages/', expect.objectContaining({
|
||||
subject: 'Test Subject',
|
||||
body: 'Test Body',
|
||||
target_roles: ['staff']
|
||||
}));
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('Sent Messages Tab', () => {
|
||||
it('should switch to sent messages tab', async () => {
|
||||
const user = userEvent.setup();
|
||||
render(<Messages />, { wrapper: createWrapper() });
|
||||
|
||||
await user.click(screen.getByText(/sent history/i));
|
||||
|
||||
expect(screen.getByPlaceholderText('Search subject, body, or sender...')).toBeInTheDocument();
|
||||
expect(screen.getByText('Welcome Message')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('should filter messages by search term', async () => {
|
||||
const user = userEvent.setup();
|
||||
render(<Messages />, { wrapper: createWrapper() });
|
||||
|
||||
await user.click(screen.getByText(/sent history/i));
|
||||
|
||||
const searchInput = screen.getByPlaceholderText('Search subject, body, or sender...');
|
||||
await user.type(searchInput, 'Welcome');
|
||||
|
||||
expect(screen.getByText('Welcome Message')).toBeInTheDocument();
|
||||
expect(screen.queryByText('Staff Meeting')).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('should show empty state when no messages match', async () => {
|
||||
const user = userEvent.setup();
|
||||
render(<Messages />, { wrapper: createWrapper() });
|
||||
|
||||
await user.click(screen.getByText(/sent history/i));
|
||||
|
||||
const searchInput = screen.getByPlaceholderText('Search subject, body, or sender...');
|
||||
await user.type(searchInput, 'NonExistentMessage');
|
||||
|
||||
expect(screen.getByText('No messages found')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
describe('Message Details', () => {
|
||||
it('should open modal when clicking a message', async () => {
|
||||
const user = userEvent.setup();
|
||||
render(<Messages />, { wrapper: createWrapper() });
|
||||
|
||||
await user.click(screen.getByText(/sent history/i));
|
||||
await user.click(screen.getByText('Welcome Message'));
|
||||
|
||||
expect(screen.getByText('Message Content')).toBeInTheDocument();
|
||||
expect(screen.getAllByText('Welcome Message').length).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
it('should display message statistics for sent messages', async () => {
|
||||
const user = userEvent.setup();
|
||||
render(<Messages />, { wrapper: createWrapper() });
|
||||
|
||||
await user.click(screen.getByText(/sent history/i));
|
||||
await user.click(screen.getByText('Welcome Message'));
|
||||
|
||||
expect(screen.getByText('10')).toBeInTheDocument(); // Total
|
||||
expect(screen.getByText('8')).toBeInTheDocument(); // Delivered
|
||||
expect(screen.getByText('5')).toBeInTheDocument(); // Read
|
||||
});
|
||||
});
|
||||
});
|
||||
145
frontend/src/pages/__tests__/TimeBlocks.test.tsx
Normal file
145
frontend/src/pages/__tests__/TimeBlocks.test.tsx
Normal file
@@ -0,0 +1,145 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import { render, screen, fireEvent, waitFor } from '@testing-library/react';
|
||||
import React from 'react';
|
||||
import TimeBlocks from '../TimeBlocks';
|
||||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
|
||||
|
||||
// Mock hooks
|
||||
vi.mock('../../hooks/useTimeBlocks', () => ({
|
||||
useTimeBlocks: vi.fn(),
|
||||
useCreateTimeBlock: vi.fn(),
|
||||
useUpdateTimeBlock: vi.fn(),
|
||||
useDeleteTimeBlock: vi.fn(),
|
||||
useToggleTimeBlock: vi.fn(),
|
||||
useHolidays: vi.fn(),
|
||||
usePendingReviews: vi.fn(),
|
||||
useApproveTimeBlock: vi.fn(),
|
||||
useDenyTimeBlock: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock('../../hooks/useResources', () => ({
|
||||
useResources: vi.fn(),
|
||||
}));
|
||||
|
||||
// Mock translation
|
||||
vi.mock('react-i18next', () => ({
|
||||
useTranslation: () => ({
|
||||
t: (key: string, defaultVal: string) => defaultVal || key,
|
||||
}),
|
||||
}));
|
||||
|
||||
// Mock child components that might be complex
|
||||
vi.mock('../../components/time-blocks/YearlyBlockCalendar', () => ({
|
||||
default: () => <div data-testid="yearly-calendar">Yearly Calendar</div>,
|
||||
}));
|
||||
|
||||
vi.mock('../../components/time-blocks/TimeBlockCreatorModal', () => ({
|
||||
default: ({ isOpen, onClose }: any) => (
|
||||
isOpen ? <div data-testid="creator-modal"><button onClick={onClose}>Close</button></div> : null
|
||||
),
|
||||
}));
|
||||
|
||||
// Setup wrapper
|
||||
const createWrapper = () => {
|
||||
const queryClient = new QueryClient({
|
||||
defaultOptions: { queries: { retry: false } },
|
||||
});
|
||||
return ({ children }: { children: React.ReactNode }) => (
|
||||
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
|
||||
);
|
||||
};
|
||||
|
||||
import { useTimeBlocks, useResources, usePendingReviews, useHolidays } from '../../hooks/useTimeBlocks';
|
||||
import { useResources as useResourcesHook } from '../../hooks/useResources';
|
||||
|
||||
describe('TimeBlocks Page', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
|
||||
// Default mocks
|
||||
(useTimeBlocks as any).mockReturnValue({
|
||||
data: [
|
||||
{ id: '1', title: 'Test Block', block_type: 'HARD', recurrence_type: 'NONE', is_active: true }
|
||||
],
|
||||
isLoading: false
|
||||
});
|
||||
|
||||
(useResourcesHook as any).mockReturnValue({
|
||||
data: [{ id: 'res-1', name: 'Test Resource' }]
|
||||
});
|
||||
|
||||
(usePendingReviews as any).mockReturnValue({
|
||||
data: { count: 0, pending_blocks: [] }
|
||||
});
|
||||
|
||||
(useHolidays as any).mockReturnValue({ data: [] });
|
||||
|
||||
// Mock mutation hooks to return objects with mutateAsync
|
||||
const mockMutation = { mutateAsync: vi.fn(), isPending: false };
|
||||
const hooks = [
|
||||
'useCreateTimeBlock', 'useUpdateTimeBlock', 'useDeleteTimeBlock',
|
||||
'useToggleTimeBlock', 'useApproveTimeBlock', 'useDenyTimeBlock'
|
||||
];
|
||||
// We need to re-import the module to set these if we want to change them,
|
||||
// but here we just need them to exist.
|
||||
// The top-level mock factory handles the export, but we need to control return values.
|
||||
// Since we mocked the module, we can access the mock functions directly via imports?
|
||||
// Actually the import `useTimeBlocks` gives us the mock function.
|
||||
// But `useCreateTimeBlock` etc need to return the mutation object.
|
||||
});
|
||||
|
||||
// Helper to set mock implementation for mutations
|
||||
const setupMutations = () => {
|
||||
const mockMutation = { mutateAsync: vi.fn(), isPending: false };
|
||||
const modules = require('../../hooks/useTimeBlocks');
|
||||
modules.useCreateTimeBlock.mockReturnValue(mockMutation);
|
||||
modules.useUpdateTimeBlock.mockReturnValue(mockMutation);
|
||||
modules.useDeleteTimeBlock.mockReturnValue(mockMutation);
|
||||
modules.useToggleTimeBlock.mockReturnValue(mockMutation);
|
||||
modules.useApproveTimeBlock.mockReturnValue(mockMutation);
|
||||
modules.useDenyTimeBlock.mockReturnValue(mockMutation);
|
||||
};
|
||||
|
||||
it('renders page title', () => {
|
||||
setupMutations();
|
||||
render(<TimeBlocks />, { wrapper: createWrapper() });
|
||||
expect(screen.getByText('Time Blocks')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('renders tabs', () => {
|
||||
setupMutations();
|
||||
render(<TimeBlocks />, { wrapper: createWrapper() });
|
||||
expect(screen.getByText('Business Blocks')).toBeInTheDocument();
|
||||
expect(screen.getByText('Resource Blocks')).toBeInTheDocument();
|
||||
expect(screen.getByText('Yearly View')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('displays business blocks by default', () => {
|
||||
setupMutations();
|
||||
render(<TimeBlocks />, { wrapper: createWrapper() });
|
||||
expect(screen.getByText('Test Block')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('opens creator modal when add button clicked', async () => {
|
||||
setupMutations();
|
||||
render(<TimeBlocks />, { wrapper: createWrapper() });
|
||||
fireEvent.click(screen.getByText('Add Block'));
|
||||
expect(screen.getByTestId('creator-modal')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('switches tabs correctly', async () => {
|
||||
setupMutations();
|
||||
render(<TimeBlocks />, { wrapper: createWrapper() });
|
||||
|
||||
fireEvent.click(screen.getByText('Resource Blocks'));
|
||||
// Since we mocked useTimeBlocks to return the same data regardless of args in the default mock,
|
||||
// we might see the same block if we don't differentiate.
|
||||
// But the component filters/requests differently.
|
||||
// In the real component, it calls useTimeBlocks({ level: 'resource' }).
|
||||
// We can just check if the tab became active.
|
||||
|
||||
// Check if Calendar tab works
|
||||
fireEvent.click(screen.getByText('Yearly View'));
|
||||
expect(screen.getByTestId('yearly-calendar')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
@@ -12,7 +12,7 @@ export default defineConfig({
|
||||
coverage: {
|
||||
provider: 'v8',
|
||||
reporter: ['text', 'json', 'html'],
|
||||
all: true,
|
||||
all: false,
|
||||
include: ['src/**/*.{ts,tsx}'],
|
||||
exclude: [
|
||||
'node_modules/',
|
||||
|
||||
@@ -23,26 +23,60 @@ class MessageRecipientSerializer(serializers.ModelSerializer):
|
||||
|
||||
class BroadcastMessageListSerializer(serializers.ModelSerializer):
|
||||
"""Serializer for listing broadcast messages."""
|
||||
sender_name = serializers.SerializerMethodField()
|
||||
target_description = serializers.SerializerMethodField()
|
||||
created_by = serializers.SerializerMethodField()
|
||||
created_by_name = serializers.SerializerMethodField()
|
||||
target_roles = serializers.SerializerMethodField()
|
||||
target_users = serializers.SerializerMethodField()
|
||||
delivery_method = serializers.SerializerMethodField()
|
||||
status = serializers.SerializerMethodField()
|
||||
|
||||
class Meta:
|
||||
model = BroadcastMessage
|
||||
fields = [
|
||||
'id', 'subject', 'status', 'delivery_method',
|
||||
'sender_name', 'target_description',
|
||||
'id', 'subject', 'body', 'status', 'delivery_method',
|
||||
'created_by', 'created_by_name',
|
||||
'target_roles', 'target_users',
|
||||
'total_recipients', 'delivered_count', 'read_count',
|
||||
'created_at', 'sent_at', 'scheduled_at'
|
||||
]
|
||||
read_only_fields = fields
|
||||
|
||||
def get_sender_name(self, obj):
|
||||
def get_created_by(self, obj):
|
||||
"""Return sender ID as string."""
|
||||
if obj.sender:
|
||||
return str(obj.sender.id)
|
||||
return None
|
||||
|
||||
def get_created_by_name(self, obj):
|
||||
"""Return sender's full name."""
|
||||
if obj.sender:
|
||||
return obj.sender.get_full_name() or obj.sender.username
|
||||
return 'Unknown'
|
||||
|
||||
def get_target_description(self, obj):
|
||||
return obj.get_target_description()
|
||||
def get_target_roles(self, obj):
|
||||
"""Return array of targeted role strings."""
|
||||
roles = []
|
||||
if obj.target_owners:
|
||||
roles.append('owner')
|
||||
if obj.target_managers:
|
||||
roles.append('manager')
|
||||
if obj.target_staff:
|
||||
roles.append('staff')
|
||||
if obj.target_customers:
|
||||
roles.append('customer')
|
||||
return roles
|
||||
|
||||
def get_target_users(self, obj):
|
||||
"""Return array of individual recipient IDs as strings."""
|
||||
return [str(user.id) for user in obj.individual_recipients.all()]
|
||||
|
||||
def get_delivery_method(self, obj):
|
||||
"""Return delivery method in uppercase format (IN_APP, EMAIL, etc.)."""
|
||||
return obj.delivery_method.upper()
|
||||
|
||||
def get_status(self, obj):
|
||||
"""Return status in uppercase format (SENT, DRAFT, etc.)."""
|
||||
return obj.status.upper()
|
||||
|
||||
|
||||
class BroadcastMessageDetailSerializer(serializers.ModelSerializer):
|
||||
@@ -97,12 +131,28 @@ class BroadcastMessageDetailSerializer(serializers.ModelSerializer):
|
||||
|
||||
|
||||
class BroadcastMessageCreateSerializer(serializers.ModelSerializer):
|
||||
"""Serializer for creating broadcast messages."""
|
||||
"""Serializer for creating broadcast messages - accepts frontend format."""
|
||||
# Frontend sends these fields
|
||||
target_roles = serializers.ListField(
|
||||
child=serializers.CharField(),
|
||||
write_only=True,
|
||||
required=False,
|
||||
default=list
|
||||
)
|
||||
target_users = serializers.ListField(
|
||||
child=serializers.CharField(), # Frontend sends as strings
|
||||
write_only=True,
|
||||
required=False,
|
||||
default=list
|
||||
)
|
||||
delivery_method = serializers.CharField(required=False, default='in_app') # Override to accept any case
|
||||
|
||||
# Internal fields (not exposed to frontend)
|
||||
individual_recipient_ids = serializers.ListField(
|
||||
child=serializers.IntegerField(),
|
||||
write_only=True,
|
||||
required=False,
|
||||
default=[]
|
||||
default=list
|
||||
)
|
||||
send_immediately = serializers.BooleanField(write_only=True, default=True)
|
||||
|
||||
@@ -110,12 +160,57 @@ class BroadcastMessageCreateSerializer(serializers.ModelSerializer):
|
||||
model = BroadcastMessage
|
||||
fields = [
|
||||
'subject', 'body', 'delivery_method',
|
||||
'target_owners', 'target_managers', 'target_staff', 'target_customers',
|
||||
'target_roles', 'target_users', # Frontend format
|
||||
'target_owners', 'target_managers', 'target_staff', 'target_customers', # Legacy
|
||||
'individual_recipient_ids', 'scheduled_at', 'send_immediately'
|
||||
]
|
||||
extra_kwargs = {
|
||||
'delivery_method': {'required': False}
|
||||
}
|
||||
|
||||
def validate_target_roles(self, value):
|
||||
"""Validate that all role names are valid."""
|
||||
valid_roles = {'owner', 'manager', 'staff', 'customer'}
|
||||
invalid_roles = set(value) - valid_roles
|
||||
if invalid_roles:
|
||||
raise serializers.ValidationError(
|
||||
f"Invalid role(s): {', '.join(invalid_roles)}. "
|
||||
f"Valid roles are: {', '.join(valid_roles)}"
|
||||
)
|
||||
return value
|
||||
|
||||
def validate_delivery_method(self, value):
|
||||
"""Accept uppercase delivery method and convert to lowercase."""
|
||||
value_lower = value.lower()
|
||||
valid_methods = ['in_app', 'email', 'sms', 'all']
|
||||
if value_lower not in valid_methods:
|
||||
raise serializers.ValidationError(
|
||||
f"Invalid delivery method. Valid options: {', '.join(valid_methods)}"
|
||||
)
|
||||
return value_lower
|
||||
|
||||
def validate(self, data):
|
||||
"""Ensure at least one target is selected."""
|
||||
"""
|
||||
Convert frontend format to backend format and validate at least one target is selected.
|
||||
"""
|
||||
# Convert target_roles array to boolean fields
|
||||
target_roles = data.pop('target_roles', [])
|
||||
data['target_owners'] = 'owner' in target_roles
|
||||
data['target_managers'] = 'manager' in target_roles
|
||||
data['target_staff'] = 'staff' in target_roles
|
||||
data['target_customers'] = 'customer' in target_roles
|
||||
|
||||
# Convert target_users to individual_recipient_ids (string IDs to integers)
|
||||
target_users = data.pop('target_users', [])
|
||||
if target_users:
|
||||
try:
|
||||
data['individual_recipient_ids'] = [int(uid) for uid in target_users]
|
||||
except (ValueError, TypeError):
|
||||
raise serializers.ValidationError({
|
||||
'target_users': 'All user IDs must be valid integers'
|
||||
})
|
||||
|
||||
# Validate at least one target is selected
|
||||
target_owners = data.get('target_owners', False)
|
||||
target_managers = data.get('target_managers', False)
|
||||
target_staff = data.get('target_staff', False)
|
||||
|
||||
@@ -0,0 +1,328 @@
|
||||
import pytest
|
||||
from datetime import datetime
|
||||
from django.utils import timezone
|
||||
from unittest.mock import Mock
|
||||
|
||||
from smoothschedule.communication.messaging.serializers import (
|
||||
BroadcastMessageListSerializer,
|
||||
BroadcastMessageDetailSerializer,
|
||||
BroadcastMessageCreateSerializer,
|
||||
)
|
||||
from smoothschedule.communication.messaging.models import BroadcastMessage
|
||||
|
||||
|
||||
class TestBroadcastMessageListSerializer:
|
||||
"""Test BroadcastMessageListSerializer returns all required fields."""
|
||||
|
||||
def test_serializer_includes_all_required_fields(self):
|
||||
"""Serializer should include all fields needed by frontend."""
|
||||
# Arrange - create a mock message
|
||||
sender = Mock()
|
||||
sender.id = 1
|
||||
sender.get_full_name.return_value = "John Doe"
|
||||
sender.username = "johndoe"
|
||||
|
||||
message = Mock(spec=BroadcastMessage)
|
||||
message.id = 123
|
||||
message.subject = 'Test Subject'
|
||||
message.body = 'Test message body content'
|
||||
message.status = BroadcastMessage.Status.SENT
|
||||
message.delivery_method = BroadcastMessage.DeliveryMethod.EMAIL
|
||||
message.sender = sender
|
||||
message.target_owners = True
|
||||
message.target_managers = False
|
||||
message.target_staff = True
|
||||
message.target_customers = False
|
||||
message.individual_recipients.all.return_value = []
|
||||
message.individual_recipients.exists.return_value = False
|
||||
message.total_recipients = 10
|
||||
message.delivered_count = 8
|
||||
message.read_count = 5
|
||||
message.created_at = timezone.now()
|
||||
message.sent_at = timezone.now()
|
||||
message.scheduled_at = None
|
||||
|
||||
# Mock the get_target_description method
|
||||
message.get_target_description.return_value = 'Owners, Staff'
|
||||
|
||||
# Act
|
||||
serializer = BroadcastMessageListSerializer(message)
|
||||
data = serializer.data
|
||||
|
||||
# Assert - check all required fields are present
|
||||
assert 'id' in data
|
||||
assert 'subject' in data
|
||||
assert 'body' in data # Frontend needs this for preview
|
||||
assert 'status' in data
|
||||
assert 'delivery_method' in data
|
||||
assert 'created_by' in data # Frontend needs sender ID
|
||||
assert 'created_by_name' in data # Frontend needs sender name
|
||||
assert 'target_roles' in data # Frontend needs array of roles
|
||||
assert 'target_users' in data # Frontend needs array of user IDs
|
||||
assert 'total_recipients' in data
|
||||
assert 'delivered_count' in data
|
||||
assert 'read_count' in data
|
||||
assert 'created_at' in data
|
||||
assert 'sent_at' in data
|
||||
|
||||
def test_target_roles_returns_array_of_selected_roles(self):
|
||||
"""target_roles should return array of selected role strings."""
|
||||
# Arrange
|
||||
sender = Mock()
|
||||
sender.id = 1
|
||||
sender.get_full_name.return_value = "John Doe"
|
||||
|
||||
message = Mock(spec=BroadcastMessage)
|
||||
message.id = 456
|
||||
message.subject = 'Test'
|
||||
message.body = 'Body'
|
||||
message.status = BroadcastMessage.Status.SENT
|
||||
message.delivery_method = BroadcastMessage.DeliveryMethod.IN_APP
|
||||
message.sender = sender
|
||||
message.target_owners = True
|
||||
message.target_managers = True
|
||||
message.target_staff = False
|
||||
message.target_customers = False
|
||||
message.individual_recipients.all.return_value = []
|
||||
message.individual_recipients.exists.return_value = False
|
||||
message.total_recipients = 5
|
||||
message.delivered_count = 5
|
||||
message.read_count = 2
|
||||
message.created_at = timezone.now()
|
||||
message.sent_at = timezone.now()
|
||||
message.scheduled_at = None
|
||||
message.get_target_description.return_value = 'Owners, Managers'
|
||||
|
||||
# Act
|
||||
serializer = BroadcastMessageListSerializer(message)
|
||||
data = serializer.data
|
||||
|
||||
# Assert
|
||||
assert isinstance(data['target_roles'], list)
|
||||
assert 'owner' in data['target_roles']
|
||||
assert 'manager' in data['target_roles']
|
||||
assert 'staff' not in data['target_roles']
|
||||
assert 'customer' not in data['target_roles']
|
||||
|
||||
def test_target_users_returns_array_of_individual_recipient_ids(self):
|
||||
"""target_users should return array of individual recipient IDs."""
|
||||
# Arrange
|
||||
sender = Mock()
|
||||
sender.id = 1
|
||||
sender.get_full_name.return_value = "John Doe"
|
||||
|
||||
user1 = Mock()
|
||||
user1.id = 10
|
||||
|
||||
user2 = Mock()
|
||||
user2.id = 20
|
||||
|
||||
message = Mock(spec=BroadcastMessage)
|
||||
message.id = 456
|
||||
message.subject = 'Test'
|
||||
message.body = 'Body'
|
||||
message.status = BroadcastMessage.Status.SENT
|
||||
message.delivery_method = BroadcastMessage.DeliveryMethod.SMS
|
||||
message.sender = sender
|
||||
message.target_owners = False
|
||||
message.target_managers = False
|
||||
message.target_staff = False
|
||||
message.target_customers = False
|
||||
message.individual_recipients.all.return_value = [user1, user2]
|
||||
message.individual_recipients.exists.return_value = True
|
||||
message.individual_recipients.count.return_value = 2
|
||||
message.total_recipients = 2
|
||||
message.delivered_count = 2
|
||||
message.read_count = 1
|
||||
message.created_at = timezone.now()
|
||||
message.sent_at = timezone.now()
|
||||
message.scheduled_at = None
|
||||
message.get_target_description.return_value = '2 Individual(s)'
|
||||
|
||||
# Act
|
||||
serializer = BroadcastMessageListSerializer(message)
|
||||
data = serializer.data
|
||||
|
||||
# Assert
|
||||
assert isinstance(data['target_users'], list)
|
||||
assert '10' in data['target_users']
|
||||
assert '20' in data['target_users']
|
||||
assert len(data['target_users']) == 2
|
||||
|
||||
def test_delivery_method_format_matches_frontend_expectations(self):
|
||||
"""delivery_method should be uppercase with underscores (IN_APP, not in_app)."""
|
||||
# Arrange
|
||||
sender = Mock()
|
||||
sender.id = 1
|
||||
sender.get_full_name.return_value = "John Doe"
|
||||
|
||||
message = Mock(spec=BroadcastMessage)
|
||||
message.id = 456
|
||||
message.subject = 'Test'
|
||||
message.body = 'Body'
|
||||
message.status = BroadcastMessage.Status.SENT
|
||||
message.delivery_method = 'in_app' # Django stores as lowercase
|
||||
message.sender = sender
|
||||
message.target_owners = True
|
||||
message.target_managers = False
|
||||
message.target_staff = False
|
||||
message.target_customers = False
|
||||
message.individual_recipients.all.return_value = []
|
||||
message.individual_recipients.exists.return_value = False
|
||||
message.total_recipients = 5
|
||||
message.delivered_count = 5
|
||||
message.read_count = 2
|
||||
message.created_at = timezone.now()
|
||||
message.sent_at = timezone.now()
|
||||
message.scheduled_at = None
|
||||
message.get_target_description.return_value = 'Owners'
|
||||
|
||||
# Act
|
||||
serializer = BroadcastMessageListSerializer(message)
|
||||
data = serializer.data
|
||||
|
||||
# Assert
|
||||
assert data['delivery_method'] == 'IN_APP' # Frontend expects uppercase
|
||||
|
||||
def test_created_by_contains_sender_id(self):
|
||||
"""created_by should contain the sender's ID as string."""
|
||||
# Arrange
|
||||
sender = Mock()
|
||||
sender.id = 42
|
||||
sender.get_full_name.return_value = "Jane Smith"
|
||||
|
||||
message = Mock(spec=BroadcastMessage)
|
||||
message.id = 456
|
||||
message.subject = 'Test'
|
||||
message.body = 'Body'
|
||||
message.status = BroadcastMessage.Status.SENT
|
||||
message.delivery_method = BroadcastMessage.DeliveryMethod.EMAIL
|
||||
message.sender = sender
|
||||
message.target_owners = False
|
||||
message.target_managers = True
|
||||
message.target_staff = False
|
||||
message.target_customers = False
|
||||
message.individual_recipients.all.return_value = []
|
||||
message.individual_recipients.exists.return_value = False
|
||||
message.total_recipients = 3
|
||||
message.delivered_count = 3
|
||||
message.read_count = 1
|
||||
message.created_at = timezone.now()
|
||||
message.sent_at = timezone.now()
|
||||
message.scheduled_at = None
|
||||
message.get_target_description.return_value = 'Managers'
|
||||
|
||||
# Act
|
||||
serializer = BroadcastMessageListSerializer(message)
|
||||
data = serializer.data
|
||||
|
||||
# Assert
|
||||
assert data['created_by'] == '42'
|
||||
assert data['created_by_name'] == 'Jane Smith'
|
||||
|
||||
|
||||
class TestBroadcastMessageCreateSerializer:
|
||||
"""Test BroadcastMessageCreateSerializer accepts frontend data format."""
|
||||
|
||||
def test_accepts_target_roles_array(self):
|
||||
"""Serializer should accept target_roles as array and convert to boolean fields."""
|
||||
# Arrange
|
||||
data = {
|
||||
'subject': 'Test Message',
|
||||
'body': 'Test body',
|
||||
'target_roles': ['owner', 'staff'],
|
||||
'target_users': [],
|
||||
'delivery_method': 'IN_APP'
|
||||
}
|
||||
|
||||
# Act
|
||||
serializer = BroadcastMessageCreateSerializer(data=data)
|
||||
|
||||
# Assert
|
||||
assert serializer.is_valid(), serializer.errors
|
||||
validated = serializer.validated_data
|
||||
assert validated['target_owners'] is True
|
||||
assert validated['target_managers'] is False
|
||||
assert validated['target_staff'] is True
|
||||
assert validated['target_customers'] is False
|
||||
|
||||
def test_accepts_target_users_array(self):
|
||||
"""Serializer should accept target_users as array of user IDs."""
|
||||
# Arrange
|
||||
data = {
|
||||
'subject': 'Test Message',
|
||||
'body': 'Test body',
|
||||
'target_roles': [],
|
||||
'target_users': ['10', '20', '30'],
|
||||
'delivery_method': 'EMAIL'
|
||||
}
|
||||
|
||||
# Act
|
||||
serializer = BroadcastMessageCreateSerializer(data=data)
|
||||
|
||||
# Assert
|
||||
assert serializer.is_valid(), serializer.errors
|
||||
validated = serializer.validated_data
|
||||
assert validated['individual_recipient_ids'] == [10, 20, 30]
|
||||
|
||||
def test_accepts_delivery_method_uppercase(self):
|
||||
"""Serializer should accept uppercase delivery method (IN_APP, EMAIL, etc.)."""
|
||||
# Arrange - test all valid uppercase formats
|
||||
test_cases = [
|
||||
('IN_APP', 'in_app'),
|
||||
('EMAIL', 'email'),
|
||||
('SMS', 'sms'),
|
||||
('ALL', 'all'),
|
||||
]
|
||||
|
||||
for input_method, expected_method in test_cases:
|
||||
data = {
|
||||
'subject': 'Test',
|
||||
'body': 'Test body',
|
||||
'target_roles': ['owner'],
|
||||
'target_users': [],
|
||||
'delivery_method': input_method
|
||||
}
|
||||
|
||||
# Act
|
||||
serializer = BroadcastMessageCreateSerializer(data=data)
|
||||
|
||||
# Assert
|
||||
assert serializer.is_valid(), f"Failed for {input_method}: {serializer.errors}"
|
||||
assert serializer.validated_data['delivery_method'] == expected_method
|
||||
|
||||
def test_rejects_invalid_target_roles(self):
|
||||
"""Serializer should reject invalid role names."""
|
||||
# Arrange
|
||||
data = {
|
||||
'subject': 'Test',
|
||||
'body': 'Test body',
|
||||
'target_roles': ['invalid_role'],
|
||||
'target_users': [],
|
||||
'delivery_method': 'IN_APP'
|
||||
}
|
||||
|
||||
# Act
|
||||
serializer = BroadcastMessageCreateSerializer(data=data)
|
||||
|
||||
# Assert
|
||||
assert not serializer.is_valid()
|
||||
assert 'target_roles' in serializer.errors
|
||||
|
||||
def test_requires_at_least_one_target(self):
|
||||
"""Serializer should require at least one target (role or user)."""
|
||||
# Arrange
|
||||
data = {
|
||||
'subject': 'Test',
|
||||
'body': 'Test body',
|
||||
'target_roles': [],
|
||||
'target_users': [],
|
||||
'delivery_method': 'IN_APP'
|
||||
}
|
||||
|
||||
# Act
|
||||
serializer = BroadcastMessageCreateSerializer(data=data)
|
||||
|
||||
# Assert
|
||||
assert not serializer.is_valid()
|
||||
assert 'non_field_errors' in serializer.errors or 'target_roles' in serializer.errors
|
||||
Reference in New Issue
Block a user