Skip to content
Open
Show file tree
Hide file tree
Changes from 7 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
15 changes: 15 additions & 0 deletions src/billable-services/billable-service.resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
type ServiceConcept,
type CreateBillableServicePayload,
type UpdateBillableServicePayload,
type CashPointPayload,
} from '../types';
import type { BillingConfig } from '../config-schema';

Expand Down Expand Up @@ -99,3 +100,17 @@ export const updateBillableService = (uuid: string, payload: UpdateBillableServi
},
});
};

export const updateOrCreateCashPoint = (uuid: string | null, payload: CashPointPayload) => {
let url = `${apiBasePath}cashPoint`;
if (uuid) {
url = `${apiBasePath}cashPoint/${uuid}`;
}
return openmrsFetch(url, {
method: 'POST',
body: 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 { updateOrCreateCashPoint } from '../billable-service.resource';
import type { CashPoint, CashPointPayload } from '../../types';

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: CashPointPayload = {
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',
});

if (cashPointToEdit) {
await updateOrCreateCashPoint(cashPointToEdit.uuid, payload);
} else {
await updateOrCreateCashPoint(null, payload);
}
closeModal();
reset({ name: '', uuid: '', location: '' });
reset({
name: cashPointToEdit?.name || '',
uuid: cashPointToEdit?.uuid || '',
location: cashPointToEdit?.location.uuid || '',
});
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
10 changes: 9 additions & 1 deletion src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,22 @@ interface Location {
links: LocationLink[];
}

interface CashPoint {
export interface CashPoint {
uuid: string;
name: string;
description: string;
retired: boolean;
location: Location;
}

export interface CashPointPayload {
uuid: string;
name: string;
location: {
uuid: string;
};
}

interface ProviderLink {
rel: string;
uri: string;
Expand Down
1 change: 1 addition & 0 deletions translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
"discountAmount": "Discount amount",
"editBillableService": "Edit billable service",
"editBillLineItem": "Edit bill line item",
"editCashPoint": "Edit cash point",
"editThisBillItem": "Edit this bill item",
"enterAmount": "Enter amount",
"enterReferenceNumber": "Enter reference number",
Expand Down