diff --git a/src/api/actions.jsx b/src/api/actions.jsx deleted file mode 100644 index 824d3ff..0000000 --- a/src/api/actions.jsx +++ /dev/null @@ -1,13 +0,0 @@ -export async function getExternalData() { - try { - const response = await fetch('http://portainer.white-enciso.pro:4001/api/v1/MongoSample/GetAll'); - if (!response.ok) { - throw new Error(`HTTP error! status: ${response.status}`); - } - const data = await response.json(); - return data; - } catch (error) { - console.error('Failed to fetch data:', error); - return []; - } -} diff --git a/src/api/mongo/actions.jsx b/src/api/mongo/actions.jsx new file mode 100644 index 0000000..736f8bc --- /dev/null +++ b/src/api/mongo/actions.jsx @@ -0,0 +1,65 @@ +const API_BASE_URL = 'http://portainer.white-enciso.pro:4001/api/v1/MongoSample'; + +export async function getExternalData() { + try { + const response = await fetch(`${API_BASE_URL}/GetAll`); + if (!response.ok) throw new Error('Failed to fetch external data'); + const data = await response.json(); + return data; + } catch (error) { + console.error('Error fetching external data:', error); + return []; + } +} + +export async function createExternalData(data) { + try { + const response = await fetch(`${API_BASE_URL}/Create`, { + method: 'POST', + headers: { + 'Content-Type': 'application/json' + }, + body: JSON.stringify(data) + }); + if (!response.ok) throw new Error('Failed to create external data'); + const result = await response.json(); + return result; + } catch (error) { + console.error('Error creating external data:', error); + throw error; + } +} + +export async function updateExternalData(data) { + const response = await fetch(`${API_BASE_URL}/Update`, { + method: 'PUT', + headers: { + 'Content-Type': 'application/json' + }, + body: JSON.stringify(data) + }); + + if (!response.ok) { + throw new Error('Failed to update item'); + } + + return await response.json(); +} + +export async function deleteExternalData(_Id) { + try { + const response = await fetch(`${API_BASE_URL}/Delete`, { + method: 'DELETE', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ _Id }), + }); + + if (!response.ok) throw new Error('Failed to delete external data'); + return await response.json(); + } catch (error) { + console.error('Error deleting external data:', error); + throw error; + } +} \ No newline at end of file diff --git a/src/private/mongo/AddOrEditAdminForm.jsx b/src/private/mongo/AddOrEditAdminForm.jsx index fb7dd03..b64fd56 100644 --- a/src/private/mongo/AddOrEditAdminForm.jsx +++ b/src/private/mongo/AddOrEditAdminForm.jsx @@ -1,12 +1,6 @@ -import { useEffect, useState } from 'react'; -import { - Box, - Button, - TextField, - Typography, - Paper, - MenuItem -} from '@mui/material'; +import { useState, useEffect } from 'react'; +import { Box, Button, TextField, MenuItem } from '@mui/material'; +import { createExternalData, updateExternalData } from '../../api/mongo/actions'; export default function AddOrEditAdminForm({ onAdd, initialData, onCancel }) { const [formData, setFormData] = useState({ @@ -17,7 +11,7 @@ export default function AddOrEditAdminForm({ onAdd, initialData, onCancel }) { useEffect(() => { if (initialData) { - setFormData(initialData); + setFormData({ ...initialData }); } else { setFormData({ name: '', @@ -29,65 +23,58 @@ export default function AddOrEditAdminForm({ onAdd, initialData, onCancel }) { const handleChange = (e) => { const { name, value } = e.target; - setFormData((prev) => ({ - ...prev, - [name]: value - })); + setFormData(prev => ({ ...prev, [name]: value })); }; - const handleSubmit = () => { - if (onAdd) { - onAdd(formData); + const handleSubmit = async () => { + try { + if (initialData) { + await updateExternalData(formData); + } else { + await createExternalData(formData); + } + if (onAdd) onAdd(); + } catch (error) { + console.error('Error submitting form:', error); } }; return ( - - - - Item details - - + + + + + Active + Inactive + + + + + - - - - Active - Inactive - - - - - - - ); } \ No newline at end of file diff --git a/src/private/mongo/Admin.jsx b/src/private/mongo/Admin.jsx index 97bb861..5d7f614 100644 --- a/src/private/mongo/Admin.jsx +++ b/src/private/mongo/Admin.jsx @@ -5,7 +5,7 @@ import { Typography, Button, Dialog, DialogTitle, DialogContent, IconButton, Box import EditRoundedIcon from '@mui/icons-material/EditRounded'; import DeleteRoundedIcon from '@mui/icons-material/DeleteRounded'; import AddOrEditAdminForm from './AddOrEditAdminForm'; -import { getExternalData } from '../../api/actions'; +import { getExternalData, deleteExternalData } from '../../api/mongo/actions'; const columnsBase = [ { field: 'name', headerName: 'Name', flex: 2 }, @@ -41,13 +41,23 @@ export default function Admin() { const [rowToDelete, setRowToDelete] = useState(null); useEffect(() => { - getExternalData().then(data => { - const safeData = Array.isArray(data) ? data : []; - setRows(safeData); - }).catch(error => { - console.error('Error loading data:', error); - setRows([]); - }); + let isMounted = true; + + getExternalData() + .then(data => { + if (isMounted) { + const safeData = Array.isArray(data) ? data : []; + setRows(safeData); + } + }) + .catch(error => { + console.error('Error loading data:', error); + if (isMounted) setRows([]); + }); + + return () => { + isMounted = false; + }; }, []); const handleEditClick = (params) => { @@ -60,6 +70,18 @@ export default function Admin() { setConfirmOpen(true); }; + const handleConfirmDelete = async () => { + try { + await deleteExternalData(rowToDelete._Id); + setRows((prevRows) => prevRows.filter(r => r._Id !== rowToDelete._Id)); + } catch (error) { + console.error('Delete failed:', error); + } finally { + setConfirmOpen(false); + setRowToDelete(null); + } + }; + const columns = [ ...columnsBase, { @@ -118,7 +140,7 @@ export default function Admin() { - + @@ -140,4 +162,4 @@ export default function Admin() { ); -} +} \ No newline at end of file