Update category colors
This commit is contained in:
parent
2fa8b6261f
commit
a932321324
@ -34,6 +34,7 @@ import { useForm } from "react-hook-form";
|
|||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
|
|
||||||
import { zUpdateCategoryRequestSchema, ZUpdateCategoryRequest } from "@lifetracker/shared/types/categories";
|
import { zUpdateCategoryRequestSchema, ZUpdateCategoryRequest } from "@lifetracker/shared/types/categories";
|
||||||
|
import { Textarea } from "@/components/ui/textarea";
|
||||||
|
|
||||||
export default function EditCategoryDialog({
|
export default function EditCategoryDialog({
|
||||||
category: initialCategory,
|
category: initialCategory,
|
||||||
@ -88,51 +89,70 @@ export default function EditCategoryDialog({
|
|||||||
<Form {...form}>
|
<Form {...form}>
|
||||||
<form onSubmit={form.handleSubmit((val) => mutate(val))}>
|
<form onSubmit={form.handleSubmit((val) => mutate(val))}>
|
||||||
<div className="flex w-full flex-col space-y-2">
|
<div className="flex w-full flex-col space-y-2">
|
||||||
<FormField
|
<div style={{ display: "grid", gridTemplateColumns: "5em 1fr 75px", gap: "10px" }}>
|
||||||
control={form.control}
|
<FormField
|
||||||
name="name"
|
control={form.control}
|
||||||
render={({ field }) => (
|
name="code"
|
||||||
<FormItem>
|
render={({ field }) => (
|
||||||
<FormLabel>Name</FormLabel>
|
<FormItem>
|
||||||
<FormControl>
|
{/* <FormLabel>Code</FormLabel> */}
|
||||||
<Input
|
<FormControl>
|
||||||
type="text"
|
<Input
|
||||||
placeholder="Name"
|
type="text"
|
||||||
{...field}
|
placeholder="Code"
|
||||||
className="w-full rounded border p-2"
|
{...field}
|
||||||
/>
|
className="w-full rounded border p-2"
|
||||||
</FormControl>
|
/>
|
||||||
<FormMessage />
|
</FormControl>
|
||||||
</FormItem>
|
<FormMessage />
|
||||||
)}
|
</FormItem>
|
||||||
/>
|
)}
|
||||||
<FormField
|
/>
|
||||||
control={form.control}
|
<FormField
|
||||||
name="code"
|
control={form.control}
|
||||||
render={({ field }) => (
|
name="name"
|
||||||
<FormItem>
|
render={({ field }) => (
|
||||||
<FormLabel>Code</FormLabel>
|
<FormItem>
|
||||||
<FormControl>
|
{/* <FormLabel>Name</FormLabel> */}
|
||||||
<Input
|
<FormControl>
|
||||||
type="number"
|
<Input
|
||||||
placeholder="Code"
|
type="text"
|
||||||
{...field}
|
placeholder="Name"
|
||||||
className="w-full rounded border p-2"
|
{...field}
|
||||||
/>
|
className="w-full rounded border p-2"
|
||||||
</FormControl>
|
/>
|
||||||
<FormMessage />
|
</FormControl>
|
||||||
</FormItem>
|
<FormMessage />
|
||||||
)}
|
</FormItem>
|
||||||
/>
|
)}
|
||||||
|
/>
|
||||||
|
<FormField
|
||||||
|
control={form.control}
|
||||||
|
name="color.name"
|
||||||
|
render={({ field }) => (
|
||||||
|
<FormItem>
|
||||||
|
{/* <FormLabel>Color</FormLabel> */}
|
||||||
|
<FormControl>
|
||||||
|
<Input
|
||||||
|
type="text"
|
||||||
|
placeholder="Color"
|
||||||
|
{...field}
|
||||||
|
className="w-full rounded border p-2"
|
||||||
|
/>
|
||||||
|
</FormControl>
|
||||||
|
<FormMessage />
|
||||||
|
</FormItem>
|
||||||
|
)}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
<FormField
|
<FormField
|
||||||
control={form.control}
|
control={form.control}
|
||||||
name="description"
|
name="description"
|
||||||
render={({ field }) => (
|
render={({ field }) => (
|
||||||
<FormItem>
|
<FormItem>
|
||||||
<FormLabel>Description</FormLabel>
|
{/* <FormLabel>Description</FormLabel> */}
|
||||||
<FormControl>
|
<FormControl>
|
||||||
<Input
|
<Textarea
|
||||||
type="text"
|
|
||||||
placeholder="Description"
|
placeholder="Description"
|
||||||
{...field}
|
{...field}
|
||||||
className="w-full rounded border p-2"
|
className="w-full rounded border p-2"
|
||||||
@ -142,24 +162,6 @@ export default function EditCategoryDialog({
|
|||||||
</FormItem>
|
</FormItem>
|
||||||
)}
|
)}
|
||||||
/>
|
/>
|
||||||
{/* <FormField
|
|
||||||
control={form.control}
|
|
||||||
name="colorId"
|
|
||||||
render={({ field }) => (
|
|
||||||
<FormItem>
|
|
||||||
<FormLabel>Color</FormLabel>
|
|
||||||
<FormControl>
|
|
||||||
<Input
|
|
||||||
type="text"
|
|
||||||
placeholder="Color"
|
|
||||||
{...field}
|
|
||||||
className="w-full rounded border p-2"
|
|
||||||
/>
|
|
||||||
</FormControl>
|
|
||||||
<FormMessage />
|
|
||||||
</FormItem>
|
|
||||||
)}
|
|
||||||
/> */}
|
|
||||||
<DialogFooter className="sm:justify-end">
|
<DialogFooter className="sm:justify-end">
|
||||||
<DialogClose asChild>
|
<DialogClose asChild>
|
||||||
<Button type="button" variant="secondary">
|
<Button type="button" variant="secondary">
|
||||||
|
|||||||
@ -32,11 +32,13 @@ export const zGetCategoryResponseSchema = z.object({
|
|||||||
export type ZGetCategoryResponse = z.infer<typeof zGetCategoryResponseSchema>;
|
export type ZGetCategoryResponse = z.infer<typeof zGetCategoryResponseSchema>;
|
||||||
|
|
||||||
export const zUpdateCategoryRequestSchema = z.object({
|
export const zUpdateCategoryRequestSchema = z.object({
|
||||||
code: z.number().nullish(),
|
code: z.number(),
|
||||||
name: z.string().optional(),
|
name: z.string().optional(),
|
||||||
description: z.string().optional(),
|
description: z.string().optional(),
|
||||||
colorId: z.string().nullish(),
|
|
||||||
parentId: z.string().nullish(),
|
parentId: z.string().nullish(),
|
||||||
categoryId: z.string(),
|
categoryId: z.string(),
|
||||||
|
color: z.object({
|
||||||
|
name: z.string(),
|
||||||
|
}),
|
||||||
});
|
});
|
||||||
export type ZUpdateCategoryRequest = z.infer<typeof zUpdateCategoryRequestSchema>;
|
export type ZUpdateCategoryRequest = z.infer<typeof zUpdateCategoryRequestSchema>;
|
||||||
@ -222,13 +222,19 @@ export const categoriesAppRouter = router({
|
|||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
.mutation(async ({ input, ctx }) => {
|
.mutation(async ({ input, ctx }) => {
|
||||||
console.log("Updating");
|
const colorIdQuery = await ctx.db.select({ id: colors.id }).from(colors).where(
|
||||||
|
and(
|
||||||
|
eq(colors.name, input.color.name),
|
||||||
|
eq(colors.userId, ctx.user.id),
|
||||||
|
)
|
||||||
|
);
|
||||||
const res = await ctx.db
|
const res = await ctx.db
|
||||||
.update(categories)
|
.update(categories)
|
||||||
.set({
|
.set({
|
||||||
name: input.name,
|
name: input.name,
|
||||||
description: input.description,
|
description: input.description,
|
||||||
code: input.code,
|
code: input.code,
|
||||||
|
colorId: colorIdQuery[0].id,
|
||||||
})
|
})
|
||||||
.where(
|
.where(
|
||||||
and(
|
and(
|
||||||
@ -236,7 +242,6 @@ export const categoriesAppRouter = router({
|
|||||||
eq(categories.userId, ctx.user.id),
|
eq(categories.userId, ctx.user.id),
|
||||||
),
|
),
|
||||||
).returning();
|
).returning();
|
||||||
console.log(res);
|
|
||||||
return res[0];
|
return res[0];
|
||||||
}),
|
}),
|
||||||
delete: authedProcedure
|
delete: authedProcedure
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user