Skip to content

Commit d3e3af0

Browse files
committed
fix: refactor with suggested changes
1 parent ebebe23 commit d3e3af0

File tree

2 files changed

+23
-29
lines changed

2 files changed

+23
-29
lines changed

zod/app/routes/index.tsx

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { ActionArgs, json } from "@remix-run/node";
1+
import type { ActionArgs } from "@remix-run/node";
2+
import { json } from "@remix-run/node";
23
import { Form, Link, useActionData, useNavigation } from "@remix-run/react";
34
import * as z from "zod";
45

@@ -108,13 +109,11 @@ export default function RegisterView() {
108109
name="firstName"
109110
defaultValue={actionData?.fields?.firstName}
110111
/>
111-
{actionData?.fieldErrors?.firstName
112-
? actionData.fieldErrors.firstName.map((error, index) => (
113-
<p style={errorTextStyle} key={`first-name-error-${index}`}>
114-
{error}
115-
</p>
116-
))
117-
: null}
112+
{actionData?.fieldErrors?.firstName?.map((error, index) => (
113+
<p style={errorTextStyle} key={`first-name-error-${index}`}>
114+
{error}
115+
</p>
116+
))}
118117
</div>
119118

120119
<br />
@@ -127,13 +126,11 @@ export default function RegisterView() {
127126
name="email"
128127
defaultValue={actionData?.fields?.email}
129128
/>
130-
{actionData?.fieldErrors?.email
131-
? actionData.fieldErrors.email.map((error, index) => (
132-
<p style={errorTextStyle} key={`email-error-${index}`}>
133-
{error}
134-
</p>
135-
))
136-
: null}
129+
{actionData?.fieldErrors?.email?.map((error, index) => (
130+
<p style={errorTextStyle} key={`email-error-${index}`}>
131+
{error}
132+
</p>
133+
))}
137134
</div>
138135

139136
<br />
@@ -146,13 +143,11 @@ export default function RegisterView() {
146143
name="birthday"
147144
defaultValue={actionData?.fields?.birthday}
148145
/>
149-
{actionData?.fieldErrors?.birthday
150-
? actionData.fieldErrors.birthday.map((error, index) => (
151-
<p style={errorTextStyle} key={`birthday-error-${index}`}>
152-
{error}
153-
</p>
154-
))
155-
: null}
146+
{actionData?.fieldErrors?.birthday?.map((error, index) => (
147+
<p style={errorTextStyle} key={`birthday-error-${index}`}>
148+
{error}
149+
</p>
150+
))}
156151
</div>
157152

158153
<br />

zod/app/routes/products.tsx

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { LoaderArgs, json, redirect } from "@remix-run/node";
1+
import type { LoaderArgs } from "@remix-run/node";
2+
import { json, redirect } from "@remix-run/node";
23
import {
34
Form,
45
Link,
@@ -11,7 +12,7 @@ import * as z from "zod";
1112
import { mockProducts } from "~/lib/product.server";
1213

1314
export const loader = ({ request }: LoaderArgs) => {
14-
const URLSearchParams = new URL(request.url).searchParams;
15+
const { searchParams } = new URL(request.url);
1516

1617
const schema = z.object({
1718
name: z.string().optional(),
@@ -24,9 +25,7 @@ export const loader = ({ request }: LoaderArgs) => {
2425
// filter out empty string values from query params
2526
// otherwise zod will throw while coercing them to number
2627
const parseResult = schema.safeParse(
27-
Object.fromEntries(
28-
[...URLSearchParams.entries()].filter(([, v]) => v !== ""),
29-
),
28+
Object.fromEntries([...searchParams.entries()].filter(([, v]) => v !== "")),
3029
);
3130

3231
if (!parseResult.success) {
@@ -67,8 +66,8 @@ export const loader = ({ request }: LoaderArgs) => {
6766

6867
if (pagination.page > totalPageCount) {
6968
// reset page to 1 and redirect on invalid page number
70-
URLSearchParams.set("page", "1");
71-
return redirect(`?${URLSearchParams.toString()}`, {
69+
searchParams.set("page", "1");
70+
return redirect(`?${searchParams.toString()}`, {
7271
status: 303,
7372
});
7473
}

0 commit comments

Comments
 (0)