Add checkboxes for documents table
Helps #167 Signed-off-by: Sacha Al Himdani <sacha@getprobo.com>
This commit is contained in:
committed by
Sacha Al Himdani
parent
25b330f948
commit
a4606f95f5
@@ -15,6 +15,10 @@ import {
|
|||||||
ActionDropdown,
|
ActionDropdown,
|
||||||
DropdownItem,
|
DropdownItem,
|
||||||
IconBell2,
|
IconBell2,
|
||||||
|
Checkbox,
|
||||||
|
IconCrossLargeX,
|
||||||
|
IconSignature,
|
||||||
|
IconCheckmark1,
|
||||||
} from "@probo/ui";
|
} from "@probo/ui";
|
||||||
import {
|
import {
|
||||||
useFragment,
|
useFragment,
|
||||||
@@ -30,7 +34,7 @@ import {
|
|||||||
useSendSigningNotificationsMutation,
|
useSendSigningNotificationsMutation,
|
||||||
} from "/hooks/graph/DocumentGraph";
|
} from "/hooks/graph/DocumentGraph";
|
||||||
import type { DocumentsPageListFragment$key } from "./__generated__/DocumentsPageListFragment.graphql";
|
import type { DocumentsPageListFragment$key } from "./__generated__/DocumentsPageListFragment.graphql";
|
||||||
import { usePageTitle } from "@probo/hooks";
|
import { useList, usePageTitle } from "@probo/hooks";
|
||||||
import { sprintf, getDocumentTypeLabel } from "@probo/helpers";
|
import { sprintf, getDocumentTypeLabel } from "@probo/helpers";
|
||||||
import { CreateDocumentDialog } from "./dialogs/CreateDocumentDialog";
|
import { CreateDocumentDialog } from "./dialogs/CreateDocumentDialog";
|
||||||
import type { DocumentsPageRowFragment$key } from "./__generated__/DocumentsPageRowFragment.graphql";
|
import type { DocumentsPageRowFragment$key } from "./__generated__/DocumentsPageRowFragment.graphql";
|
||||||
@@ -76,16 +80,19 @@ export default function DocumentsPage(props: Props) {
|
|||||||
|
|
||||||
const organization = usePreloadedQuery(
|
const organization = usePreloadedQuery(
|
||||||
documentsQuery,
|
documentsQuery,
|
||||||
props.queryRef
|
props.queryRef,
|
||||||
).organization;
|
).organization;
|
||||||
const pagination = usePaginationFragment(
|
const pagination = usePaginationFragment(
|
||||||
documentsFragment,
|
documentsFragment,
|
||||||
organization as DocumentsPageListFragment$key
|
organization as DocumentsPageListFragment$key,
|
||||||
);
|
);
|
||||||
|
|
||||||
const documents = pagination.data.documents.edges.map((edge) => edge.node);
|
const documents = pagination.data.documents.edges.map((edge) => edge.node);
|
||||||
const connectionId = pagination.data.documents.__id;
|
const connectionId = pagination.data.documents.__id;
|
||||||
const [sendSigningNotifications] = useSendSigningNotificationsMutation();
|
const [sendSigningNotifications] = useSendSigningNotificationsMutation();
|
||||||
|
const { list: selection, toggle, clear, reset } = useList<string>([]);
|
||||||
|
|
||||||
|
// TODO : Add mutation to handle publishing multiple documents and request multiple signatures
|
||||||
|
|
||||||
usePageTitle(__("Documents"));
|
usePageTitle(__("Documents"));
|
||||||
|
|
||||||
@@ -119,19 +126,52 @@ export default function DocumentsPage(props: Props) {
|
|||||||
</PageHeader>
|
</PageHeader>
|
||||||
<SortableTable {...pagination}>
|
<SortableTable {...pagination}>
|
||||||
<Thead>
|
<Thead>
|
||||||
<Tr>
|
{selection.length === 0 ? (
|
||||||
<SortableTh field="TITLE">{__("Name")}</SortableTh>
|
<Tr>
|
||||||
<Th>{__("Status")}</Th>
|
<Th>
|
||||||
<SortableTh field="DOCUMENT_TYPE">{__("Type")}</SortableTh>
|
<Checkbox
|
||||||
<Th>{__("Owner")}</Th>
|
checked={selection.length === documents.length}
|
||||||
<Th>{__("Last update")}</Th>
|
onChange={() => reset(documents.map((d) => d.id))}
|
||||||
<Th>{__("Signatures")}</Th>
|
/>
|
||||||
<Th></Th>
|
</Th>
|
||||||
</Tr>
|
<SortableTh field="TITLE">{__("Name")}</SortableTh>
|
||||||
|
<Th>{__("Status")}</Th>
|
||||||
|
<SortableTh field="DOCUMENT_TYPE">{__("Type")}</SortableTh>
|
||||||
|
<Th>{__("Owner")}</Th>
|
||||||
|
<Th>{__("Last update")}</Th>
|
||||||
|
<Th>{__("Signatures")}</Th>
|
||||||
|
<Th></Th>
|
||||||
|
</Tr>
|
||||||
|
) : (
|
||||||
|
<Tr>
|
||||||
|
<Th colspan={8}>
|
||||||
|
<div className="flex justify-between items-center">
|
||||||
|
<div className="flex gap-2 items-center">
|
||||||
|
{sprintf(__("%s documents selected"), selection.length)} -
|
||||||
|
<button
|
||||||
|
onClick={clear}
|
||||||
|
className="flex gap-1 items-center hover:text-txt-primary"
|
||||||
|
>
|
||||||
|
<IconCrossLargeX size={12} />
|
||||||
|
{__("Clear selection")}
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
<div className="flex gap-2">
|
||||||
|
<Button icon={IconCheckmark1}>{__("Publish")}</Button>
|
||||||
|
<Button variant="secondary" icon={IconSignature}>
|
||||||
|
{__("Request signature")}
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</Th>
|
||||||
|
</Tr>
|
||||||
|
)}
|
||||||
</Thead>
|
</Thead>
|
||||||
<Tbody>
|
<Tbody>
|
||||||
{documents.map((document) => (
|
{documents.map((document) => (
|
||||||
<DocumentRow
|
<DocumentRow
|
||||||
|
checked={selection.includes(document.id)}
|
||||||
|
onCheck={() => toggle(document.id)}
|
||||||
key={document.id}
|
key={document.id}
|
||||||
document={document}
|
document={document}
|
||||||
organizationId={organization.id}
|
organizationId={organization.id}
|
||||||
@@ -178,21 +218,25 @@ function DocumentRow({
|
|||||||
document: documentKey,
|
document: documentKey,
|
||||||
organizationId,
|
organizationId,
|
||||||
connectionId,
|
connectionId,
|
||||||
|
checked,
|
||||||
|
onCheck,
|
||||||
}: {
|
}: {
|
||||||
document: DocumentsPageRowFragment$key;
|
document: DocumentsPageRowFragment$key;
|
||||||
organizationId: string;
|
organizationId: string;
|
||||||
connectionId: string;
|
connectionId: string;
|
||||||
|
checked: boolean;
|
||||||
|
onCheck: () => void;
|
||||||
}) {
|
}) {
|
||||||
const document = useFragment<DocumentsPageRowFragment$key>(
|
const document = useFragment<DocumentsPageRowFragment$key>(
|
||||||
rowFragment,
|
rowFragment,
|
||||||
documentKey
|
documentKey,
|
||||||
);
|
);
|
||||||
const lastVersion = document.versions.edges[0].node;
|
const lastVersion = document.versions.edges[0].node;
|
||||||
const isDraft = lastVersion.status === "DRAFT";
|
const isDraft = lastVersion.status === "DRAFT";
|
||||||
const { __, dateFormat } = useTranslate();
|
const { __, dateFormat } = useTranslate();
|
||||||
const signatures = lastVersion.signatures.edges.map((edge) => edge.node);
|
const signatures = lastVersion.signatures.edges.map((edge) => edge.node);
|
||||||
const signedCount = signatures.filter(
|
const signedCount = signatures.filter(
|
||||||
(signature) => signature.state === "SIGNED"
|
(signature) => signature.state === "SIGNED",
|
||||||
).length;
|
).length;
|
||||||
const [deleteDocument] = useDeleteDocumentMutation();
|
const [deleteDocument] = useDeleteDocumentMutation();
|
||||||
const confirm = useConfirm();
|
const confirm = useConfirm();
|
||||||
@@ -209,16 +253,19 @@ function DocumentRow({
|
|||||||
{
|
{
|
||||||
message: sprintf(
|
message: sprintf(
|
||||||
__(
|
__(
|
||||||
'This will permanently delete the document "%s". This action cannot be undone.'
|
'This will permanently delete the document "%s". This action cannot be undone.',
|
||||||
),
|
),
|
||||||
document.title
|
document.title,
|
||||||
),
|
),
|
||||||
}
|
},
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Tr to={`/organizations/${organizationId}/documents/${document.id}`}>
|
<Tr to={`/organizations/${organizationId}/documents/${document.id}`}>
|
||||||
|
<Td noLink>
|
||||||
|
<Checkbox checked={checked} onChange={onCheck} />
|
||||||
|
</Td>
|
||||||
<Td>
|
<Td>
|
||||||
<div className="flex gap-4 items-center">
|
<div className="flex gap-4 items-center">
|
||||||
<img
|
<img
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
export { usePageTitle } from "./usePageTitle";
|
export { usePageTitle } from "./usePageTitle";
|
||||||
export { useToggle } from "./useToggle";
|
export { useToggle } from "./useToggle";
|
||||||
export { useRefSync } from "./useRefSync";
|
export { useRefSync } from "./useRefSync";
|
||||||
|
export { useList } from "./useList";
|
||||||
|
|||||||
25
packages/hooks/src/useList.ts
Normal file
25
packages/hooks/src/useList.ts
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
import { useCallback, useState } from "react";
|
||||||
|
|
||||||
|
export function useList<T>(items: T[]) {
|
||||||
|
const [list, setList] = useState(items);
|
||||||
|
const push = useCallback(
|
||||||
|
(item: T) => setList((prev) => [...prev, item]),
|
||||||
|
[],
|
||||||
|
);
|
||||||
|
const remove = useCallback(
|
||||||
|
(item: T) => setList((prev) => prev.filter((i) => i !== item)),
|
||||||
|
[],
|
||||||
|
);
|
||||||
|
const toggle = useCallback(
|
||||||
|
(item: T) =>
|
||||||
|
setList((prev) =>
|
||||||
|
prev.includes(item)
|
||||||
|
? prev.filter((i) => i !== item)
|
||||||
|
: [...prev, item],
|
||||||
|
),
|
||||||
|
[],
|
||||||
|
);
|
||||||
|
const reset = useCallback((items: T[]) => setList(items), []);
|
||||||
|
const clear = useCallback(() => setList([]), []);
|
||||||
|
return { list, push, remove, toggle, reset, clear };
|
||||||
|
}
|
||||||
12
packages/ui/src/Atoms/Checkbox/Checkbox.stories.tsx
Normal file
12
packages/ui/src/Atoms/Checkbox/Checkbox.stories.tsx
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
import { Checkbox } from "./Checkbox";
|
||||||
|
import type { Meta, StoryObj } from "@storybook/react";
|
||||||
|
|
||||||
|
export default {
|
||||||
|
title: "Atoms/Checkbox",
|
||||||
|
component: Checkbox,
|
||||||
|
argTypes: {},
|
||||||
|
} satisfies Meta<typeof Checkbox>;
|
||||||
|
|
||||||
|
type Story = StoryObj<typeof Checkbox>;
|
||||||
|
|
||||||
|
export const Default: Story = {};
|
||||||
37
packages/ui/src/Atoms/Checkbox/Checkbox.tsx
Normal file
37
packages/ui/src/Atoms/Checkbox/Checkbox.tsx
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
import { useState } from "react";
|
||||||
|
import { tv } from "tailwind-variants";
|
||||||
|
import { IconCheckmark1 } from "../Icons";
|
||||||
|
|
||||||
|
type Props = {
|
||||||
|
checked: boolean;
|
||||||
|
onChange: (checked: boolean) => void;
|
||||||
|
};
|
||||||
|
|
||||||
|
const checkbox = tv({
|
||||||
|
base: "size-4 border border-border-mid relative rounded-sm flex items-center justify-center",
|
||||||
|
variants: {
|
||||||
|
isFocused: {
|
||||||
|
true: "shadow shadow-focus",
|
||||||
|
},
|
||||||
|
checked: {
|
||||||
|
true: "bg-accent text-invert",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
export function Checkbox({ checked, onChange }: Props) {
|
||||||
|
const [isFocused, setFocus] = useState(false);
|
||||||
|
return (
|
||||||
|
<div className={checkbox({ isFocused, checked })}>
|
||||||
|
<input
|
||||||
|
className="absolute inset-0 opacity-0"
|
||||||
|
type="checkbox"
|
||||||
|
checked={checked}
|
||||||
|
onChange={(e) => onChange(e.target.checked)}
|
||||||
|
onFocus={() => setFocus(true)}
|
||||||
|
onBlur={() => setFocus(false)}
|
||||||
|
/>
|
||||||
|
{checked && <IconCheckmark1 size={12} />}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -5,6 +5,7 @@ import {
|
|||||||
type HTMLAttributes,
|
type HTMLAttributes,
|
||||||
type PropsWithChildren,
|
type PropsWithChildren,
|
||||||
type ReactNode,
|
type ReactNode,
|
||||||
|
type ThHTMLAttributes,
|
||||||
} from "react";
|
} from "react";
|
||||||
import { Card } from "../Card/Card";
|
import { Card } from "../Card/Card";
|
||||||
import { Link } from "react-router";
|
import { Link } from "react-router";
|
||||||
@@ -34,9 +35,15 @@ export function Th({
|
|||||||
children,
|
children,
|
||||||
className,
|
className,
|
||||||
width,
|
width,
|
||||||
}: PropsWithChildren<{ className?: string; width?: number }>) {
|
...props
|
||||||
|
}: {
|
||||||
|
className?: string;
|
||||||
|
width?: number;
|
||||||
|
colspan?: number;
|
||||||
|
} & ThHTMLAttributes<HTMLTableCellElement>) {
|
||||||
return (
|
return (
|
||||||
<th
|
<th
|
||||||
|
{...props}
|
||||||
className={clsx(
|
className={clsx(
|
||||||
"first:pl-6 last:pr-6 py-3 whitespace-nowrap",
|
"first:pl-6 last:pr-6 py-3 whitespace-nowrap",
|
||||||
className,
|
className,
|
||||||
|
|||||||
@@ -37,6 +37,7 @@ export { ControlItem } from "./Atoms/ControlItem/ControlItem";
|
|||||||
export { InfiniteScrollTrigger } from "./Atoms/InfiniteScrollTrigger/InfiniteScrollTrigger";
|
export { InfiniteScrollTrigger } from "./Atoms/InfiniteScrollTrigger/InfiniteScrollTrigger";
|
||||||
export { PriorityLevel } from "./Atoms/PriorityLevel/PriorityLevel.tsx";
|
export { PriorityLevel } from "./Atoms/PriorityLevel/PriorityLevel.tsx";
|
||||||
export { TaskStateIcon } from "./Atoms/Icons/TaskStateIcon";
|
export { TaskStateIcon } from "./Atoms/Icons/TaskStateIcon";
|
||||||
|
export { Checkbox } from "./Atoms/Checkbox/Checkbox";
|
||||||
|
|
||||||
// Molecules
|
// Molecules
|
||||||
export {
|
export {
|
||||||
|
|||||||
Reference in New Issue
Block a user