Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/billable-services/billable-service.resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,25 @@ export const updateBillableService = (uuid: string, payload: UpdateBillableServi
},
});
};

export const createCashPoint = (payload: any) => {
const url = `${apiBasePath}cashPoint`;
return openmrsFetch(url, {
method: 'POST',
body: payload,
headers: {
'Content-Type': 'application/json',
},
});
};

export const updateCashPoint = (uuid: string, payload: any) => {
const url = `${apiBasePath}/cashPoint/${uuid}`;
return openmrsFetch(url, {
method: 'POST',
body: JSON.stringify(payload),
headers: {
'Content-Type': 'application/json',
},
});
};
39 changes: 22 additions & 17 deletions src/billable-services/cash-point/add-cash-point.modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { z } from 'zod';
import { zodResolver } from '@hookform/resolvers/zod';
import { Button, Dropdown, Form, ModalBody, ModalFooter, ModalHeader, TextInput } from '@carbon/react';
import { showSnackbar, openmrsFetch, restBaseUrl, getCoreTranslation } from '@openmrs/esm-framework';
import { type CashPoint } from '../../types';
import { createCashPoint, updateCashPoint } from '../billable-service.resource';

type CashPointFormValues = {
name: string;
Expand All @@ -13,11 +15,12 @@ type CashPointFormValues = {
};

interface AddCashPointModalProps {
cashPointToEdit?: CashPoint;
closeModal: () => void;
onCashPointAdded: () => void;
}

const AddCashPointModal: React.FC<AddCashPointModalProps> = ({ closeModal, onCashPointAdded }) => {
const AddCashPointModal: React.FC<AddCashPointModalProps> = ({ cashPointToEdit, closeModal, onCashPointAdded }) => {
const { t } = useTranslation();
const [locations, setLocations] = useState([]);

Expand All @@ -41,9 +44,9 @@ const AddCashPointModal: React.FC<AddCashPointModalProps> = ({ closeModal, onCas
} = useForm<CashPointFormValues>({
resolver: zodResolver(cashPointSchema),
defaultValues: {
name: '',
uuid: '',
location: '',
name: cashPointToEdit?.name || '',
uuid: cashPointToEdit?.uuid || '',
location: cashPointToEdit?.location?.uuid || '',
},
});

Expand All @@ -70,27 +73,29 @@ const AddCashPointModal: React.FC<AddCashPointModalProps> = ({ closeModal, onCas
}, [fetchLocations]);

const onSubmit = async (data: CashPointFormValues) => {
const payload = {
name: data.name,
uuid: data.uuid,
location: { uuid: data.location },
};
try {
await openmrsFetch(`${restBaseUrl}/billing/cashPoint`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: {
name: data.name,
uuid: data.uuid,
location: { uuid: data.location },
},
});

showSnackbar({
title: t('success', 'Success'),
subtitle: t('cashPointSaved', 'Cash point was successfully saved.'),
kind: 'success',
});

closeModal();
reset({ name: '', uuid: '', location: '' });
reset({
name: cashPointToEdit?.name || '',
uuid: cashPointToEdit?.uuid || '',
location: cashPointToEdit?.location.uuid || '',
});
if (cashPointToEdit) {
await updateCashPoint(cashPointToEdit.uuid, payload);
} else {
await createCashPoint(payload);
}
onCashPointAdded();
} catch (err) {
showSnackbar({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import React, { useState, useEffect, useCallback } from 'react';
import {
Button,
DataTable,
OverflowMenu,
OverflowMenuItem,
Table,
TableBody,
TableCell,
Expand All @@ -12,13 +14,23 @@ import {
} from '@carbon/react';
import { Add } from '@carbon/react/icons';
import { useTranslation } from 'react-i18next';
import { showSnackbar, openmrsFetch, restBaseUrl, showModal, getCoreTranslation } from '@openmrs/esm-framework';
import {
showSnackbar,
openmrsFetch,
restBaseUrl,
showModal,
getCoreTranslation,
isDesktop,
useLayoutType,
} from '@openmrs/esm-framework';
import { CardHeader } from '@openmrs/esm-patient-common-lib';
import styles from './cash-point-configuration.scss';
import { type CashPoint } from '../../types/index';

const CashPointConfiguration: React.FC = () => {
const { t } = useTranslation();
const [cashPoints, setCashPoints] = useState([]);
const layout = useLayoutType();

const fetchCashPoints = useCallback(async () => {
try {
Expand All @@ -45,7 +57,15 @@ const CashPointConfiguration: React.FC = () => {
});
};

const rowData = cashPoints.map((point) => ({
const handleEditCashPoint = (point: CashPoint) => {
const dispose = showModal('add-cash-point-modal', {
cashPointToEdit: point,
onCashPointAdded: fetchCashPoints,
closeModal: () => dispose(),
});
};

const rowData = cashPoints.map((point: CashPoint) => ({
id: point.uuid,
name: point.name,
uuid: point.uuid,
Expand All @@ -67,7 +87,7 @@ const CashPointConfiguration: React.FC = () => {
</Button>
</CardHeader>
<div>
<DataTable rows={rowData} headers={headerData} isSortable size="lg">
<DataTable rows={rowData} headers={headerData} isSortable size="lg" overflowMenuOnHover={isDesktop(layout)}>
{({ rows, headers, getTableProps, getHeaderProps, getRowProps }) => (
<TableContainer>
<Table className={styles.table} {...getTableProps()}>
Expand All @@ -78,6 +98,7 @@ const CashPointConfiguration: React.FC = () => {
{header.header}
</TableHeader>
))}
<TableHeader aria-label={getCoreTranslation('actions')} />
</TableRow>
</TableHead>
<TableBody>
Expand All @@ -86,6 +107,15 @@ const CashPointConfiguration: React.FC = () => {
{row.cells.map((cell) => (
<TableCell key={cell.id}>{cell.value}</TableCell>
))}
<TableCell className="cds--table-column-menu">
<OverflowMenu size="lg" flipped>
<OverflowMenuItem
className={styles.menuItem}
itemText={t('editCashPoint', 'Edit cash point')}
onClick={() => handleEditCashPoint(cashPoints.find((point) => point.uuid === row.id))}
/>
</OverflowMenu>
</TableCell>
</TableRow>
))}
</TableBody>
Expand Down
2 changes: 1 addition & 1 deletion src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ interface Location {
links: LocationLink[];
}

interface CashPoint {
export interface CashPoint {
uuid: string;
name: string;
description: string;
Expand Down
1 change: 1 addition & 0 deletions translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
"editBillableService": "Edit billable service",
"editBillableServices": "Edit Billable Services",
"editBillLineItem": "Edit bill line item",
"editCashPoint": "Edit cash point",
"editThisBillItem": "Edit this bill item",
"enterAmount": "Enter amount",
"enterConcept": "Associated concept",
Expand Down