Skip to content

Commit

Permalink
Validation on bed type and bed tags
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriel090 committed Jan 23, 2024
1 parent bbf1776 commit 7c90e17
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 33 deletions.
17 changes: 16 additions & 1 deletion src/bed-admission/bed-tag/bed-tags-admin-form.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,21 @@ import { Location } from "@openmrs/esm-framework";
import type { BedTagData } from "../../types";

const BedTagAdministrationSchema = z.object({
name: z.string().max(255),
name: z
.string()
.max(255)
.refine(
(value) => {
return (
typeof value === "string" &&
value.trim().length > 0 &&
!/\d/.test(value)
);
},
{
message: "Bed tag name must not contain numbers",
}
),
});

interface BedTagAdministrationFormProps {
Expand Down Expand Up @@ -88,6 +102,7 @@ const BedTagsAdministrationForm: React.FC<BedTagAdministrationFormProps> = ({
<Controller
name="name"
control={control}
rules={{ required: true }}
render={({ field, fieldState }) => (
<>
<TextInput
Expand Down
35 changes: 33 additions & 2 deletions src/bed-admission/bed-type/bed-type-admin-form.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,36 @@ import { Location } from "@openmrs/esm-framework";
import type { BedType, BedTypeData } from "../../types";

const BedTypeAdministrationSchema = z.object({
name: z.string().max(255),
displayName: z.string().max(255),
name: z
.string()
.max(255)
.refine(
(value) => {
return (
typeof value === "string" &&
value.trim().length > 0 &&
!/\d/.test(value)
);
},
{
message: "Bed Name must be a non-empty string without numbers",
}
),
displayName: z
.string()
.max(255)
.refine(
(value) => {
return (
typeof value === "string" &&
value.trim().length > 0 &&
!/\d/.test(value)
);
},
{
message: "Display name must be a non-empty string without numbers",
}
),
description: z.string().max(255),
});

Expand Down Expand Up @@ -92,6 +120,7 @@ const BedTypeAdministrationForm: React.FC<BedAdministrationFormProps> = ({
<Controller
name="name"
control={control}
rules={{ required: true }}
render={({ field, fieldState }) => (
<>
<TextInput
Expand All @@ -109,6 +138,7 @@ const BedTypeAdministrationForm: React.FC<BedAdministrationFormProps> = ({
<Controller
name="displayName"
control={control}
rules={{ required: true }}
render={({ field, fieldState }) => (
<>
<TextInput
Expand All @@ -126,6 +156,7 @@ const BedTypeAdministrationForm: React.FC<BedAdministrationFormProps> = ({
<Controller
name="description"
control={control}
rules={{ required: true }}
render={({ field, fieldState }) => (
<>
<TextArea
Expand Down
112 changes: 82 additions & 30 deletions src/summary/summary.resource.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import useSWR from "swr";
import { FetchResponse, openmrsFetch } from "@openmrs/esm-framework";
import { FetchResponse, openmrsFetch, showToast } from "@openmrs/esm-framework";
import type { AdmissionLocation, Bed, MappedBedData } from "../types";

export const useLocationsByTag = (locationUuid: string) => {
Expand Down Expand Up @@ -163,49 +163,101 @@ interface BedTag {
export async function saveBedType({
bedPayload,
}): Promise<FetchResponse<BedType>> {
const response: FetchResponse = await openmrsFetch(`/ws/rest/v1/bedtype`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: bedPayload,
});
return response;
try {
const response: FetchResponse = await openmrsFetch(`/ws/rest/v1/bedtype`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: bedPayload,
});
return response;
} catch (error) {
const errorMessages = extractErrorMessagesFromResponse(error);
showToast({
description: errorMessages.join(", "),
title: "Error on saving form",
kind: "error",
critical: true,
});
}
}

export async function saveBedTag({
bedPayload,
}): Promise<FetchResponse<BedTag>> {
const response: FetchResponse = await openmrsFetch(`/ws/rest/v1/bedTag/`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: bedPayload,
});
return response;
try {
const response: FetchResponse = await openmrsFetch(`/ws/rest/v1/bedTag/`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: bedPayload,
});
return response;
} catch (error) {
const errorMessages = extractErrorMessagesFromResponse(error);
showToast({
description: errorMessages.join(", "),
title: "Error on saving form",
kind: "error",
critical: true,
});
}
}
export async function editBedType({
bedPayload,
bedTypeId,
}): Promise<FetchResponse<BedType>> {
const response: FetchResponse = await openmrsFetch(
`/ws/rest/v1/bedtype/${bedTypeId}`,
{
method: "POST",
headers: { "Content-Type": "application/json" },
body: bedPayload,
}
);
return response;
try {
const response: FetchResponse = await openmrsFetch(
`/ws/rest/v1/bedtype/${bedTypeId}`,
{
method: "POST",
headers: { "Content-Type": "application/json" },
body: bedPayload,
}
);
return response;
} catch (error) {
const errorMessages = extractErrorMessagesFromResponse(error);
showToast({
description: errorMessages.join(", "),
title: "Error on saving form",
kind: "error",
critical: true,
});
}
}
export async function editBedTag({
bedPayload,
bedTagId,
}): Promise<FetchResponse<BedType>> {
const response: FetchResponse = await openmrsFetch(
`/ws/rest/v1/bedTag/${bedTagId}`,
{
method: "POST",
headers: { "Content-Type": "application/json" },
body: bedPayload,
}
try {
const response: FetchResponse = await openmrsFetch(
`/ws/rest/v1/bedTag/${bedTagId}`,
{
method: "POST",
headers: { "Content-Type": "application/json" },
body: bedPayload,
}
);
return response;
} catch (error) {
const errorMessages = extractErrorMessagesFromResponse(error);
showToast({
description: errorMessages.join(", "),
title: "Error on saving form",
kind: "error",
critical: true,
});
}
}

export function extractErrorMessagesFromResponse(errorObject) {
const fieldErrors = errorObject?.responseBody?.error?.fieldErrors;

if (!fieldErrors) {
return [errorObject?.responseBody?.error?.message ?? errorObject?.message];
}

return Object.values(fieldErrors).flatMap((errors: Array<Error>) =>
errors.map((error) => error.message)
);
return response;
}

0 comments on commit 7c90e17

Please sign in to comment.