improve genre picker cell
This commit is contained in:
parent
5018422353
commit
0586b33bcd
BIN
prisma/dev.db
BIN
prisma/dev.db
Binary file not shown.
|
@ -51,14 +51,9 @@ export const columns: ColumnDef<StoryWithGenres>[] = [
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
columnHelper.accessor("genres", {
|
columnHelper.accessor("genres", {
|
||||||
// cell: props => {
|
|
||||||
// const genres = props.getValue()
|
|
||||||
// return <GenreBadges genres={genres} />
|
|
||||||
// },
|
|
||||||
cell: GenrePickerInputCell,
|
cell: GenrePickerInputCell,
|
||||||
filterFn: "arrIncludes",
|
filterFn: "arrIncludes",
|
||||||
meta: {}
|
meta: {}
|
||||||
//TODO - write custom filter function, to account for an array of objects
|
|
||||||
}),
|
}),
|
||||||
|
|
||||||
]
|
]
|
||||||
|
|
|
@ -4,13 +4,14 @@ import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover
|
||||||
import { Button } from "@/components/ui/button"
|
import { Button } from "@/components/ui/button"
|
||||||
import { Checkbox } from "@/components/ui/checkbox"
|
import { Checkbox } from "@/components/ui/checkbox"
|
||||||
import { ComponentProps, useState } from "react"
|
import { ComponentProps, useState } from "react"
|
||||||
import { useForm, UseFormReturn } from "react-hook-form"
|
import { EventType, useForm, UseFormReturn } from "react-hook-form"
|
||||||
import { CellContext } from "@tanstack/react-table"
|
import { CellContext } from "@tanstack/react-table"
|
||||||
import { z } from "zod"
|
import { z } from "zod"
|
||||||
import { zodResolver } from "@hookform/resolvers/zod"
|
import { zodResolver } from "@hookform/resolvers/zod"
|
||||||
import { toast } from "@/components/ui/use-toast"
|
import { toast } from "@/components/ui/use-toast"
|
||||||
import GenreBadges from "app/ui/genreBadges"
|
import GenreBadges from "app/ui/genreBadges"
|
||||||
import { updateField, updateGenres } from "app/lib/update"
|
import { updateField, updateGenres } from "app/lib/update"
|
||||||
|
import { Genre } from "@prisma/client"
|
||||||
export default function GenrePickerInputCell(props: CellContext<any, any>) {
|
export default function GenrePickerInputCell(props: CellContext<any, any>) {
|
||||||
|
|
||||||
|
|
||||||
|
@ -23,23 +24,25 @@ export default function GenrePickerInputCell(props: CellContext<any, any>) {
|
||||||
const genres = props.table.options.meta.genres
|
const genres = props.table.options.meta.genres
|
||||||
const [isActive, setIsActive] = useState(false)
|
const [isActive, setIsActive] = useState(false)
|
||||||
|
|
||||||
async function onSubmit({ genres }: { genres: number[] }) {
|
async function onSubmit({ genres }: { genres: number[] }, event: Event) {
|
||||||
|
event.preventDefault()
|
||||||
const genresArray = genres.map((e) => { return { id: e } })
|
const genresArray = genres.map((e) => { return { id: e } })
|
||||||
toast({
|
console.log(`genres: ${genres}, genresArray: ${JSON.stringify(genresArray)}`)
|
||||||
title: "You submitted the following values:",
|
// toast({
|
||||||
description: (
|
// title: "You submitted the following values:",
|
||||||
<pre className="mt-2 w-[340px] rounded-md bg-slate-950 p-4">
|
// description: (
|
||||||
<code className="text-white">{JSON.stringify(genresArray, null, 2)}</code>
|
// <pre className="mt-2 w-[340px] rounded-md bg-slate-950 p-4">
|
||||||
</pre>
|
// <code className="text-white">{JSON.stringify(genres)}</code>
|
||||||
),
|
// </pre>
|
||||||
})
|
// ),
|
||||||
const res = await updateGenres({
|
// })
|
||||||
id,
|
// const res = await updateGenres({
|
||||||
table,
|
// id,
|
||||||
genres: genresArray,
|
// table,
|
||||||
pathname
|
// genres: genresArray,
|
||||||
})
|
// pathname
|
||||||
setIsActive(false)
|
// })
|
||||||
|
// setIsActive(false)
|
||||||
}
|
}
|
||||||
|
|
||||||
function onErrors(errors) {
|
function onErrors(errors) {
|
||||||
|
@ -54,7 +57,7 @@ export default function GenrePickerInputCell(props: CellContext<any, any>) {
|
||||||
console.log(JSON.stringify(errors))
|
console.log(JSON.stringify(errors))
|
||||||
}
|
}
|
||||||
const formSchema = z.object({
|
const formSchema = z.object({
|
||||||
genres: z.array(z.number())
|
genres: z.array(z.coerce.number())
|
||||||
})
|
})
|
||||||
const form = useForm<z.infer<typeof formSchema>>({
|
const form = useForm<z.infer<typeof formSchema>>({
|
||||||
resolver: zodResolver(formSchema),
|
resolver: zodResolver(formSchema),
|
||||||
|
@ -63,9 +66,9 @@ export default function GenrePickerInputCell(props: CellContext<any, any>) {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
return (
|
return (
|
||||||
|
|
||||||
<Form {...form}>
|
<Form {...form}>
|
||||||
<form onSubmit={form.handleSubmit(onSubmit, onErrors)} id="editGenresForm">
|
<form onSubmit={form.handleSubmit(onSubmit, onErrors)} id="editGenresForm">
|
||||||
|
|
||||||
<Popover modal={true}>
|
<Popover modal={true}>
|
||||||
<FormField
|
<FormField
|
||||||
control={form.control}
|
control={form.control}
|
||||||
|
@ -73,14 +76,14 @@ export default function GenrePickerInputCell(props: CellContext<any, any>) {
|
||||||
render={({ field }) => (
|
render={({ field }) => (
|
||||||
<FormItem className="flex flex-col">
|
<FormItem className="flex flex-col">
|
||||||
<PopoverTrigger>
|
<PopoverTrigger>
|
||||||
{value.length > 0 ? <GenreBadges genres={value} /> : <Button variant="ghost">Add genres</Button>
|
{value.length > 0 ? <GenreBadges genres={value} /> : <p>Add genres</p>
|
||||||
}
|
}
|
||||||
</PopoverTrigger>
|
</PopoverTrigger>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<PopoverContent align="start">
|
<PopoverContent align="start">
|
||||||
{genres.map((item) => (
|
{genres.map((item: Genre) => (
|
||||||
< FormField
|
< FormField
|
||||||
key={item.id}
|
key={item.id}
|
||||||
control={form.control}
|
control={form.control}
|
||||||
|
@ -97,7 +100,9 @@ export default function GenrePickerInputCell(props: CellContext<any, any>) {
|
||||||
onCheckedChange={(checked) => {
|
onCheckedChange={(checked) => {
|
||||||
console.log(field.value)
|
console.log(field.value)
|
||||||
return checked
|
return checked
|
||||||
? field.onChange([...field.value, item.id])
|
? field.onChange(
|
||||||
|
[...field.value, item.id]
|
||||||
|
)
|
||||||
: field.onChange(
|
: field.onChange(
|
||||||
field.value?.filter(
|
field.value?.filter(
|
||||||
(value) => value !== item.id
|
(value) => value !== item.id
|
||||||
|
@ -122,9 +127,9 @@ export default function GenrePickerInputCell(props: CellContext<any, any>) {
|
||||||
)}
|
)}
|
||||||
/>
|
/>
|
||||||
</Popover>
|
</Popover>
|
||||||
|
|
||||||
</form>
|
</form>
|
||||||
</Form>
|
</Form>
|
||||||
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue