Skip to content
Merged
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
306 changes: 172 additions & 134 deletions apps/web/src/app/[lang]/lists/_components/list-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ import {
DialogTitle,
DialogTrigger,
} from '@plotwist/ui/components/ui/dialog'
import {
Drawer,
DrawerContent,
DrawerHeader,
DrawerTitle,
DrawerTrigger,
} from '@plotwist/ui/components/ui/drawer'
import {
Form,
FormControl,
Expand All @@ -34,9 +41,15 @@ import { getGetListsQueryKey, usePostList, usePutListId } from '@/api/list'
import { useLanguage } from '@/context/language'
import { type ListFormValues, listFormSchema } from './list-form-schema'

type ListFormProps = { trigger: React.ReactNode; list?: GetListById200List }
type ListFormMode = 'dialog' | 'drawer'

type ListFormProps = {
trigger: React.ReactNode
list?: GetListById200List
mode?: ListFormMode
}

export const ListForm = ({ trigger, list }: ListFormProps) => {
export const ListForm = ({ trigger, list, mode = 'dialog' }: ListFormProps) => {
const [open, setOpen] = useState(false)
const { dictionary, language } = useLanguage()
const { push, refresh } = useRouter()
Expand Down Expand Up @@ -94,142 +107,167 @@ export const ListForm = ({ trigger, list }: ListFormProps) => {
)
}

const Content = (
<>
<Form {...form}>
<form
onSubmit={form.handleSubmit(onSubmit)}
className="w-full space-y-4"
>
<FormField
control={form.control}
name="title"
render={({ field }) => (
<FormItem>
<FormLabel>{dictionary.title}</FormLabel>

<FormControl>
<Input
placeholder={dictionary.list_form.name_placeholder}
{...field}
/>
</FormControl>

<FormMessage />
</FormItem>
)}
/>

<FormField
control={form.control}
name="description"
render={({ field }) => (
<FormItem>
<FormLabel>{dictionary.list_form.description}</FormLabel>
<FormControl>
<Textarea
className="resize-none"
rows={3}
placeholder={dictionary.list_form.description_placeholder}
{...field}
/>
</FormControl>

<FormMessage />
</FormItem>
)}
/>

<FormField
control={form.control}
name="visibility"
render={({ field: { onChange, value } }) => (
<FormItem className="space-y-2">
<FormLabel>{dictionary.list_form.visibility}</FormLabel>
<FormControl>
<RadioGroup
onValueChange={onChange}
defaultValue={value}
className="flex flex-col gap-4"
>
<div className="flex flex-col gap-1">
<FormItem className="flex items-center space-x-3 space-y-0">
<FormControl>
<RadioGroupItem value="PUBLIC" />
</FormControl>
<FormLabel className="font-normal">
{dictionary.list_form.visibility_option_public}
</FormLabel>
</FormItem>
<span className="pl-7 text-sm text-muted-foreground">
{
dictionary.list_form
.visibility_option_description_public
}
</span>
</div>

<div className="flex flex-col gap-1">
<FormItem className="flex items-center space-x-3 space-y-0">
<FormControl>
<RadioGroupItem value="NETWORK" disabled />
</FormControl>
<FormLabel className="font-normal text-muted-foreground">
{dictionary.list_form.visibility_option_network}
</FormLabel>
</FormItem>
<span className="pl-7 text-sm text-muted-foreground">
{
dictionary.list_form
.visibility_option_description_network
}
</span>
</div>

<div className="flex flex-col gap-1">
<FormItem className="flex items-center space-x-3 space-y-0">
<FormControl>
<RadioGroupItem value="PRIVATE" />
</FormControl>
<FormLabel className="font-normal">
{dictionary.list_form.visibility_option_private}
</FormLabel>
</FormItem>
<span className="pl-7 text-sm text-muted-foreground">
{
dictionary.list_form
.visibility_option_description_private
}
</span>
</div>
</RadioGroup>
</FormControl>
<FormMessage />

<FormMessage />
</FormItem>
)}
/>

<div className="flex justify-end space-x-2">
<Button type="submit" loading={form.formState.isSubmitting}>
{dictionary.list_form.submit}
</Button>
</div>
</form>
</Form>
</>
)

if (mode === 'dialog') {
return (
<Dialog open={open} onOpenChange={setOpen}>
<DialogTrigger asChild>{trigger}</DialogTrigger>

<DialogContent>
<DialogHeader>
<DialogTitle>
{list
? dictionary.list_form.edit_list
: dictionary.list_form.create_new_list}
</DialogTitle>
</DialogHeader>
{Content}
</DialogContent>
</Dialog>
)
}

return (
<Dialog open={open} onOpenChange={setOpen}>
<DialogTrigger asChild>{trigger}</DialogTrigger>
<Drawer open={open} onOpenChange={setOpen}>
<DrawerTrigger asChild>{trigger}</DrawerTrigger>

<DialogContent>
<DialogHeader>
<DialogTitle>
<DrawerContent className="w-full p-6">
<DrawerHeader>
<DrawerTitle>
{list
? dictionary.list_form.edit_list
: dictionary.list_form.create_new_list}
</DialogTitle>
</DialogHeader>

<Form {...form}>
<form
onSubmit={form.handleSubmit(onSubmit)}
className="w-full space-y-4"
>
<FormField
control={form.control}
name="title"
render={({ field }) => (
<FormItem>
<FormLabel>{dictionary.title}</FormLabel>

<FormControl>
<Input
placeholder={dictionary.list_form.name_placeholder}
{...field}
/>
</FormControl>

<FormMessage />
</FormItem>
)}
/>

<FormField
control={form.control}
name="description"
render={({ field }) => (
<FormItem>
<FormLabel>{dictionary.list_form.description}</FormLabel>
<FormControl>
<Textarea
className="resize-none"
rows={3}
placeholder={dictionary.list_form.description_placeholder}
{...field}
/>
</FormControl>

<FormMessage />
</FormItem>
)}
/>

<FormField
control={form.control}
name="visibility"
render={({ field: { onChange, value } }) => (
<FormItem className="space-y-2">
<FormLabel>{dictionary.list_form.visibility}</FormLabel>
<FormControl>
<RadioGroup
onValueChange={onChange}
defaultValue={value}
className="flex flex-col gap-4"
>
<div className="flex flex-col gap-1">
<FormItem className="flex items-center space-x-3 space-y-0">
<FormControl>
<RadioGroupItem value="PUBLIC" />
</FormControl>
<FormLabel className="font-normal">
{dictionary.list_form.visibility_option_public}
</FormLabel>
</FormItem>
<span className="pl-7 text-sm text-muted-foreground">
{
dictionary.list_form
.visibility_option_description_public
}
</span>
</div>

<div className="flex flex-col gap-1">
<FormItem className="flex items-center space-x-3 space-y-0">
<FormControl>
<RadioGroupItem value="NETWORK" disabled />
</FormControl>
<FormLabel className="font-normal text-muted-foreground">
{dictionary.list_form.visibility_option_network}
</FormLabel>
</FormItem>
<span className="pl-7 text-sm text-muted-foreground">
{
dictionary.list_form
.visibility_option_description_network
}
</span>
</div>

<div className="flex flex-col gap-1">
<FormItem className="flex items-center space-x-3 space-y-0">
<FormControl>
<RadioGroupItem value="PRIVATE" />
</FormControl>
<FormLabel className="font-normal">
{dictionary.list_form.visibility_option_private}
</FormLabel>
</FormItem>
<span className="pl-7 text-sm text-muted-foreground">
{
dictionary.list_form
.visibility_option_description_private
}
</span>
</div>
</RadioGroup>
</FormControl>
<FormMessage />

<FormMessage />
</FormItem>
)}
/>

<div className="flex justify-end space-x-2">
<Button type="submit" loading={form.formState.isSubmitting}>
{dictionary.list_form.submit}
</Button>
</div>
</form>
</Form>
</DialogContent>
</Dialog>
</DrawerTitle>
</DrawerHeader>

{Content}
</DrawerContent>
</Drawer>
)
}
2 changes: 2 additions & 0 deletions apps/web/src/components/lists/lists-dropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ export const ListsDropdown = ({ item, ...props }: ListsDropdownProps) => {
<DrawerTitle className="flex items-center justify-between">
{myLists}
<ListForm
mode="drawer"
trigger={
<Button
size="icon"
Expand Down Expand Up @@ -288,6 +289,7 @@ export const ListsDropdown = ({ item, ...props }: ListsDropdownProps) => {
{myLists}

<ListForm
mode="dialog"
trigger={
<Button
size="icon"
Expand Down