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

Added Sort By widget to Oxygen Dashboard #185

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
55 changes: 12 additions & 43 deletions src/components/DistrictDashboard/OxygenMonitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import useSWR from "swr";

import { careSummary } from "../../utils/api";
import {
OXYGEN_TYPES,
OXYGEN_INVENTORY,
OXYGEN_INVENTORY_NAME,
OXYGEN_CAPACITY_TRANSLATION,
Expand All @@ -24,6 +25,8 @@ import Pagination from "../Pagination";
import { Button, Input } from "@windmill/react-ui";
import fuzzysort from "fuzzysort";

import SortByWidget from "../SortByWidget/SortByWidget";

dayjs.extend(relativeTime);
dayjs.extend(customParseFormat);

Expand Down Expand Up @@ -231,36 +234,12 @@ const oxygenSelector = (selector) => {
}
};

const selectorToText = (selector) => {
switch (selector) {
case "inventoryModifiedDate":
return "last updated date";
case "tte_tank":
return "liquid oxygen time to empty";
case "tte_b_cylinders":
return "B type cylinder time to empty";
case "tte_c_cylinders":
return "C type cylinder time to empty";
case "tte_d_cylinders":
return "D type cylinder time to empty";
default:
return null;
}
};

function OxygenMonitor({ filterDistrict, filterFacilityTypes, date }) {
const [orderBy, setOrderBy] = useState({
selector: "inventoryModifiedDate",
selector: oxygenSelector("Last Updated"),
order: 1,
});
const setOrderByHandler = (selector) => {
const orderBySelector = oxygenSelector(selector);
setOrderBy(
orderBySelector
? { selector: orderBySelector, order: -(orderBy?.order || 1) }
: undefined
);
};

const { data } = useSWR(
["OxygenMonitor", date, filterDistrict.id],
(url, date, district) =>
Expand Down Expand Up @@ -439,25 +418,15 @@ function OxygenMonitor({ filterDistrict, filterFacilityTypes, date }) {
)}
</div>

{/* Commented for now... to be added as a seperate component*/}
{/* {orderBy && (
<div className="flex items-center mt-4 space-x-2">
<div className="dark:text-white text-xs">
Showing Results Filtered by: {selectorToText(orderBy.selector)}{" "}
{orderBy.order === 1 ? "ASC" : "DESC"}
</div>
<div
onClick={(_) => setOrderBy()}
className="focus:shadow-outline-primary inline-flex items-center justify-center px-2 text-white text-xs leading-5 bg-primary-500 active:bg-primary-500 hover:bg-primary-600 border border-transparent rounded-lg focus:outline-none cursor-pointer transition-colors duration-150"
>
X Clear Filter
</div>
</div>
)} */}

<div id="facility-oxygen-cards" className="mb-16 mt-16">
<SectionTitle>Facilities</SectionTitle>
<div className="flex flex-col items-center justify-between md:flex-row">
<SectionTitle>Facilities</SectionTitle>
<SortByWidget
orderBy={orderBy}
setOrderBy={(state) => setOrderBy(state)}
selectors={["Last Updated", ...Object.values(OXYGEN_TYPES)]}
selectorMapping={oxygenSelector}
/>
<div className="flex max-w-full space-x-4">
{exported && (
<CSVLink data={exported.data} filename={exported.filename}>
Expand Down
72 changes: 72 additions & 0 deletions src/components/SortByWidget/SortByWidget.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { useState } from "react";
import { Button, Dropdown, DropdownItem } from "@windmill/react-ui";
import { ChevronDown } from "react-feather";

const SortByWidget = ({ orderBy, setOrderBy, selectors, selectorMapping }) => {
const [widgetOpen, setWidgetOpen] = useState(false);
const [widgetInfo, setWidgetInfo] = useState("Last Updated");

const handleDropdownSelect = (selector) => {
setOrderBy({ ...orderBy, selector: selectorMapping(selector) });
setWidgetInfo(selector);
setWidgetOpen(false);
};

return (
<div>
<div className="flex flex-row">
<div className="relative ml-1 h-10 dark:bg-gray-900 bg-white rounded-lg">
<Button
layout="link"
onClick={() => setWidgetOpen(true)}
className="dark:bg-gray-800"
iconRight={ChevronDown}
disabled={widgetOpen}
>
{`Sorted By: ${widgetInfo}`}
</Button>
<Dropdown
isOpen={widgetOpen}
onClose={() => setWidgetOpen(false)}
align="left"
className="absolute"
>
{selectors.map((selector, idx) => (
<DropdownItem
key={idx}
className=""
onClick={() => handleDropdownSelect(selector)}
>
{selector}
</DropdownItem>
))}
</Dropdown>
</div>
<div className="flex justify-evenly ml-10 w-1/4 dark:text-gray-700 dark:bg-gray-900 bg-white rounded-lg">
<Button
layout="link"
onClick={() => setOrderBy({ ...orderBy, order: 1 })}
className={`${
orderBy.order === 1 ? "border-r-2 border-gray-900" : ""
} bg-gray-800 rounded-r-none shadow-xs`}
disabled={orderBy.order === 1 ? true : false}
>
<span>ASC</span>
</Button>
<Button
layout="link"
onClick={() => setOrderBy({ ...orderBy, order: -1 })}
className={`${
orderBy.order === -1 ? "border-l-2 border-gray-900" : ""
} bg-gray-800 rounded-l-none shadow-xs`}
disabled={orderBy.order === 1 ? false : true}
>
<span>DESC</span>
</Button>
</div>
</div>
</div>
);
};

export default SortByWidget;