generated from METS-Programme/esm-ugandaemr-template-app
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a7c9fd1
commit cdf0ba9
Showing
16 changed files
with
1,212 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
217 changes: 217 additions & 0 deletions
217
src/bed-admission/bed-tag/bed-tag-administration-table.component.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,217 @@ | ||
import React, { useMemo, useState } from "react"; | ||
import { useTranslation } from "react-i18next"; | ||
import { | ||
Button, | ||
DataTable, | ||
DataTableSkeleton, | ||
InlineLoading, | ||
Pagination, | ||
Table, | ||
TableBody, | ||
TableCell, | ||
TableContainer, | ||
TableHead, | ||
TableHeader, | ||
TableRow, | ||
Tile, | ||
} from "@carbon/react"; | ||
import { Add, Edit } from "@carbon/react/icons"; | ||
import { | ||
isDesktop as desktopLayout, | ||
useLayoutType, | ||
} from "@openmrs/esm-framework"; | ||
import { CardHeader, ErrorState } from "@openmrs/esm-patient-common-lib"; | ||
import type { BedTagData } from "../../types"; | ||
import { useBedTag } from "../../summary/summary.resource"; | ||
import Header from "../../header/header.component"; | ||
import styles from "../../bed-administration/bed-administration-table.scss"; | ||
import BedTagForm from "./new-tag-form.component"; | ||
import EditBedTagForm from "./edit-tag-form.component"; | ||
|
||
const BedTagAdministrationTable: React.FC = () => { | ||
const { t } = useTranslation(); | ||
const headerTitle = t("bedTag", "Bed Tag"); | ||
const layout = useLayoutType(); | ||
const isTablet = layout === "tablet"; | ||
const responsiveSize = isTablet ? "lg" : "sm"; | ||
const isDesktop = desktopLayout(layout); | ||
const [isBedDataLoading] = useState(false); | ||
const [showBedTagsModal, setAddBedTagsModal] = useState(false); | ||
const [showEditBedModal, setShowEditBedModal] = useState(false); | ||
const [editData, setEditData] = useState<BedTagData>(); | ||
const [currentPage, setCurrentPage] = useState(1); | ||
const [pageSize] = useState(10); | ||
const { bedTypeData, isError, loading, validate, mutate } = useBedTag(); | ||
const [currentPageSize, setPageSize] = useState(10); | ||
const pageSizes = [10, 20, 30, 40, 50]; | ||
|
||
const tableHeaders = [ | ||
{ | ||
header: t("ids", "Id"), | ||
key: "ids", | ||
}, | ||
{ | ||
header: t("name", "Name"), | ||
key: "name", | ||
}, | ||
{ | ||
header: t("actions", "Actions"), | ||
key: "actions", | ||
}, | ||
]; | ||
|
||
const tableRows = useMemo(() => { | ||
return bedTypeData?.map((entry) => ({ | ||
id: entry.uuid, | ||
ids: entry.id, | ||
name: entry?.name, | ||
actions: ( | ||
<> | ||
<Button | ||
enterDelayMs={300} | ||
renderIcon={Edit} | ||
onClick={(e) => { | ||
e.preventDefault(); | ||
setEditData(entry); | ||
setShowEditBedModal(true); | ||
setAddBedTagsModal(false); | ||
}} | ||
kind={"ghost"} | ||
iconDescription={t("editBedTag", "Edit Bed Tag")} | ||
hasIconOnly | ||
size={responsiveSize} | ||
tooltipAlignment="start" | ||
/> | ||
</> | ||
), | ||
})); | ||
}, [responsiveSize, bedTypeData, t]); | ||
|
||
if (isBedDataLoading || loading) { | ||
return ( | ||
<> | ||
<Header route="Bed Tag" /> | ||
<div className={styles.widgetCard}> | ||
<DataTableSkeleton role="progressbar" compact={isDesktop} zebra /> | ||
</div> | ||
</> | ||
); | ||
} | ||
|
||
if (isError) { | ||
return ( | ||
<> | ||
<Header route="Bed Tag" /> | ||
<div className={styles.widgetCard}> | ||
<ErrorState error={isError} headerTitle={headerTitle} /> | ||
</div> | ||
</> | ||
); | ||
} | ||
|
||
return ( | ||
<> | ||
<Header route="Bed Tag" /> | ||
|
||
<div className={styles.widgetCard}> | ||
{showBedTagsModal ? ( | ||
<BedTagForm | ||
onModalChange={setAddBedTagsModal} | ||
showModal={showBedTagsModal} | ||
mutate={mutate} | ||
/> | ||
) : null} | ||
{showEditBedModal ? ( | ||
<EditBedTagForm | ||
onModalChange={setShowEditBedModal} | ||
showModal={showEditBedModal} | ||
editData={editData} | ||
mutate={mutate} | ||
/> | ||
) : null} | ||
<CardHeader title={headerTitle}> | ||
<span className={styles.backgroundDataFetchingIndicator}> | ||
<span>{validate ? <InlineLoading /> : null}</span> | ||
</span> | ||
{bedTypeData?.length ? ( | ||
<Button | ||
kind="ghost" | ||
renderIcon={(props) => <Add size={16} {...props} />} | ||
onClick={() => setAddBedTagsModal(true)} | ||
> | ||
{t("addBedTag", "Add Bed Tag")} | ||
</Button> | ||
) : null} | ||
</CardHeader> | ||
<DataTable | ||
rows={tableRows} | ||
headers={tableHeaders} | ||
isSortable | ||
size={isTablet ? "lg" : "sm"} | ||
useZebraStyles | ||
> | ||
{({ rows, headers, getTableProps }) => ( | ||
<TableContainer> | ||
<Table {...getTableProps()}> | ||
<TableHead> | ||
<TableRow> | ||
{headers.map((header) => ( | ||
<TableHeader> | ||
{header.header?.content ?? header.header} | ||
</TableHeader> | ||
))} | ||
</TableRow> | ||
</TableHead> | ||
<TableBody> | ||
{rows.map((row) => ( | ||
<TableRow key={row.id}> | ||
{row.cells.map((cell) => ( | ||
<TableCell key={cell.id}> | ||
{cell.value?.content ?? cell.value} | ||
</TableCell> | ||
))} | ||
</TableRow> | ||
))} | ||
</TableBody> | ||
</Table> | ||
{rows.length === 0 ? ( | ||
<div className={styles.tileContainer}> | ||
<Tile className={styles.tile}> | ||
<div className={styles.tileContent}> | ||
<p className={styles.content}> | ||
{t("No data", "No data to display")} | ||
</p> | ||
<p className={styles.helper}> | ||
{t("checkFilters", "Check the filters above")} | ||
</p> | ||
</div> | ||
<p className={styles.separator}>{t("or", "or")}</p> | ||
<Button | ||
kind="ghost" | ||
size="sm" | ||
renderIcon={(props) => <Add size={16} {...props} />} | ||
onClick={() => setAddBedTagsModal(true)} | ||
> | ||
{t("bedTag", "Add Bed Tag")} | ||
</Button> | ||
</Tile> | ||
</div> | ||
) : null} | ||
<Pagination | ||
page={currentPage} | ||
pageSize={pageSize} | ||
pageSizes={[10, 20, 30, 40, 50]} | ||
totalItems={bedTypeData.length} | ||
onChange={({ page, pageSize }) => { | ||
setCurrentPage(page); | ||
setPageSize(pageSize); | ||
}} | ||
/> | ||
</TableContainer> | ||
)} | ||
</DataTable> | ||
</div> | ||
</> | ||
); | ||
}; | ||
export default BedTagAdministrationTable; |
131 changes: 131 additions & 0 deletions
131
src/bed-admission/bed-tag/bed-tags-admin-form.component.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
import React, { useState } from "react"; | ||
import { z } from "zod"; | ||
import { useForm, Controller } from "react-hook-form"; | ||
import { zodResolver } from "@hookform/resolvers/zod"; | ||
import { | ||
Button, | ||
ComposedModal, | ||
Form, | ||
FormGroup, | ||
ModalBody, | ||
ModalFooter, | ||
ModalHeader, | ||
Stack, | ||
TextInput, | ||
InlineNotification, | ||
} from "@carbon/react"; | ||
import { useTranslation } from "react-i18next"; | ||
import { Location } from "@openmrs/esm-framework"; | ||
import type { BedTagData } from "../../types"; | ||
|
||
const BedTagAdministrationSchema = z.object({ | ||
name: z.string().max(255), | ||
}); | ||
|
||
interface BedTagAdministrationFormProps { | ||
showModal: boolean; | ||
onModalChange: (showModal: boolean) => void; | ||
availableBedTypes: Array<BedTagData>; | ||
allLocations: Location[]; | ||
handleCreateQuestion?: (formData: BedTagData) => void; | ||
handleDeleteBedTag?: () => void; | ||
headerTitle: string; | ||
initialData: BedTagData; | ||
} | ||
|
||
interface ErrorType { | ||
message: string; | ||
} | ||
|
||
const BedTagsAdministrationForm: React.FC<BedTagAdministrationFormProps> = ({ | ||
showModal, | ||
onModalChange, | ||
handleCreateQuestion, | ||
headerTitle, | ||
initialData, | ||
}) => { | ||
const { t } = useTranslation(); | ||
|
||
const [showErrorNotification, setShowErrorNotification] = useState(false); | ||
const [formStateError, setFormStateError] = useState(""); | ||
|
||
const { | ||
handleSubmit, | ||
control, | ||
formState: { isDirty }, | ||
} = useForm<BedTagData>({ | ||
mode: "all", | ||
resolver: zodResolver(BedTagAdministrationSchema), | ||
defaultValues: { | ||
name: initialData.name || "", | ||
}, | ||
}); | ||
|
||
const onSubmit = (formData: BedTagData) => { | ||
const result = BedTagAdministrationSchema.safeParse(formData); | ||
if (result.success) { | ||
setShowErrorNotification(false); | ||
handleCreateQuestion(formData); | ||
} | ||
}; | ||
|
||
const onError = (error: { [key: string]: ErrorType }) => { | ||
setFormStateError(Object.entries(error)[0][1].message); | ||
setShowErrorNotification(true); | ||
}; | ||
|
||
return ( | ||
<ComposedModal | ||
open={showModal} | ||
onClose={() => onModalChange(false)} | ||
preventCloseOnClickOutside | ||
> | ||
<ModalHeader title={headerTitle} /> | ||
<Form onSubmit={handleSubmit(onSubmit, onError)}> | ||
<ModalBody hasScrollingContent> | ||
<Stack gap={3}> | ||
<FormGroup legendText={""}> | ||
<Controller | ||
name="name" | ||
control={control} | ||
render={({ field, fieldState }) => ( | ||
<> | ||
<TextInput | ||
id="bedTag" | ||
labelText={t("bedTag", "Bed Tag Name")} | ||
placeholder={t("bedTagPlaceholder", "")} | ||
invalidText={fieldState.error?.message} | ||
{...field} | ||
/> | ||
</> | ||
)} | ||
/> | ||
</FormGroup> | ||
|
||
{showErrorNotification && ( | ||
<InlineNotification | ||
lowContrast | ||
title={t("error", "Error")} | ||
style={{ minWidth: "100%", margin: "0rem", padding: "0rem" }} | ||
role="alert" | ||
kind="error" | ||
subtitle={t("pleaseFillField", formStateError) + "."} | ||
onClose={() => setShowErrorNotification(false)} | ||
/> | ||
)} | ||
</Stack> | ||
</ModalBody> | ||
<ModalFooter> | ||
<Button onClick={() => onModalChange(false)} kind="secondary"> | ||
{t("cancel", "Cancel")} | ||
</Button> | ||
<Button disabled={!isDirty} type="submit"> | ||
<span>{t("save", "Save")}</span> | ||
</Button> | ||
</ModalFooter> | ||
</Form> | ||
</ComposedModal> | ||
); | ||
}; | ||
|
||
export default BedTagsAdministrationForm; |
Oops, something went wrong.