2024-07-01 15:23:48 +00:00
|
|
|
"use client"
|
2024-06-30 21:22:46 +00:00
|
|
|
import { Input } from "@/components/ui/input";
|
|
|
|
import { CellContext } from "@tanstack/react-table";
|
|
|
|
import { updateField } from "app/lib/update";
|
|
|
|
import { useState } from "react";
|
2024-07-01 15:23:48 +00:00
|
|
|
import { z } from "zod";
|
|
|
|
import { useForm } from "react-hook-form";
|
|
|
|
import { zodResolver } from "@hookform/resolvers/zod";
|
|
|
|
import { toast } from "@/components/ui/use-toast";
|
2024-07-02 14:59:55 +00:00
|
|
|
import { Form, FormControl, FormField, FormItem, FormMessage } from "@/components/ui/form";
|
2024-06-30 21:22:46 +00:00
|
|
|
|
2024-09-24 10:25:31 +00:00
|
|
|
export default function NumberInputCell({ cellContext, className }: { cellContext: CellContext<any, any>, className: string }) {
|
2024-06-30 21:22:46 +00:00
|
|
|
const [isActive, setIsActive] = useState(false)
|
|
|
|
|
2024-09-24 10:25:31 +00:00
|
|
|
const table = cellContext.table.options.meta.tableName
|
|
|
|
const id = cellContext.row.original.id
|
|
|
|
const column = cellContext.column.id
|
|
|
|
const pathname = cellContext.table.options.meta.pathname
|
|
|
|
const value = cellContext.cell.getValue()
|
|
|
|
const formSchema = cellContext.column.columnDef.meta.formSchema.pick({ [column]: true })
|
2024-07-01 15:23:48 +00:00
|
|
|
|
|
|
|
const form = useForm<z.infer<typeof formSchema>>({
|
|
|
|
resolver: zodResolver(formSchema),
|
|
|
|
defaultValues: {
|
2024-09-24 10:25:31 +00:00
|
|
|
[column]: cellContext.cell.getValue()
|
2024-07-02 14:59:55 +00:00
|
|
|
},
|
2024-07-02 20:52:10 +00:00
|
|
|
|
2024-07-01 15:23:48 +00:00
|
|
|
})
|
|
|
|
|
2024-09-20 13:19:31 +00:00
|
|
|
async function onSubmit(value: z.infer<typeof formSchema>) {
|
|
|
|
try {
|
|
|
|
const res = await updateField({
|
|
|
|
id,
|
|
|
|
table,
|
|
|
|
datum: value[column],
|
|
|
|
column,
|
|
|
|
pathname
|
|
|
|
})
|
|
|
|
if (res === undefined) throw new Error("something went wrong")
|
|
|
|
toast({ title: "Field updated successfully." })
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error)
|
|
|
|
toast({ title: "Something went wrong." })
|
|
|
|
}
|
2024-07-02 20:16:17 +00:00
|
|
|
setIsActive(false)
|
2024-06-30 21:22:46 +00:00
|
|
|
}
|
|
|
|
|
2024-09-20 13:19:31 +00:00
|
|
|
function onErrors(errors: Error) {
|
2024-07-01 15:23:48 +00:00
|
|
|
toast({
|
|
|
|
title: "You have errors",
|
2024-09-20 13:19:31 +00:00
|
|
|
description: errors.message,
|
2024-07-01 15:23:48 +00:00
|
|
|
})
|
|
|
|
console.log(JSON.stringify(errors))
|
|
|
|
}
|
2024-06-30 21:22:46 +00:00
|
|
|
return (
|
2024-07-02 14:59:55 +00:00
|
|
|
<div
|
2024-09-20 13:19:31 +00:00
|
|
|
onDoubleClick={() => setIsActive(true)}
|
2024-09-24 10:25:31 +00:00
|
|
|
className={className + " w-full h-fit flex items-center justify-center"}
|
2024-07-02 14:59:55 +00:00
|
|
|
tabIndex={0}
|
|
|
|
onKeyDown={e => {
|
2024-07-02 21:01:26 +00:00
|
|
|
if (e.code === "Enter" && !isActive) {
|
2024-07-02 20:52:10 +00:00
|
|
|
e.preventDefault()
|
2024-07-02 14:59:55 +00:00
|
|
|
setIsActive(true)
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
>
|
2024-06-30 21:22:46 +00:00
|
|
|
{isActive ?
|
2024-07-01 15:23:48 +00:00
|
|
|
<Form {...form}>
|
2024-07-02 15:18:23 +00:00
|
|
|
<form onSubmit={form.handleSubmit(onSubmit, onErrors)}
|
|
|
|
>
|
2024-07-01 15:23:48 +00:00
|
|
|
<FormField
|
|
|
|
control={form.control}
|
|
|
|
name={column}
|
|
|
|
render={({ field }) => (
|
2024-07-02 14:59:55 +00:00
|
|
|
<FormItem
|
|
|
|
onBlur={() => setIsActive(false)}
|
|
|
|
>
|
|
|
|
<FormControl
|
|
|
|
>
|
2024-07-01 15:23:48 +00:00
|
|
|
<Input
|
2024-09-21 15:23:31 +00:00
|
|
|
className="md:w-24"
|
2024-07-02 20:16:17 +00:00
|
|
|
type="number"
|
2024-07-01 15:23:48 +00:00
|
|
|
autoFocus={true}
|
2024-09-24 10:25:31 +00:00
|
|
|
step={cellContext.column.columnDef.meta?.step}
|
2024-07-01 15:23:48 +00:00
|
|
|
{...field}
|
|
|
|
/>
|
|
|
|
</FormControl>
|
2024-07-02 14:59:55 +00:00
|
|
|
<FormMessage />
|
2024-07-01 15:23:48 +00:00
|
|
|
</FormItem>
|
|
|
|
)}
|
|
|
|
/>
|
2024-07-02 20:52:10 +00:00
|
|
|
|
2024-07-01 15:23:48 +00:00
|
|
|
</form>
|
|
|
|
</Form>
|
2024-09-24 10:25:31 +00:00
|
|
|
: <p>{cellContext.cell.getValue()}</p>
|
2024-06-30 21:22:46 +00:00
|
|
|
}
|
2024-07-02 14:59:55 +00:00
|
|
|
</div >
|
2024-06-30 21:22:46 +00:00
|
|
|
)
|
|
|
|
}
|