import React, { useCallback, useMemo } from 'react';
import { useTranslation } from 'react-i18next';
import {
Button,
DataTable,
DataTableSkeleton,
InlineLoading,
Pagination,
Table,
TableBody,
TableCell,
TableContainer,
TableExpandedRow,
TableExpandHeader,
TableExpandRow,
TableHead,
TableHeader,
TableRow,
TableToolbar,
TableToolbarAction,
TableToolbarContent,
TableToolbarMenu,
TableToolbarSearch,
Tile,
} from '@carbon/react';
import {
CheckmarkOutline,
Copy,
Download,
IncompleteCancel,
MisuseOutline,
View,
WarningAltFilled,
} from '@carbon/react/icons';
import { isDesktop, restBaseUrl, useSession } from '@openmrs/esm-framework';
import { useGetReports } from '../stock-reports.resource';
import {
URL_BATCH_JOB_ARTIFACT,
APP_STOCKMANAGEMENT_REPORTS_VIEW,
TASK_STOCKMANAGEMENT_REPORTS_MUTATE,
} from '../../constants';
import { formatDisplayDateTime } from '../../core/utils/datetimeUtils';
import {
BatchJobStatusCancelled,
BatchJobStatusCompleted,
BatchJobStatusExpired,
BatchJobStatusFailed,
BatchJobStatusPending,
} from '../../core/api/types/BatchJob';
import { handleMutate } from '../../utils';
import { PrivilegedView } from '../../core/components/privileged-view-component/privileged-view.component';
import NewReportActionButton from './new-report-button.component';
import StockReportStatus from './stock-report-status.component';
import StockReportParameters from './stock-report-parameters.component';
import styles from './stock-reports.scss';
const StockReports: React.FC = () => {
const { t } = useTranslation();
const handleRefresh = () => {
handleMutate(`${restBaseUrl}/stockmanagement/batchjob?batchJobType=Report&v=default`);
};
const { reports, isLoading, currentPage, pageSizes, totalItems, goTo, currentPageSize, setPageSize } =
useGetReports();
const { user } = useSession();
const canViewReports =
user.privileges.filter((privilage) => privilage.display === APP_STOCKMANAGEMENT_REPORTS_VIEW).length > 0;
const canCreateReport =
user.privileges.filter((privilage) => privilage.display === TASK_STOCKMANAGEMENT_REPORTS_MUTATE).length > 0;
const tableHeaders = useMemo(
() => [
{
id: 0,
header: t('report', 'Report'),
key: 'report',
},
{
id: 1,
header: t('parameters', 'Parameters'),
key: 'parameters',
},
{
id: 2,
header: t('dateRequested', 'Date Requested'),
key: 'dateRequested',
},
{
id: 3,
header: t('requestedBy', 'Requested By'),
key: 'requestedBy',
},
{
id: 4,
header: t('status', 'Status'),
key: 'status',
},
{
id: 5,
header: t('timeTaken', 'Time Taken'),
key: 'timeTaken',
},
{
id: 8,
header: t('actions', 'Actions'),
key: 'actions',
},
],
[t],
);
const onDownloadReportClick = useCallback((uuid: string, fileExit: string | undefined | null) => {
if (uuid) {
window.open(URL_BATCH_JOB_ARTIFACT(uuid, true), '_blank');
}
}, []);
const tableRows = useMemo(() => {
return reports?.map((batchJob, index) => ({
...batchJob,
checkbox: 'isBatchJobActive',
id: batchJob?.uuid,
key: `key-${batchJob?.uuid}`,
uuid: `${batchJob?.uuid}`,
batchJobType: batchJob?.batchJobType,
dateRequested: formatDisplayDateTime(batchJob.dateCreated),
parameters:
{t('noReportsToDisplay', 'No Stock reports to display')}
{t('checkFilters', 'Check the filters above')}