Fixed: Remove [Default] alert template option
- The Alert Template field in the Service dialog will now correctly fetch data from service_notification_templates
This commit is contained in:
@@ -1,4 +1,3 @@
|
|||||||
|
|
||||||
import { FormControl, FormField, FormItem, FormLabel, FormDescription } from "@/components/ui/form";
|
import { FormControl, FormField, FormItem, FormLabel, FormDescription } from "@/components/ui/form";
|
||||||
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select";
|
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select";
|
||||||
import { Switch } from "@/components/ui/switch";
|
import { Switch } from "@/components/ui/switch";
|
||||||
@@ -8,6 +7,7 @@ import { UseFormReturn } from "react-hook-form";
|
|||||||
import { ServiceFormData } from "./types";
|
import { ServiceFormData } from "./types";
|
||||||
import { useQuery } from "@tanstack/react-query";
|
import { useQuery } from "@tanstack/react-query";
|
||||||
import { alertConfigService, AlertConfiguration } from "@/services/alertConfigService";
|
import { alertConfigService, AlertConfiguration } from "@/services/alertConfigService";
|
||||||
|
import { serviceNotificationTemplateService, ServiceNotificationTemplate } from "@/services/serviceNotificationTemplateService";
|
||||||
import { useState, useEffect } from "react";
|
import { useState, useEffect } from "react";
|
||||||
|
|
||||||
interface ServiceNotificationFieldsProps {
|
interface ServiceNotificationFieldsProps {
|
||||||
@@ -22,18 +22,18 @@ export function ServiceNotificationFields({ form }: ServiceNotificationFieldsPro
|
|||||||
const notificationChannels = form.watch("notificationChannels") || [];
|
const notificationChannels = form.watch("notificationChannels") || [];
|
||||||
const alertTemplate = form.watch("alertTemplate");
|
const alertTemplate = form.watch("alertTemplate");
|
||||||
|
|
||||||
// console.log("Current notification values:", {
|
|
||||||
// notificationStatus,
|
|
||||||
// notificationChannels,
|
|
||||||
// alertTemplate
|
|
||||||
// });
|
|
||||||
|
|
||||||
// Fetch alert configurations for notification channels
|
// Fetch alert configurations for notification channels
|
||||||
const { data: alertConfigsData } = useQuery({
|
const { data: alertConfigsData } = useQuery({
|
||||||
queryKey: ['alertConfigs'],
|
queryKey: ['alertConfigs'],
|
||||||
queryFn: () => alertConfigService.getAlertConfigurations(),
|
queryFn: () => alertConfigService.getAlertConfigurations(),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Fetch service notification templates
|
||||||
|
const { data: serviceTemplates, isLoading: isLoadingTemplates } = useQuery({
|
||||||
|
queryKey: ['serviceNotificationTemplates'],
|
||||||
|
queryFn: () => serviceNotificationTemplateService.getTemplates(),
|
||||||
|
});
|
||||||
|
|
||||||
// Update alert configs when data is loaded
|
// Update alert configs when data is loaded
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (alertConfigsData) {
|
if (alertConfigsData) {
|
||||||
@@ -42,13 +42,19 @@ export function ServiceNotificationFields({ form }: ServiceNotificationFieldsPro
|
|||||||
setAlertConfigs(enabledChannels);
|
setAlertConfigs(enabledChannels);
|
||||||
|
|
||||||
// Debug log to check what alert configs are loaded
|
// Debug log to check what alert configs are loaded
|
||||||
// console.log("Loaded alert configurations:", enabledChannels);
|
|
||||||
}
|
}
|
||||||
}, [alertConfigsData]);
|
}, [alertConfigsData]);
|
||||||
|
|
||||||
|
// Debug log for service templates
|
||||||
|
useEffect(() => {
|
||||||
|
if (serviceTemplates) {
|
||||||
|
// console.log("Loaded service notification templates:", serviceTemplates);
|
||||||
|
}
|
||||||
|
}, [serviceTemplates]);
|
||||||
|
|
||||||
// Log when form values change to debug
|
// Log when form values change to debug
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
// console.log("Notification values changed:", {
|
// console.log("Notification values changed:", {
|
||||||
// notificationStatus: form.getValues("notificationStatus"),
|
// notificationStatus: form.getValues("notificationStatus"),
|
||||||
// notificationChannels: form.getValues("notificationChannels")
|
// notificationChannels: form.getValues("notificationChannels")
|
||||||
// });
|
// });
|
||||||
@@ -159,9 +165,7 @@ export function ServiceNotificationFields({ form }: ServiceNotificationFieldsPro
|
|||||||
control={form.control}
|
control={form.control}
|
||||||
name="alertTemplate"
|
name="alertTemplate"
|
||||||
render={({ field }) => {
|
render={({ field }) => {
|
||||||
// Don't convert existing values to "default"
|
// console.log("Rendering alert template field with value:", field.value);
|
||||||
const displayValue = field.value || "default";
|
|
||||||
// console.log("Rendering alert template field with value:", displayValue);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<FormItem>
|
<FormItem>
|
||||||
@@ -170,17 +174,20 @@ export function ServiceNotificationFields({ form }: ServiceNotificationFieldsPro
|
|||||||
<Select
|
<Select
|
||||||
onValueChange={(value) => {
|
onValueChange={(value) => {
|
||||||
console.log("Alert template changed to:", value);
|
console.log("Alert template changed to:", value);
|
||||||
field.onChange(value === "default" ? "" : value);
|
field.onChange(value);
|
||||||
}}
|
}}
|
||||||
value={displayValue}
|
value={field.value || ""}
|
||||||
disabled={notificationStatus !== "enabled"}
|
disabled={notificationStatus !== "enabled" || isLoadingTemplates}
|
||||||
>
|
>
|
||||||
<SelectTrigger className={notificationStatus !== "enabled" ? 'opacity-50' : ''}>
|
<SelectTrigger className={notificationStatus !== "enabled" ? 'opacity-50' : ''}>
|
||||||
<SelectValue placeholder="Select an alert template" />
|
<SelectValue placeholder={isLoadingTemplates ? "Loading templates..." : "Select an alert template"} />
|
||||||
</SelectTrigger>
|
</SelectTrigger>
|
||||||
<SelectContent>
|
<SelectContent>
|
||||||
<SelectItem value="default">Default</SelectItem>
|
{serviceTemplates?.map((template) => (
|
||||||
{/* Add templates here when available */}
|
<SelectItem key={template.id} value={template.id}>
|
||||||
|
{template.name}
|
||||||
|
</SelectItem>
|
||||||
|
))}
|
||||||
</SelectContent>
|
</SelectContent>
|
||||||
</Select>
|
</Select>
|
||||||
</FormControl>
|
</FormControl>
|
||||||
|
|||||||
Reference in New Issue
Block a user