Skip to content

Commit

Permalink
Migrate to new TRPC backend
Browse files Browse the repository at this point in the history
  • Loading branch information
BlueZeeKing committed Oct 31, 2023
1 parent a6ee8c4 commit 85ecf8a
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 42 deletions.
34 changes: 17 additions & 17 deletions src/app/(dashboard)/app/modules/[id]/_components/add-task.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,16 @@ export default function AddTask(props: {
reset,
} = useZodForm({
schema: z.object({
name: z.string(),
title: z.string(),
note: z.string().optional(),
dueAt: z
dueDate: z
.string()
.refine((str) => !isNaN(Date.parse(str)), { message: "Invalid date" }),
priority: z.enum(["low", "high", "medium"]),
priority: z.enum(["LOW", "HIGH", "MEDIUM"]),
}),
});

const { mutateAsync, isLoading } = trpc.todos.create.new.useMutation();
const { mutateAsync, isLoading } = trpc.task.post.create.useMutation();
const utils = trpc.useUtils();

return (
Expand All @@ -50,12 +50,12 @@ export default function AddTask(props: {
onSubmit={handleSubmit(async (data) => {
await mutateAsync({
moduleId: props.moduleId,
name: data.name,
title: data.title,
priority: data.priority,
note: data.note,
dueAt: new Date(Date.parse(data.dueAt)),
notes: data.note ?? "",
dueDate: new Date(Date.parse(data.dueDate)).toString(),
});
await utils.todos.invalidate();
await utils.task.invalidate();

onClose();
reset();
Expand All @@ -71,9 +71,9 @@ export default function AddTask(props: {
variant="bordered"
labelPlacement="outside"
isRequired
isInvalid={!!errors.name}
errorMessage={errors.name?.message}
{...register("name")}
isInvalid={!!errors.title}
errorMessage={errors.title?.message}
{...register("title")}
/>
<Textarea
label="Note"
Expand All @@ -98,13 +98,13 @@ export default function AddTask(props: {
errorMessage={errors.priority?.message}
{...register("priority")}
>
<SelectItem key="low" value="low">
<SelectItem key="LOW" value="LOW">
Low
</SelectItem>
<SelectItem key="medium" value="medium">
<SelectItem key="MEDIUM" value="MEDIUM">
Medium
</SelectItem>
<SelectItem key="high" value="high">
<SelectItem key="HIGH" value="HIGH">
High
</SelectItem>
</Select>
Expand All @@ -117,9 +117,9 @@ export default function AddTask(props: {
variant="bordered"
labelPlacement="outside"
isRequired
isInvalid={!!errors.dueAt}
errorMessage={errors.dueAt?.message}
{...register("dueAt")}
isInvalid={!!errors.dueDate}
errorMessage={errors.dueDate?.message}
{...register("dueDate")}
/>
</ModalBody>
<ModalFooter>
Expand Down
14 changes: 7 additions & 7 deletions src/app/(dashboard)/app/modules/[id]/_components/task-item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ const Task = forwardRef<
name: string;
dueAt: Date;
}
>(({ task, checked, name, dueAt }, ref) => {
const setChecked = trpc.todos.update.setChecked.useMutation();
const remove = trpc.todos.delete.byId.useMutation();
>(function Task({ task, checked, name, dueAt }, ref) {
const setChecked = trpc.task.patch.switchDone.useMutation();
const remove = trpc.task.delete.byId.useMutation();
const { onOpen, onOpenChange, isOpen } = useDisclosure();

const utils = trpc.useUtils();
Expand All @@ -34,8 +34,8 @@ const Task = forwardRef<
isSelected={checked}
onChange={async (e) => {
e.preventDefault();
await setChecked.mutateAsync({ checked: e.target.checked, id: task });
await utils.todos.invalidate();
await setChecked.mutateAsync({ done: e.target.checked, id: task });
await utils.task.invalidate();
}}
/>
<button>
Expand Down Expand Up @@ -79,8 +79,8 @@ const Task = forwardRef<
color="danger"
isLoading={remove.isLoading}
onClick={async () => {
await remove.mutateAsync(task);
await utils.todos.invalidate();
await remove.mutateAsync({ id: task });
await utils.task.invalidate();
onClose();
}}
>
Expand Down
8 changes: 4 additions & 4 deletions src/app/(dashboard)/app/modules/[id]/_components/tasks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const MotionTask = motion(Task);

export default function Tasks(props: { id: string }) {
const id = Number(props.id);
const [todos] = trpc.todos.get.byModule.useSuspenseQuery({ moduleId: id });
const [todos] = trpc.task.get.byModule.useSuspenseQuery({ moduleId: id });

return (
<div className="m-1 mt-4 min-h-full w-1/4 rounded border border-gray-900">
Expand All @@ -19,10 +19,10 @@ export default function Tasks(props: { id: string }) {
<MotionTask
layout
key={todo.id}
checked={todo.checked}
name={todo.name}
checked={todo.done ?? false}
name={todo.title}
task={todo.id}
dueAt={new Date(todo.dueAt)}
dueAt={new Date(todo.dueDate)}
/>
))}
</ol>
Expand Down
10 changes: 5 additions & 5 deletions src/app/(dashboard)/app/modules/[id]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default function Page({ params }: { params: { id: string } }) {

const id = Number(params.id);

const [tasks] = trpc.todos.get.byModule.useSuspenseQuery({ moduleId: id });
const [tasks] = trpc.task.get.byModule.useSuspenseQuery({ moduleId: id });

// FIXME: weird animation bug when moving
return (
Expand All @@ -26,14 +26,14 @@ export default function Page({ params }: { params: { id: string } }) {
</div>
) : (
<ul className="p-4">
{tasks.map(({ name, id, checked, dueAt }) => (
{tasks.map(({ title, id, done, dueDate }) => (
<MotionTask
layout
key={id}
task={id}
name={name}
checked={checked}
dueAt={new Date(dueAt)}
name={title}
checked={done ?? false}
dueAt={new Date(dueDate)}
/>
))}
</ul>
Expand Down
2 changes: 1 addition & 1 deletion src/db/schema/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const moduleTable = sqliteTable("module", {

export const moduleTableRelations = relations(moduleTable, ({ many }) => ({
notebooks: many(notebooks),
tasks: many(taskTable)
tasks: many(taskTable),
}));

export const insertModuleSchema = createInsertSchema(moduleTable, {
Expand Down
6 changes: 3 additions & 3 deletions src/server/api/routers/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ export const moduleRouter = createTRPCRouter({
try {
const modules = await ctx.db.query.moduleTable.findMany({
where: (table, { eq }) => eq(table.userId, ctx.auth.userId),
with:{
tasks: true
}
with: {
tasks: true,
},
});

return modules;
Expand Down
42 changes: 37 additions & 5 deletions src/server/api/routers/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ export const taskRouter = createTRPCRouter({
}),
patch: createTRPCRouter({
switchDone: protectedProcedure
.input(selectTaskSchema.pick({ id: true }))
.input(selectTaskSchema.pick({ id: true, done: true }))
.mutation(async ({ ctx, input }) => {
try {
const [taskWithModule] = await ctx.db
Expand All @@ -165,13 +165,11 @@ export const taskRouter = createTRPCRouter({
});
}

const { task } = taskWithModule;

const res = await ctx.db
.update(taskTable)
.set({
done: !task.done,
doneAt: !task.done ? new Date().toString() : null,
done: input.done,
doneAt: !input.done ? new Date().toString() : null,
})
.where(eq(taskTable.id, input.id))
.returning();
Expand All @@ -187,4 +185,38 @@ export const taskRouter = createTRPCRouter({
}
}),
}),
delete: createTRPCRouter({
byId: protectedProcedure
.input(selectTaskSchema.pick({ id: true }))
.mutation(async ({ ctx, input }) => {
try {
const [task] = await ctx.db
.select({ id: taskTable.id })
.from(taskTable)
.leftJoin(moduleTable, eq(moduleTable.id, taskTable.moduleId))
.where(
and(
eq(taskTable.id, input.id),
eq(moduleTable.userId, ctx.auth.userId),
),
);

if (!task) {
throw new TRPCError({
code: "BAD_REQUEST",
message: "The task you are trying to delete doesn't exist",
});
}

await ctx.db.delete(taskTable).where(eq(taskTable.id, task.id));
} catch (err) {
console.error(err);
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message:
"An error occurred while switching the task's done status.",
});
}
}),
}),
});

0 comments on commit 85ecf8a

Please sign in to comment.