Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

O3-4250 (feat) Ability to add Cash Points and Payment modes via a UI #74

Merged
merged 5 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@ import {
} from '@carbon/react';
import { Add } from '@carbon/react/icons';
import { useTranslation } from 'react-i18next';
import axios from 'axios';
import { useForm, Controller } from 'react-hook-form';
import { z } from 'zod';
import { zodResolver } from '@hookform/resolvers/zod';
import { showSnackbar } from '@openmrs/esm-framework';
import { showSnackbar, openmrsFetch } from '@openmrs/esm-framework';
import { CardHeader } from '@openmrs/esm-patient-common-lib';
import styles from './cash-point-configuration.scss';

Expand All @@ -42,7 +41,6 @@ const CashPointConfiguration: React.FC = () => {
const [cashPoints, setCashPoints] = useState([]);
const [locations, setLocations] = useState([]);
const [isModalOpen, setIsModalOpen] = useState(false);
const baseUrl = `${window.location.origin}/openmrs/ws/rest/v1`;

const {
control,
Expand All @@ -60,7 +58,7 @@ const CashPointConfiguration: React.FC = () => {

const fetchCashPoints = useCallback(async () => {
try {
const response = await axios.get(`${baseUrl}/billing/cashPoint?v=full`);
const response = await openmrsFetch('/ws/rest/v1/billing/cashPoint?v=full');
setCashPoints(response.data.results || []);
} catch (err) {
showSnackbar({
Expand All @@ -70,11 +68,11 @@ const CashPointConfiguration: React.FC = () => {
isLowContrast: false,
});
}
}, [baseUrl, t]);
}, [t]);

const fetchLocations = useCallback(async () => {
try {
const response = await axios.get(`${baseUrl}/location?v=default`);
const response = await openmrsFetch('/ws/rest/v1/location?v=default');
const allLocations = response.data.results.map((loc) => ({
id: loc.uuid,
label: loc.display,
Expand All @@ -88,7 +86,7 @@ const CashPointConfiguration: React.FC = () => {
isLowContrast: false,
});
}
}, [baseUrl, t]);
}, [t]);

useEffect(() => {
fetchCashPoints();
Expand All @@ -114,21 +112,37 @@ const CashPointConfiguration: React.FC = () => {
}

try {
await axios.post(`${baseUrl}/billing/cashPoint`, {
name: data.name,
uuid: data.uuid,
location: { uuid: data.location },
const response = await openmrsFetch('/ws/rest/v1/billing/cashPoint', {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you could use restBaseUrl otherthan completing the entire url

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

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 (response.ok) {
showSnackbar({
title: t('success', 'Success'),
subtitle: t('cashPointSaved', 'Cash point was successfully saved.'),
kind: 'success',
});

setIsModalOpen(false);
reset({ name: '', uuid: '', location: '' });
fetchCashPoints();
setIsModalOpen(false);
reset({ name: '', uuid: '', location: '' });
fetchCashPoints();
} else {
const errorData = response.data || {};
showSnackbar({
title: t('error', 'Error'),
subtitle: errorData.message || t('errorSavingCashPoint', 'An error occurred while saving the cash point.'),
kind: 'error',
isLowContrast: false,
});
}
} catch (err) {
showSnackbar({
title: t('error', 'Error'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@ import {
} from '@carbon/react';
import { Add } from '@carbon/react/icons';
import { useTranslation } from 'react-i18next';
import axios from 'axios';
import { useForm, Controller } from 'react-hook-form';
import { z } from 'zod';
import { zodResolver } from '@hookform/resolvers/zod';
import { showSnackbar } from '@openmrs/esm-framework';
import { showSnackbar, openmrsFetch } from '@openmrs/esm-framework';
import { CardHeader } from '@openmrs/esm-patient-common-lib';
import styles from './payment-modes-config.scss';

Expand All @@ -38,7 +37,6 @@ const PaymentModesConfig: React.FC = () => {
const [isModalOpen, setIsModalOpen] = useState(false);
const [isDeleteModalOpen, setIsDeleteModalOpen] = useState(false);
const [selectedPaymentMode, setSelectedPaymentMode] = useState(null);
const baseUrl = `${window.location.origin}/openmrs/ws/rest/v1`;

const {
control,
Expand All @@ -55,7 +53,7 @@ const PaymentModesConfig: React.FC = () => {

const fetchPaymentModes = useCallback(async () => {
try {
const response = await axios.get(`${baseUrl}/billing/paymentMode?v=full`);
const response = await openmrsFetch('/ws/rest/v1/billing/paymentMode?v=full');
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here too

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

setPaymentModes(response.data.results || []);
} catch (err) {
showSnackbar({
Expand All @@ -65,7 +63,7 @@ const PaymentModesConfig: React.FC = () => {
isLowContrast: false,
});
}
}, [baseUrl, t]);
}, [t]);

useEffect(() => {
fetchPaymentModes();
Expand All @@ -89,20 +87,37 @@ const PaymentModesConfig: React.FC = () => {
}

try {
await axios.post(`${baseUrl}/billing/paymentMode`, {
name: data.name,
description: data.description,
const response = await openmrsFetch('/ws/rest/v1/billing/paymentMode', {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here too

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
name: data.name,
description: data.description,
}),
});

showSnackbar({
title: t('success', 'Success'),
subtitle: t('paymentModeSaved', 'Payment mode was successfully saved.'),
kind: 'success',
});
if (response.ok) {
showSnackbar({
title: t('success', 'Success'),
subtitle: t('paymentModeSaved', 'Payment mode was successfully saved.'),
kind: 'success',
});

setIsModalOpen(false);
reset({ name: '', description: '' });
fetchPaymentModes();
setIsModalOpen(false);
reset({ name: '', description: '' });
fetchPaymentModes();
} else {
const errorData = response.data || {};
showSnackbar({
title: t('error', 'Error'),
subtitle:
errorData.message || t('errorSavingPaymentMode', 'An error occurred while saving the payment mode.'),
kind: 'error',
isLowContrast: false,
});
}
} catch (err) {
showSnackbar({
title: t('error', 'Error'),
Expand All @@ -117,7 +132,9 @@ const PaymentModesConfig: React.FC = () => {
if (!selectedPaymentMode) return;

try {
await axios.delete(`${baseUrl}/billing/paymentMode/${selectedPaymentMode.uuid}`);
await openmrsFetch(`/ws/rest/v1/billing/paymentMode/${selectedPaymentMode.uuid}`, {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here too

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

method: 'DELETE',
});

showSnackbar({
title: t('success', 'Success'),
Expand Down
2 changes: 2 additions & 0 deletions translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@
"descriptionPlaceholder": "e.g., Used for all cash transactions",
"discard": "Discard",
"discount": "Discount",
"duplicateCashPointError": "A cash point with the same name or UUID already exists. Please use a unique name and UUID.",
"duplicatePaymentModeError": "A payment mode with the same name already exists. Please create another payment mode",
"editBillableService": "Edit billable service",
"editBillableServices": "Edit Billable Services",
"editBillLineItem": "Edit bill line item?",
Expand Down
Loading