-
Notifications
You must be signed in to change notification settings - Fork 6
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
Related pubs in form #910
Draft
allisonking
wants to merge
21
commits into
main
Choose a base branch
from
aking/791/related-pubs-in-form
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Related pubs in form #910
Changes from 14 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
8de637b
Add RelatedPubsElement
allisonking ef1047f
Add side panel
allisonking c5dc1a1
Add pub selection
allisonking 44f9d2e
wip saving values
allisonking 4e32399
Remove pubtype check from related pubs form element
allisonking c998757
maybe normalize pub update?
allisonking f76e2ab
Add related pubs when creating a pub
allisonking 3402e49
Start adding value component
allisonking 5d7466c
Use useFieldArray to make array manipulation easier
allisonking 6e32a06
Refactor so related pubs can use the same form element
allisonking 4a48630
Make updatePub work for related values
allisonking 3178f14
Fix tests
allisonking 46379df
A few more fixes
allisonking 1995b69
Handle null related fields
allisonking 7586f01
Fix demo component for text input
allisonking 33c2bfd
Add related pub e2e test
allisonking 0b7067d
Fix button type to stop inadvertent submitting
allisonking 7dcea5d
Merge branch 'main' into aking/791/related-pubs-in-form
allisonking ef7321f
Try increasing timeout
allisonking bcefc7b
Add wait for
allisonking 226b602
Merge branch 'main' into aking/791/related-pubs-in-form
allisonking File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,7 @@ import type { FormBuilderSchema, FormElementData, PanelEvent, PanelState } from | |
import type { Form as PubForm } from "~/lib/server/form"; | ||
import { renderWithPubTokens } from "~/lib/server/render/pub/renderWithPubTokens"; | ||
import { didSucceed, useServerAction } from "~/lib/serverActions"; | ||
import { PanelHeader, PanelWrapper, SidePanel } from "../SidePanel"; | ||
import { saveForm } from "./actions"; | ||
import { ElementPanel } from "./ElementPanel"; | ||
import { FormBuilderProvider, useFormBuilder } from "./FormBuilderContext"; | ||
|
@@ -95,49 +96,12 @@ const elementPanelTitles: Record<PanelState["state"], string> = { | |
editingButton: "Edit Submission Button", | ||
}; | ||
|
||
const PanelHeader = ({ state }: { state: PanelState["state"] }) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I moved all the side panel stuff to another component so I could reuse it in the form fill page. I think this would be better off using a drawer or maybe even the new side bar component from shadcn, but for now I reused what we had to keep things consistent |
||
const { dispatch } = useFormBuilder(); | ||
return ( | ||
<> | ||
<div className="flex items-center justify-between"> | ||
<div className="text-sm uppercase text-gray-500">{elementPanelTitles[state]}</div> | ||
{state !== "initial" && ( | ||
<Button | ||
aria-label="Cancel" | ||
variant="ghost" | ||
size="sm" | ||
className="" | ||
onClick={() => dispatch({ eventName: "cancel" })} | ||
> | ||
<X size={16} className="text-muted-foreground" /> | ||
</Button> | ||
)} | ||
</div> | ||
<hr /> | ||
</> | ||
); | ||
}; | ||
|
||
type Props = { | ||
pubForm: PubForm; | ||
id: string; | ||
stages: Stages[]; | ||
}; | ||
|
||
// Render children in a portal so they can safely use <form> components | ||
function PanelWrapper({ | ||
children, | ||
sidebar, | ||
}: { | ||
children: React.ReactNode; | ||
sidebar: Element | null; | ||
}) { | ||
if (!sidebar) { | ||
return null; | ||
} | ||
return createPortal(children, sidebar); | ||
} | ||
|
||
export function FormBuilder({ pubForm, id, stages }: Props) { | ||
const router = useRouter(); | ||
const pathname = usePathname(); | ||
|
@@ -336,7 +300,17 @@ export function FormBuilder({ pubForm, id, stages }: Props) { | |
</div> | ||
<PanelWrapper sidebar={sidebarRef.current}> | ||
<FormItem className="relative flex h-screen flex-col"> | ||
<PanelHeader state={panelState.state} /> | ||
<PanelHeader | ||
title={ | ||
elementPanelTitles[panelState.state] | ||
} | ||
showCancel={ | ||
!(panelState.state === "initial") | ||
} | ||
onCancel={() => | ||
dispatch({ eventName: "cancel" }) | ||
} | ||
/> | ||
<FormControl> | ||
<ElementPanel panelState={panelState} /> | ||
</FormControl> | ||
|
@@ -351,10 +325,7 @@ export function FormBuilder({ pubForm, id, stages }: Props) { | |
<TabsContent value="preview">Preview your form here</TabsContent> | ||
</div> | ||
</Tabs> | ||
<div | ||
ref={sidebarRef} | ||
className="fixed right-0 top-[72px] z-10 flex h-[calc(100%-72px)] w-[380px] flex-col gap-10 overflow-auto border-l border-gray-200 bg-gray-50 p-4 pr-6 shadow" | ||
></div> | ||
<SidePanel ref={sidebarRef} /> | ||
</FormBuilderProvider> | ||
</TokenProvider> | ||
); | ||
|
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,67 @@ | ||
import { forwardRef } from "react"; | ||
import { createPortal } from "react-dom"; | ||
|
||
import { Button } from "ui/button"; | ||
import { X } from "ui/icon"; | ||
import { cn } from "utils"; | ||
|
||
// Render children in a portal so they can safely use <form> components | ||
export const PanelWrapper = ({ | ||
children, | ||
sidebar, | ||
}: { | ||
children: React.ReactNode; | ||
sidebar: Element | null; | ||
}) => { | ||
if (!sidebar) { | ||
return null; | ||
} | ||
return createPortal(children, sidebar); | ||
}; | ||
|
||
export const PanelHeader = ({ | ||
title, | ||
showCancel, | ||
onCancel, | ||
}: { | ||
title: string; | ||
showCancel: boolean; | ||
onCancel: () => void; | ||
}) => { | ||
return ( | ||
<> | ||
<div className="flex items-center justify-between"> | ||
<div className="text-sm uppercase text-gray-500">{title}</div> | ||
{showCancel && ( | ||
<Button | ||
aria-label="Cancel" | ||
variant="ghost" | ||
size="sm" | ||
className="" | ||
onClick={onCancel} | ||
> | ||
<X size={16} className="text-muted-foreground" /> | ||
</Button> | ||
)} | ||
</div> | ||
<hr /> | ||
</> | ||
); | ||
}; | ||
|
||
export const SidePanel = forwardRef<HTMLDivElement, React.HTMLAttributes<HTMLDivElement>>( | ||
({ children, className, ...rest }, ref) => { | ||
return ( | ||
<div | ||
{...rest} | ||
ref={ref} | ||
className={cn( | ||
"fixed right-0 top-[72px] z-10 flex h-[calc(100%-72px)] w-[380px] flex-col gap-10 overflow-auto border-l border-gray-200 bg-gray-50 p-4 pr-6 shadow", | ||
className | ||
)} | ||
> | ||
{children} | ||
</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,100 @@ | ||
"use client"; | ||
|
||
import type { ColumnDef } from "@tanstack/react-table"; | ||
|
||
import { useRef, useState } from "react"; | ||
|
||
import type { PubsId } from "db/public"; | ||
import { pubFieldsIdSchema, pubsIdSchema } from "db/public"; | ||
import { Button } from "ui/button"; | ||
import { Checkbox } from "ui/checkbox"; | ||
import { DataTableColumnHeader } from "ui/data-table"; | ||
|
||
import type { GetPubsResult } from "~/lib/server"; | ||
import { PanelHeader, SidePanel } from "~/app/components/SidePanel"; | ||
import { DataTable } from "../DataTable/v2/DataTable"; | ||
|
||
const getColumns = () => | ||
[ | ||
{ | ||
id: "select", | ||
header: ({ table }) => ( | ||
<Checkbox | ||
checked={ | ||
table.getIsAllPageRowsSelected() || | ||
(table.getIsSomePageRowsSelected() && "indeterminate") | ||
} | ||
onCheckedChange={(value) => table.toggleAllPageRowsSelected(!!value)} | ||
aria-label="Select all" | ||
/> | ||
), | ||
cell: ({ row }) => ( | ||
<Checkbox | ||
checked={row.getIsSelected()} | ||
onCheckedChange={(value) => row.toggleSelected(!!value)} | ||
aria-label="Select row" | ||
className="mr-2 h-4 w-4" | ||
/> | ||
), | ||
enableSorting: false, | ||
enableHiding: false, | ||
}, | ||
{ | ||
header: ({ column }) => <DataTableColumnHeader column={column} title="Name" />, | ||
accessorKey: "name", | ||
cell: ({ row }) => { | ||
return ( | ||
<div className="flex items-center gap-2"> | ||
<span>{row.original.title}</span> | ||
</div> | ||
); | ||
}, | ||
}, | ||
] as const satisfies ColumnDef<GetPubsResult[number], unknown>[]; | ||
|
||
export const AddRelatedPubsPanel = ({ | ||
title, | ||
onCancel, | ||
onAdd, | ||
pubs, | ||
}: { | ||
title: string; | ||
onCancel: () => void; | ||
onAdd: (pubs: GetPubsResult) => void; | ||
pubs: GetPubsResult; | ||
}) => { | ||
const sidebarRef = useRef(null); | ||
const [selected, setSelected] = useState<Record<string, boolean>>({}); | ||
|
||
const handleAdd = () => { | ||
const selectedPubIds = Object.entries(selected) | ||
.filter(([pubId, selected]) => selected) | ||
.map((selection) => selection[0] as PubsId); | ||
const selectedPubs = pubs.filter((p) => selectedPubIds.includes(p.id)); | ||
onAdd(selectedPubs); | ||
onCancel(); | ||
}; | ||
|
||
return ( | ||
<SidePanel ref={sidebarRef} className="justify-between"> | ||
<div className="flex flex-col gap-2"> | ||
<PanelHeader title={title} showCancel onCancel={onCancel} /> | ||
<DataTable | ||
columns={getColumns()} | ||
data={pubs} | ||
selectedRows={selected} | ||
setSelectedRows={setSelected} | ||
getRowId={(d) => d.id} | ||
/> | ||
</div> | ||
<div className="flex w-full justify-between gap-2"> | ||
<Button variant="outline" className="flex-1" onClick={onCancel}> | ||
Cancel | ||
</Button> | ||
<Button onClick={handleAdd} className="flex-1 bg-blue-500 hover:bg-blue-600"> | ||
Add | ||
</Button> | ||
</div> | ||
</SidePanel> | ||
); | ||
}; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Email was showing up as a number component in the demo component part