Spaces:
Configuration error
Configuration error
File size: 4,372 Bytes
447ebeb |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
import React, { useState, useEffect } from "react";
import { Card, Text, Grid, Button } from "@tremor/react";
import { Typography, message, Divider, Spin, Checkbox } from "antd";
import { getEmailEventSettings, updateEmailEventSettings, resetEmailEventSettings } from "../networking";
import { EmailEvent } from "../../types";
import { EmailEventSetting } from "./types";
const { Title } = Typography;
interface EmailEventSettingsProps {
accessToken: string | null;
}
const EmailEventSettings: React.FC<EmailEventSettingsProps> = ({
accessToken,
}) => {
const [loading, setLoading] = useState(true);
const [eventSettings, setEventSettings] = useState<EmailEventSetting[]>([]);
// Fetch email event settings on component mount
useEffect(() => {
fetchEventSettings();
}, [accessToken]);
const fetchEventSettings = async () => {
if (!accessToken) return;
setLoading(true);
try {
const response = await getEmailEventSettings(accessToken);
setEventSettings(response.settings);
} catch (error) {
console.error("Failed to fetch email event settings:", error);
message.error("Failed to fetch email event settings");
} finally {
setLoading(false);
}
};
const handleCheckboxChange = (event: EmailEvent, checked: boolean) => {
const updatedSettings = eventSettings.map(setting =>
setting.event === event ? { ...setting, enabled: checked } : setting
);
setEventSettings(updatedSettings);
};
const handleSaveSettings = async () => {
if (!accessToken) return;
try {
await updateEmailEventSettings(accessToken, { settings: eventSettings });
message.success("Email event settings updated successfully");
} catch (error) {
console.error("Failed to update email event settings:", error);
message.error("Failed to update email event settings");
}
};
const handleResetSettings = async () => {
if (!accessToken) return;
try {
await resetEmailEventSettings(accessToken);
message.success("Email event settings reset to defaults");
// Refresh settings after reset
fetchEventSettings();
} catch (error) {
console.error("Failed to reset email event settings:", error);
message.error("Failed to reset email event settings");
}
};
// Helper function to get a description for each event type
const getEventDescription = (event: EmailEvent): string => {
// Convert event name to a sentence with more context
if (event.includes("Virtual Key Created")) {
return "An email will be sent to the user when a new virtual key is created with their user ID";
} else if (event.includes("New User Invitation")) {
return "An email will be sent to the email address of the user when a new user is created";
} else {
// Handle any other event type from the API
const words = event.split(/(?=[A-Z])/).join(' ').toLowerCase();
return `Receive an email notification when ${words}`;
}
};
return (
<Card>
<Title level={4}>Email Notifications</Title>
<Text>
Select which events should trigger email notifications.
</Text>
<Divider />
{loading ? (
<div style={{ textAlign: 'center', padding: '20px' }}>
<Spin size="large" />
</div>
) : (
<div className="space-y-4">
{eventSettings.map((setting) => (
<div key={setting.event} className="flex items-center">
<Checkbox
checked={setting.enabled}
onChange={(e) => handleCheckboxChange(setting.event, e.target.checked)}
/>
<div className="ml-3">
<Text>{setting.event}</Text>
<div className="text-sm text-gray-500 block">
{getEventDescription(setting.event)}
</div>
</div>
</div>
))}
</div>
)}
<div className="mt-6 flex space-x-4">
<Button
onClick={handleSaveSettings}
disabled={loading}
>
Save Changes
</Button>
<Button
onClick={handleResetSettings}
variant="secondary"
disabled={loading}
>
Reset to Defaults
</Button>
</div>
</Card>
);
};
export default EmailEventSettings; |