Edit visibility for options depending on administrator status

This commit is contained in:
pmoharana-cmd 2025-01-07 15:30:34 -05:00
parent 55a03ff3fd
commit 9c970ca40e
3 changed files with 97 additions and 46 deletions

View File

@ -27,7 +27,7 @@ export function RowOpenAction<T extends DataPoint>({
updateRoute, updateRoute,
}: RowOpenActionProps<T>) { }: RowOpenActionProps<T>) {
return ( return (
<div className="font-semibold group flex flex-row items-center justify-between pr-2"> <div className="font-semibold group flex flex-row items-center justify-between px-2">
{title} {title}
<span> <span>
<Drawer <Drawer

View File

@ -5,41 +5,78 @@ import {
ArrowUpRightIcon, ArrowUpRightIcon,
EllipsisVerticalIcon, EllipsisVerticalIcon,
EyeSlashIcon, EyeSlashIcon,
EyeIcon,
} from "@heroicons/react/24/solid"; } from "@heroicons/react/24/solid";
import Button from "../Button"; import Button from "../Button";
import { useState, useEffect, useRef } from "react"; import { useState, useEffect, useRef } from "react";
import { RowOption } from "./RowOption"; import { RowOption } from "./RowOption";
export const RowOptionMenu = ({ onDelete, onHide }) => { interface RowOptionMenuProps {
const [menuOpen, setMenuOpen] = useState(false); onDelete?: () => void;
const openMenu = () => setMenuOpen(true); onHide: () => void;
const closeMenu = () => setMenuOpen(false); visible: boolean;
}
// TODO: Hide menu if clicked elsewhere export const RowOptionMenu = ({
onDelete,
onHide,
visible,
}: RowOptionMenuProps) => {
const [menuOpen, setMenuOpen] = useState(false);
const menuRef = useRef<HTMLDivElement>(null);
const buttonRef = useRef<HTMLButtonElement>(null);
useEffect(() => {
const handleClickOutside = (event: MouseEvent) => {
if (
menuRef.current &&
buttonRef.current &&
!menuRef.current.contains(event.target as Node) &&
!buttonRef.current.contains(event.target as Node)
) {
setMenuOpen(false);
}
};
document.addEventListener("mousedown", handleClickOutside);
return () => {
document.removeEventListener("mousedown", handleClickOutside);
};
}, []);
return ( return (
<> <>
<button <button
ref={buttonRef}
className="items-end" className="items-end"
onClick={() => setMenuOpen(!menuOpen)} onClick={() => setMenuOpen(!menuOpen)}
> >
<EllipsisVerticalIcon className="h-4" /> <EllipsisVerticalIcon className="h-4" />
</button> </button>
<div <div
ref={menuRef}
className={ className={
"justify-start border border-gray-200 shadow-lg flex flex-col absolute bg-white w-auto p-2 rounded [&>*]:rounded z-10" + "justify-start border border-gray-200 shadow-lg flex flex-col absolute bg-white w-auto p-2 rounded [&>*]:rounded z-10" +
(!menuOpen ? " invisible" : "") (!menuOpen ? " invisible" : "")
} }
> >
<RowOption icon={TrashIcon} label="Delete" onClick={onDelete} /> {onDelete && (
<RowOption <RowOption
icon={TrashIcon}
label="Delete"
onClick={onDelete}
/>
)}
{/* <RowOption
icon={ArrowUpRightIcon} icon={ArrowUpRightIcon}
label="Open" label="Open"
onClick={() => { onClick={() => {}}
/* handle open */ /> */}
}} <RowOption
icon={visible ? EyeSlashIcon : EyeIcon}
label={visible ? "Hide" : "Show"}
onClick={onHide}
/> />
<RowOption icon={EyeSlashIcon} label="Hide" onClick={onHide} />
</div> </div>
</> </>
); );

View File

@ -97,9 +97,9 @@ export default function Table<T extends DataPoint>({
return response; return response;
}; };
// /** Sorting function based on visibility */ /** Sorting function based on visibility */
// const visibilitySort = (a: T, b: T) => const visibilitySort = (a: T, b: T) =>
// a.visible === b.visible ? 0 : a.visible ? -1 : 1; a.visible === b.visible ? 0 : a.visible ? -1 : 1;
// // Sort data on load // // Sort data on load
// useEffect(() => { // useEffect(() => {
@ -115,36 +115,43 @@ export default function Table<T extends DataPoint>({
// ); // );
// }; // };
// const hideData = (dataId: number) => { const hideData = (dataId: number) => {
// console.log(`Toggling visibility for data with ID: ${dataId}`); console.log(`Toggling visibility for data with ID: ${dataId}`);
// setData((currentData) => { setData((currentData) => {
// const newData = currentData const newData = currentData
// .map((data) => .map((data) =>
// data.id === dataId data.id === dataId
// ? { ...data, visible: !data.visible } ? { ...data, visible: !data.visible }
// : data : data
// ) )
// .sort(visibilitySort); .sort((a, b) => {
// First sort by visibility
const visibilityResult = visibilitySort(a, b);
if (visibilityResult !== 0) return visibilityResult;
// Then sort by id
return a.id - b.id;
});
// console.log(newData); console.log(newData);
// return newData; return newData;
// }); });
// }; };
// Add data manipulation options to the first column // Add data manipulation options to the first column
columns.unshift( if (isAdmin) {
columnHelper.display({ columns.unshift(
id: "options", columnHelper.display({
cell: (props) => ( id: "options",
<RowOptionMenu cell: (props) => (
onDelete={() => {}} <RowOptionMenu
onHide={() => {}} onDelete={() => console.log("delete")}
// onDelete={() => deleteData(props.row.original.id)} onHide={() => hideData(props.row.original.id)}
// onHide={() => hideData(props.row.original.id)} visible={props.row.original.visible}
/> />
), ),
}) })
); );
}
// Searching // Searching
const [query, setQuery] = useState(""); const [query, setQuery] = useState("");
@ -186,7 +193,12 @@ export default function Table<T extends DataPoint>({
scope="col" scope="col"
className={ className={
"p-2 border-gray-200 border-y font-medium " + "p-2 border-gray-200 border-y font-medium " +
(1 < i && i < columns.length - 1 ((!isAdmin &&
0 < i &&
i < columns.length - 1) ||
(isAdmin &&
1 < i &&
i < columns.length - 1)
? "border-x" ? "border-x"
: "") : "")
} }
@ -215,9 +227,11 @@ export default function Table<T extends DataPoint>({
{row.getVisibleCells().map((cell, i) => ( {row.getVisibleCells().map((cell, i) => (
<td <td
key={cell.id} key={cell.id}
className={ className={`relative first:text-left first:px-0 last:border-none ${
"[&:nth-child(n+3)]:border-x relative first:text-left first:px-0 last:border-none" isAdmin
} ? "[&:nth-child(n+3)]"
: "[&:nth-child(n+2)]"
}:border-x`}
> >
{flexRender( {flexRender(
cell.column.columnDef.cell, cell.column.columnDef.cell,