Skip to content

Commit

Permalink
Testing PR
Browse files Browse the repository at this point in the history
  • Loading branch information
Daphne210 committed Nov 28, 2023
1 parent afbfd57 commit 20269c7
Showing 1 changed file with 166 additions and 0 deletions.
166 changes: 166 additions & 0 deletions src/facility-metrics/user-dashboard/user-dashboard.resource.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
import useSWR from "swr";
import { openmrsFetch } from "@openmrs/esm-framework";
import { TabPanel } from "@carbon/react";
import React from "react";
import { pivotRender } from "./user-dashboard.component";

Check failure on line 5 in src/facility-metrics/user-dashboard/user-dashboard.resource.tsx

View workflow job for this annotation

GitHub Actions / build

Cannot find module './user-dashboard.component' or its corresponding type declarations.
import GridLayout from "react-grid-layout";

Check failure on line 6 in src/facility-metrics/user-dashboard/user-dashboard.resource.tsx

View workflow job for this annotation

GitHub Actions / build

Cannot find module 'react-grid-layout' or its corresponding type declarations.
type saveDashboardRequest = {
name: string;
description: string;
items: Array<string>;
};

const layout = [
{ i: "a", x: 0, y: 0, w: 1, h: 50, isBounded: true },
{ i: "b", x: 1, y: 0, w: 1, h: 50, isBounded: true },
{ i: "c", x: 4, y: 0, w: 1, h: 50, isBounded: true },
];

export function useGetSaveReports() {
const apiUrl = `/ws/rest/v1/dashboardReport/`;
const { data, error, isLoading, isValidating, mutate } = useSWR<
{ data: { results: any } },
Error
>(apiUrl, openmrsFetch);
return {
savedReports: data ? mapDataElements(data?.data["results"]) : [],
isError: error,
isLoading: isLoading,
mutate,
};
}

export function useSaveDashboard(params: saveDashboardRequest) {
const apiUrl = params.name ? `/ws/rest/v1/dashboard` : null;
const abortController = new AbortController();

const fetcher = () =>
openmrsFetch(apiUrl, {
method: "POST",
signal: abortController.signal,
headers: {
"Content-Type": "application/json",
},
body: {
name: params.name,
description: params?.description,
items: params?.items,
},
});

const { data, error, isLoading, isValidating } = useSWR<
{
data: {
results: any;
};
},
Error
>(apiUrl, fetcher);

return {
hasSavedDashboard: !!data,
isErrorInSavingDashboard: error,
isLoadingSaveDashboard: isLoading,
isValidatingSaveDashboard: isValidating,
};
}

export async function saveDashboard(params: saveDashboardRequest) {
const abortController = new AbortController();

return openmrsFetch(`/ws/rest/v1/dashboard`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
signal: abortController.signal,
body: {
name: params.name,
description: params?.description,
items: params?.items,
},
});
}

export function useGetSavedDashboards() {
const apiUrl = `/ws/rest/v1/dashboard`;
const { data, error, isLoading, isValidating, mutate } = useSWR<
{ data: { results: any } },
Error
>(apiUrl, openmrsFetch);

const keysArray = ["a", "c", "b"];
const dashboardItems = [];
if (data) {
const savedDashboards = mapDataElements(
data?.data["results"],
"dashboards"
);
savedDashboards?.map((dashboardItem: savedDashboard) => {

Check failure on line 99 in src/facility-metrics/user-dashboard/user-dashboard.resource.tsx

View workflow job for this annotation

GitHub Actions / build

Cannot find name 'savedDashboard'.
dashboardItems.push({
label: dashboardItem.name,
description: dashboardItem.description,
panel: (
<TabPanel>
<GridLayout
className="layout"
layout={layout}
cols={1}
rowHeight={5}
width={1200}
onDragStart={() => {
// This is an empty function
}}
>
{dashboardItem?.items?.map((item, index) =>
pivotRender(item, index, keysArray)
)}
</GridLayout>
</TabPanel>
),
dashboardItems: dashboardItem?.items,
});
});
}

return {
dashboardItems,
isErrorInSavedDashboard: error,
isLoadingSavedDashboard: isLoading,
mutate,
};
}

export function mapDataElements(
dataArray: Array<Record<string, string>>,
category?: string
) {
const arrayToReturn: Array<savedReport | savedDashboard> = [];

Check failure on line 138 in src/facility-metrics/user-dashboard/user-dashboard.resource.tsx

View workflow job for this annotation

GitHub Actions / build

Cannot find name 'savedReport'.

Check failure on line 138 in src/facility-metrics/user-dashboard/user-dashboard.resource.tsx

View workflow job for this annotation

GitHub Actions / build

Cannot find name 'savedDashboard'.
if (dataArray) {
if (category === "dashboards") {
dataArray.map((savedDashboard: Record<string, string>, index) => {
arrayToReturn.push({
uuid: savedDashboard.uuid,
name: savedDashboard.name,
description: savedDashboard.description,
items: savedDashboard.items,
});
});
} else {
dataArray.map((savedReport: Record<string, string>, index) => {
arrayToReturn.push({
id: savedReport.uuid,
label: savedReport.name,
description: savedReport?.description,
type: savedReport?.type,
columns: savedReport?.columns,
rows: savedReport?.rows,
aggregator: savedReport?.aggregator,
report_request_object: savedReport?.report_request_object,
});
});
}
}

return arrayToReturn;
}

0 comments on commit 20269c7

Please sign in to comment.