All files / src/views/devices DeviceAdd.js

0% Statements 0/15
0% Branches 0/6
0% Functions 0/3
0% Lines 0/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                                                                                                                           
// import React from 'react';
import { DynamicForm } from '@components/Dynamic';
import deviceTypes from '@config/deviceTypes';
import { apiService } from '@services';
 
import { CContainer, CAlert } from '@coreui/react'
 
const devicesApi = apiService('devices');
 
const DeviceAdd = ({ popupStatus, dbAllDevices, setDbAllDevices, onDeviceAdded }) => {
 
	const addDeviceFormFields = [
		{
			name: 'type',
			label: 'Device Type',
			type: 'select',
			options: deviceTypes.map(d => ({ label: d.label, value: d.value })),
			required: true,
		},
		{
			name: 'macAddress',
			label: 'Address',
			type: 'text',
			placeholder: 'Enter address | Upper Case',
			required: true,
		},
		{
			name: 'name',
			label: 'Device Name',
			type: 'text',
			placeholder: 'Enter device name',
			required: true,
		},
	];
 
	// Handle successful form submit
	const addDeviceHandleFormSubmit = async (formData) => {
		try {
			// See that the API returns the newly added device
			const newDevice = await devicesApi.add(formData);
 
			// Add a new device to the list through a setter from the parent
			if (setDbAllDevices && dbAllDevices) setDbAllDevices([...dbAllDevices, newDevice]);
 
			// Close the popup
			if (popupStatus) popupStatus.current.close();
 
			// Trigger parent function (AddMarker)
			onDeviceAdded?.(newDevice.macAddress);
 
 
		} catch (err) {
			console.error('Something went wrong while adding:', err);
			alert('Something went wrong while adding');
		}
	};
 
	return (
		<CContainer>
			<CAlert color="secondary">Make sure your address is always Upper case!</CAlert>
			<DynamicForm
				fields={addDeviceFormFields}
				onSubmit={addDeviceHandleFormSubmit}
				submitButtonLabel="Add Device"
			/>
		</CContainer>
	);
};
 
export default DeviceAdd;