mirror of
https://github.com/Studio-Saelix/sencho.git
synced 2026-08-28 11:17:07 +00:00
feat: refactor API calls in NotificationSettingsModal and StackAlertSheet components to use apiFetch
This commit is contained in:
@@ -13,6 +13,7 @@ import { Button } from '@/components/ui/button';
|
|||||||
import { Label } from '@/components/ui/label';
|
import { Label } from '@/components/ui/label';
|
||||||
import { Slider } from '@/components/ui/slider';
|
import { Slider } from '@/components/ui/slider';
|
||||||
import { toast } from 'sonner';
|
import { toast } from 'sonner';
|
||||||
|
import { apiFetch } from '@/lib/api';
|
||||||
|
|
||||||
interface Agent {
|
interface Agent {
|
||||||
type: 'discord' | 'slack' | 'webhook';
|
type: 'discord' | 'slack' | 'webhook';
|
||||||
@@ -51,7 +52,7 @@ export function NotificationSettingsModal({ isOpen, onClose }: NotificationSetti
|
|||||||
|
|
||||||
const fetchAgents = async () => {
|
const fetchAgents = async () => {
|
||||||
try {
|
try {
|
||||||
const res = await fetch('/api/agents');
|
const res = await apiFetch('/agents');
|
||||||
if (res.ok) {
|
if (res.ok) {
|
||||||
const data: Agent[] = await res.json();
|
const data: Agent[] = await res.json();
|
||||||
const newAgents = { ...agents };
|
const newAgents = { ...agents };
|
||||||
@@ -67,7 +68,7 @@ export function NotificationSettingsModal({ isOpen, onClose }: NotificationSetti
|
|||||||
|
|
||||||
const fetchSettings = async () => {
|
const fetchSettings = async () => {
|
||||||
try {
|
try {
|
||||||
const res = await fetch('/api/settings');
|
const res = await apiFetch('/settings');
|
||||||
if (res.ok) {
|
if (res.ok) {
|
||||||
const data = await res.json();
|
const data = await res.json();
|
||||||
setSettings(prev => ({ ...prev, ...data }));
|
setSettings(prev => ({ ...prev, ...data }));
|
||||||
@@ -91,9 +92,8 @@ export function NotificationSettingsModal({ isOpen, onClose }: NotificationSetti
|
|||||||
const saveAgent = async (type: string) => {
|
const saveAgent = async (type: string) => {
|
||||||
setIsLoading(true);
|
setIsLoading(true);
|
||||||
try {
|
try {
|
||||||
const res = await fetch('/api/agents', {
|
const res = await apiFetch('/agents', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: { 'Content-Type': 'application/json' },
|
|
||||||
body: JSON.stringify(agents[type])
|
body: JSON.stringify(agents[type])
|
||||||
});
|
});
|
||||||
if (res.ok) {
|
if (res.ok) {
|
||||||
@@ -115,9 +115,8 @@ export function NotificationSettingsModal({ isOpen, onClose }: NotificationSetti
|
|||||||
}
|
}
|
||||||
setIsLoading(true);
|
setIsLoading(true);
|
||||||
try {
|
try {
|
||||||
const res = await fetch('/api/notifications/test', {
|
const res = await apiFetch('/notifications/test', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: { 'Content-Type': 'application/json' },
|
|
||||||
body: JSON.stringify({ type, url: agents[type].url })
|
body: JSON.stringify({ type, url: agents[type].url })
|
||||||
});
|
});
|
||||||
if (res.ok) {
|
if (res.ok) {
|
||||||
@@ -137,9 +136,8 @@ export function NotificationSettingsModal({ isOpen, onClose }: NotificationSetti
|
|||||||
setIsLoading(true);
|
setIsLoading(true);
|
||||||
try {
|
try {
|
||||||
for (const [key, value] of Object.entries(settings)) {
|
for (const [key, value] of Object.entries(settings)) {
|
||||||
await fetch('/api/settings', {
|
await apiFetch('/settings', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: { 'Content-Type': 'application/json' },
|
|
||||||
body: JSON.stringify({ key, value })
|
body: JSON.stringify({ key, value })
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ import {
|
|||||||
} from '@/components/ui/select';
|
} from '@/components/ui/select';
|
||||||
import { Trash2 } from 'lucide-react';
|
import { Trash2 } from 'lucide-react';
|
||||||
import { toast } from 'sonner';
|
import { toast } from 'sonner';
|
||||||
|
import { apiFetch } from '@/lib/api';
|
||||||
|
|
||||||
interface StackAlert {
|
interface StackAlert {
|
||||||
id?: number;
|
id?: number;
|
||||||
@@ -54,7 +55,7 @@ export function StackAlertSheet({ isOpen, onClose, stackName }: StackAlertSheetP
|
|||||||
|
|
||||||
const fetchAlerts = async () => {
|
const fetchAlerts = async () => {
|
||||||
try {
|
try {
|
||||||
const res = await fetch(`/api/alerts?stackName=${stackName}`);
|
const res = await apiFetch(`/alerts?stackName=${stackName}`);
|
||||||
if (res.ok) {
|
if (res.ok) {
|
||||||
const data = await res.json();
|
const data = await res.json();
|
||||||
setAlerts(data);
|
setAlerts(data);
|
||||||
@@ -81,9 +82,8 @@ export function StackAlertSheet({ isOpen, onClose, stackName }: StackAlertSheetP
|
|||||||
};
|
};
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const res = await fetch('/api/alerts', {
|
const res = await apiFetch('/alerts', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: { 'Content-Type': 'application/json' },
|
|
||||||
body: JSON.stringify(newAlert)
|
body: JSON.stringify(newAlert)
|
||||||
});
|
});
|
||||||
if (res.ok) {
|
if (res.ok) {
|
||||||
@@ -103,7 +103,7 @@ export function StackAlertSheet({ isOpen, onClose, stackName }: StackAlertSheetP
|
|||||||
const deleteAlert = async (id: number) => {
|
const deleteAlert = async (id: number) => {
|
||||||
setIsLoading(true);
|
setIsLoading(true);
|
||||||
try {
|
try {
|
||||||
const res = await fetch(`/api/alerts/${id}`, { method: 'DELETE' });
|
const res = await apiFetch(`/alerts/${id}`, { method: 'DELETE' });
|
||||||
if (res.ok) {
|
if (res.ok) {
|
||||||
toast.success('Alert rule deleted.');
|
toast.success('Alert rule deleted.');
|
||||||
fetchAlerts();
|
fetchAlerts();
|
||||||
|
|||||||
Reference in New Issue
Block a user