forked from ohcnetwork/care_fe
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Grouped Medicine Administrations in Prescriptions Table (ohcnetwork#6176
) * Grouped Administrations in Prescriptions Table * fix dependency change issue * fix refresh logic * sort discontinued down
- Loading branch information
1 parent
cfd43d8
commit ce3938c
Showing
9 changed files
with
739 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { ReactNode } from "react"; | ||
import CareIcon from "../icons/CareIcon"; | ||
import RecordMeta from "./RecordMeta"; | ||
|
||
interface Props { | ||
title: ReactNode; | ||
lastModified?: string; | ||
className?: string; | ||
options?: ReactNode; | ||
} | ||
|
||
export default function SubHeading(props: Props) { | ||
return ( | ||
<div className="flex flex-wrap items-center justify-between py-2"> | ||
<div className="flex items-center"> | ||
<span className="text-lg font-semibold leading-relaxed text-gray-900"> | ||
{props.title} | ||
</span> | ||
{props.lastModified && ( | ||
<div className="ml-3 flex flex-row gap-2 text-xs font-medium text-gray-600"> | ||
<CareIcon className="care-l-history-alt text-sm" /> | ||
<RecordMeta time={props.lastModified} prefix="Last modified" /> | ||
</div> | ||
)} | ||
</div> | ||
{props.options && ( | ||
<div className="flex w-full flex-col gap-2 sm:w-auto sm:flex-row sm:justify-end"> | ||
{props.options} | ||
</div> | ||
)} | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
import { useEffect, useMemo, useState } from "react"; | ||
|
||
interface DateRange { | ||
start: Date; | ||
end: Date; | ||
} | ||
|
||
interface Props { | ||
bounds: DateRange; | ||
perPage: number; | ||
slots?: number; | ||
defaultEnd?: boolean; | ||
} | ||
|
||
const useRangePagination = ({ bounds, perPage, ...props }: Props) => { | ||
const [currentRange, setCurrentRange] = useState( | ||
getInitialBounds(bounds, perPage, props.defaultEnd) | ||
); | ||
|
||
useEffect(() => { | ||
setCurrentRange(getInitialBounds(bounds, perPage, props.defaultEnd)); | ||
}, [bounds, perPage, props.defaultEnd]); | ||
|
||
const next = () => { | ||
const { end } = currentRange; | ||
const deltaBounds = bounds.end.valueOf() - bounds.start.valueOf(); | ||
const deltaCurrent = end.valueOf() - bounds.start.valueOf(); | ||
|
||
if (deltaCurrent + perPage > deltaBounds) { | ||
setCurrentRange({ | ||
start: new Date(bounds.end.valueOf() - perPage), | ||
end: bounds.end, | ||
}); | ||
} else { | ||
setCurrentRange({ | ||
start: new Date(end.valueOf()), | ||
end: new Date(end.valueOf() + perPage), | ||
}); | ||
} | ||
}; | ||
|
||
const previous = () => { | ||
const { start } = currentRange; | ||
const deltaCurrent = start.valueOf() - bounds.start.valueOf(); | ||
|
||
if (deltaCurrent - perPage < 0) { | ||
setCurrentRange({ | ||
start: bounds.start, | ||
end: new Date(bounds.start.valueOf() + perPage), | ||
}); | ||
} else { | ||
setCurrentRange({ | ||
start: new Date(start.valueOf() - perPage), | ||
end: new Date(start.valueOf()), | ||
}); | ||
} | ||
}; | ||
|
||
const slots = useMemo(() => { | ||
if (!props.slots) { | ||
return []; | ||
} | ||
|
||
const slots: DateRange[] = []; | ||
const { start } = currentRange; | ||
const delta = perPage / props.slots; | ||
|
||
for (let i = 0; i < props.slots; i++) { | ||
slots.push({ | ||
start: new Date(start.valueOf() + delta * i), | ||
end: new Date(start.valueOf() + delta * (i + 1)), | ||
}); | ||
} | ||
|
||
return slots; | ||
}, [currentRange, props.slots, perPage]); | ||
|
||
return { | ||
currentRange, | ||
hasNext: currentRange.end < bounds.end, | ||
hasPrevious: currentRange.start > bounds.start, | ||
previous, | ||
next, | ||
slots, | ||
}; | ||
}; | ||
|
||
export default useRangePagination; | ||
|
||
const getInitialBounds = ( | ||
bounds: DateRange, | ||
perPage: number, | ||
defaultEnd?: boolean | ||
) => { | ||
const deltaBounds = bounds.end.valueOf() - bounds.start.valueOf(); | ||
|
||
if (deltaBounds < perPage) { | ||
return bounds; | ||
} | ||
|
||
if (defaultEnd) { | ||
return { | ||
start: new Date(bounds.end.valueOf() - perPage), | ||
end: bounds.end, | ||
}; | ||
} | ||
|
||
return { | ||
start: bounds.start, | ||
end: new Date(bounds.start.valueOf() + perPage), | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.