Skip to content

Commit

Permalink
fix: Casting id in project edit lead to error
Browse files Browse the repository at this point in the history
  • Loading branch information
vogelino committed May 8, 2024
1 parent ad217e0 commit a939bc6
Show file tree
Hide file tree
Showing 15 changed files with 67 additions and 123 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default async function ClientEditModalRoute({
const record = await db.query.clients.findFirst({
where: eq(clients.id, +id),
});
serverQueryClient.prefetchQuery({
await serverQueryClient.prefetchQuery({
queryKey: ["project", id],
queryFn: () => record,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default async function ExpenseEditModalRoute({
}) {
const record = await getExpense(+id);
if (!record) return null;
serverQueryClient.prefetchQuery({
await serverQueryClient.prefetchQuery({
queryKey: ["project", `${id}`],
queryFn: () => record,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default async function ProjectEditModalRoute({
const record = await db.query.projects.findFirst({
where: eq(projects.id, +id),
});
serverQueryClient.prefetchQuery({
await serverQueryClient.prefetchQuery({
queryKey: ["project", id],
queryFn: () => record,
});
Expand Down
2 changes: 1 addition & 1 deletion src/app/(resource-pages)/clients/edit/[id]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default async function ClientEditPageRoute({
const record = await db.query.clients.findFirst({
where: eq(clients.id, +id),
});
serverQueryClient.prefetchQuery({
await serverQueryClient.prefetchQuery({
queryKey: ["clients", `${id}`],
queryFn: () => record,
});
Expand Down
4 changes: 2 additions & 2 deletions src/app/(resource-pages)/clients/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { HydrationBoundary, dehydrate } from "@tanstack/react-query";
import ClientList from "./page.client";

export const dynamic = "force-dynamic";
export default function ClientsPageServer() {
serverQueryClient.prefetchQuery({
export default async function ClientsPageServer() {
await serverQueryClient.prefetchQuery({
queryKey: ["clients"],
queryFn: () => db.query.clients.findMany(),
});
Expand Down
2 changes: 1 addition & 1 deletion src/app/(resource-pages)/expenses/edit/[id]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default async function ExpenseEditPageRoute({
}) {
const record = await getExpense(+id);
if (!record) return null;
serverQueryClient.prefetchQuery({
await serverQueryClient.prefetchQuery({
queryKey: ["expenses", `${id}`],
queryFn: () => record,
});
Expand Down
2 changes: 1 addition & 1 deletion src/app/(resource-pages)/expenses/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import ExpensesPage from "./ExpensesPage";

export const dynamic = "force-dynamic";
export default async function ExpensesPageServer() {
serverQueryClient.prefetchQuery({
await serverQueryClient.prefetchQuery({
queryKey: ["clients"],
queryFn: getExpenses,
});
Expand Down
4 changes: 2 additions & 2 deletions src/app/(resource-pages)/projects/edit/[id]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import { SaveIcon } from "lucide-react";
import Link from "next/link";

export const dynamic = "force-dynamic";
export default function ProjectEditPageRoute({
export default async function ProjectEditPageRoute({
params: { id },
}: {
params: { id: string };
}) {
serverQueryClient.prefetchQuery({
await serverQueryClient.prefetchQuery({
queryKey: ["projects", `${id}`],
queryFn: () =>
db.query.projects.findFirst({
Expand Down
4 changes: 2 additions & 2 deletions src/app/(resource-pages)/projects/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { HydrationBoundary, dehydrate } from "@tanstack/react-query";
import ProjectsList from "./page.client";

export const dynamic = "force-dynamic";
export default function ProjectsPageServer() {
serverQueryClient.prefetchQuery({
export default async function ProjectsPageServer() {
await serverQueryClient.prefetchQuery({
queryKey: ["projects"],
queryFn: () => db.query.projects.findMany(),
});
Expand Down
20 changes: 0 additions & 20 deletions src/components/ClientDisplay.tsx

This file was deleted.

4 changes: 2 additions & 2 deletions src/components/ExpenseEdit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import FormInputWrapper from "@/components/FormInputWrapper";
import { PillText } from "@/components/PillText";
import CurrencyInput from "@/components/ui/currency-input";
import {
type ExpenseType,
expenseCategoryEnum,
expenseRateEnum,
expenseTypeEnum,
type ExpenseType,
} from "@/db/schema";
import env from "@/env";
import useExpense from "@/utility/data/useExpense";
Expand Down Expand Up @@ -88,7 +88,7 @@ export default function ExpenseEdit({
onSubmit={handleSubmit((values) => {
if (id) {
editMutation.mutate({
id: +id,
id,
...values,
type,
category,
Expand Down
36 changes: 0 additions & 36 deletions src/components/ProjectDisplay.tsx

This file was deleted.

34 changes: 17 additions & 17 deletions src/utility/data/useClientEdit.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
"use client";

import {
type ClientType,
type ResourceType,
clientInsertSchema,
clientInsertSchema,
type ClientType,
type ResourceType,
} from "@/db/schema";
import createMutationHook from "./createMutationHook";
import createQueryFunction, { type ActionType } from "./createQueryFunction";
Expand All @@ -13,24 +13,24 @@ const action: ActionType = "edit";
const inputZodSchema = clientInsertSchema;

const useClientEdit = createMutationHook<ClientType[]>({
resourceName,
action,
inputZodSchema,
mutationFn: createQueryFunction<void>({
resourceName,
action,
inputZodSchema,
}),
createOptimisticDataEntry,
resourceName,
action,
inputZodSchema,
mutationFn: createQueryFunction<void>({
resourceName,
action,
inputZodSchema,
}),
createOptimisticDataEntry,
});

export default useClientEdit;

function createOptimisticDataEntry(
oldData: ClientType[] | undefined,
editedData: ClientType,
oldData: ClientType[] | undefined,
editedData: ClientType
) {
return (oldData || []).filter((c) =>
c.id === editedData.id ? { ...c, ...editedData } : c,
);
return (oldData || []).map((c) =>
c.id === editedData.id ? { ...c, ...editedData } : c
);
}
36 changes: 18 additions & 18 deletions src/utility/data/useExpenseEdit.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
"use client";

import {
type ExpenseType,
type ExpenseWithMonthlyCLPPriceType,
type ResourceType,
expenseInsertSchema,
expenseInsertSchema,
type ExpenseType,
type ExpenseWithMonthlyCLPPriceType,
type ResourceType,
} from "@/db/schema";
import createMutationHook from "./createMutationHook";
import createQueryFunction, { type ActionType } from "./createQueryFunction";
Expand All @@ -14,24 +14,24 @@ const action: ActionType = "edit";
const inputZodSchema = expenseInsertSchema;

const useClientEdit = createMutationHook<ExpenseWithMonthlyCLPPriceType[]>({
resourceName,
action,
inputZodSchema,
mutationFn: createQueryFunction<void>({
resourceName,
action,
inputZodSchema,
}),
createOptimisticDataEntry,
resourceName,
action,
inputZodSchema,
mutationFn: createQueryFunction<void>({
resourceName,
action,
inputZodSchema,
}),
createOptimisticDataEntry,
});

export default useClientEdit;

function createOptimisticDataEntry(
oldData: ExpenseWithMonthlyCLPPriceType[] | undefined,
editedData: ExpenseType,
oldData: ExpenseWithMonthlyCLPPriceType[] | undefined,
editedData: ExpenseType
) {
return (oldData || []).filter((c) =>
c.id === editedData.id ? { ...c, ...editedData } : c,
);
return (oldData || []).map((c) =>
c.id === editedData.id ? { ...c, ...editedData } : c
);
}
36 changes: 18 additions & 18 deletions src/utility/data/useProjectEdit.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
"use client";

import {
type ExpenseType,
type ExpenseWithMonthlyCLPPriceType,
type ResourceType,
projectInsertSchema,
projectInsertSchema,
type ExpenseType,
type ExpenseWithMonthlyCLPPriceType,
type ResourceType,
} from "@/db/schema";
import createMutationHook from "./createMutationHook";
import createQueryFunction, { type ActionType } from "./createQueryFunction";
Expand All @@ -14,24 +14,24 @@ const action: ActionType = "edit";
const inputZodSchema = projectInsertSchema;

const useProjectEdit = createMutationHook<ExpenseWithMonthlyCLPPriceType[]>({
resourceName,
action,
inputZodSchema,
mutationFn: createQueryFunction<void>({
resourceName,
action,
inputZodSchema,
}),
createOptimisticDataEntry,
resourceName,
action,
inputZodSchema,
mutationFn: createQueryFunction<void>({
resourceName,
action,
inputZodSchema,
}),
createOptimisticDataEntry,
});

export default useProjectEdit;

function createOptimisticDataEntry(
oldData: ExpenseWithMonthlyCLPPriceType[] | undefined,
editedData: ExpenseType,
oldData: ExpenseWithMonthlyCLPPriceType[] | undefined,
editedData: ExpenseType
) {
return (oldData || []).filter((c) =>
c.id === editedData.id ? { ...c, ...editedData } : c,
);
return (oldData || []).map((c) =>
c.id === editedData.id ? { ...c, ...editedData } : c
);
}

0 comments on commit a939bc6

Please sign in to comment.