Skip to content
Snippets Groups Projects
modal.view.tsx 4.73 KiB
Newer Older
  • Learn to ignore specific revisions
  • Matthias Feyll's avatar
    Matthias Feyll committed
    import { NetworkelementAddListRequest, useNetworkElementServiceAddListMutation } from '@api/api';
    import React, { useState } from 'react';
    import { Button, Form, Modal } from 'react-bootstrap';
    
    interface AddDeviceModalProps {
        show: boolean;
        onHide: () => void;
    }
    
    interface FormData {
        address: '',
        mneName: '',
        transportOption: undefined,
        gnmiSubscribePaths: [],
    }
    
    const AddDeviceModal: React.FC<AddDeviceModalProps> = ({ show, onHide }) => {
        const [addNetworkElement] = useNetworkElementServiceAddListMutation();
        const [formData, setFormData] = useState<FormData>({
            address: '',
            mneName: '',
            transportOption: undefined,
            gnmiSubscribePaths: [],
        });
    
        const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
            const { name, value } = e.target;
            setFormData(prev => ({
                ...prev,
                [name]: value
            }));
        };
    
        const handleSubmit = async (e: React.FormEvent) => {
            e.preventDefault();
    
            const request: NetworkelementAddListRequest = {
                timestamp: Date.now().toString(), // Convert to nanoseconds if needed
                mne: [formData],
                pid: formData.pid
            };
    
            try {
                await addNetworkElement({ networkelementAddListRequest: request });
                handleReset();
                // You might want to add a success notification here
            } catch (error) {
                console.error('Failed to add device:', error);
                // You might want to add an error notification here
            }
        };
    
        const handleReset = () => {
            setFormData({
                address: '',
                pid: '',
                pluginId: '',
                mneName: '',
                transportOption: undefined,
                gnmiSubscribePaths: [],
                mneId: ''
            });
            onHide();
        };
    
        return (
            <Modal show={show} onHide={handleReset} centered>
                <Modal.Header closeButton>
                    <Modal.Title>Add New Device</Modal.Title>
                </Modal.Header>
                <Form onSubmit={handleSubmit}>
                    <Modal.Body>
                        <Form.Group className="mb-3">
                            <Form.Label>Address</Form.Label>
                            <Form.Control
                                type="text"
                                name="address"
                                value={formData.address}
                                onChange={handleInputChange}
                                placeholder="Enter device address"
                            />
                        </Form.Group>
    
                        <Form.Group className="mb-3">
                            <Form.Label>PID</Form.Label>
                            <Form.Control
                                type="text"
                                name="pid"
                                value={formData.pid}
                                onChange={handleInputChange}
                                placeholder="Enter PID"
                            />
                        </Form.Group>
    
                        <Form.Group className="mb-3">
                            <Form.Label>Plugin ID</Form.Label>
                            <Form.Control
                                type="text"
                                name="pluginId"
                                value={formData.pluginId}
                                onChange={handleInputChange}
                                placeholder="Enter plugin ID"
                            />
                        </Form.Group>
    
                        <Form.Group className="mb-3">
                            <Form.Label>MNE Name</Form.Label>
                            <Form.Control
                                type="text"
                                name="mneName"
                                value={formData.mneName}
                                onChange={handleInputChange}
                                placeholder="Enter MNE name"
                            />
                        </Form.Group>
    
                        <Form.Group className="mb-3">
                            <Form.Label>MNE ID</Form.Label>
                            <Form.Control
                                type="text"
                                name="mneId"
                                value={formData.mneId}
                                onChange={handleInputChange}
                                placeholder="Enter MNE ID"
                            />
                        </Form.Group>
                    </Modal.Body>
                    <Modal.Footer>
                        <Button variant="secondary" onClick={handleReset}>
                            Cancel
                        </Button>
                        <Button variant="primary" type="submit">
                            Add Device
                        </Button>
                    </Modal.Footer>
                </Form>
            </Modal>
        );
    };
    
    export default AddDeviceModal;