Skip to content

Commit

Permalink
added serial number field, fix network bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Codebmk committed Feb 4, 2025
1 parent 35ca1d0 commit c0c8b8c
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 27 deletions.
113 changes: 87 additions & 26 deletions netmanager/src/views/components/DataDisplay/Devices.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,15 +277,17 @@ const CreateDevice = ({ open, setOpen, network }) => {
const newDeviceInitState = {
long_name: '',
category: CATEGORIES[0].value,
network: network,
description: ''
network: network.net_name,
description: '',
serial_number: ''
};

const initialErrors = {
long_name: '',
category: '',
network: '',
description: ''
description: '',
serial_number: ''
};

const [newDevice, setNewDevice] = useState(newDeviceInitState);
Expand All @@ -303,6 +305,13 @@ const CreateDevice = ({ open, setOpen, network }) => {
long_name: newValue.trim() === '' ? 'Device name is required' : ''
});
}

if (key === 'serial_number') {
setErrors({
...errors,
serial_number: newValue.trim() === '' ? 'Serial number is required' : ''
});
}
};

const handleDropdownChange = (event, { name }) => {
Expand All @@ -322,14 +331,19 @@ const CreateDevice = ({ open, setOpen, network }) => {
setNewDevice({
long_name: '',
category: CATEGORIES[0].value,
network: network,
description: ''
network: network.net_name,
description: '',
serial_number: ''
});
setErrors({ long_name: '', category: '', network: '', description: '' });
setErrors({ long_name: '', category: '', network: '', description: '', serial_number: '' });
};

const isFormValid = () => {
return newDevice.long_name.trim() !== '' && newDevice.category !== '';
return (
newDevice.long_name.trim() !== '' &&
newDevice.category !== '' &&
newDevice.serial_number.trim() !== ''
);
};

const handleRegisterSubmit = (e) => {
Expand All @@ -350,16 +364,26 @@ const CreateDevice = ({ open, setOpen, network }) => {
return;
}

// Create a copy of newDevice
const deviceDataToSend = { ...newDevice };

// Remove fields with empty values
Object.keys(deviceDataToSend).forEach((key) => {
if (!deviceDataToSend[key]) {
delete deviceDataToSend[key];
}
});

createAxiosInstance()
.post(REGISTER_DEVICE_URI, newDevice, {
.post(REGISTER_DEVICE_URI, deviceDataToSend, {
headers: { 'Content-Type': 'application/json' }
})
.then((res) => res.data)
.then((resData) => {
handleRegisterClose();
dispatch(loadStatus(false));
if (!isEmpty(network)) {
dispatch(loadDevicesData(network));
if (!isEmpty(network.net_name)) {
dispatch(loadDevicesData(network.net_name));
}
dispatch(
updateMainAlert({
Expand Down Expand Up @@ -440,6 +464,18 @@ const CreateDevice = ({ open, setOpen, network }) => {
required
/>

<TextField
margin="dense"
label="Serial Number"
variant="outlined"
value={newDevice.serial_number}
onChange={handleDeviceDataChange('serial_number')}
fullWidth
required
error={!!errors.serial_number}
helperText={errors.serial_number}
/>

<TextField
fullWidth
margin="dense"
Expand Down Expand Up @@ -500,7 +536,8 @@ const SoftCreateDevice = ({ open, setOpen, network }) => {
device_number: '',
writeKey: '',
readKey: '',
description: ''
description: '',
serial_number: ''
};

const initialErrors = {
Expand All @@ -510,7 +547,8 @@ const SoftCreateDevice = ({ open, setOpen, network }) => {
device_number: '',
writeKey: '',
readKey: '',
description: ''
description: '',
serial_number: ''
};

const [newDevice, setNewDevice] = useState(newDeviceInitState);
Expand All @@ -529,6 +567,13 @@ const SoftCreateDevice = ({ open, setOpen, network }) => {
long_name: newValue.trim() === '' ? 'Device name is required' : ''
});
}

if (key === 'serial_number') {
setErrors({
...errors,
serial_number: newValue.trim() === '' ? 'Serial number is required' : ''
});
}
};

const handleDropdownChange = (event, { name }) => {
Expand All @@ -553,7 +598,8 @@ const SoftCreateDevice = ({ open, setOpen, network }) => {
device_number: '',
writeKey: '',
readKey: '',
description: ''
description: '',
serial_number: ''
});
setErrors({
long_name: '',
Expand All @@ -562,12 +608,17 @@ const SoftCreateDevice = ({ open, setOpen, network }) => {
device_number: '',
writeKey: '',
readKey: '',
description: ''
description: '',
serial_number: ''
});
};

const isFormValid = () => {
return newDevice.long_name.trim() !== '' && newDevice.category !== '';
return (
newDevice.long_name.trim() !== '' &&
newDevice.category !== '' &&
newDevice.serial_number.trim() !== ''
);
};

const handleRegisterSubmit = async (e) => {
Expand All @@ -590,16 +641,12 @@ const SoftCreateDevice = ({ open, setOpen, network }) => {
// Create a copy of newDevice
const deviceDataToSend = { ...newDevice };

// Remove device_number, writeKey, and readKey if they're empty
if (!deviceDataToSend.device_number) {
delete deviceDataToSend.device_number;
}
if (!deviceDataToSend.writeKey) {
delete deviceDataToSend.writeKey;
}
if (!deviceDataToSend.readKey) {
delete deviceDataToSend.readKey;
}
// Remove fields with empty values
Object.keys(deviceDataToSend).forEach((key) => {
if (!deviceDataToSend[key]) {
delete deviceDataToSend[key];
}
});

const resData = await softCreateDeviceApi(deviceDataToSend, {
headers: { 'Content-Type': 'application/json' }
Expand All @@ -612,7 +659,7 @@ const SoftCreateDevice = ({ open, setOpen, network }) => {
dispatch(
updateMainAlert({
message: `${resData.message}. ${
newDevice.network !== network
newDevice.network !== network.net_name
? `Switch to the ${newDevice.network} organisation to see the new device.`
: ''
}`,
Expand Down Expand Up @@ -671,6 +718,7 @@ const SoftCreateDevice = ({ open, setOpen, network }) => {
error={!!errors.long_name}
helperText={errors.long_name}
/>

<Select
fullWidth
label="Category"
Expand All @@ -687,6 +735,19 @@ const SoftCreateDevice = ({ open, setOpen, network }) => {
}}
required
/>

<TextField
margin="dense"
label="Serial Number"
variant="outlined"
value={newDevice.serial_number}
onChange={handleDeviceDataChange('serial_number')}
fullWidth
required
error={!!errors.serial_number}
helperText={errors.serial_number}
/>

<TextField
fullWidth
margin="dense"
Expand Down
7 changes: 6 additions & 1 deletion netmanager/src/views/layouts/Main.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,12 @@ const Main = (props) => {
}, []);

useEffect(() => {
if (!isEmpty(activeNetwork)) {
if (isEmpty(activeNetwork)) {
const activeNetworkStorage = localStorage.getItem('activeNetwork');
if (activeNetworkStorage) {
dispatch(addActiveNetwork(JSON.parse(activeNetworkStorage)));
}
} else {
dispatch(addCurrentUserRole(activeNetwork.role));
localStorage.setItem('currentUserRole', JSON.stringify(activeNetwork.role));
}
Expand Down

0 comments on commit c0c8b8c

Please sign in to comment.