Skip to content

Commit

Permalink
fix: Subscriber page crashes when webui returns nil (#193)
Browse files Browse the repository at this point in the history
  • Loading branch information
gruyaume authored Nov 29, 2023
1 parent 6979066 commit f2e60c3
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 33 deletions.
70 changes: 48 additions & 22 deletions components/CreateSubscriberModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,22 @@ const CreateSubscriberModal = ({ toggleModal }: Props) => {
const queryClient = useQueryClient();

const SubscriberSchema = Yup.object().shape({
imsi: Yup.string().min(14).max(15)
.matches(/^[0-9]+$/, { message: 'Only numbers are allowed.'})
imsi: Yup.string()
.min(14)
.max(15)
.matches(/^[0-9]+$/, { message: "Only numbers are allowed." })
.required("IMSI must be 14 or 15 digits"),
opc: Yup.string().length(32)
.matches(/^[A-Za-z0-9]+$/, { message: 'Only alphanumeric characters are allowed.'})
opc: Yup.string()
.length(32)
.matches(/^[A-Za-z0-9]+$/, {
message: "Only alphanumeric characters are allowed.",
})
.required("OPC must be a 32 character hexadecimal"),
key: Yup.string().length(32)
.matches(/^[A-Za-z0-9]+$/, { message: 'Only alphanumeric characters are allowed.'})
key: Yup.string()
.length(32)
.matches(/^[A-Za-z0-9]+$/, {
message: "Only alphanumeric characters are allowed.",
})
.required("Key must be a 32 character hexadecimal"),
sequenceNumber: Yup.string().required("Sequence number is required"),
deviceGroup: Yup.string().required(""),
Expand All @@ -57,7 +65,6 @@ const CreateSubscriberModal = ({ toggleModal }: Props) => {
onSubmit: async (values) => {
await createSubscriber({
imsi: values.imsi,
plmnId: "20893",
opc: values.opc,
key: values.key,
sequenceNumber: values.sequenceNumber,
Expand All @@ -73,35 +80,50 @@ const CreateSubscriberModal = ({ toggleModal }: Props) => {
queryFn: getNetworkSlices,
});

const selectedSlice = slices.find((slice) => slice.SliceName === formik.values.selectedSlice);
const selectedSlice = slices.find(
(slice) => slice.SliceName === formik.values.selectedSlice,
);

const setDeviceGroup = useCallback((deviceGroup: string) => {
if (formik.values.deviceGroup !== deviceGroup) {
formik.setFieldValue("deviceGroup", deviceGroup);
}
}, [formik]);
const setDeviceGroup = useCallback(
(deviceGroup: string) => {
if (formik.values.deviceGroup !== deviceGroup) {
formik.setFieldValue("deviceGroup", deviceGroup);
}
},
[formik],
);

useEffect(() => {
if (selectedSlice && selectedSlice["site-device-group"] && selectedSlice["site-device-group"].length === 1) {
if (
selectedSlice &&
selectedSlice["site-device-group"] &&
selectedSlice["site-device-group"].length === 1
) {
setDeviceGroup(selectedSlice["site-device-group"][0]);
} else {
setDeviceGroup("");
}
}, [slices, selectedSlice, setDeviceGroup]);

const setSelectedSlice = useCallback((newSlice: string) => {
if (formik.values.selectedSlice !== newSlice) {
formik.setFieldValue("selectedSlice", newSlice);
}
}, [formik]);
const setSelectedSlice = useCallback(
(newSlice: string) => {
if (formik.values.selectedSlice !== newSlice) {
formik.setFieldValue("selectedSlice", newSlice);
}
},
[formik],
);

useEffect(() => {
if (!selectedSlice && slices.length > 0) {
setSelectedSlice(slices[0].SliceName);
}
}, [slices, selectedSlice, setSelectedSlice]);

const deviceGroupOptions = selectedSlice && selectedSlice["site-device-group"] ? selectedSlice["site-device-group"] : [];
const deviceGroupOptions =
selectedSlice && selectedSlice["site-device-group"]
? selectedSlice["site-device-group"]
: [];

return (
<Modal
Expand Down Expand Up @@ -162,15 +184,19 @@ const CreateSubscriberModal = ({ toggleModal }: Props) => {
stacked
required
{...formik.getFieldProps("sequenceNumber")}
error={formik.touched.sequenceNumber ? formik.errors.sequenceNumber : null}
error={
formik.touched.sequenceNumber ? formik.errors.sequenceNumber : null
}
/>
<Select
id="network-slice"
label="Network Slice"
stacked
required
{...formik.getFieldProps("selectedSlice")}
error={formik.touched.selectedSlice ? formik.errors.selectedSlice : null}
error={
formik.touched.selectedSlice ? formik.errors.selectedSlice : null
}
options={[
{
disabled: true,
Expand Down
15 changes: 7 additions & 8 deletions pages/api/subscriber/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { NextApiRequest, NextApiResponse } from "next";


const WEBUI_ENDPOINT = process.env.WEBUI_ENDPOINT;


export default async function handleSubscribers(req: NextApiRequest, res: NextApiResponse) {
switch (req.method) {
case "GET":
Expand All @@ -14,7 +12,6 @@ export default async function handleSubscribers(req: NextApiRequest, res: NextAp
}
}


async function handleGET(req: NextApiRequest, res: NextApiResponse) {
const url = `${WEBUI_ENDPOINT}/api/subscriber/`;

Expand All @@ -27,18 +24,20 @@ async function handleGET(req: NextApiRequest, res: NextApiResponse) {
});

if (!response.ok) {
throw new Error(
`Error retrieving subscriber. Error code: ${response.status}`
);
throw new Error(`Error retrieving subscriber. Error code: ${response.status}`);
}

const data = await response.json();
let data = await response.json();

if (!Array.isArray(data) || data.length === 0) {
data = [];
}

res.status(200).json(data);
} catch (error) {
console.error("Error details:", error);
res.status(500).json({
error: "An error occurred while retrieving subscriber",
error: "An error occurred while retrieving subscribers",
});
}
}
3 changes: 0 additions & 3 deletions utils/createSubscriber.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
interface CreateSubscriberArgs {
imsi: string;
plmnId: string;
opc: string;
key: string;
sequenceNumber: string;
Expand All @@ -9,15 +8,13 @@ interface CreateSubscriberArgs {

export const createSubscriber = async ({
imsi,
plmnId,
opc,
key,
sequenceNumber,
deviceGroupName,
}: CreateSubscriberArgs) => {
const subscriberData = {
UeId: imsi,
plmnId: plmnId,
opc: opc,
key: key,
sequenceNumber: sequenceNumber,
Expand Down

0 comments on commit f2e60c3

Please sign in to comment.